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集群內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: