skip to Main Content

Inspired by this nice article I tried to setup a very basic Traefik environment on Kubernetes. I know this is the minimum setup without security, etc. We use Traefik for many years on Docker Swarm and want to move on to Kubernetes.

In short: this is the basic traefik yml file with deployment, service and ingressroute:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: traefik
spec:
  selector:
    matchLabels:
      app: traefik
  replicas: 1
  template:
    metadata:
      labels:
        app: traefik
    spec:
      containers:
      - name: traefik
        image: traefik:v2.5
        ports:
        - name: http
          containerPort: 80
        - name: admin
          containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: traefik
spec:
  selector:
    app: traefik
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: http
  - name: admin
    port: 8080
    protocol: TCP
    targetPort: admin
  type: NodePort
---
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  name: traefik-web-ui
spec:
  entryPoints:
    - web
  routes:
  - match: PathPrefix(`/dashboard`)
    kind: Rule
    services:
    - name: traefik
      port: 8080

The NgInx yaml is:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - name: http
          containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  selector:
    app: nginx
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: http
---
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  name: nginx
spec:
  entryPoints:
    - web
  routes:
  - match: PathPrefix(`/nginx`)
    kind: Rule
    services:
    - name: nginx
      port: http

Applying I do with:

$ kubectl apply -f traefik.yaml
$ kubectl apply -f nginx.yaml

All is running:

enter image description here

enter image description here

Browsing to either http://localhost/dashboard, http://localhost/dashboard/, http://localhost/nginx or http://localhost/nginx/ gives no result but ERR_CONNECTION_REFUSED.

2

Answers


  1. Chosen as BEST ANSWER

    Thank you Fayezk13! You showed me the way to streamline my search on the internet.

    Based on the 'helm install' way I found 3 ways for routing traffic via Traefik v2. I hope you can appreciate the results of hours searching on the internet.

    1. Via Ingress

    An example is:

    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: whoami-ingress
    spec:
      rules:
        - http:
            paths:
              - path: /whoami
                pathType: Prefix
                backend:
                  service:
                    name: whoami
                    port:
                      name: web
    

    2. Via IngressRoute (traefik.io/v1alpha1)

    You first have to load the CRD from the Traefik site.

    An example is:

    apiVersion: traefik.io/v1alpha1
    kind: IngressRoute
    metadata:
      name: nginx
      namespace: default
    spec:
      entryPoints:
        - web
      routes:
        - match: PathPrefix(`/nginx`)
          kind: Rule
          middlewares:
            - name: nginx-stripprefix
          services:
            - name: nginx
              port: 80
    ---
    apiVersion: traefik.io/v1alpha1
    kind: Middleware
    metadata:
      name: nginx-stripprefix
    spec:
      stripPrefix:
        prefixes:
          - /nginx
    

    3. Via traefik.containo.us/v1alpha1.

    You first have to load the CRD from the Traefik site.

    An example:

    apiVersion: traefik.containo.us/v1alpha1
    kind: IngressRoute
    metadata:
      name: nginx3
      namespace: default
    spec:
      entryPoints:
        - web
      routes:
        - kind: Rule
          match: PathPrefix(`/third`)
          middlewares:
            - name: nginx3-stripprefix
          services:
            - name: nginx
              port: 80
        - kind: Rule
          match: PathPrefix(`/fourth`)
          middlewares:
            - name: nginx4-pathreplace
          services:
            - name: nginx
              port: 80
    ---
    apiVersion: traefik.containo.us/v1alpha1
    kind: Middleware
    metadata:
      name: nginx3-stripprefix
    spec:
      stripPrefix:
        prefixes:
          - /third
    ---
    apiVersion: traefik.containo.us/v1alpha1
    kind: Middleware
    metadata:
      name: nginx4-pathreplace
    spec:
      replacePathRegex:
        regex: ^/fourth/(.*)
        replacement: /third/$1
    

  2. I would drop this method of deploying Traefik.

    The official way is much more easier to deploy and manage:

    1. Install the helm chart
    helm repo add traefik https://traefik.github.io/charts
    helm repo update
    helm install traefik traefik/traefik
    
    1. Expose the dashboard
    kubectl port-forward $(kubectl get pods --selector "app.kubernetes.io/name=traefik" --output=name) 9000:9000
    
    1. Access on http://127.0.0.1:9000/dashboard/
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search