我之前给大家介绍的都是在一台服务器上面操作的,显然有时候我们需要两台甚至更多的服务器,那么这个时候容器之间还可以通信嘛?答案是肯定的,可以!那具体该怎么操作呢?往下看!
比如现在我们有两台linux服务器,一个是192.168.205.10,一个是192.168.205.11,这两台服务器是网络是互通的可以相互传递数据包。关于这里面更多的知识,大家可以去搜索VXLAN,相信你可以学的更多。
在这里给大家介绍一个分布式存储的工具etcd,这个可以让我们不同容器之间的ip是不会相同的,即使是不同的服务器上面也不会相同。
我们进入第一台服务器里面,安装服务器的方式之前已经讲过这里就不过多的讲解了不懂的可以留言。
vagrant@docker-node1:~$ wget 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.205.10:2380 \
--listen-peer-urls http://192.168.205.10:2380 \
--listen-client-urls http://192.168.205.10:2379,http://127.0.0.1:2379 \
--advertise-client-urls http://192.168.205.10:2379 \
--initial-cluster-token etcd-cluster \
--initial-cluster docker-node1=http://192.168.205.10:2380,docker-node2=http://192.168.205.11:2380 \
--initial-cluster-state new&
在你的第二台服务器上面依然是这么进行操作的,安装完成以后执行
[vagrant@docker-node2 etcd-v3.0.12-linux-amd64]$ ./etcdctl cluster-health
member 21eca106efe4caee is healthy: got healthy result from http://192.168.205.10:2379
member 8614974c83d1cc6d is healthy: got healthy result from http://192.168.205.11:2379
cluster is healthy
[vagrant@docker-node1 etcd-v3.0.12-linux-amd64]$ ./etcdctl cluster-health
member 21eca106efe4caee is healthy: got healthy result from http://192.168.205.10:2379
member 8614974c83d1cc6d is healthy: got healthy result from http://192.168.205.11:2379
cluster is healthy
好的,两台服务器都搭建好了etcd分布式存储,接下来重启docker
sudo service docker stop
sudo /usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock --cluster-store=etcd://192.168.205.10:2379 --cluster-advertise=192.168.205.10:2375&
记住,两台服务器上面的都是一样的操作.
[vagrant@docker-node2 ~]$ sudo docker network ls
NETWORK ID NAME DRIVER SCOPE
51bb8a874f53 bridge bridge local
365d9aa40572 host host local
353efcf35ca9 none null local
[vagrant@docker-node1 ~]$ sudo docker network ls
NETWORK ID NAME DRIVER SCOPE
3fb456a2d940 bridge bridge local
e937e8bacd9e host host local
7de133b14f97 none null local
[vagrant@docker-node1 ~]$ sudo docker network create -d overlay demo
e37fd1fc050c19fd9e08bed8f2903c6e23559219f9214ca18d9c622d37805052
# 执行完上面这条命令以后我们发现多了一个demo的网络
[vagrant@docker-node1 ~]$ sudo docker network ls
NETWORK ID NAME DRIVER SCOPE
3fb456a2d940 bridge bridge local
e37fd1fc050c demo overlay global
e937e8bacd9e host host local
7de133b14f97 none null local
此时我们再次查看docker-node2的网络情况发现。。。。
[vagrant@docker-node2 ~]$ sudo docker network ls
NETWORK ID NAME DRIVER SCOPE
51bb8a874f53 bridge bridge local
e37fd1fc050c demo overlay global
365d9aa40572 host host local
353efcf35ca9 none null local
这说明这两边是同步的,那么它是怎么做到的呢?这就是etcd起的作用。
接下来我们在两台服务器上面创建容器
[vagrant@docker-node1 ~]$ sudo docker run -d --name appleone --net demo busybox sh -c "while true; do sleep 3600; done"
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
f70adabe43c0: Pull complete
Digest: sha256:58ac43b2cc92c687a32c8be6278e50a063579655fe3090125dcb2af0ff9e1a64
Status: Downloaded newer image for busybox:latest
2b6522b25ec88f72871776a823eee483d08ab644b4bfd40ebcd4dab2e6e4871e
[vagrant@docker-node2 etcd-v3.0.12-linux-amd64]$ sudo docker run -d --name appleone --net demo busybox sh -c "while true; do sleep 3600; done"
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
f70adabe43c0: Pull complete
Digest: sha256:58ac43b2cc92c687a32c8be6278e50a063579655fe3090125dcb2af0ff9e1a64
Status: Downloaded newer image for busybox:latest
38397d5234b21799a2d9971fafa61efbc0275ed713d8d13bb033c931ae8d629d
docker: Error response from daemon: endpoint with name appleone already exists in network demo.
这个时候小伙伴们会发现如果我们想在第二台服务器上面,创建一样名字的容器就会报错,这个也是etcd起的作用。
我们给它换个名字就OK了。
[vagrant@docker-node2 etcd-v3.0.12-linux-amd64]$ sudo docker run -d --name appletwo --net demo busybox sh -c "while true; do sleep 3600; done"
a85984fed576f9f0e37756e2a0db68c9026db835a20eb621fd939d041c646509
我们来查看一下两个容器的ip地址
[vagrant@docker-node2 etcd-v3.0.12-linux-amd64]$ sudo docker exec appletwo ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
7: eth0@if8: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1450 qdisc noqueue
link/ether 02:42:0a:00:00:03 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.3/24 brd 10.0.0.255 scope global eth0
valid_lft forever preferred_lft forever
10: eth1@if11: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:ac:12:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.18.0.2/16 brd 172.18.255.255 scope global eth1
valid_lft forever preferred_lft forever
[vagrant@docker-node1 ~]$ sudo docker exec appleone ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
19: eth0@if20: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1450 qdisc noqueue
link/ether 02:42:0a:00:00:02 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.2/24 brd 10.0.0.255 scope global eth0
valid_lft forever preferred_lft forever
22: eth1@if23: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:ac:12:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.18.0.2/16 brd 172.18.255.255 scope global eth1
valid_lft forever preferred_lft forever
我们来ping一把
[vagrant@docker-node2 etcd-v3.0.12-linux-amd64]$ docker exec appletwo ping 10.0.0.2
PING 10.0.0.2 (10.0.0.2): 56 data bytes
64 bytes from 10.0.0.2: seq=0 ttl=64 time=2.435 ms
64 bytes from 10.0.0.2: seq=1 ttl=64 time=0.735 ms
我们发现两个容器是可以ping通的
走到这里,我们基本已经把docker的网络知识都搞明白了,从下节开始我们接触新的知识,数据。小伙伴们一起加油把!!!!!Go! Go! Go!
网友评论