利用etcd实现docker跨主机通信

Posted 豆浆D

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用etcd实现docker跨主机通信相关的知识,希望对你有一定的参考价值。

etcd实现分布是存储,然后让通信等数据共享。

 

步骤:

安装etcd

docker重启加载etcd参数

建立docker overlay网络验证跨主机通信

 

准备:两台安装了docker 的linux主机

 

安装etcd


在docker-node1上

 
ubuntu@docker-node1:~$ wget https://github.com/coreos/etcd/releases/download/v3.0.12/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://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&
 

在docker-node2上

 
ubuntu@docker-node2:~$ wget https://github.com/coreos/etcd/releases/download/v3.0.12/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://192.168.205.11:2380
--listen-peer-urls http://192.168.205.11:2380
--listen-client-urls http://192.168.205.11:2379,http://127.0.0.1:2379
--advertise-client-urls http://192.168.205.11: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&
 

 

重启docker加载etcd

 

检查cluster状态


ubuntu@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


## 重启docker服务


在docker-node1上

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


在docker-node2上


$ 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.11:2379 --cluster-advertise=192.168.205.11:2375&

  

创建docker overlay网络验证通信

 

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

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

在docker-node2上也看到一个demo的overlay network
ubuntu@docker-node2:~$ 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

  

以上是关于利用etcd实现docker跨主机通信的主要内容,如果未能解决你的问题,请参考以下文章

跨主机容器之间通信实现方式:etcd+flanned

DOCKER使用 FLANNEL(ETCD+FLANNEL)网络

docker容器跨主机网络overlay

部署Flannel,实现跨主机Docker容器通信

如何实现跨主机之间docker容器通信问题

Docker容器跨主机多网段通信解决方案