K3s Traefik expose service

By installing K3s, Traefik comes preinstalled in the cluster. You can test that as soon as you install the K3s cluster and hit the curl localhost you will get a response 404 not found.

That means that Traefik is already installed. If that is not the case you can follow the tutorial to install Traefik on the K3s.

K3s install traefik
Redeploy Traefik on the k3s To configure Traefik create a new YAML file and provide the needed details as shown below. apiVersion: helm.cattle.io/v1 kind: HelmChartConfig metadata: name: traefik namespace: kube-system spec: valuesContent: |- additionalArguments: - ”--api” - ”--api.dashboard=true” - ”--api.insecure=true” - ”--log.level=

Exposing service using ingress

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: nginx
  replicas: 1
  template:
    metadata:
      labels:
        app.kubernetes.io/name: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  selector:
    app.kubernetes.io/name: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: ClusterIP

After applying these resources to the cluster we will have service nginx created. This service is only available in the cluster and not to the outside.

To expose the service we will use ingress. Traefik will handle ingres traffic and route it to the correct service inside the cluster.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx-server
  namespace: default
spec:
  rules:
    - host: localhost
      http:
        paths:
          - path: /nginx
            pathType: Prefix
            backend:
              service:
                name:  nginx
                port:
                  number: 80

After applying the ingress resource now nginx will be accessible via localhost/nginx.

We can confirm this by doing the following:

curl localhost/nginx

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

Traefik can handle complex rules. To investigate possibilities with using Traefik you can read article below.

Traffic engineering with Traefik
Introduction Traefik is one of the most popular ingress controllers on Kubernetes. Traefik v2 brought some major changes in the usage of the controller itself. It brought the approach of heavy usage of Custom Resources on Kubernetes to provide reconfigurability and expanded fields of operation apart from the Ingress controller