• docker-compose搭建etcd集群(三节点)


    1. 环境说明

    节点IP
    etcd110.10.239.31
    etcd210.10.239.32
    etcd310.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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 持久化

    如果此时启动,会在当前目录下创建 ./data 目录,挂载到容器的 /bitnami/etcd ,但我们要注意的是,容器中启动etcd的用户并不是root用户,很可能没有在目录下创建文件的权限。我们有两个方案:

    【方法一】给777权限(不建议)

    【方法二】所属主改成对应宿主机用户(建议)

    方法二操作:

    • 先给./data目录777权限
    • 查看新生成的文件属主是宿主机哪个用户,就将./data目录属主改为哪个用户
    • 再将权限改回755或750,重启容器
    • 启动服务
    [root@liubei-01 etcd1]# docker-compose up -d
    
    • 1
    • 查看结果
    [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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 持久化(同上)

    • 启动服务(同上)

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 持久化(同上)

    • 启动服务(同上)

    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 |
    +------------------+---------+-------+--------------------------+--------------------------+------------+
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 查看本节点状态
    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 |        |
    +----------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 查看本节点健康状态
    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 |       |
    +----------------+--------+------------+-------+
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 登录另一个节点,进入容器,读取数据
    [root@liubei-02 ~]# docker exec -it 555 bash
    I have no name!@555187fb758c:/opt/bitnami/etcd$ etcdctl get name
    name
    liubei
    
    • 1
    • 2
    • 3
    • 4
  • 相关阅读:
    每日一题 1993. 树上的操作
    二重积分一般计算步骤
    rmq nameserver
    Dart(2)-变量
    计算机网络常见面试题
    用DolphinScheduler轻松实现Flume数据采集任务自动化!
    Rethinking on Multi-Stage Networks for Human Pose Estimation 源码分析
    LeetCode-297-二叉树的序列化与反序列化
    python opencv实现灰度图的符号化(仿qgis单波段伪彩色)
    2022年全球市场肌腱固定螺钉总体规模、主要生产商、主要地区、产品和应用细分研究报告
  • 原文地址:https://blog.csdn.net/xingzuo_1840/article/details/125558919