docker-compose搭建etcd集群的实现(三节点)

1. 环境说明

节点 IP
etcd1 10.10.239.31
etcd2 10.10.239.32
etcd3 10.10.239.33

2. 部署

etcd1

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

etcd2

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

持久化(同上)

启动服务(同上)

etcd3

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

持久化(同上)

启动服务(同上)

3. 验证

3.1 查看集群状态

查看集群成员

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 |       |
+----------------+--------+------------+-------+

3.2 读写测试

登录其中一个节点,进入容器,写一条数据
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集群的实现(三节点)的文章就介绍到这了,更多相关docker-compose搭建etcd集群内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(docker-compose搭建etcd集群的实现(三节点))