在单机Docker上安装 Traefik 反向代理-负载均衡器

一、创建Traefik和容器应用的连接网络

sudo docker network create traefik-net

二、下载Traefik样本配置文件
wget https://raw.githubusercontent.com/containous/traefik/master/traefik.sample.toml

三、编辑Traefik配置文件
sudo cp ./traefik.sample.toml ./traefik.toml
sudo vi ./traefik.toml

编辑traefik.toml文件内容:

debug = false
logLevel = "ERROR"
defaultEntryPoints = ["http"]

[entryPoints]
[entryPoints.http]
address = ":80"

[retry]

[docker]
endpoint = "unix:///var/run/docker.sock"
domain = "docker.localhost"
watch = true
exposedByDefault = false
usebindportip = true
swarmMode = false
network = "traefik-net"

四、在主机上运行Traefik反向代理容器

Docker运行 Traefik 命令:

docker run -d
--name traefik
-p 8080:8080
-p 80:80
-v $PWD/traefik.toml:/etc/traefik/traefik.toml
-v /var/run/docker.sock:/var/run/docker.sock
traefik


五、运行Docker容器应用
运行 Docker 容器应用web1:
docker run -d
--name nginx
--label "traefik.backend=nginx"
--label "traefik.protocol=http"
--label "traefik.port=80"
--label "traefik.docker.network=traefik-net"
--label "traefik.domain=abc.com"
--label "traefik.frontend.rule=Host:web1.abc.com"
--label "traefik.enable=true"
nginx

运行 Docker 容器应用web2:
docker run -d
--name httpd
--label "traefik.backend=httpd"
--label "traefik.protocol=http"
--label "traefik.port=80"
--label "traefik.docker.network=traefik-net"
--label "traefik.domain=abc.com"
--label "traefik.frontend.rule=Host:web2.abc.com"
--label "traefik.enable=true"
httpd

六、配置客户端 hosts 主机文件,通过浏览器访问容器应用
修改 Win7 客户端电脑上 C:WindowsSystem32driversetchosts文件:
192.168.3.168 web1.abc.com
192.168.3.168 web2.abc.com

通过客户端电脑上的浏览器访问容器应用
http://web1.abc.com
http://web2.abc.com

七、查看Traefik管理后台
http://192.168.3.168:8080


注意事项:运行容器应用时,--label 中,等号左右不能有空格!


附图:
00-traefik.toml配置文件

01-运行 Traefik容器

02-运行 web1 和 web2 容器应用

03-运行中的容器

04-Traefik 管理后台

05-Traefik 管理后台2

06-通过web1.abc.com访问网站

07-通过web2.abc.com访问网站

 

参考链接:

https://docs.traefik.io/configuration/backends/docker/#docker

原文地址:https://www.cnblogs.com/rancher-maomao/p/10003570.html