Setting up LetEncrypt HTTPS Traefik Ingress for Kubernetes Cluster

12/20/2018

I've setup Kubernetes to use the Traefik Ingress to provide name based routing. I am a little lost in terms of how to configure for the automatic LetsEncrypt SSL certs. How do I reference the TOML files and configure for HTTPs. I am using a simple container below with the NGINX image to test this.

The below is my YAML for the deployment/service/ingress.

apiVersion: v1
kind: Service
metadata:
  name: web
  labels:
    app: hmweb
spec:
  ports:
    - name: http
      port: 80
      targetPort: 80
      protocol: TCP
  selector:
    app: hmweb

---

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: web-ingress
  annotations:
    kubernetes.io/ingress.class: traefik
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /
        backend:
          serviceName: web
          servicePort: http

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hmweb-deployment
  labels:
    app: hmweb
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hmweb
  template:
    metadata:
      labels:
        app: hmweb
    spec:
      containers:
      - name: hmweb
        image: nginx:latest

        envFrom:
          - configMapRef:
              name: config
        ports:
        - containerPort: 80

I have also included my ingress.yaml

--
apiVersion: v1
kind: ServiceAccount
metadata:
  name: traefik-ingress-controller

---
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: traefik-ingress-controller
  namespace: kube-system
  labels:
    k8s-app: traefik-ingress-lb
spec:
  replicas: 1
  selector:
    matchLabels:
      k8s-app: traefik-ingress-lb
  template:
    metadata:
      labels:
        k8s-app: traefik-ingress-lb
        name: traefik-ingress-lb
    spec:
      serviceAccountName: traefik-ingress-controller
      terminationGracePeriodSeconds: 60
      containers:
      - image: traefik
        name: traefik-ingress-lb
        ports:
        - name: http
          containerPort: 80
        - name: admin
          containerPort: 8080
        args:
        - --api
        - --kubernetes
        - --logLevel=INFO
---
kind: Service
apiVersion: v1
metadata:
  name: traefik-ingress-service
  namespace: kube-system
spec:
  selector:
    k8s-app: traefik-ingress-lb
  ports:
    - protocol: TCP
      port: 80
      name: web
    - protocol: TCP
      port: 8080
      name: admin
  type: LoadBalancer
-- Rutnet
kubernetes
traefik
traefik-ingress

1 Answer

9/23/2019

You could build a custom image and include the toml file that way, however that would NOT be best practice. Here's how I did it:

1) Deploy your toml configuration to kubernetes as a ConfigMap like so:

apiVersion: v1
kind: ConfigMap
metadata:
  name: cfg-traefik
  labels:
    app: traefik
data:
  traefik.toml: |
    defaultEntryPoints = ["http", "https"]
    [entryPoints]
      [entryPoints.http]
      address = ":80"
        [entryPoints.http.redirect]
        entryPoint = "https"
      [entryPoints.https]
      address = ":443"
        [entryPoints.https.tls]
    [acme]
    email = "you@email.com"
    storage = "/storage/acme.json"
    entryPoint = "https"
    acmeLogging = true
    onHostRule = true
    [acme.tlsChallenge]

2) Connect the configuration to your Traefik deployment. Here's my configuration:

kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: dpl-traefik
  labels:
    k8s-app: traefik
spec:
  replicas: 1
  selector:
    matchLabels:
      k8s-app: traefik
  template:
    metadata:
      labels:
        k8s-app: traefik
        name: traefik
    spec:
      serviceAccountName: svc-traefik
      terminationGracePeriodSeconds: 60
      volumes:
      - name: config
        configMap:
          name: cfg-traefik
      - name: cert-storage
        persistentVolumeClaim:
          claimName: pvc-traefik
      containers:
      - image: traefik:alpine
        name: traefik
        volumeMounts:
        - mountPath: "/config"
          name: "config"
        - mountPath: "/storage"
          name: cert-storage
        ports:
        - name: http
          containerPort: 80
        - name: https
          containerPort: 443
        - name: admin
          containerPort: 8080
        args:
        - --api
        - --kubernetes
        - --logLevel=INFO
        - --configFile=/config/traefik.toml
-- Nibbletz
Source: StackOverflow