Can't run Traefik load balancer without hostPort

9/23/2018

I am trying to setup traefik loadbalancer in the google cloud engine. My configuration:

kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: traefik-ingress-controller
  namespace: default
  labels:
    k8s-app: traefik-ingress-lb
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: traefik-ingress-lb
        name: traefik-ingress-lb
      annotations:
         traefik.ingress.kubernetes.io/frontend-entry-points: http,https
    spec:
      terminationGracePeriodSeconds: 60
      serviceAccountName: traefik-ingress-controller
      volumes:
        - name: config
          configMap:
            name: traefik-config
      containers:
      - image: traefik
        name: traefik-ingress-lb
        imagePullPolicy: Always
        volumeMounts:
          - mountPath: "/config"
            name: "config"
        ports:
        - name: admin
          containerPort: 8080
        - name: http
          containerPort: 80
          #hostPort: 80
        - name: https
          containerPort: 443
        args:
        - --api
        - --kubernetes
        - --logLevel=DEBUG
---
kind: Service
apiVersion: v1
metadata:
  name: traefik-ingress-service
  namespace: default
spec:
  type: LoadBalancer
  selector:
    k8s-app: traefik-ingress-lb
  ports:
    - name: http
      protocol: TCP
      port: 80
      targetPort: 80
    - name: https
      protocol: TCP
      port: 443
      targetPort: 443
    - name: admin
      protocol: TCP
      port: 8080

Traefik ui:

apiVersion: v1
kind: Service
metadata:
  name: traefik-web-ui
  namespace: default
spec:
  selector:
    k8s-app: traefik-ingress-lb
  ports:
    - protocol: TCP
      port: 8080
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: traefik-web-ui
  namespace: default
  annotations:
    kubernetes.io/ingress.class: traefik
spec:
  rules:
  - http:
      paths:
      - path: /
        backend:
          serviceName: traefik-web-ui
          servicePort: 8080

Unfortunately this does not work. The service and pods are running and there are no errors. But when I try to connect it's infinitely loading (funnily a ping works). If I adjust my configuration to use hostPort it is working for one pod. The other pod will never be created, because the hostPort is already used by the first pod (error: pod fails to fit host port).

So how can I fix this to use multiple pods without using a demonset?

-- Marco
kubernetes
traefik

1 Answer

9/23/2018

If you need to connect it to a Google Cloud Load balancer you'll need a host port from your VM. What happens is that Kubernetes probably gives it a random port on the host but your Google Cloud Load Balancer is pointing to 80. You might want to try to assign targetPort on your service the name of the backend port in your service. That would be http or https or admin in your case I believe.

kind: Service
apiVersion: v1
metadata:
  name: traefik-ingress-service
  namespace: default
spec:
  type: LoadBalancer
  selector:
    k8s-app: traefik-ingress-lb
  ports:
    - name: http
      protocol: TCP
      port: 80
      targetPort: http
    - name: https
      protocol: TCP
      port: 443
      targetPort: https
    - name: admin
      protocol: TCP
      port: 8080
      targetPort: admin

Hope it helps!

-- Rico
Source: StackOverflow