爲支持容器跨主機通訊,Docker 提供了 overlay driver,使用戶能夠建立基於 VxLAN 的 overlay 網絡。VxLAN 可將二層數據封裝到 UDP 進行傳輸,VxLAN 提供與 VLAN 相同的以太網二層服務,可是擁有更強的擴展性和靈活性。docker
Docerk overlay 網絡須要一個 key-value 數據庫用於保存網絡狀態信息,包括 Network、Endpoint、IP 等。Consul、Etcd 和 ZooKeeper 都是 Docker 支持的 key-vlaue 軟件,咱們這裏使用 Consul。數據庫
咱們會直接使用上一章 docker-machine 建立的實驗環境。在 docker 主機 host1(10.0.0.21)和 host2(10.0.0.22)上實踐各類跨主機網絡方案,在 10.0.0.20 上部署支持的組件,好比 Consul。bootstrap
最簡單的方式是以容器方式運行 Consul:網絡
root@cuiyongchao:~# docker run -d -p 8500:8500 -h consul --name consul progrium/consul -server -bootstrap Unable to find image 'progrium/consul:latest' locally latest: Pulling from progrium/consul Image docker.io/progrium/consul:latest uses outdated schema1 manifest format. Please upgrade to a schema2 image for better future compatibility. More information at https://docs.docker.com/registry/spec/deprecated-schema-v1/ c862d82a67a2: Pull complete 0e7f3c08384e: Pull complete 0e221e32327a: Pull complete 09a952464e47: Pull complete 60a1b927414d: Pull complete 4c9f46b5ccce: Pull complete 417d86672aa4: Pull complete b0d47ad24447: Pull complete fd5300bd53f0: Pull complete a3ed95caeb02: Pull complete d023b445076e: Pull complete ba8851f89e33: Pull complete 5d1cefca2a28: Pull complete Digest: sha256:8cc8023462905929df9a79ff67ee435a36848ce7a10f18d6d0faba9306b97274 Status: Downloaded newer image for progrium/consul:latest dd2630bfaa0d702a3bcaaccb2d5ac5de206bb5306bdef64e7c1196323a9bfe65 root@cuiyongchao:~#
容器啓動後,能夠經過10.0.0.20:8500 訪問 Consul。tcp
接下來修改 host1 和 host2 的 docker daemon 的配置文件/etc/systemd/system/docker.service.d/10-machine.conf。ide
root@host1:~# vi /lib/systemd/system/docker.service ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2376 -H unix:///var/run/docker.sock --storage-driver overlay2 --tlsverify --tlscacert /etc/docker/ca.pem --tlscert /etc/docker/server.pem --tlskey /etc/docker/server-key.pem --label provider=generic --insecure-registry 10.0.0.20:5000 --cluster-store consul://10.0.0.20:8500 --cluster-advertise 10.0.0.22:2376 --insecure-registry 10.0.0.20:5000 --cluster-store consul://10.0.0.20:8500 --cluster-advertise 10.0.0.21:2376 root@host1:~#
--cluster-store
指定 consul 的地址。 --cluster-advertise
告知 consul 本身的鏈接地址。ui
重啓 docker daemon。unix
systemctl daemon-reload systemctl restart docker.service
host1 和 host2 將自動註冊到 Consul 數據庫中。rest