docker 创建etcd集群

docker 创建etcd集群

 

1、本文采用etcd3.5.1版本,首先下载etcd3.5.1 docker镜像

docker pull quay.io/coreos/etcd:v3.5.1
2、在自己设定的目录,比如/home/etcd下创建docker-compose.yml,内容如下

##xx.xx.xx.xx 为自己服务器ip地址
version: "3.7"

services:
etcd0:
image: "quay.io/coreos/etcd:v3.5.1"
container_name: etcd0
ports:
- "23800:2380"
- "23790:2379"
environment:
- ALLOW_NONE_AUTHENTICATION=yes
- ETCD_NAME=etcd0
- ETCD_LISTEN_PEER_URLS=http://0.0.0.0:2380
- ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379
- ETCD_ADVERTISE_CLIENT_URLS=http://xx.xx.xx.xx:23790
- ETCD_INITIAL_ADVERTISE_PEER_URLS=http://xx.xx.xx.xx:23800
- ETCD_INITIAL_CLUSTER_TOKEN=etcd-cluster
- ETCD_INITIAL_CLUSTER=etcd0=http://xx.xx.xx.xx:23800,etcd1=http://xx.xx.xx.xx:23801,etcd2=http://xx.xx.xx.xx:23802
- ETCD_INITIAL_CLUSTER_STATE=new

etcd1:
image: "quay.io/coreos/etcd:v3.5.1"
container_name: etcd1
ports:
- "23801:2380"
- "23791:2379"
environment:
- ALLOW_NONE_AUTHENTICATION=yes
- ETCD_NAME=etcd1
- ETCD_LISTEN_PEER_URLS=http://0.0.0.0:2380
- ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379
- ETCD_ADVERTISE_CLIENT_URLS=http://xx.xx.xx.xx:23791
- ETCD_INITIAL_ADVERTISE_PEER_URLS=http://xx.xx.xx.xx:23801
- ETCD_INITIAL_CLUSTER_TOKEN=etcd-cluster
- ETCD_INITIAL_CLUSTER=etcd0=http://xx.xx.xx.xx:23800,etcd1=http://xx.xx.xx.xx:23801,etcd2=http://xx.xx.xx.xx:23802
- ETCD_INITIAL_CLUSTER_STATE=new

etcd2:
image: "quay.io/coreos/etcd:v3.5.1"
container_name: etcd2
ports:
- "23802:2380"
- "23792:2379"
environment:
- ALLOW_NONE_AUTHENTICATION=yes
- ETCD_NAME=etcd2
- ETCD_LISTEN_PEER_URLS=http://0.0.0.0:2380
- ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379
- ETCD_ADVERTISE_CLIENT_URLS=http://xx.xx.xx.xx:23792
- ETCD_INITIAL_ADVERTISE_PEER_URLS=http://xx.xx.xx.xx:23802
- ETCD_INITIAL_CLUSTER_TOKEN=etcd-cluster
- ETCD_INITIAL_CLUSTER=etcd0=http://xx.xx.xx.xx:23800,etcd1=http://xx.xx.xx.xx:23801,etcd2=http://xx.xx.xx.xx:23802
- ETCD_INITIAL_CLUSTER_STATE=new
3、在home/etcd目录下执行docker-compose up -d

4、验证是否成功,输入docker ps

 

然后根据容器id,查看每个容器中 etcd集群内成员信息,正常情况下,三个容器出现的结果是一致的。

[root@iZbp1bq6vb70qq3lbmjlh0Z ~]# docker exec -it 2b900596bc82 etcdctl member list
10708a1c20394730, started, etcd1, http://xx.xx.xx.xx:23801, http://xx.xx.xx.xx:23791, false
816ab764a1780c22, started, etcd2, http://xx.xx.xx.xx:23802, http://xx.xx.xx.xx:23792, false
e87b893102f81d9f, started, etcd0, http://xx.xx.xx.xx:23800, http://xx.xx.xx.xx:23790, false
[root@iZbp1bq6vb70qq3lbmjlh0Z ~]# docker exec -it cf932f70bd6a etcdctl member list
10708a1c20394730, started, etcd1, http://xx.xx.xx.xx:23801, http://xx.xx.xx.xx:23791, false
816ab764a1780c22, started, etcd2, http://xx.xx.xx.xx:23802, http://xx.xx.xx.xx:23792, false
e87b893102f81d9f, started, etcd0, http://xx.xx.xx.xx:23800, http://xx.xx.xx.xx:23790, false
[root@iZbp1bq6vb70qq3lbmjlh0Z ~]# docker exec -it 208ae882d104 etcdctl member list
10708a1c20394730, started, etcd1, http://xx.xx.xx.xx:23801, http://xx.xx.xx.xx:23791, false
816ab764a1780c22, started, etcd2, http://xx.xx.xx.xx:23802, http://xx.xx.xx.xx:23792, false
e87b893102f81d9f, started, etcd0, http://xx.xx.xx.xx:23800, http://xx.xx.xx.xx:23790, false
测试一下,在容器A(etcd0)中创建一个key,在容器B(etcd1)和容器C(etcd2)中读取。

[root@iZbp1bq6vb70qq3lbmjlh0Z ~]# docker exec -it cf932f70bd6a etcdctl put Test "hello etcd"
OK
[root@iZbp1bq6vb70qq3lbmjlh0Z ~]# docker exec -it 2b900596bc82 etcdctl get Test --prefix
Test
hello etcd
[root@iZbp1bq6vb70qq3lbmjlh0Z ~]# docker exec -it 208ae882d104 etcdctl get Test --prefix
Test
hello etcd
[root@iZbp1bq6vb70qq3lbmjlh0Z ~]#
可以看到,三个容器中的etcd已实现了数据同步过程。至此,etcd利用docker容器生成多节点模拟多台服务器来完成etcd集群的部署已完成。
————————————————
版权声明:本文为CSDN博主「u010046549」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u010046549/article/details/121394600

posted @ 2022-10-20 15:50  ianCloud  阅读(434)  评论(0编辑  收藏  举报