在单机上使用 docker-compose 部署 etcd v3 集群

假设 centos 服务器公网地址为 192.168.6.132

1
安装 Docker-ce(省略)


2
安装 Docker-Compose

yum -y install epel-release
yum -y install python-pip
确认版本
pip --version
更新pip
pip install --upgrade pip
安装docker-compose
pip install docker-compose
查看版本 
docker-compose version


3
下载 ETCD V3 镜像:
docker pull k8s.gcr.io/etcd:3.3.10

如果下载不了: 
docker pull ascs/etcd:3.3.10
docker image tag ascs/etcd:3.3.10 k8s.gcr.io/etcd:3.3.10


4
请运维把外网端口 32775/32777/32779 映射到 docker 容器的 2379
把外网端口 32774/32776/32778 映射到 docker 容器的 2380

 

5
编写 docker-compose.yaml 文件


version: '2'
networks:
  byfn:

services:
  etcd1:
    image: ascs/etcd:3.3.10
    container_name: etcd1
    command: etcd -name etcd1 -advertise-client-urls http://192.168.6.132:32775 -listen-client-urls http://0.0.0.0:2379 -listen-peer-urls http://0.0.0.0:32774 -initial-cluster-token etcd-cluster -initial-cluster "etcd1=http://etcd1:32774,etcd2=http://etcd2:32776,etcd3=http://etcd3:32778" -initial-cluster-state new
    ports:
      - "32775:2379"
      - "32774:2380"
    networks:
      - byfn

  etcd2:
    image: ascs/etcd:3.3.10
    container_name: etcd2
    command: etcd -name etcd2 -advertise-client-urls http://192.168.6.132:32777 -listen-client-urls http://0.0.0.0:2379 -listen-peer-urls http://0.0.0.0:32776 -initial-cluster-token etcd-cluster -initial-cluster "etcd1=http://etcd1:32774,etcd2=http://etcd2:32776,etcd3=http://etcd3:32778" -initial-cluster-state new
    ports:
      - "32777:2379"
      - "32776:2380"
    networks:
      - byfn

  etcd3:
    image: ascs/etcd:3.3.10
    container_name: etcd3
    command: etcd -name etcd3 -advertise-client-urls http://192.168.6.132:32779 -listen-client-urls http://0.0.0.0:2379 -listen-peer-urls http://0.0.0.0:32778 -initial-cluster-token etcd-cluster -initial-cluster "etcd1=http://etcd1:32774,etcd2=http://etcd2:32776,etcd3=http://etcd3:32778" -initial-cluster-state new
    ports:
      - "32779:2379"
      - "32778:2380"
    networks:
      - byfn


启动服务: docker-compose up


6
使用 etcdctl v3 进行测试:
在 ubuntu 18 开发机上安装 etcdctl v3 后, 在 /etc/profile 中添加环境变量:
ETCDCTL_API=3
source /etc/profile


下面两步正常执行的话,就说明 etcd v3 集群搭建成功了
etcdctl --endpoints=http://192.168.6.132:32775 set foo "test111" // 在一个节点上写入
// 显示 test111 

etcdctl --endpoints=http://192.168.6.132:32777 get foo // 从另外一个节点上读出
// 显示 test111

 

7
使用 etcd go-client api v3 的时候, 把 192.168.6.132:32775, 192.168.6.132:32777, 192.168.6.132:32779 作为 clientv3.New 方法中 Endpoints 参数的值:

cli, err := clientv3.New(clientv3.Config{

        EndPoints:[]string{"192.168.6.132:32775", "192.168.6.132:32777", "192.168.6.132:32779"},

......})

 

参考
https://blog.51cto.com/9291927/2310444
https://blog.csdn.net/happyfreeangel/article/details/100020742


 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值