假设 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