Traefik是一个开源反向代理,可轻松处理微服务和/或仅包含应用程序的容器。
反向代理服务器(反向代理,反向代理)用于将来自外部网络的请求中继到内部网络上的任何服务器/服务(例如,Web服务器,数据库或文件存储),并允许:
- 确保隐藏内部网络的结构及其内部服务的详细信息;
- 在相同服务或具有相同任务的服务器之间执行负载平衡;
- 在客户端和任何服务之间提供加密的(HTTPS)连接,在这种情况下,客户端和代理之间将创建SSL会话,并且内部网络上的代理和服务之间将建立未加密的HTTP连接,如果该服务支持HTTPS,则可以在内部网络上组织加密的连接;
- 组织对服务的访问控制(客户端身份验证),以及安装防火墙(防火墙)。
本文将介绍在Docker中使用Traefik作为其他Docker容器以及非容器化服务的反向代理。
介绍
Traefik “Edge Router”, . , , : -, Traefik ; -, Traefik EE — , HA (Hight Availability, ), (), , . , Traefik.
Traefik (“ ”) , .
:
- Docker
- Kubernetes
- Consul Catalog
- Marathon
- Rancher
- File
.
, , — “File”, ( ), - , , -. .
Traefik, “File” TOML YAML, YAML , - , . Traefik Docker. docker-compose, .
* Linux.
Traefik
docker docker-compose, .
traefik
, ,
mkdir ~/traefik
cd ~/traefik
() Traefik docker-compose.yml
. :
version: '3'
services:
traefik:
image: traefik:v2.2
container_name: traefik
restart: unless-stopped
security_opt:
- no-new-privileges:true
ports:
- 80:80
- 443:443
volumes:
- /etc/localtime:/etc/localtime:ro
- /var/run/docker.sock:/var/run/docker.sock:ro
- ./data/traefik.yml:/traefik.yml:ro
80 443 HTTP HTTPS . Docker . Traefik traefik.yml
data
.
networks Docker-, Traefik .
.
( , ):
entryPoints:
http:
address: ":80"
https:
address: ":443"
http
https
( , a
b
) .
— Docker, :
providers:
docker:
endpoint: "unix:///var/run/docker.sock"
exposedByDefault: false
Traefik , . — Traefik ( ).
HTTP HTTPS ( ):
http:
routers:
http-catchall:
rule: HostRegexp(`{host:.+}`)
entrypoints:
- http
middlewares:
- redirect-to-https
middlewares:
redirect-to-https:
redirectScheme:
scheme: https
permanent: false
Traefik HTTP , TCP UDP, http
.
Traefik 2 routers () middlewares( ), .
:
http-catchall
— , ,http
Traefik;rule:
— , ,HostRegexp
,Host
.+
( ), Traefik — (host
),{name:reg_exp}
;entrypoints
— , ,http
;middlewares
— , ( ).
redirect-to-https
— , ,http
Traefik;redirectScheme
— , ;scheme: https
— HTTPS ;permanent: false
— .
entryPoints:
http:
address: ":80"
https:
address: ":443"
http:
routers:
http-catchall:
rule: hostregexp(`{host:.+}`)
entrypoints:
- http
middlewares:
- redirect-to-https
middlewares:
redirect-to-https:
redirectScheme:
scheme: https
permanent: false
providers:
docker:
endpoint: "unix:///var/run/docker.sock"
exposedByDefault: false
.
sudo docker-compose up -d
, (sudo docker-compose logs -f
) , .
Let's Encrypt
HTTPS - SSL , , Let's Encrypt.
(traefik.yml
) :
certificatesResolvers:
letsEncrypt:
acme:
email: postmaster@example.com
storage: acme.json
caServer: "https://acme-staging-v02.api.letsencrypt.org/directory"
httpChallenge:
entryPoint: http
:
letsEncrypt
— ;acme
— ( - );storage
— , ;httpChallenge
— acme-, — ;caServer: "https://acme-staging-v02.api.letsencrypt.org/directory"
— Let's Encrypt , API ( , ).
volumes
docker-compose.yml
, ( data/acme.json
):
volumes:
- /etc/localtime:/etc/localtime:ro
- /var/run/docker.sock:/var/run/docker.sock:ro
- ./data/traefik.yml:/traefik.yml:ro
- ./data/acme.json:/acme.json
Docker
HTTPS , , Traefik, Traefik Docker, .
Docker Traefik (labels) . docker-compose.yml
:
labels:
- "traefik.enable=true"
- "traefik.http.routers.traefik.entrypoints=https"
- "traefik.http.routers.traefik.rule=Host(`traefik.example.com`)"
- "traefik.http.routers.traefik.tls=true"
- "traefik.http.routers.traefik.tls.certresolver=letsEncrypt"
- "traefik.http.routers.traefik.service=api@internal"
- "traefik.http.services.traefik-traefik.loadbalancer.server.port=888"
:
traefik.enable=true
— Traefik , ;
traefik.http.routers.traefik.entrypoints=https
— https
;
traefik.http.routers.traefik.rule=Host(
traefik.example.com)
— traefik.example.com;
traefik.http.routers.traefik.tls=true
— TLS;
traefik.http.routers.traefik.tls.certresolver=letsEncrypt
— ;
traefik.http.routers.traefik.service=api@internal
— , — api@internal
, , , ;
traefik.http.services.traefik-traefik.loadbalancer.server.port=888
— , , .
, traefik.yml
:
api:
dashboard: true
( docker-compose.yml
):
sudo docker-compose down && sudo docker-compose up -d
traefik.example.com
( , Traefik) .
, , , BasicAuth, Traefik middleware.
(admin/password)^
$ htpasswd -nb admin password
admin:$apr1$vDSqkf.v$GTJOtsd9CBiAFFnHTI2Ds1
docker-compose.yml
:
- "traefik.http.middlewares.traefik-auth.basicauth.users=admin:$$apr1$$vDSqkf.v$$GTJOtsd9CBiAFFnHTI2Ds1"
- "traefik.http.routers.traefik.middlewares=traefik-auth"
, $
$$
.
traefik.http.middlewares.traefik-auth.basicauth.users=...
— middleware basicauth
users
;
traefik.http.routers.traefik.middlewares=traefik-auth
— traefik
- middleware.
version: '3'
services:
traefik:
image: traefik:v2.2
container_name: traefik
restart: unless-stopped
security_opt:
- no-new-privileges:true
ports:
- 80:80
- 443:443
volumes:
- /etc/localtime:/etc/localtime:ro
- /var/run/docker.sock:/var/run/docker.sock:ro
- ./data/traefik.yml:/traefik.yml:ro
- ./data/acme.json:/acme.json
labels:
- "traefik.enable=true"
- "traefik.http.routers.traefik.entrypoints=https"
- "traefik.http.routers.traefik.rule=Host(`traefik.example.com`)"
- "traefik.http.routers.traefik.tls=true"
- "traefik.http.routers.traefik.tls.certresolver=letsEncrypt"
- "traefik.http.routers.traefik.service=api@internal"
- "traefik.http.services.traefik-traefik.loadbalancer.server.port=888"
- "traefik.http.middlewares.traefik-auth.basicauth.users=admin:$$apr1$$vDSqkf.v$$GTJOtsd9CBiAFFnHTI2Ds1"
- "traefik.http.routers.traefik.middlewares=traefik-auth"
.
, docker-compose ( docker):
labels:
- "traefik.enable=true"
- "traefik.http.routers.test.entrypoints=https"
- "traefik.http.routers.test.rule=Host(`test.example.com`)"
- "traefik.http.routers.test.tls=true"
- "traefik.http.routers.test.tls.certresolver=letsEncrypt"
- "traefik.http.services.test-service.loadbalancer.server.port=80"
traefik.http.services.test-service.loadbalancer.server.port=80
— test-service
80, test
, Traefik , .
File
, - ( IP 192.168.1.222 8080) , HTTPS. .
docker-compose.yml
volume
:
volumes:
- /etc/localtime:/etc/localtime:ro
- /var/run/docker.sock:/var/run/docker.sock:ro
- ./data/traefik.yml:/traefik.yml:ro
- ./data/custom/:/custom/:ro
- ./data/acme.json:/acme.json
data/custom/
( , ).
traefik.yml
file :
providers:
...
file:
directory: /custom
watch: true
docker-compose.yml
, watch: true
Traefik ( “ ”, , ).
Traefik (data/custom/host.yml
):
http:
routers:
host:
entryPoints:
- https
service: service-host
rule: Host(`host.example.com`)
tls:
certResolver: letsEncrypt
services:
service-host:
loadBalancer:
servers:
- url: http://192.168.1.222:8080/
passHostHeader: true
, service: service-host
— , TLS.
:
_:
loadBalancer:
servers:
-
- ...
passHostHeader: true
, .
:
version: '3'
services:
traefik:
image: traefik:v2.2
container_name: traefik
restart: unless-stopped
security_opt:
- no-new-privileges:true
ports:
- 80:80
- 443:443
volumes:
- /etc/localtime:/etc/localtime:ro
- /var/run/docker.sock:/var/run/docker.sock:ro
- ./data/traefik.yml:/traefik.yml:ro
- ./data/custom/:/custom/:ro
- ./data/acme.json:/acme.json
labels:
- "traefik.enable=true"
- "traefik.http.routers.traefik.entrypoints=https"
- "traefik.http.routers.traefik.rule=Host(`traefik.example.com`)"
- "traefik.http.routers.traefik.tls=true"
- "traefik.http.routers.traefik.tls.certresolver=letsEncrypt"
- "traefik.http.routers.traefik.service=api@internal"
- "traefik.http.services.traefik-traefik.loadbalancer.server.port=888"
- "traefik.http.middlewares.traefik-auth.basicauth.users=admin:$$apr1$$vDSqkf.v$$GTJOtsd9CBiAFFnHTI2Ds1"
- "traefik.http.routers.traefik.middlewares=traefik-auth"
api:
dashboard: true
entryPoints:
http:
address: ":80"
https:
address: ":443"
http:
routers:
http-catchall:
rule: hostregexp(`{host:.+}`)
entrypoints:
- http
middlewares:
- redirect-to-https
middlewares:
redirect-to-https:
redirectScheme:
scheme: https
permanent: false
providers:
docker:
endpoint: "unix:///var/run/docker.sock"
exposedByDefault: false
file:
directory: /custom
watch: true
certificatesResolvers:
letsEncrypt:
acme:
email: postmaster@example.com
storage: acme.json
#caServer: "https://acme-staging-v02.api.letsencrypt.org/directory"
httpChallenge:
entryPoint: http
http:
routers:
host:
entryPoints:
- https
service: service-host
rule: Host(`host.example.com`)
tls:
certResolver: letsEncrypt
services:
service-host:
loadBalancer:
servers:
- url: http://192.168.1.222:8080/
passHostHeader: true
Traefik HTTP Docker File. SSL Let's Encrypt, HTTPS, .
TCP UDP ( , — TCP), , Traefik .
.
Traefik允许您以各种格式收集有关您的工作的信息,请考虑使用Prometheus如何完成此工作。
添加一个新的入口点
data/traefik.yml
::
entryPoints:
...
metrics:
address: ":8082"
docker-compose.yml
:
ports:
- 80:80
- 443:443
- 8082:8082
并添加了从以下端口收集Prometheus指标的功能data/traefik.yml
:
metrics:
prometheus:
entryPoint: metrics
仅需配置Prometheus即可从中收集指标traefik_ip:8082
。
我们使用获得的配置给出文件的内容:
version: '3'
services:
traefik:
image: traefik:v2.2
container_name: traefik
restart: unless-stopped
security_opt:
- no-new-privileges:true
ports:
- 80:80
- 443:443
- 8082:8082
volumes:
- /etc/localtime:/etc/localtime:ro
- /var/run/docker.sock:/var/run/docker.sock:ro
- ./data/traefik.yml:/traefik.yml:ro
- ./data/custom/:/custom/:ro
- ./data/acme.json:/acme.json
labels:
- "traefik.enable=true"
- "traefik.http.routers.traefik.entrypoints=https"
- "traefik.http.routers.traefik.rule=Host(`traefik.example.com`)"
- "traefik.http.routers.traefik.tls=true"
- "traefik.http.routers.traefik.tls.certresolver=letsEncrypt"
- "traefik.http.routers.traefik.service=api@internal"
- "traefik.http.services.traefik-traefik.loadbalancer.server.port=888"
- "traefik.http.middlewares.traefik-auth.basicauth.users=admin:$$apr1$$vDSqkf.v$$GTJOtsd9CBiAFFnHTI2Ds1"
- "traefik.http.routers.traefik.middlewares=traefik-auth"
api:
dashboard: true
entryPoints:
http:
address: ":80"
https:
address: ":443"
metrics:
address: ":8082"
metrics:
prometheus:
entryPoint: metrics
http:
routers:
http-catchall:
rule: hostregexp(`{host:.+}`)
entrypoints:
- http
middlewares:
- redirect-to-https
middlewares:
redirect-to-https:
redirectScheme:
scheme: https
permanent: false
providers:
docker:
endpoint: "unix:///var/run/docker.sock"
exposedByDefault: false
file:
directory: /custom
watch: true
certificatesResolvers:
letsEncrypt:
acme:
email: postmaster@example.com
storage: acme.json
#caServer: "https://acme-staging-v02.api.letsencrypt.org/directory"
httpChallenge:
entryPoint: http