Skip to main content
  1. Posts/

Konfigurasi Routing di Traefik

·2 mins·
traefik traefik docker
Table of Contents

Saat menggunakan traefik di docker, Traefik menggunakan containers label untuk mengambil konfigurasi perutean. Sehingga pada panduan ini akan membahas beberapa cara untuk merutekan request yang masuk ke traefik agar dapat didistribusikan ke container sesuai kebutuhan.

Routing Configurations
#

Docker CLI

docker run -dit --name test \
-l test --label 'traefik.http.routers.test3.rule=Host(`web5.example.com`)' \
traefik/whoami
Host subdomain dapat Anda sesuaikan dan pastikan subdomain sudah disetting A record ke tujuan host traefik

Docker compose

version: '3'

services:
...
  web:
   image: traefik/whoami
   labels:
    - traefik.http.routers.web.rule=Host(`web5.example.com`)

List rules yang dapat Anda gunakan.

Rule Description
Headers(`key`, `value`) Check if there is a key keydefined in the headers, with the value value
HeadersRegexp(`key`, `regexp`) Check if there is a key keydefined in the headers, with a value that matches the regular expression regexp
Host(`example.com`, ...) Check if the request domain (host header value) targets one of the given domains.
HostHeader(`example.com`, ...) Same as Host, only exists for historical reasons.
HostRegexp(`example.com`, `{subdomain:[a-z]+}.example.com`, ...) Match the request domain. See “Regexp Syntax” below.
Method(`GET`, ...) Check if the request method is one of the given methods (GET, POST, PUT, DELETE, PATCH, HEAD)
Path(`/path`, `/articles/{cat:[a-z]+}/{id:[0-9]+}`, ...) Match exact request path. See “Regexp Syntax” below.
PathPrefix(`/products/`, `/articles/{cat:[a-z]+}/{id:[0-9]+}`) Match request prefix path. See “Regexp Syntax” below.
Query(`foo=bar`, `bar=baz`) Match Query String parameters. It accepts a sequence of key=value pairs.
ClientIP(`10.0.0.0/16`, `::1`) Match if the request client IP is one of the given IP/CIDR. It accepts IPv4, IPv6 and CIDR formats.

Port Detection
#

Untuk simulasinya adalah menginstall MinIO lalu setting traefik ke tujuan dashboard MinIO (:9090) agar dapat diakses dengan domain atau subdomain.

Docker CLI

Install MinIO

docker run -dit --name minio \
-p 9000:9000 -p 9090:9090 \
-v /mnt/disk1:/data \
-l minio --label 'traefik.http.routers.minio.rule=Host(`web5.example.com`)' \
--label 'traefik.http.services.minio.loadbalancer.server.port=9090' \
-e MINIO_ROOT_USER=admin \
-e MINIO_ROOT_PASSWORD=admin123 \
minio/minio server /data --console-address ":9090"

Lalu install traefik

docker run -dit --name traefik \
-p 8080:8080 -p 80:80 \
-v /opt/traefik/traefik.yml:/etc/traefik/traefik.yml \
-v /var/run/docker.sock:/var/run/docker.sock \
traefik --api=true --api.debug=true --api.dashboard=true --api.insecure=true

Selanjutnya coba akses http://web5.example.com

Docker compose

version: '3'

services:
  traefik:
   image: traefik
   command: --api=true --api.debug=true --api.dashboard=true --api.insecure=true
   ports:
    - "80:80"
    - "8080:8080"
   volumes:
    - "/root/traefik/traefik.yml:/etc/traefik/traefik.yml"
    - "/var/run/docker.sock:/var/run/docker.sock"

  minio:
   image: minio/minio
   command: server /data --console-address ":9090"
   environment:
    - "MINIO_ROOT_USER=admin"
    - "MINIO_ROOT_PASSWORD=admin123"
   ports:
    - 9000:9000
   labels:
    - "traefik.http.routers.minio.rule=Host(`web5.example.com`)"
    - "traefik.http.services.minio.loadbalancer.server.port=9090"
   volumes:
    - "/mnt/disk1:/data"

Deploy stack

docker stack deploy -c docker-compose.yml traefik

Selanjutnya coba akses http://web5.example.com

Referensi :

Related

Install Traefik di Docker
·3 mins
traefik docker traefik
Install MinIO di Docker
·2 mins
minio docker minio
Install Consul dengan Docker
·2 mins
consul docker consul
Configure Network Aliases Docker
·2 mins
docker docker
Using IPvlan network in Docker
·5 mins
docker docker
Using Host Network in Docker
·1 min
docker docker