Docker多机容器部署

对于一个项目来说,通常包含多个容器实例,而且这多个容器实例通常不会在同一台机器上。因此,如何在多个主机上通过容器来部署项目,需要解决的就是不同主机上容器的通信问题。


  • 角色划分:
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": {
   
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值