对于一个项目来说,通常包含多个容器实例,而且这多个容器实例通常不会在同一台机器上。因此,如何在多个主机上通过容器来部署项目,需要解决的就是不同主机上容器的通信问题。
- 角色划分:
etcd 192.168.30.128
192.168.30.129
flask 192.168.30.128
redis 192.168.30.129
- 环境准备:
# systemctl stop firewalld && systemctl disable firewalld
# sed -i 's/=enforcing/=disabled/g' /etc/selinux/config && setenforce 0
- 安装docker:
# curl http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker.repo
# yum makecache fast
# yum install -y docker-ce
# systemctl start docker && systemctl enable docker
- 建立etcd集群:
192.168.30.128
# cd /software
# wget https://github.com/etcd-io/etcd/releases/download/v3.3.17/etcd-v3.3.17-linux-amd64.tar.gz
# tar xf etcd-v3.3.17-linux-amd64.tar.gz
# cd etcd-v3.3.17-linux-amd64/
# nohup ./etcd --name docker-node1 --initial-advertise-peer-urls http://192.168.30.128:2380 \
--listen-peer-urls http://192.168.30.128:2380 \
--listen-client-urls http://192.168.30.128:2379,http://127.0.0.1:2379 \
--advertise-client-urls http://192.168.30.128:2379 \
--initial-cluster-token etcd-cluster \
--initial-cluster docker-node1=http://192.168.30.128:2380,docker-node2=http://192.168.30.129:2380 \
--initial-cluster-state new &
192.168.30.129
# cd /software
# wget https://github.com/etcd-io/etcd/releases/download/v3.3.17/etcd-v3.3.17-linux-amd64.tar.gz
# tar xf etcd-v3.3.17-linux-amd64.tar.gz
# cd etcd-v3.3.17-linux-amd64/
# nohup ./etcd --name docker-node2 --initial-advertise-peer-urls http://192.168.30.129:2380 \
--listen-peer-urls http://192.168.30.129:2380 \
--listen-client-urls http://192.168.30.129:2379,http://127.0.0.1:2379 \
--advertise-client-urls http://192.168.30.129:2379 \
--initial-cluster-token etcd-cluster \
--initial-cluster docker-node1=http://192.168.30.128:2380,docker-node2=http://192.168.30.129:2380 \
--initial-cluster-state new &
检查etcd集群状态(两台机器都可执行)
# ./etcdctl cluster-health
member 1a3a8b811f89111 is healthy: got healthy result from http://192.168.30.128:2379
member deb21af19c6dc76c is healthy: got healthy result from http://192.168.30.129:2379
cluster is healthy
- 重启docker:
192.168.30.128
# systemctl stop docker
# /usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock --cluster-store=etcd://192.168.30.128:2379 --cluster-advertise=192.168.30.128:2375 &
192.168.30.129
# systemctl stop docker
# /usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock --cluster-store=etcd://192.168.30.129:2379 --cluster-advertise=192.168.30.129:2375 &
- 创建overlay network:
192.168.30.128上创建一个demo的overlay network
# docker network create -d overlay demo
41dad4bca4c9c8cc30ccd36861a3cf5f2ad7e86b774136bb4878cd54ffbf6e0b
# docker network ls
NETWORK ID NAME DRIVER SCOPE
66e5abd85e23 bridge bridge local
41dad4bca4c9 demo overlay global
535808221d2e host host local
2addad8d8857 none null local
# docker network inspect demo
[
{
"Name": "demo",
"Id": "41dad4bca4c9c8cc30ccd36861a3cf5f2ad7e86b774136bb4878cd54ffbf6e0b",
"Created": "2019-10-22T13:44:32.228005687+08:00",
"Scope": "global",
"Driver": "overlay",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {