天天看點

07-etcd實作Docker多機容通信1. 實驗準備2. 搭建etcd叢集3. 重新開機docker服務4. 建立overlay5. 實驗

etcd實作Docker多機容通信

  • 1. 實驗準備
  • 2. 搭建etcd叢集
  • 3. 重新開機docker服務
  • 4. 建立overlay
  • 5. 實驗
    • 5.1 建立兩個busybox容器
    • 5.2 驗證連通性

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狀态

[[email protected] 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狀态

[[email protected] etcd-v3.0.12-linux-amd64]# sudo systemctl stop firewalld

[[email protected] 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服務

$ 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上

[[email protected] 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上

[[email protected] 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

[[email protected] 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上

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

172.28.65.126上

[[email protected] 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.

[[email protected] opt]# docker run -d --name test2 --net demo busybox sh -c "while true; do sleep 3600; done
           

5.2 驗證連通性

172.28.65.114上網絡狀态

[[email protected] 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上網絡狀态

[[email protected] 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)容器

[[email protected] 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"