首頁 > 軟體

docker-compose搭建etcd叢集的實現(三節點)

2022-07-05 22:01:28

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

持久化

如果此時啟動,會在當前目錄下建立 ./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叢集內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com