How to add host mapping to /etc/hosts for Traefik Helm?

Previously I learned traefik by using docker compose to be a load balancer for my demo web application by using dynamic configuration and it works like a charm as the following configuration: -

#
# dynamic-config.yaml
#
http:
  routers:
    edas-admin:
      rule: Host(`my.host.com`) && (PathPrefix(`/myapp`))
      service: myapp-service
      entryPoints:
        - websecure
      middlewares:
        - myapp-middleware
      tls:
        options: default
  middlewares:
    myapp-middleware:
      redirectRegex:
        regex: "^(.*)/myapp$$"
        replacement: "${1}/myapp/"
  services:
    myapp-service:
      loadBalancer:
        servers:
        - url: "http://my.local1.com:8080/myapp" # <---  resolved by /etc/host
        - url: "http://my.local2.com:8080/myapp" # <---  resolved by /etc/host
        sticky:
          cookie:
            name: edas-admin-stickiness
            secure: true
            httpOnly: true
            samesite: strict
#
# docker-compose.yaml
#
version: '3'
  traefik:
    image: traefik:v2.4
    restart: always
    ports:
      - "61543:61543" # SSL
    extra_hosts:
      - "my.local1.com:192.168.x.x" # <---  Append to /etc/host
      - "my.local2.com:192.168.x.y" # <---  Append to /etc/host

At the moment, I've tried to learn the Traefik with Kubernetes by using Traefik Helm [1] and minikube. Everything also works like a charm until I try to make it to be a load balancer for my demo web application by using dynamic configuration.

At this point, I have no any clue how to add host mapping to /etc/hosts for Traefik Helm. Could you please help to advise?

[1] https://doc.traefik.io/traefik/getting-started/install-traefik/#use-the-helm-chart

Hello @charleech

I recommend you to have a look on the workshop I prepared entitled: Getting started with Traefik on Kubernetes. In the workshop I presented how to start with Traefik using the official Helm chart.

If you use Minikube and you would like to create a service type load balancer you need to start tunnel by executing command minikube tunnel and Traefik exposed ports will be binded to localhost belonging to your workstation.

Please note that in my workshop I use K3D - it is another alternative solution of deploying K8S cluster for learning purposes. Personally, I prefer that solution to deploy a cluster for testing / learning purposes.

Thank you,

1 Like

Hi @jakubhajek

Thank you very much for sharing the great and very intensive workshop video. Anyhow after finished watching, I cannot adapt to my current use case and situation as the following detail: -

  1. The Traefik is deployed inside the kubernetes via Helm and it serves ping, metrics and dashboard perfectly.
  2. On the other hand, my demo web application are not deployed inside the kubernetes. They reside at another machine with the plain old Tomcat and does not have any DNS.
  3. I would like to make the Traefik to be a proxy/load balancer for them.
  4. Inside the Traefik Pod. I'm able to use curl to make a connection to them via their IP address, but I would like to use the host name instead.
  5. At the moment, I've no any clue how to append the host mapping to the /etc/hosts of the Traefik Pod as the same as I used to map with extra_hosts within the docker-compose.

Could you please help to advise further?

hi @charleech

Thanks for your feedback.

If I correctly understood you, Traefik is deployed on Kubernetes and Tomcat is running as a standalne instance outside the cluster. You would like to use Traefik to act as load balancer for the Tomcat instance.

If so, you need to add to the Traefik's configuration FILE provider and create the configuration manually using Kubernetes ConfigMap.

In your file config you have to create router and service manually. You can of course follow our official docs to the File provider , routers and services that I highly encourage you to review. You can also use that example as a reference:

http:
  routers:
    router0:
      service: service0
      rule: Host(`tomcat-server.mydomain.com`)

  services:
    service0:
      loadBalancer:
        servers:
          - url: http://10.11.0.2:8080

In order to enable file provider and refer to the created ConfigMap see that:

Cheers,
Jakub

1 Like

Hi @jakubhajek,

Thank you for quick reply. May I ask further about the url: http://10.11.0.2:8080 ? Am I able to use the host name rather than the IP address? Something like append the additional to the /etc/hosts inside the traefik pod ? Or the only way is having to use the IP address ? Could you please help to advise further?

Yes, you can use hostname instead of IP. :wink: Make sure that the URL is reachable by Traefik.
Cheers, Jakub

Ahhh Yes :grinning:. That's my question and my trying to achieve. How to make that URL to be reachable by the Traefik?

I have no any clue and hope that there is a way to edit the /etc/hosts via values.yaml. e.g. by using hostAliases [1]. But sadly, the Traefik _podtemplate.tpl [2] does not provide this to be happened and lead me to post this topic here.

[1] Adding entries to Pod /etc/hosts with HostAliases | Kubernetes
[2] traefik-helm-chart/_podtemplate.tpl at master · traefik/traefik-helm-chart · GitHub