07-etcd实现Docker多机容通信

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/huangjun0210/article/details/86083901

1. 实验准备

准备两台可以相互通信的linux主机,并安装好docker。本实验准备的两台主机ip分别为:172.28.65.114和172.28.65.126

2. 搭建etcd集群

etcd是开源免费的分布式存储工具,官网 https://coreos.com/etcd.
在两台机器上分别装上etcd
172.28.65.114上

// 下载etcd
[root@eshop-cache04 opt]# wget https://github.com/coreos/etcd/releases/download/v3.0.12/etcd-v3.0.12-linux-amd64.tar.gz

// 解压
[root@eshop-cache04 opt]# tar zxvf etcd-v3.0.12-linux-amd64.tar.gz

//进入目录
[root@eshop-cache04 opt]# cd etcd-v3.0.12-linux-amd64

//安装etcd
[root@eshop-cache04 etcd-v3.0.12-linux-amd64]#  nohup ./etcd --name docker-node1 --initial-advertise-peer-urls http://172.28.65.114:2380 \
--listen-peer-urls http://172.28.65.114:2380 \
--listen-client-urls http://172.28.65.114:2379,http://127.0.0.1:2379 \
--advertise-client-urls http://172.28.65.114:2379 \
--initial-cluster-token etcd-cluster \
--initial-cluster docker-node1=http://172.28.65.114:2380,docker-node2=http://172.28.65.126:2380 \
--initial-cluster-state new&

172.28.65.126上

// 下载etcd
[root@eshop-cache04 opt]# wget https://github.com/coreos/etcd/releases/download/v3.0.12/etcd-v3.0.12-linux-amd64.tar.gz

// 解压
[root@eshop-cache04 opt]# tar zxvf etcd-v3.0.12-linux-amd64.tar.gz

//进入目录
[root@eshop-cache04 opt]# cd etcd-v3.0.12-linux-amd64

//安装etcd
[root@eshop-cache04 etcd-v3.0.12-linux-amd64]#  nohup ./etcd --name docker-node2 --initial-advertise-peer-urls http://172.28.65.126:2380 \
--listen-peer-urls http://172.28.65.126:2380 \
--listen-client-urls http://172.28.65.126:2379,http://127.0.0.1:2379 \
--advertise-client-urls http://172.28.65.126:2379 \
--initial-cluster-token etcd-cluster \
--initial-cluster docker-node1=http://172.28.65.114:2380,docker-node2=http://172.28.65.126:2380 \
--initial-cluster-state new&

检查cluster状态

[root@eshop-cache05 etcd-v3.0.12-linux-amd64]# ./etcdctl cluster-health
cluster may be unhealthy: failed to list members
Error:  client: etcd cluster is unavailable or misconfigured
error #0: dial tcp 127.0.0.1:4001: getsockopt: connection refused
error #1: client: endpoint http://127.0.0.1:2379 exceeded header timeout

检查是防火墙已开启,关闭防火墙,再查看cluster状态

[root@eshop-cache05 etcd-v3.0.12-linux-amd64]# sudo systemctl stop firewalld

[root@eshop-cache05 etcd-v3.0.12-linux-amd64]# ./etcdctl cluster-health
member 3769a4d777a29309 is healthy: got healthy result from http://172.28.65.126:2379
member 4ac06614f93070c0 is healthy: got healthy result from http://172.28.65.114:2379
cluster is healthy

至此,etcd集群搭建成功。

3. 重启docker服务

172.28.65.114上,停止docker服务

$ service docker stop

启动docker

$ /usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock --cluster-store=etcd://172.28.65.114:2379 --cluster-advertise=172.28.65.114:2375&

172.28.65.126上,停止docker服务

扫描二维码关注公众号,回复: 5031851 查看本文章
$ service docker stop

启动docker

$ /usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock --cluster-store=etcd://172.28.65.126:2379 --cluster-advertise=172.28.65.126:2375&

4. 创建overlay

在172.28.65.114上创建overlay

$ docker network create -d overlay demo

查看network
172.28.65.114上

[root@eshop-cache04 opt]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
1570c6f0cf99        bridge              bridge              local
b9861814863a        demo                overlay             global
b9941cbbb213        host                host                local
977e41d92f6d        none                null                local

172.28.65.126上

[root@eshop-cache05 opt]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
750012e174af        bridge              bridge              local
b9861814863a        demo                overlay             global
dbbce56b6248        host                host                local
7a061d72a8e4        none                null                local

查看创建的网络模式demo

[root@eshop-cache05 opt]# docker network inspect demo
[
    {
        "Name": "demo",
        "Id": "b9861814863a4b3ce7f8d80414612bd77d3755d59c995334ad70b3981e86282b",
        "Created": "2019-01-08T08:04:21.9061569-05:00",
        "Scope": "global",
        "Driver": "overlay",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "10.0.0.0/24",
                    "Gateway": "10.0.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]

5. 实验

5.1 创建两个busybox容器

172.28.65.114上

[root@eshop-cache04 opt]# sudo docker run -d --name test1 --net demo busybox sh -c "while true; do sleep 3600; done"

172.28.65.126上

[root@eshop-cache05 opt]# 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
57c14dd66db0: Pull complete 
Digest: sha256:7964ad52e396a6e045c39b5a44438424ac52e12e4d5a25d94895f2058cb863a0
Status: Downloaded newer image for busybox:latest
b7d4a8a3c5c9e46986346614f210ea1b1e16669d30450a2e7bc79e6f8ce3d9ea
docker: Error response from daemon: endpoint with name test1 already exists in network demo.

[root@eshop-cache05 opt]# docker run -d --name test2 --net demo busybox sh -c "while true; do sleep 3600; done

5.2 验证连通性

172.28.65.114上网络状态

[root@eshop-cache04 opt]# docker exec test1 ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:0A:00:00:02  
          inet addr:10.0.0.2  Bcast:10.0.0.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1450  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:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

eth1      Link encap:Ethernet  HWaddr 02:42:AC:12:00:02  
          inet addr:172.18.0.2  Bcast:172.18.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:14 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:1116 (1.0 KiB)  TX bytes:0 (0.0 B)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          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:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

172.28.65.126上网络状态

[root@eshop-cache05 opt]# sudo docker exec -it test2 ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:0A:00:00:03  
          inet addr:10.0.0.3  Bcast:10.0.0.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1450  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:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

eth1      Link encap:Ethernet  HWaddr 02:42:AC:13:00:02  
          inet addr:172.19.0.2  Bcast:172.19.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:16 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:1248 (1.2 KiB)  TX bytes:0 (0.0 B)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          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)

在172.28.65.114上执行命令,可以ping通172.28.65.126上创建的busybox(10.0.0.3)容器

[root@eshop-cache04 opt]# 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=6.775 ms
64 bytes from 10.0.0.3: seq=1 ttl=64 time=0.458 ms
64 bytes from 10.0.0.3: seq=2 ttl=64 time=0.401 ms
64 bytes from 10.0.0.3: seq=3 ttl=64 time=0.405 ms
64 bytes from 10.0.0.3: seq=4 ttl=64 time=0.395 ms
^Z
[1]+  Stopped                 sudo docker exec test1 sh -c "ping 10.0.0.3"

猜你喜欢

转载自blog.csdn.net/huangjun0210/article/details/86083901