本文小编为大家详细介绍“怎么使用docker compose搭建etcd集群”,内容详细,步骤清晰,细节处理妥当,希望这篇“怎么使用docker compose搭建etcd集群”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
节点 | IP |
---|---|
etcd1 | 10.10.239.31 |
etcd2 | 10.10.239.32 |
etcd3 | 10.10.239.33 |
ymal文件
version: "3.0" services: etcd1: image: bitnami/etcd:latest container_name: etcd1 restart: always ports: - "2379:2379" - "2380:2380" environment: - ALLOW_NONE_AUTHENTICATION=yes #示例中我们不设置密码 - ETCD_NAME=etcd1 #节点自己的名字 - ETCD_ADVERTISE_CLIENT_URLS=http://10.10.239.31:2379 #告知集群自己的客户端地址 - ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379 #设置监听客户端通讯的URL列表 - ETCD_INITIAL_ADVERTISE_PEER_URLS=http://10.10.239.31:2380 #告知集群自己集群通讯地址 - ETCD_LISTEN_PEER_URLS=http://0.0.0.0:2380 #用于监听伙伴通讯的URL列表 - ETCD_INITIAL_CLUSTER_TOKEN=etcd-cluster #etcd 集群的初始化集群记号 - ETCD_INITIAL_CLUSTER=etcd1=http://10.10.239.31:2380,etcd2=http://10.10.239.32:2380,etcd3=http://10.10.239.33:2380 #集群成员 - ETCD_INITIAL_CLUSTER_STATE=new #初始化集群状态 volumes: - ./data:/bitnami/etcd
持久化
如果此时启动,会在当前目录下创建 ./data 目录,挂载到容器的 /bitnami/etcd ,但我们要注意的是,容器中启动etcd的用户并不是root用户,很可能没有在目录下创建文件的权限。我们有两个方案:
【方法一】给777权限(不建议)
【方法二】所属主改成对应宿主机用户(建议)
方法二操作:
先给./data目录777权限
查看新生成的文件属主是宿主机哪个用户,就将./data目录属主改为哪个用户
再将权限改回755或750,重启容器
启动服务
[root@liubei-01 etcd1]# docker-compose up -d
查看结果
[root@liubei-01 etcd1]# docker-compose ps Name Command State Ports ----------------------------------------------------------------------------------------------- etcd1 /opt/bitnami/scripts/etcd/ ... Up 0.0.0.0:2379->2379/tcp, 0.0.0.0:2380->2380/tcp
ymal文件
version: "3.0" services: etcd1: image: bitnami/etcd:latest container_name: etcd2 restart: always ports: - "2379:2379" - "2380:2380" environment: - ALLOW_NONE_AUTHENTICATION=yes - ETCD_NAME=etcd2 - ETCD_ADVERTISE_CLIENT_URLS=http://10.10.239.32:2379 - ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379 - ETCD_INITIAL_ADVERTISE_PEER_URLS=http://10.10.239.32:2380 - ETCD_LISTEN_PEER_URLS=http://0.0.0.0:2380 - ETCD_INITIAL_CLUSTER_TOKEN=etcd-cluster - ETCD_INITIAL_CLUSTER=etcd1=http://10.10.239.31:2380,etcd2=http://10.10.239.32:2380,etcd3=http://10.10.239.33:2380 - ETCD_INITIAL_CLUSTER_STATE=new volumes: - ./data:/bitnami/etcd
持久化(同上)
启动服务(同上)
ymal文件
version: "3.0" services: etcd1: image: bitnami/etcd:latest container_name: etcd3 restart: always ports: - "2379:2379" - "2380:2380" environment: - ALLOW_NONE_AUTHENTICATION=yes - ETCD_NAME=etcd3 - ETCD_ADVERTISE_CLIENT_URLS=http://10.10.239.33:2379 - ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379 - ETCD_INITIAL_ADVERTISE_PEER_URLS=http://10.10.239.33:2380 - ETCD_LISTEN_PEER_URLS=http://0.0.0.0:2380 - ETCD_INITIAL_CLUSTER_TOKEN=etcd-cluster - ETCD_INITIAL_CLUSTER=etcd1=http://10.10.239.31:2380,etcd2=http://10.10.239.32:2380,etcd3=http://10.10.239.33:2380 - ETCD_INITIAL_CLUSTER_STATE=new volumes: - ./data:/bitnami/etcd
持久化(同上)
启动服务(同上)
查看集群成员
I have no name!@555187fb758c:/opt/bitnami/etcd$ etcdctl --write-out=table member list +------------------+---------+-------+--------------------------+--------------------------+------------+ | ID | STATUS | NAME | PEER ADDRS | CLIENT ADDRS | IS LEARNER | +------------------+---------+-------+--------------------------+--------------------------+------------+ | 57d6f74aaa8d5d3e | started | etcd1 | http://10.10.239.31:2380 | http://10.10.239.31:2379 | false | | 9107e491ab549324 | started | etcd3 | http://10.10.239.33:2380 | http://10.10.239.33:2379 | false | | e7b147006e212ca5 | started | etcd2 | http://10.10.239.32:2380 | http://10.10.239.32:2379 | false | +------------------+---------+-------+--------------------------+--------------------------+------------+
查看本节点状态
I have no name!@555187fb758c:/opt/bitnami/etcd$ etcdctl --write-out=table endpoint status +----------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+ | ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS | +----------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+ | 127.0.0.1:2379 | 57d6f74aaa8d5d3e | 3.5.4 | 20 kB | true | false | 2 | 10 | 10 | | +----------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
查看本节点健康状态
I have no name!@555187fb758c:/opt/bitnami/etcd$ etcdctl --write-out=table endpoint health +----------------+--------+------------+-------+ | ENDPOINT | HEALTH | TOOK | ERROR | +----------------+--------+------------+-------+ | 127.0.0.1:2379 | true | 3.736825ms | | +----------------+--------+------------+-------+
登录其中一个节点,进入容器,写一条数据
zai
[root@liubei-01 etcd3]# docker exec -it 788 bash I have no name!@788dd01aaa79:/opt/bitnami/etcd$ etcd etcd etcdctl I have no name!@788dd01aaa79:/opt/bitnami/etcd$ etcdctl put name "liubei" OK
登录另一个节点,进入容器,读取数据
[root@liubei-02 ~]# docker exec -it 555 bash I have no name!@555187fb758c:/opt/bitnami/etcd$ etcdctl get name name liubei
读到这里,这篇“怎么使用docker compose搭建etcd集群”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。