容器跨主机通信之docker default overlay

时间:2021-09-18 14:22:44
default docker overlay基于libnetwork和libkv,这里使用etcd作为key value store

VM1: 129.107.126.123
VM2: 129.107.126.142

container c1放在vm1上,container c2放在vm2上。etcd放在vm1上。vm1和vm2加入一个overlay网络。


1, run etcd

安装etcd不谈

$ etcd --advertise-client-urls 'http://129.107.126.123:2379' --listen-client-urls 'http://0.0.0.0:2379'

本地检查:

$ etcdctl member list

ce2a822cea30bfca: name=default peerURLs=http://localhost:2380,http://localhost:7001 clientURLs=http://129.107.126.123:2379

检查功能是否正确,能否正确设置和获取

$ etcdctl mk key value
$ etcdctl get key 
value


远程检查:

$ etcdctl --endpoint "http://129.107.126.123:2379" get key
value

$ etcdctl -endpoint http://192.168.15.232:2379 mk key2 value2
value2

$ etcdctl -endpoint http://192.168.15.232:2379 get key2 
value2



2, add daemon.json to /etc/docker in VM1

$ sudo vim /etc/docker/daemon.json

{
    "cluster-store":"etcd://129.107.126.123:2379",
    "cluster-advertise":"129.107.126.123:2376"
}

VM2:

{
    "cluster-store":"etcd://129.107.126.123:2379",
    "cluster-advertise":"129.107.126.142:2376"
}

其中cluster-store指向etcd所在设备ip,  cluster-advertise内填写本机ip地址


$ sudo service docker restart


$ sudo docker info | grep "Cluster"
WARNING: No swap limit support
Cluster Store: etcd://129.107.126.123:2379
Cluster Advertise: 129.107.126.123:2376


说明集群信息正确配置进入了docker deamon



3, 创建overlay network

$ sudo docker network create -d overlay myoverlay-1
17c7751410e7305e88e205521775e3bf99dc2406c80b1219b72274fa884bd3c3

$ sudo docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
da561e6ef90c        bridge              bridge              local
26362372b64b        host                host                local
17c7751410e7        myoverlay-1         overlay             global
a72c2bebcc6a        none                null                local

可以看到网络已建立,是global类型

在其他vm上执行,docker network ls,也能看到




4,容器加入现有network

加入网络  myoverlay-1

$ sudo docker run -t -i --name  overlay-docker1  --net myoverlay-1  ubuntu:16.10 /bin/bash

已经在运行的容器也可以加入网络,通过执行命令:

$ docker run -id --name t233_t1 busybox sh
$ docker network connect myoverlay-1   t233_out




分别在vm1和vm2上面建立2个container,加入network myoverlay-1

container c1 on vm1: 10.0.0.2
container c2 on vm2: 10.0.0.3

容器跨主机通信之docker default overlay
可以ping通

done