Steve mitchell in Side notes 5 minutes

How to Expose a Service with Traefik

This post walks through exposing a service with Traefik ingress.

Nginx Welcome Page
Nginx Welcome Page

Create the Namespace

First, create a namespace for this example.

1
kubectl create namespace sample-nginx

Deploy the Sample Nginx Application

Create the deployment YAML for Nginx. The deployment name is “sample-nginx-deployment” with an exposed container port of 80.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: sample-nginx-deployment
  namespace: sample-nginx
spec:
  selector:
    matchLabels:
      run: sample-nginx
  replicas: 2
  template:
    metadata:
      labels:
        run: sample-nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
sample-nginx-deployment.yaml

Deploy the application to Kubernetes.

1
kubectl apply -f sample-nginx-deployment.yaml

Inspect the deployment.

1
kubectl describe deployment sample-nginx-deployment -n sample-nginx
Deployment Description
Deployment Description

Verify the pods.

1
kubectl get pods -n sample-nginx -o wide
Get Pods
Get Pods

Add a Service

Create the YAML for the service.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
---
apiVersion: v1
kind: Service
metadata:
  name: sample-nginx
  namespace: sample-nginx
  labels:
    run: sample-nginx
spec:
  ports:
  - port: 80
    protocol: TCP
  selector:
    run: sample-nginx
sample-nginx-service-deployment.yaml

Apply the service YAML.

1
kubectl apply -f sample-nginx-service-deployment.yaml

Verify the service.

1
kubectl get svc -n sample-nginx
Get Service
Get Service

Add the Ingress

Create the Ingress YAML.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: sample-nginx-ingress
  namespace: sample-nginx
  annotations:
    ingress.kubernetes.io/ssl-redirect: "false"
spec:
  rules:
  - host: byteworksinc.com
  - http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: sample-nginx
            port:
              number: 80
Sample-nginx-ingress-deployment.yaml

The “host” attribute is optional. Most demonstrations use a host of “example.com.” If you keep the host attribute, you need to edit your /etc/hosts files to route that domain to the cluster. If you leave it out, you can open a web browser directly to any of the nodes, for example, http://192.168.1.10, in my case.

Since I set “host” to “byteworksinc.com” in my ingress yaml file, I updated my host file to point byteworkinc.com to the Kubernetes cluster.

1
2
echo 192.168.1.10 byteworksinc.com | sudo tee -a /etc/hosts
cat /etc/hosts | tail -n 1
Confirm /etc/hosts Change
Confirm /etc/hosts Change

Apply the ingress YAML file.

1
kubectl apply -f sample-nginx-ingress-deployment.yaml

Refresh the Traefik Dashboard to see the updated information.

Updated Traefik Dashboard
Updated Traefik Dashboard

The frontend shows the root context rule “/.” The backend lists the two nodes where Nginx is running.

Open a web browser to http://byteworksinc.com to see if the default Nginx page opens.

Nginx Welcome Page
Nginx Welcome Page

The Traefik ingress rule is working as intended.


References

k3d/exposing_services.md at main · rancher/k3d · GitHub Create a Kubernetes TLS Ingress from scratch in Minikube https://github.com/prometheus-operator/prometheus-operator/issues/800#issuecomment-349439795