原文地址git
介紹一種生產環境Docker overlay network的配置方案。github
先講一下生產環境中的問題:docker
所以生產環境中會同時存在bash
docker service create
/docker stack deploy
建立的。docker run
/docker compose up
建立的。但願兩種應用可以鏈接到同一個overlay網絡,在網絡內部可以經過網絡
tasks.<service-name>
DNS name 鏈接到無狀態應用(見Container discovery)<container-name>
DNS name 鏈接到有狀態應用解決辦法:post
到manager節點上建立attachable的overlay network,名字叫作prod-overlay:3d
docker network create -d overlay --attachable prod-overlay
在manager節點上查看這個網絡是否建立成功:code
$ docker network ls NETWORK ID NAME DRIVER SCOPE fbfde97ed12a bridge bridge local 73ab6bbac970 docker_gwbridge bridge local a2adb3de5f7a host host local nm7pgzuh6ww4 ingress overlay swarm 638e550dab67 none null local qqf78g8iio10 prod-overlay overlay swarm
在worker節點上查看這個網絡,這時你看不到這個網絡,不過不要擔憂,當後面在worker節點上建立工做負載後就能看到了:部署
$ docker network ls NETWORK ID NAME DRIVER SCOPE fbfde97ed12a bridge bridge local 73ab6bbac970 docker_gwbridge bridge local a2adb3de5f7a host host local nm7pgzuh6ww4 ingress overlay swarm 638e550dab67 none null local
在manager上建立容器c1
,掛到prod-overlay
network上:get
docker run --name c1 --network prod-overlay -itd busybox
在worker上建立容器c2
,掛到prod-overlay
network上:
docker run --name c2 --network prod-overlay -itd busybox
在manager上建立service c
,掛到prod-overlay
network上:
docker service create -td --name c --replicas 2 --network prod-overlay busybox
以前在worker節點上沒有看到prod-overlay
network,如今你應該能夠看見了:
$ docker network ls NETWORK ID NAME DRIVER SCOPE 01180b9d4833 bridge bridge local cd94df435afc docker_gwbridge bridge local 74721e7670eb host host local nm7pgzuh6ww4 ingress overlay swarm 32e6853ea78d none null local dw8kd2nb2yl3 prod-overlay overlay swarm
到manager節點上,讓c1
ping c2
$ docker exec c1 ping -c 2 c2 PING c2 (10.0.2.2): 56 data bytes 64 bytes from 10.0.2.2: seq=0 ttl=64 time=0.682 ms 64 bytes from 10.0.2.2: seq=1 ttl=64 time=0.652 ms
到manager節點上,讓c1
ping tasks.c
,tasks.c
是以前建立的service c
的DNS name:
$ docker exec c1 ping -c 2 tasks.c PING tasks.c (10.0.2.8): 56 data bytes 64 bytes from 10.0.2.8: seq=0 ttl=64 time=2.772 ms 64 bytes from 10.0.2.8: seq=1 ttl=64 time=0.694 ms
到manager節點上,讓c1
查詢 tasks.c
的DNS name,能夠看到tasks.c
有兩條記錄:
$ docker exec c1 nslookup -type=a tasks.c Server: 127.0.0.11 Address: 127.0.0.11:53 Non-authoritative answer: Name: tasks.c Address: 10.0.2.7 Name: tasks.c Address: 10.0.2.8
到manager節點上,查看service c
的task,看到有c.1
、c.2
兩個task,分別部署在兩個節點上:
$ docker service ps c ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS p5n70vhtnz2f c.1 busybox:latest docker-learn-1 Running Running 17 minutes ago byuoox1t7cve c.2 busybox:latest docker-learn-2 Running Running 17 minutes ago
到c.1
task所在的節點上,查看task c.1
的容器名:
$ docker ps -f name=c.1 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 795a3bd3c20a busybox:latest "sh" 21 minutes ago Up 21 minutes c.1.p5n70vhtnz2f5q8p2pcvbyfmw
而後在c1
裏ping task c.1
的容器名:
$ docker exec c1 ping -c 2 c.1.p5n70vhtnz2f5q8p2pcvbyfmw PING c.1.p5n70vhtnz2f5q8p2pcvbyfmw (10.0.2.7): 56 data bytes 64 bytes from 10.0.2.7: seq=0 ttl=64 time=0.198 ms 64 bytes from 10.0.2.7: seq=1 ttl=64 time=0.128 ms
你一樣能夠:
在c2
裏:
c1
tasks.c
c.1
、c.2
的容器在task c.1
、c.2
的容器裏:
c1
、c2
;tasks.c
c.1
、c.2
的容器經過docker run
/ docker compose up
建立的容器的名字,要保證在整個集羣裏是惟一的。docker 不會幫你檢查名稱衝突的狀況,若是名稱衝突了那麼會獲得錯誤的DNS結果。