《Docker》_通过etcd和overlay网络实现多机通信

通过zookeeper的watcher机制联想到了这个同步机制,当在容器1与容器2自动通信,原理是etcd(分布式数据库)存能互相访问的ip及端口通过overlay网络,记录一下

环境准备

1.俩台能ping通的docker环境
2.通过weget下载好etcd
3.在docker-node1上

vagrant@docker-node1:~$ wget [https://github.com/coreos/etcd/releases/download/v3.0.12/etcd-v3.0.12-linux-amd64.tar.gz](https://github.com/coreos/etcd/releases/download/v3.0.12/etcd-v3.0.12-linux-amd64.tar.gz)

vagrant@docker-node1:~$ tar zxvf etcd-v3.0.12-linux-amd64.tar.gz

vagrant@docker-node1:~$ cd etcd-v3.0.12-linux-amd64

vagrant@docker-node1:~$ nohup ./etcd --name docker-node1 --initial-advertise-peer-urls [http://192.168.59.2:2380](http://192.168.59.2:2380/)\

--listen-peer-urls [http://192.168.59.2:2380](http://192.168.59.2:2380/)\

--listen-client-urls [http://192.168.59.2:2379,http://127.0.0.1:2379](http://192.168.59.2:2379,http://127.0.0.1:2379)\

--advertise-client-urls [http://192.168.59.2:2379](http://192.168.59.2:2379/)\

--initial-cluster-token etcd-cluster \

--initial-cluster docker-node1=[http://192.168.59.2:2380,docker-node2=http://192.168.59.3:2380](http://192.168.59.2:2380,docker-node2=http://192.168.59.3:2380)\

--initial-cluster-state new&

4.在docker-node2上


vagrant@docker-node2:~$ wget [https://github.com/coreos/etcd/releases/download/v3.0.12/etcd-v3.0.12-linux-amd64.tar.gz](https://github.com/coreos/etcd/releases/download/v3.0.12/etcd-v3.0.12-linux-amd64.tar.gz)

vagrant@docker-node2:~$ tar zxvf etcd-v3.0.12-linux-amd64.tar.gz

vagrant@docker-node2:~$ cd etcd-v3.0.12-linux-amd64/

vagrant@docker-node2:~$ nohup ./etcd --name docker-node2 --initial-advertise-peer-urls [http://192.168.59.3:2380](http://192.168.59.3:2380/)\

--listen-peer-urls [http://192.168.59.3:2380](http://192.168.59.3:2380/)\

--listen-client-urls [http://192.168.59.3:2379,http://127.0.0.1:2379](http://192.168.59.3:2379,http://127.0.0.1:2379)\

--advertise-client-urls [http://192.168.59.3:2379](http://192.168.59.3:2379/)\

--initial-cluster-token etcd-cluster \

--initial-cluster docker-node1=[http://192.168.59.2:2380,docker-node2=http://192.168.59.3:2380](http://192.168.59.2:2380,docker-node2=http://192.168.59.3:2380)\

--initial-cluster-state new&

检查cluster状态


vagrant@docker-node2:~/etcd-v3.0.12-linux-amd64$ ./etcdctl cluster-health

member 21eca106efe4caee is healthy: got healthy result from [http://192.168.59.2:2379](http://192.168.59.2:2379/)

member 8614974c83d1cc6d is healthy: got healthy result from [http://192.168.59.3:2379](http://192.168.59.3:2379/)

cluster is healthy

以上结果如图

image.png

在docker-node1上


$ sudo service docker stop

$ sudo /usr/bin/dockerd -H [tcp://0.0.0.0:2375](tcp://0.0.0.0:2375)-H unix:///var/run/docker.sock --cluster-store=[etcd://192.168.59.2:2379](etcd://192.168.59.2:2379)--cluster-advertise=192.168.59.2:2375&

在docker-node2上


$ sudo service docker stop

$ sudo /usr/bin/dockerd -H [tcp://0.0.0.0:2375](tcp://0.0.0.0:2375)-H unix:///var/run/docker.sock --cluster-store=[etcd://192.168.59.3:2379](etcd://192.168.59.3:2379)--cluster-advertise=192.168.59.3:2375&

效果如图(错误忽略)


image.png

创建overlay network

在docker-node1上创建一个demo_huey的overlay network

image.png

node2上可看到demo_huey overlay网络

image.png

通过查看etcd的key-value, 我们获取到,这个demo的network是通过etcd从node1同步到node2的(命令看截图)

image.png

创建连接demo网络的容器

在docker-node1上

vagrant@docker-node1:~$ sudo docker run -d --name test1 --net demo busybox sh -c "while true; do sleep 3600; done"
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
56bec22e3559: Pull complete
Digest: sha256:29f5d56d12684887bdfa50dcd29fc31eea4aaf4ad3bec43daf19026a7ce69912
Status: Downloaded newer image for busybox:latest
a95a9466331dd9305f9f3c30e7330b5a41aae64afda78f038fc9e04900fcac54
vagrant@docker-node1:~$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
a95a9466331d        busybox             "sh -c 'while true; d"   4 seconds ago       Up 3 seconds                            test1
vagrant@docker-node1:~$ sudo docker exec test1 ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:0A:00:00:02
          inet addr:10.0.0.2  Bcast:0.0.0.0  Mask:255.255.255.0
          inet6 addr: fe80::42:aff:fe00:2/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1450  Metric:1
          RX packets:15 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:1206 (1.1 KiB)  TX bytes:648 (648.0 B)

eth1      Link encap:Ethernet  HWaddr 02:42:AC:12:00:02
          inet addr:172.18.0.2  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::42:acff:fe12:2/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:8 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:648 (648.0 B)  TX bytes:648 (648.0 B)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

vagrant@docker-node1:~$

在docker-node2上

vagrant@docker-node2:~$ sudo docker run -d --name test1 --net demo busybox sh -c "while true; do sleep 3600; done"
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
56bec22e3559: Pull complete
Digest: sha256:29f5d56d12684887bdfa50dcd29fc31eea4aaf4ad3bec43daf19026a7ce69912
Status: Downloaded newer image for busybox:latest
fad6dc6538a85d3dcc958e8ed7b1ec3810feee3e454c1d3f4e53ba25429b290b
docker: Error response from daemon: service endpoint with name test1 already exists.
vagrant@docker-node2:~$ sudo docker run -d --name test2 --net demo busybox sh -c "while true; do sleep 3600; done"
9d494a2f66a69e6b861961d0c6af2446265bec9b1d273d7e70d0e46eb2e98d20

验证连通性。

vagrant@docker-node2:~$ sudo docker exec -it test2 ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:0A:00:00:03
          inet addr:10.0.0.3  Bcast:0.0.0.0  Mask:255.255.255.0
          inet6 addr: fe80::42:aff:fe00:3/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1450  Metric:1
          RX packets:208 errors:0 dropped:0 overruns:0 frame:0
          TX packets:201 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:20008 (19.5 KiB)  TX bytes:19450 (18.9 KiB)

eth1      Link encap:Ethernet  HWaddr 02:42:AC:12:00:02
          inet addr:172.18.0.2  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::42:acff:fe12:2/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:8 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:648 (648.0 B)  TX bytes:648 (648.0 B)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

  vagrant@docker-node1:~$ sudo docker exec test1 sh -c "ping 10.0.0.3"
  PING 10.0.0.3 (10.0.0.3): 56 data bytes
  64 bytes from 10.0.0.3: seq=0 ttl=64 time=0.579 ms
  64 bytes from 10.0.0.3: seq=1 ttl=64 time=0.411 ms
  64 bytes from 10.0.0.3: seq=2 ttl=64 time=0.483 ms
  ^C
  vagrant@docker-node1:~$

总结

ping通即可,为研究k8s做准备

参考

官网:https://docs.docker.com
书籍:Docker技术入门与实战
网课: 推荐 慕课网 图灵学院 谷粒学院

你可能感兴趣的:(《Docker》_通过etcd和overlay网络实现多机通信)