Docker跨主机网络联通之etcd实现

搭建ETCD集群

查看NODE1机器IP,并启动ETCD

ubuntu@docker-node1:~$ ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1450
        inet 172.16.1.15  netmask 255.255.255.0  broadcast 172.16.1.255
        inet6 fe80::f816:3eff:fe33:11a8  prefixlen 64  scopeid 0x20<link>
        ether fa:16:3e:33:11:a8  txqueuelen 1000  (Ethernet)
        RX packets 11765471  bytes 10784121723 (10.0 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 10697881  bytes 7319647448 (6.8 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


ubuntu@docker-node1:~$ wget http://192.168.9.251:9000/software/etcd-v3.0.12-linux-amd64.tar.gz
ubuntu@docker-node1:~$ tar zxvf etcd-v3.0.12-linux-amd64.tar.gz
ubuntu@docker-node1:~$ cd etcd-v3.0.12-linux-amd64
ubuntu@docker-node1:~$ nohup ./etcd --name docker-node1 --initial-advertise-peer-urls http://172.16.1.15:2380 --listen-peer-urls http://172.16.1.15:2380 --listen-client-urls http://172.16.1.15:2379,http://127.0.0.1:2379 --advertise-client-urls http://172.16.1.15:2379 --initial-cluster-token etcd-cluster --initial-cluster docker-node1=http://172.16.1.15:2380,docker-node2=http://172.16.1.36:2380 --initial-cluster-state new&

查看NODE2机器IP、启动ETCD,并ETCD健康检查

ubuntu@docker-node2:~$ ifconfig eth0
eth0      Link encap:Ethernet  HWaddr fa:16:3e:ec:fc:12
          inet addr:172.16.1.36  Bcast:172.16.1.255  Mask:255.255.255.0
          inet6 addr: fe80::f816:3eff:feec:fc12/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1450  Metric:1
          RX packets:18479729 errors:0 dropped:0 overruns:0 frame:0
          TX packets:16843586 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:7717777368 (7.7 GB)  TX bytes:6506041953 (6.5 GB)


ubuntu@docker-node2:~$ wget http://192.168.9.251:9000/software/etcd-v3.0.12-linux-amd64.tar.gz
ubuntu@docker-node2:~$ tar zxvf etcd-v3.0.12-linux-amd64.tar.gz
ubuntu@docker-node2:~$ cd etcd-v3.0.12-linux-amd64
ubuntu@docker-node2:~$ nohup ./etcd --name docker-node2 --initial-advertise-peer-urls http://172.16.1.36:2380 --listen-peer-urls http://172.16.1.36:2380 --listen-client-urls http://172.16.1.36:2379,http://127.0.0.1:2379 --advertise-client-urls http://172.16.1.36:2379 --initial-cluster-token etcd-cluster --initial-cluster docker-node1=http://172.16.1.15:2380,docker-node2=http://172.16.1.36:2380 --initial-cluster-state new&

ubuntu@docker-node2:~/etcd-v3.0.12-linux-amd64$ ./etcdctl cluster-health
member cd18d4410e46bbd1 is healthy: got healthy result from http://172.16.1.36:2379
member d05d0bbb1534c7ee is healthy: got healthy result from http://172.16.1.15:2379
cluster is healthy

修改docker配置文件支持etcd,重启docker

查看配置文件位于哪里

systemctl show --property=FragmentPath docker

编辑配置文件内容,接收所有IP请求

sudo vim /usr/lib/systemd/system/docker.service

修改配置ExecStart ,在后面追加 --cluster-store=etcd://172.16.1.36:2379 --cluster-advertise=172.16.1.36:2375

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

重新加载配置文件,重启DOCKER DAEMON

sudo systemctl daemon-reload
sudo systemctl restart docker

两台服务器都需要上述操作

创建全局网络,并在网络中添加容器

创建全局网络

ubuntu@docker-node1:~$ sudo docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
0e7bef3f143a        bridge              bridge              local
a5c7daf62325        host                host                local
3198cae88ab4        none                null                local
ubuntu@docker-node1:~$ sudo docker network create -d overlay demo
3d430f3338a2c3496e9edeccc880f0a7affa06522b4249497ef6c4cd6571eaa9
ubuntu@docker-node1:~$ sudo docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
0e7bef3f143a        bridge              bridge              local
3d430f3338a2        demo                overlay             global
a5c7daf62325        host                host                local
3198cae88ab4        none                null                local
ubuntu@docker-node1:~$ sudo docker network inspect demo
[
    {
        "Name": "demo",
        "Id": "3d430f3338a2c3496e9edeccc880f0a7affa06522b4249497ef6c4cd6571eaa9",
        "Scope": "global",
        "Driver": "overlay",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "10.0.0.0/24",
                    "Gateway": "10.0.0.1/24"
                }
            ]
        },
        "Internal": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]

添加容器

在node1上添加cli1.yaml,内容如下,网络指定为上面创建的全局网络

version: ‘2‘
services:
  cli:
    container_name: cli
    image: hyperledger/fabric-tools:raft
    tty: true
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer

networks:
  default:
    external:
      name: demo

在node2上添加cli2.yaml,内容如下,网络指定为上面创建的全局网络

version: ‘2‘
services:
  cli2:
    container_name: cli2
    image: hyperledger/fabric-tools:raft
    tty: true
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer

networks:
  default:
    external:
      name: demo

利用docker-compose分别启动两个节点。

docker-compose -f cli1.yaml up -d

docker-compose -f cli2.yaml up -d

查看demo网络详情,可以看到两个容器已经加入同一网络。

ubuntu@docker-node1:~$ sudo docker network inspect demo
[
    {
        "Name": "demo",
        "Id": "9dc055222c85e1ca09db6d66910c1dba27d342e070f1ca6f976533071344e939",
        "Created": "2018-05-23T10:44:36.454709725+08: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,
        "Containers": {
            "21da2d4d9b55e60b4636d7d0a9e7513b47784be55990ba46288a29a527ba470e": {
                "Name": "cli",
                "EndpointID": "411418d95a303f0452415e0481fc11ac41d6755e9680e042042f2afd93f62cca",
                "MacAddress": "02:42:0a:00:00:03",
                "IPv4Address": "10.0.0.3/24",
                "IPv6Address": ""
            },
            "ep-6725169b38e657bd6cef4d1b1cdf530575b7caf78f78a037d21c65eb2c90e6ab": {
                "Name": "cli2",
                "EndpointID": "6725169b38e657bd6cef4d1b1cdf530575b7caf78f78a037d21c65eb2c90e6ab",
                "MacAddress": "02:42:0a:00:00:02",
                "IPv4Address": "10.0.0.2/24",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

Docker跨主机网络联通之etcd实现

上一篇:App测试基本流程详解(汇总整理


下一篇:@GeneratedValue与@GenericGenerator配合使用