etcd介紹
系統要求
由於 etcd 將數據寫入磁盤,因此其性能很大程度上取決於磁盤性能。因此,強烈推薦使用 SSD。要評估磁盤是否足夠快用於 etcd,一種可能性是使用磁盤基准測試工具,例如fio。有關如何執行此操作的示例,請閱讀此處. 為了防止性能下降或無意中使鍵值存儲超載,etcd 強制將可配置的存儲大小配額默認設置為 2GB。為避免交換或內存不足,機器應至少有足夠多的 RAM 來覆蓋配額。8GB 是正常環境的建議最大大小,如果配置的值超過該值,etcd 會在啟動時發出警告。在 CoreOS,etcd 集群通常部署在具有雙核處理器、2GB RAM 和至少 80GB SSD 的專用 CoreOS Container Linux 機器上。請注意,性能本質上取決於工作負載;請在生產部署之前進行測試。有關更多建議,請參閱硬件。
為什么是奇數個集群成員?
一個 etcd 集群需要大多數節點(一個仲裁)來就集群狀態的更新達成一致。對於具有 n 個成員的集群,quorum 為 (n/2)+1。對於任何奇數大小的集群,添加一個節點總是會增加仲裁所需的節點數。盡管將節點添加到奇數大小的集群看起來更好,因為有更多的機器,但容錯性更差,因為完全相同數量的節點可能會失敗而不會丟失仲裁,但是有更多的節點可能會失敗。如果集群處於無法容忍更多故障的狀態,在刪除節點之前添加節點是危險的,因為如果新節點無法在集群中注冊(例如,地址配置錯誤),quorum 將永久丟失
最大集群大小是多少?
理論上,沒有硬性限制。然而,一個 etcd 集群可能不應該超過七個節點。谷歌 Chubby 鎖服務,類似於 etcd,並在谷歌內部廣泛部署多年,建議運行五個節點。一個 5 成員的 etcd 集群可以容忍兩個成員的故障,這在大多數情況下就足夠了。盡管較大的集群提供了更好的容錯能力,但寫入性能會受到影響,因為必須在更多機器上復制數據
我應該在刪除不健康的成員之前添加一個成員嗎?
替換 etcd 節點時,重要的是先刪除成員,然后添加其替換
為什么 etcd 會因磁盤延遲峰值而失去其領導者?
這是故意的;磁盤延遲是領導者活躍度的一部分。假設集群領導者需要一分鍾時間將 raft 日志更新同步到磁盤,但 etcd 集群有一秒的選舉超時。即使領導者可以在選舉間隔內處理網絡消息(例如,發送心跳),它實際上是不可用的,因為它不能提交任何新提案;它正在慢速磁盤上等待。如果集群由於磁盤延遲而頻繁失去其領導者,請嘗試調整磁盤設置或 etcd 時間參數
etcd集群搭建
環境:一台物理機,通過不同的端口跑出3個節點的etcd集群,建議奇數節點,以防止腦裂
第一步,下載etcd安裝包
wget -c https://github.com/etcd-io/etcd/releases/download/v3.5.2/etcd-v3.5.2-linux-amd64.tar.gz
第二步,解壓,然后新建配置etcd1.conf文件
name: etcd-1 data-dir: /root/etcd1/data listen-client-urls: http://0.0.0.0:2379 advertise-client-urls: http://127.0.0.1:2379 listen-peer-urls: http://0.0.0.0:2380 initial-advertise-peer-urls: http://127.0.0.1:2380 initial-cluster: etcd-1=http://127.0.0.1:2380,etcd-2=http://127.0.0.1:2480,etcd-3=http://127.0.0.1:2580 initial-cluster-token: etcd-cluster-my initial-cluster-state: new
etcd2.conf配置文件
name: etcd-2 data-dir: /root/etcd2/data listen-client-urls: http://0.0.0.0:2479 advertise-client-urls: http://127.0.0.1:2479 listen-peer-urls: http://0.0.0.0:2480 initial-advertise-peer-urls: http://127.0.0.1:2480 initial-cluster: etcd-1=http://127.0.0.1:2380,etcd-2=http://127.0.0.1:2480,etcd-3=http://127.0.0.1:2580 initial-cluster-token: etcd-cluster-my initial-cluster-state: new
etcd3.conf配置文件
name: etcd-3 data-dir: /root/etcd3/data listen-client-urls: http://0.0.0.0:2579 advertise-client-urls: http://127.0.0.1:2579 listen-peer-urls: http://0.0.0.0:2580 initial-advertise-peer-urls: http://127.0.0.1:2580 initial-cluster: etcd-1=http://127.0.0.1:2380,etcd-2=http://127.0.0.1:2480,etcd-3=http://127.0.0.1:2580 initial-cluster-token: etcd-cluster-my initial-cluster-state: new
編輯腳本
#!/bin/bash CRTDIR=$(pwd) servers=("etcd1" "etcd2" "etcd3") for server in ${servers[@]} do cd ${CRTDIR}/$server nohup ./etcd --config-file=etcd.conf & echo $? done
運行腳本后查看進程和端口
查看集群狀態
[root@VM-0-15-centos etcd1]# ./etcdctl --write-out=table --endpoints=127.0.0.1:2379,127.0.0.1:2479,127.0.0.1:2579 endpoint status +----------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+ | ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS | +----------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+ | 127.0.0.1:2379 | 47a42fb96a975854 | 3.5.2 | 20 kB | false | false | 4 | 31 | 31 | | | 127.0.0.1:2479 | 72ab37cc61e2023b | 3.5.2 | 20 kB | false | false | 4 | 31 | 31 | | | 127.0.0.1:2579 | 470f778210a711ed | 3.5.2 | 20 kB | true | false | 4 | 31 | 31 | | +----------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
[root@VM-0-15-centos etcd1]# ./etcdctl --endpoints=$ENDPOINTS endpoint health 127.0.0.1:2579 is healthy: successfully committed proposal: took = 5.033785ms 127.0.0.1:2479 is healthy: successfully committed proposal: took = 5.003254ms 127.0.0.1:2379 is healthy: successfully committed proposal: took = 4.990036ms
[root@VM-0-15-centos etcd1]# ./etcdctl -w table member list +------------------+---------+--------+-----------------------+-----------------------+------------+ | ID | STATUS | NAME | PEER ADDRS | CLIENT ADDRS | IS LEARNER | +------------------+---------+--------+-----------------------+-----------------------+------------+ | 470f778210a711ed | started | etcd-3 | http://127.0.0.1:2580 | http://127.0.0.1:2579 | false | | 47a42fb96a975854 | started | etcd-1 | http://127.0.0.1:2380 | http://127.0.0.1:2379 | false | | 72ab37cc61e2023b | started | etcd-2 | http://127.0.0.1:2480 | http://127.0.0.1:2479 | false | +------------------+---------+--------+-----------------------+-----------------------+------------+
日常操作
下面的ENDPOINTS="127.0.0.1:2379,127.0.0.1:2479,127.0.0.1:2579"
#添加數據 [root@VM-0-15-centos etcd1]# ./etcdctl put /etc/password 123456
#刪除數據
[root@VM-0-15-centos etcd1]# ./etcdctl del /etc/password
#取出數據 [root@VM-0-15-centos etcd1]# ./etcdctl --endpoints=$ENDPOINTS get /etc/password #查看所有key [root@VM-0-15-centos etcd1]# ./etcdctl --endpoints=$ENDPOINTS get / --prefix --keys-only #添加角色 etcdctl --endpoints=${ENDPOINTS} role add root #授予讀寫權限 etcdctl --endpoints=${ENDPOINTS} role grant-permission root readwrite foo #查看root角色 etcdctl --endpoints=${ENDPOINTS} role get root #添加用戶root etcdctl --endpoints=${ENDPOINTS} user add root #給root用戶添加root角色 etcdctl --endpoints=${ENDPOINTS} user grant-role root root #獲取root用戶 etcdctl --endpoints=${ENDPOINTS} user get root #開啟認證 etcdctl --endpoints=${ENDPOINTS} auth enable #開啟認證后添加數據 etcdctl --endpoints=${ENDPOINTS} --user=root:123 put foo bar #如果不給密碼就獲取不到foo這個key etcdctl --endpoints=${ENDPOINTS} get foo #開啟認證后查看foo這個key etcdctl --endpoints=${ENDPOINTS} --user=root:123 get foo
#監視一個key
./etcdctl watch /aaa/name
#查看報警
./etcdctl alarm list
#解除報警
./etcdctl alarm disarm
#碎片整理
介紹:DEFRAG 在 etcd 運行時對一組給定端點的后端數據庫文件進行碎片整理。當 etcd 成員從已刪除和壓縮的鍵中回收存儲空間時,該空間將保留在空閑列表中,並且數據庫文件保持相同大小。通過對數據庫進行碎片整理,etcd 成員將這些空閑空間釋放回文件系統。
注意:要離線碎片整理(--data-dir
標志),請使用:etcutl defrag
代替
請注意,對活動成員進行碎片整理會阻止系統在重建其狀態時讀取和寫入數據。
請注意,碎片整理請求不會通過集群進行復制。即請求只應用於本地節點。--endpoints
在flag 或flag 中指定所有成員--cluster
以自動查找所有集群成員
./etcdctl --endpoints=localhost:2379,badendpoint:2379 defrag
./etcdctl defrag --cluster
etcd v2遷移到etcd v3
# write key in etcd version 2 store export ETCDCTL_API=2 etcdctl --endpoints=http://$ENDPOINT set foo bar # read key in etcd v2 etcdctl --endpoints=$ENDPOINTS --output="json" get foo # stop etcd node to migrate, one by one # migrate v2 data export ETCDCTL_API=3 etcdctl --endpoints=$ENDPOINT migrate --data-dir="default.etcd" --wal-dir="default.etcd/member/wal" # restart etcd node after migrate, one by one # confirm that the key got migrated etcdctl --endpoints=$ENDPOINTS get /foo