Breaking News
Home / How To / How to install and use Traefik as a reverse proxy with Docker on CentOS 8

How to install and use Traefik as a reverse proxy with Docker on CentOS 8



Traefik is an open source HTTP reverse proxy and load balancer that helps you distribute microservices easily. You can easily integrate it with your infrastructure components including Docker, Kubernetes, Docker Swarm, Rancher, AWS and many more. If you want to run multiple applications on the same Docker host, you can use Traefik as a reverse proxy to expose ports 80 and 443 to the rest of the world.

In this tutorial we will show you how to install and configure Traefik as a reverse proxy on CentOS 8.

Conditions

  • A server running CentOS 8.
  • A valid domain name pointed to your server’s IP.
  • A root password is configured on the server.

Install Docker and Docker Compose

First you need to install the latest version of Docker and Docker Compose in your system. By default, Docker is not available in the standard CentOS 8 rope. So you need to add Docker CE repo to your system. You can add it with the following command:

dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo

Once the repo has been created, install Docker CE with the following command:

dnf install docker-ce --nobest -y

Once Docker is installed, start the Docker service and allow it to start at system startup with the following command:

systemctl start docker
systemctl enable docker

To install Docker Compose, you need to install Curl and download the Docker Compose binary into your system. You can do this with the following command:

dnf install curl
curl -L "https://github.com/docker/compose/releases/download/1.25.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

Create Docker networks

Then you need to create a new Docker network to run the traffic container. You can create it with the name “web” by running the following command:

docker network create web

Then check your network with the following command:

docker network ls

You should get the following output:

NETWORK ID          NAME                DRIVER              SCOPE
76a9475e5ab0        bridge              bridge              local
2a3d79f64747        host                host                local
4c9123229eaa        none                null                local
0b6e010b43d0        web                 bridge              local

Install and configure Traefik Proxy

Before you begin, you must install httpd tools to create the encrypted password. You can install this tool with the following command:

dnf install httpd-tools -y

Once installed, generate the encrypted password using the following command:

htpasswd -nb admin my_password

You should get the following output:

admin:$apr1$wpdR.4yZ$UpbjGS7ZFq4pZHalhH1Tl0

Then create a new Traefik configuration file with the following command:

nano traefik.toml

Add the following lines, including your valid domain name, secure password, and port:

defaultEntryPoints = ["http", "https"]

[entryPoints]
  [entryPoints.dashboard]
    address = ":8080"
    [entryPoints.dashboard.auth]
      [entryPoints.dashboard.auth.basic]
        users = ["admin:$apr1$wpdR.4yZ$UpbjGS7ZFq4pZHalhH1Tl0"]
  [entryPoints.http]
    address = ":80"
      [entryPoints.http.redirect]
        entryPoint = "https"
  [entryPoints.https]
    address = ":443"
      [entryPoints.https.tls]

[api]
entrypoint="dashboard"

[acme]
email = "[email protected]"
storage = "acme.json"
entryPoint = "https"
onHostRule = true
  [acme.httpChallenge]
  entryPoint = "http"

[docker]
domain = "traefik.linuxbuz.com"
watch = true
network = "web"

Save and close the file when you are done. Then create a blank file to store your Let us encrypt information.

touch acme.json

Then grant the correct permission to this file with the following command:

chmod 600 acme.json

Create a Docker Compose file

Next, you will need to create a new docker-compose.yml file and define your domain, port, network and volume.

You can create it with the following command:

nano docker-compose.yml

Add the following lines:

version: '3'

services:

  traefik:
    image: traefik:1.7.21-alpine
    command: --docker --docker.domain=linuxbuz.com
    ports:
      - 80:80
      - 443:443
    networks:
      - web
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./traefik.toml:/traefik.toml
      - ./acme.json:/acme.json
    labels:
      - "traefik.frontend.rule=Host:traefik.linuxbuz.com"
      - "traefik.port=8080"
    container_name: traefik
    restart: always

networks:
   web:
    external: true

Save and close the file when you are done.

Build Traefik Docker Container

At this time, the Traefik and Docker Compose configuration file is ready. Now build the Traefik container with the following command:

docker-compose up -d

You should get the following output:

Unable to find image 'traefik:1.7.21-alpine' locally
1.7.21-alpine: Pulling from library/traefik
c9b1b535fdd9: Pull complete 
c141e361698e: Pull complete 
2573c02b6f16: Pull complete 
1619fb3841d1: Pull complete 
Digest: sha256:0e4ac8ae724603898620dbd5eb9cda7ec05f405d25476eb0d32b716b490ba079
Status: Downloaded newer image for traefik:1.7.21-alpine
27e735357bb3b7d2d253ff78ddf6e78896851420eff399e8ade23affc7ff0bf7

Then check the running container with the following command:

docker ps

You should get the following output:

CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                                      NAMES
7604696507a5        traefik:1.7.21-alpine   "/entrypoint.sh --do…"   6 seconds ago       Up 4 seconds        0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp   traefik

Go to Traefik web interface

Now open your browser and open Traefik’s web interface with URL https://traefik.linuxbuz.com. You will be asked to enter your username and password:Advertisement

Traffic login

Enter the username as admin and password you have generated previously and click on Sign in button. You should see the Traefik instrument panel on the following screen:

Traffic Dashboard

click HEALTH tab. You should see the health condition of the Traefik container on the following screen:

Traffic Traffic Statistics

Conclusion

Congratulations! you have installed Traefik proxy on CentOS 8. For more information, see Traefik’s documentation.


Source link