同样是两台服务器:
准备工作:
设置容器的主机名
consul:kv类型的存储数据库(key:value) docker01、02上: vim /etc/docker/daemon.json { "hosts":["tcp://0.0.0.0:2376","unix:///var/run/docker.sock"], 这行改了要修改自己的docker配置文件 "cluster-store": "consul://10.0.0.11:8500", "cluster-advertise": "10.0.0.11:2376" 另外一台写自己的ip即可 } vim /etc/docker/daemon.json vim /usr/lib/systemd/system/docker.service ExecStart=/usr/bin/dockerd --contAInerd=/run/containerd/containerd.sock 这行改成这样,两台的都要改 systemctl daemon-reload systemctl restart docker
docker01上:
docker run -d -p 8500:8500 -h consul --name consul progrium/consul -server -bootstrap 这条命令在设置容器的主机名之后做 [root@docker1 centos_zabbix]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE 10.0.0.11:5000/kode v2 6914fda5fd44 4 hours ago 502MB kode v2 6914fda5fd44 4 hours ago 502MB 10.0.0.11:5000/centos6.9_Nginx_ssh v3 92b1087df3f5 13 hours ago 431MB centos6.9_nginx_ssh v3 92b1087df3f5 13 hours ago 431MB centos6.9_nginx_ssh v2 6307e23ee16d 13 hours ago 431MB <none> <none> b94c822488f9 13 hours ago 431MB centos6.9_nginx_ssh v1 f01eeebf918a 13 hours ago 431MB <none> <none> 54167f1d659b 13 hours ago 431MB <none> <none> 71474e616504 13 hours ago 430MB centos6.9_ssh v2 03387f99f022 14 hours ago 307MB <none> <none> 82d0e9ce844b 14 hours ago 195MB kode v1 c0f486ac58fe 15 hours ago 536MB progrium/consul latest 09ea64205e55 4 years ago 69.4MB 这里有了 [root@docker1 centos_zabbix]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1016753ee64d progrium/consul "/bin/start -server …" 4 minutes ago Exited (255) 3 minutes ago 53/tcp, 53/udp, 8300-8302/tcp, 8400/tcp, 8301-8302/udp, 0.0.0.0:8500->8500/tcp consul
最后在页面上访问:
2)创建overlay网络
docker network create -d overlay --subnet 172.16.1.0/24 --gateway 172.16.1.254 ol1 这个网段不能和宿主机上相同,并且随便在一台上执行这个命令即可
查看容器的网络,有没有出现overlay
[root@docker1 shell]# docker network ls NETWORK ID NAME DRIVER SCOPE e480a4fe5f4b bridge bridge local 0a3cbfe2473f host host local 4e5ccdc657e1 macvlan_1 macvlan local aab77f02a0b1 none null local 8ae111afded7 ol1 overlay global
3)启动容器测试
一台上: docker run -it --network ol1 --name test01 centos6.9_ssh:v2 /bin/bash 另外一台上: docker run -it --network ol1 --name test02 centos6.9_ssh:v2 /bin/bash 之后可以通过test01和test02之间联通
之后可以通过test01和test02之间联通
真实原理:
每个容器有两块网卡,eth0实现容器间的通讯,eth1实现容器访问外网