Traefik for the beginners

May 4, 2018
Docker traefik

Howto setup traefik for the beginners

In this post, I will explain you how to setup and test traefik. The level of this howto is beginner 😄

In the next episodes, you will see how to use more traefik’s capabilities.


What is traefik ?

Copied from the documentation : Træfik (pronounced like traffic) is a modern HTTP reverse proxy and load balancer made to deploy microservices with ease.

You will find more informations on the Traefik website.

And don’t forget the documentation


Setup your environment


Launch traefik

$ docker run --rm -p 80:80 -p 8080:8080 -v /var/run/docker.sock:/var/run/docker.sock --network test traefik:1.5-alpine \
         --api --loglevel=debug --entryPoints="Name:http Address::80" \
         --docker --docker.endpoint="unix:///var/run/docker.sock"

You will see something like this :

time="2018-05-04T14:13:39Z" level=info msg="Traefik version v1.5.4 built on 2018-03-15_01:35:21PM"             
time="2018-05-04T14:13:39Z" level=info msg="                                                            
Stats collection is disabled.                                                                
Help us improve Traefik by turning this feature on :)                            
More details on: https://docs.traefik.io/basics/#collected-data                    
"
time="2018-05-04T14:13:39Z" level=debug msg="Global configuration loaded {"LifeCycle":{"RequestAcceptGraceTimeout":0,"GraceTimeOut":10000000000},"GraceTimeOut":0,"Debug":false,"CheckNewVersi
on":true,"SendAnonymousUsage":false,"AccessLogsFile":"","AccessLog":null,"TraefikLogsFile":"","TraefikLog":null,"LogLevel":"debug","EntryPoints":{"http":{"Network":"","Address":":80","TLS":n
ull,"Redirect":null,"Auth":null,"WhitelistSourceRange":[],"Compress":false,"ProxyProtocol":null,"ForwardedHeaders":{"Insecure":true,"TrustedIPs":null}},"traefik":{"Network":"","Address":":80
80","TLS":null,"Redirect":null,"Auth":null,"WhitelistSourceRange":null,"Compress":false,"ProxyProtocol":null,"ForwardedHeaders":{"Insecure":true,"TrustedIPs":null}}},"Cluster":null,"Constrai
nts":[],"ACME":null,"DefaultEntryPoints":["http"],"ProvidersThrottleDuration":2000000000,"MaxIdleConnsPerHost":200,"IdleTimeout":0,"InsecureSkipVerify":false,"RootCAs":null,"Retry":null,"Hea
lthCheck":{"Interval":30000000000},"RespondingTimeouts":null,"ForwardingTimeouts":null,"Web":{"Address":":8080","CertFile":"","KeyFile":"","ReadOnly":false,"Statistics":null,"Metrics":null,"
Path":"/","Auth":null,"Debug":false},"Docker":{"Watch":true,"Filename":"","Constraints":null,"Trace":false,"DebugLogGeneratedTemplate":false,"Endpoint":"unix:///var/run/docker.sock","Domain"
:"","TLS":null,"ExposedByDefault":true,"UseBindPortIP":false,"SwarmMode":false},"File":null,"Marathon":null,"Consul":null,"ConsulCatalog":null,"Etcd":null,"Zookeeper":null,"Boltdb":null,"Kub
ernetes":null,"Mesos":null,"Eureka":null,"ECS":null,"Rancher":null,"DynamoDB":null,"ServiceFabric":null,"Rest":null,"API":{"EntryPoint":"traefik","Dashboard":true,"Debug":false,"CurrentConfi
gurations":null,"Statistics":null},"Metrics":null,"Ping":{"EntryPoint":"traefik"}}"
time="2018-05-04T14:13:39Z" level=info msg="Preparing server http &{Network: Address::80 TLS:<nil> Redirect:<nil> Auth:<nil> WhitelistSourceRange:[] Compress:false ProxyProtocol:<nil> Forwar
dedHeaders:0xc4205e0440} with readTimeout=0s writeTimeout=0s idleTimeout=3m0s"
time="2018-05-04T14:13:39Z" level=info msg="Preparing server traefik &{Network: Address::8080 TLS:<nil> Redirect:<nil> Auth:<nil> WhitelistSourceRange:[] Compress:false ProxyProtocol:<nil> F
orwardedHeaders:0xc4205e0560} with readTimeout=0s writeTimeout=0s idleTimeout=3m0s"
time="2018-05-04T14:13:39Z" level=info msg="Starting server on :80"
time="2018-05-04T14:13:39Z" level=info msg="Starting server on :8080"
time="2018-05-04T14:13:39Z" level=info msg="Starting provider *docker.Provider {"Watch":true,"Filename":"","Constraints":null,"Trace":false,"DebugLogGeneratedTemplate":false,"Endpoint":"unix
:///var/run/docker.sock","Domain":"","TLS":null,"ExposedByDefault":true,"UseBindPortIP":false,"SwarmMode":false}"
time="2018-05-04T14:13:39Z" level=debug msg="Provider connection established with docker 18.03.0-ce (API 1.37)"
time="2018-05-04T14:13:39Z" level=debug msg="Filtering container with empty frontend rule /musing_kalam"
time="2018-05-04T14:13:39Z" level=debug msg="Configuration received from provider docker: {}"
time="2018-05-04T14:13:39Z" level=info msg="Server configuration reloaded on :80"
time="2018-05-04T14:13:39Z" level=info msg="Server configuration reloaded on :8080"

Now i will explain you all the options:

Now go to http://localhost:8080 and you will see something like this:

Traefik dashboard

Traefik health

They look empty but it’s normal 😏


Use traefik with Nginx

Now that traefik is up and running, we will launch a nginx container and tell traefik to redirect the http traffic to it.

Traefik dashboard with Nginx container

Traefik health after a couple of tests


Conclusion

Now you know how to test traefik. It’s very powerfull. In the next posts, we will see more options : https, more matchers, restrictions, etc..

Stay tuned 😏

Use constraints with Swarm

September 4, 2018
Docker

Setup a Let's encrypt certificate with Traefik

May 23, 2018
Docker traefik

Docker LEMP stack: get your logs with rsyslog

February 21, 2018
Docker