docker swarm work with traefik

创建网络

docker network create --driver=overlay traefik-net

使用swarm方式启动traefix

docker service create \  
    --name traefik \
    --constraint=node.role==manager \
    --publish 82:80 \
    --publish 8082:8080 \
    --mount type=bind,source=/var/run/docker.sock,target=/var/run/docker.sock \
    --network traefik-net \
    traefik \
    --docker \
    --docker.swarmmode \
    --docker.domain=<YOUR DOMAIN> \
    --docker.watch \
    --web

启动成功之后,可以用通:8082进入界面,可以看到一些状态

启动一个服务测试是否安装正常

  docker service create \
    --name whoami0 \
    --label traefik.port=80 \
    --network traefik-net \
    emilevauge/whoami

traefik.port 为容器中监听的端口,这个很重要,设置错误会访问得到bad gateway

curl -H host:whoami0.<YOUR DOMAIN>

会得到如下信息,表示发布成功

Hostname: 945c867b2525  
IP: 127.0.0.1  
IP: ::1  
IP: 10.0.0.11  
IP: 10.0.0.10  
IP: fe80::42:aff:fe00:b  
IP: 172.18.0.6  
IP: fe80::42:acff:fe12:6  
GET / HTTP/1.1  
Host: whoami1.<YOUR DOMAIN>  
User-Agent: curl/7.35.0  
Accept: */*  
Accept-Encoding: gzip  
X-Forwarded-For: 10.255.0.3  
X-Forwarded-Host: whoami1.<YOUR DOMAIN>  
X-Forwarded-Proto: http  
X-Forwarded-Server: 4b85d208e5e4