etcd介紹
etcd是CoreOS團(tuán)隊于2013年6月發(fā)起的開源項目,它的目標(biāo)是構(gòu)建一個高可用的分布式鍵值(key-value)數(shù)據(jù)庫。etcd內(nèi)部采用raft協(xié)議作為一致性算法,etcd基于Go語言實現(xiàn)。
etcd的有點有以下:
- 簡單:安裝配置簡單,而且提供了HTTP API進(jìn)行交互,使用也很簡單
- 安全:支持SSL證書驗證
- 快速:根據(jù)官方提供的benchmark數(shù)據(jù),單實例支持每秒2k+讀操作
- 可靠:采用raft算法,實現(xiàn)分布式系統(tǒng)數(shù)據(jù)的可用性和一致性
etcd 項目詳見:https://github.com/coreos/etcd/
安裝之前
一般不建議root安裝業(yè)務(wù)中間件服務(wù),故這里需要先進(jìn)行環(huán)境初始化
| 主機(jī)IP | etcd name |
|---|---|
| 192.168.2.213 | etcd1 |
| 192.168.2.214 | etcd2 |
| 192.168.2.215 | etcd3 |
新增系統(tǒng)用戶etcd
groupadd --system etcd
useradd -s /sbin/nologin -m /var/lib/etcd --system -g etcd etcd
新增配置文件路徑
mkdir /etc/etcd
chown -R etcd:etcd /etc/etcd
修改 SELinux 為 disabled
setenforce 0
sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
安裝
下載二進(jìn)制安裝包
curl -s https://api.github.com/repos/etcd-io/etcd/releases/latest | grep browser_download_url | grep linux-amd64 |cut -d '"' -f 4 | wget -qi -
或者百度網(wǎng)盤下載
鏈接: https://pan.baidu.com/s/1xOeIYWTAs0VMcLoCuT6BTg 提取碼: 5kmf
如下進(jìn)入安裝
tar -zxvf etcd-v3.2.32-linux-amd64.tar.gz
cd etcd-v3.2.32-linux-amd64
cp etcd etcdctl /usr/local/bin
chown -R etcd:etcd /usr/local/bin/etcd*
驗證安裝
root@pts/1 $ etcd --version
etcd Version: 3.2.32
Git SHA: 7dc07f2a9
Go Version: go1.12.17
Go OS/Arch: linux/amd64
root@pts/1 $ etcdctl --version
etcdctl version: 3.2.32
API version: 2
配置和啟動
1、三臺主機(jī)分別配置/etc/hosts如下
# etcd
192.168.2.213 etc1
192.168.2.214 etc2
192.168.2.215 etc3
2、三臺主機(jī)配置新增配置文件/etc/etcd/etcd.conf 這里注意 etcd1 配置中 ETCD_INITIAL_CLUSTER_STATE 為 new 其余兩個為exist
另外參數(shù)ETCD_LISTEN_CLIENT_URLS、ETCD_ADVERTISE_CLIENT_URLS、ETCD_LISTEN_PEER_URLS 、ETCD_INITIAL_ADVERTISE_PEER_URLS 分別為當(dāng)前主機(jī)的IP,ETCD_NAME 根據(jù)開始的定義配置
# member
ETCD_NAME=etcd1
ETCD_DATA_DIR=/var/lib/etcd
ETCD_LISTEN_CLIENT_URLS=http://192.168.2.213:2379,http://127.0.0.1:2379
ETCD_ADVERTISE_CLIENT_URLS=http://192.168.2.213:2379
# cluster
ETCD_LISTEN_PEER_URLS=http://192.168.2.213:2380
ETCD_INITIAL_ADVERTISE_PEER_URLS=http://192.168.2.213:2380
ETCD_INITIAL_CLUSTER=etcd1=http://192.168.2.213:2380,etcd2=http://192.168.2.214:2380,etcd3=http://192.168.2.215:2380
ETCD_INITIAL_CLUSTER_STATE=new
ETCD_INITIAL_CLUSTER_TOKEN=k8s_etcd
3、三臺主機(jī)配置systemctl啟動方式
/usr/lib/systemd/system/etcd.service
[Unit]
Description=etcd key-value store
Documentation=https://github.com/etcd-io/etcd
After=network.target
[Service]
User=etcd
Type=notify
WorkingDirectory=/var/lib/etcd/
EnvironmentFile=/etc/etcd/etcd.conf
ExecStart=/usr/local/bin/etcd
Restart=on-failure
RestartSec=10s
LimitNOFILE=40000
[Install]
WantedBy=multi-user.target
啟動服務(wù)
systemctl enable etcd.service
systemctl start etcd.service
systemctl status etcd.service
測試和驗收
1、檢查各節(jié)點
root@pts/1 $ etcdctl member list
59b0ee3829e2b866: name=etcd2 peerURLs=http://192.168.2.214:2380 clientURLs=http://192.168.2.214:2379 isLeader=false
a8b07bac1693e30e: name=etcd1 peerURLs=http://192.168.2.213:2380 clientURLs=http://192.168.2.213:2379 isLeader=false
cf682ab5655702b8: name=etcd3 peerURLs=http://192.168.2.215:2380 clientURLs=http://192.168.2.215:2379 isLeader=true
2、檢查集群狀態(tài)
root@pts/1 $ etcdctl cluster-health
member 59b0ee3829e2b866 is healthy: got healthy result from http://192.168.2.214:2379
member a8b07bac1693e30e is healthy: got healthy result from http://192.168.2.213:2379
member cf682ab5655702b8 is healthy: got healthy result from http://192.168.2.215:2379
cluster is healthy
3、進(jìn)行測試
root@pts/1 $ etcdctl set /demo 'hello etcd'
hello etcd
root@pts/1 $ etcdctl get /demo
hello etcd
故障演練
停掉Leader節(jié)點
root@pts/1 $ etcdctl member list
59b0ee3829e2b866: name=etcd2 peerURLs=http://192.168.2.214:2380 clientURLs=http://192.168.2.214:2379 isLeader=false
a8b07bac1693e30e: name=etcd1 peerURLs=http://192.168.2.213:2380 clientURLs=http://192.168.2.213:2379 isLeader=true
cf682ab5655702b8: name=etcd3 peerURLs=http://192.168.2.215:2380 clientURLs=http://192.168.2.215:2379 isLeader=false
看到Leader從etcd3轉(zhuǎn)移到etcd1
查看集群狀態(tài)
root@pts/1 $ etcdctl cluster-health
member 59b0ee3829e2b866 is healthy: got healthy result from http://192.168.2.214:2379
member a8b07bac1693e30e is healthy: got healthy result from http://192.168.2.213:2379
failed to check the health of member cf682ab5655702b8 on http://192.168.2.215:2379: Get http://192.168.2.215:2379/health: dial tcp 192.168.2.215:2379: connect: connection refused
member cf682ab5655702b8 is unreachable: [http://192.168.2.215:2379] are all unreachable
cluster is healthy
看到etcd3連接失敗,這個時候在執(zhí)行如下命令進(jìn)行測試,發(fā)現(xiàn)請求正常
root@pts/1 $ etcdctl get /demo
hello etcd
然后啟動故障節(jié)點之后檢查,集群狀態(tài)恢復(fù)
root@pts/1 $ etcdctl cluster-health
member 59b0ee3829e2b866 is healthy: got healthy result from http://192.168.2.214:2379
member a8b07bac1693e30e is healthy: got healthy result from http://192.168.2.213:2379
member cf682ab5655702b8 is healthy: got healthy result from http://192.168.2.215:2379
cluster is healthy