在單機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:\Windows\System32\drivers\etc\hosts文件:
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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM