1. 启动consul服务,实现网络的统一配置管理
1.1 启动consul容器
docker run -d -p 8500:8500 -p 8400:8400 -h consul --name="consul" progrium/consul -server -bootstrap
consul: kv类型的存储数据库(key:value)
1.2 修改配置文件
# 节点一
vim /etc/docker/daemon.json 加入以下三行
{
"hosts": ["tcp://0.0.0.0:2376","unix://var/run/docker.sock"],
"cluster-store": "consul://10.0.0.100:8500",
"cluster-advertise": "10.0.0.100:2376"
}
vim /lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd-current -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock --cluster-store consul://10.0.0.100:8500 --cluster-advertise 10.0.0.100:2375
systemctl daemon-reload
systemctl restart docker
# 节点二
vim /etc/docker/daemon.json 加入以下三行
{
"hosts": ["tcp://0.0.0.0:2376","unix://var/run/docker.sock"],
"cluster-store": "consul://10.0.0.100:8500",
"cluster-advertise": "10.0.0.100:2376"
}
vim /lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd-current -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock --cluster-store consul://10.0.0.100:8500 --cluster-advertise 10.0.0.110:2375
systemctl daemon-reload
systemctl restart docker
2. 创建overlay网络
docker network create -d overlay --subnet 172.16.0.0/24 --gateway 172.16.0.254 overlay1
3. 启动容器测试
docker run -it --network overlay1 centos /bin/bash
容器内会生成两块网卡,一块用于与宿主机通信,一块用于容器间互相通信