DockerOverlay网络
Overlay是一种覆盖在主机上的虚拟网络,我理解的overlay他需要借助插件或工具完成对数据的保存和转发。我们这里使用etcd来完成overlay的跨主机容器通信
1. 安装etcd(这里我只启动两个节点的etcd,也可单机模式)
我这里选择用安装包方式,你也可以选择yum
wget https://github.com/etcd-io/etcd/releases/download/v3.5.0/etcd-v3.5.0-linux-amd64.tar.gz
scp etcd-v3.5.0-linux-amd64.tar.gz root@172.16.3.142:/root
以下操作两个主机均需要进行
tar -zxvf etcd-v3.5.0-linux-amd64.tar.gz
cd etcd-v3.5.0-linux-amd64
cp etcdctl /usr/local/bin/
我这里遇到一个坑,一定要加上--enable-v2否则会报错,无法创建overlay网络
nohup ./etcd --name docker-node1 --initial-advertise-peer-urls http://172.16.3.141:2380 --listen-peer-urls http://172.16.3.141:2380 --listen-client-urls http://172.16.3.141:2379,http://127.0.0.1:2379 --advertise-client-urls http://172.16.3.141:2379 --initial-cluster-token etcd-cluster --initial-cluster docker-node1=http://172.16.3.141:2380,docker-node2=http://172.16.3.142:2380 --enable-v2 --initial-cluster-state new 2>&1 &
nohup ./etcd --name docker-node2 --initial-advertise-peer-urls http://172.16.3.142:2380 --listen-peer-urls http://172.16.3.142:2380 --listen-client-urls http://172.16.3.142:2379,http://127.0.0.1:2379 --advertise-client-urls http://172.16.3.142:2379 --initial-cluster-token etcd-cluster --initial-cluster docker-node1=http://172.16.3.141:2380,docker-node2=http://172.16.3.142:2380 --enable-v2 --initial-cluster-state new 2>&1 &
etcdctl member list 查看一下节点信息
分别添加配置,指定外部存储使用etcd
vi /etc/docker/daemon.json
{
"cluster-store":"etcd://172.16.3.141:2379",
"cluster-advertise":"172.16.3.141:2375"
}
一定要重启下
systemctl daemon-reload
service docker restart
2. 创建overlay network
# 也可指定subnet
docker network create -d overlay etcdnet
在两天主机分别执行,可以看到另一台也出现了etcdnet
docker network ls
NETWORK ID NAME DRIVER SCOPE
810ca8a6e002 bridge bridge local
19234a37ec11 etcdnet overlay global
我们来看一下etcd中存储的数据格式,可以看到节点信息和