部署etcd集羣

參考: https://www.cnblogs.com/51wansheng/p/10234036.html

一、環境說明

  • ubuntu 18.04 172.18.0.30 (master)
  • ubuntu 18.04 172.18.0.26 (node1)
  • etcd 版本:v2.3.7

二、安裝etcd

分別在master和node1機器上安裝etcd

# step1:下載某版本etcd的release包
$ wget https://github.com/etcd-io/etcd/releases/download/v2.3.7/etcd-v2.3.7-linux-amd64.tar.gz

# step2:解壓安裝到 /data/ 目錄下(安裝到那個目錄可以自己定)
$ tar -C /data -zxvf etcd-v2.3.7-linux-amd64.tar.gz

# step3:檢查安裝是否成功
$ /data/etcd-v2.3.7-linux-amd64/etcdctl -v
etcdctl version 2.3.7

三、配置etcd

etcd重要配置參數的說明,不通版本的etcd可能參數選項以及選項的默認值會有些許出入,可以通過 etcd --help查看

--name										# etcd實例名稱
--data-dir									# etcd數據存儲的目錄
--listen-client-urls						# 供外部客戶端使用的url
--advertise-client-urls				# 廣播給外部客戶端使用的url
--listen-peer-urls						# 集羣內部通信使用的url
--initial-advertise-peer-urls		# 廣播給集羣內其他成員訪問的url
--initial-cluster							# 初始集羣成員列表
--initial-cluster-token				# 集羣的名稱
--initial-cluster-state					# 初始集羣狀態,new爲新建集羣(master爲new,node爲existing)

我們通過systemd來管理etcd服務,所以在/etc/systemd/system目錄下新建etcd.service文件,不瞭解systemd的可以查看阮一峯老師的博客

1、master機器(172.18.0.30)

# vim /etcsystemd/system/etcd.service

[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
Type=notify
WorkingDirectory=/data/etcd-v2.3.7-linux-amd64
User=root
ExecStart=/data/etcd-v2.3.7-linux-amd64/etcd --name etcd0 --data-dir /data/etcd_data/etcd1 \
    --listen-client-urls http://172.18.0.30:2379,http://localhost:2379 --advertise-client-urls http://172.18.0.30:2379,http://localhost:2379 \
    --listen-peer-urls http://172.18.0.30:2380 --initial-advertise-peer-urls http://172.18.0.30:2380 \
    --initial-cluster etcd0=http://172.18.0.30:2380,etcd1=http://172.18.0.26:2480 --initial-cluster-token etcd-cluster \
    --initial-cluster-state new
Restart=always

[Install]
WantedBy=multi-user.target

注:如果默認的23792380端口已被佔用,可以配置etcd監聽其他未被佔用的端口

2、node1機器(172.18.0.26)

# vim /etc/systemd/system/etcd.service

[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
Type=notify
WorkingDirectory=/data/etcd-v2.3.7-linux-amd64
User=root
ExecStart=/data/etcd-v2.3.7-linux-amd64/etcd --name etcd1 --data-dir /data/etcd_data/etcd0 \
    --listen-client-urls http://172.18.0.26:2479,http://localhost:2479 --advertise-client-urls http://172.18.0.26:2479,http://localhost:2479 \
    --listen-peer-urls http://172.18.0.26:2480 --initial-advertise-peer-urls http://172.18.0.26:2480 \
    --initial-cluster etcd0=http://172.18.0.30:2380,etcd1=http://172.18.0.26:2480 --initial-cluster-token etcd-cluster \
    --initial-cluster-state existing
Restart=always

[Install]
WantedBy=multi-user.target

三、啓動etcd服務

配置好etcd的服務後,分別啓動master和node1節點的etcd服務

# step1:重新加載systemd配置文件(修改systemd配置文件後必須重新加載)
$ systemctl daemon-reload

# step2:啓動etcd服務
$ systemctl restart etcd

# 停止etcd服務
$ systmectl stop etcd

# 查看etcd狀態
$ systemctl status etcd

# 查看etcd日誌
$ journalctl -u etcd

# 設置etcd開機啓動
$ systemctl enable etcd

四、使用etcd

# 查看etcd集羣成員(可以在集羣成員的任一臺機器上操作)
$ /data/etcd-v2.3.7-linux-amd64/etcdctl member list
bd3b67d271b1097d: name=etcd0 peerURLs=http://172.18.0.30:2380 clientURLs=http://172.18.0.30:2379,http://localhost:2379 isLeader=true
e5ad2a6d82697b13: name=etcd1 peerURLs=http://172.18.0.26:2480 clientURLs=http://172.18.0.26:2479,http://localhost:2479 isLeader=false

# 查看集羣成員健康情況
$ /data/etcd-v2.3.7-linux-amd64/etcdctl cluster-health
member bd3b67d271b1097d is healthy: got healthy result from http://172.18.0.30:2379
member e5ad2a6d82697b13 is healthy: got healthy result from http://172.18.0.26:2479
cluster is healthy

# 在master上set一個key-value
$ /data/etcd-v2.3.7-linux-amd64/etcdctl set name tab609
tab609

# 在node1上get這個key的值(注:如果監聽的不是默認端口2380則需要指定 --endpoints)
$ /data/etcd-v2.3.7-linux-amd64/etcdctl --endpoints http://localhost:2479 get name
tab609

# 更多etcdctl操作可以查看幫助
$ /data/etcd-v2.3.7-linux-amd64/etcdctl help

五、etcd集羣新增一個節點node2(172.18.0.17)

1、安裝etcd

同上安裝步驟

2、配置etcd

node2節點

#  vim /etc/systemd/sytem/etcd.service

[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
Type=notify
WorkingDirectory=/data/etcd-v2.3.7-linux-amd64
User=root
ExecStart=/data/etcd-v2.3.7-linux-amd64/etcd --name etcd2 --data-dir /data/etcd_data/etcd2 \
    --listen-client-urls http://172.18.0.17:2479,http://localhost:2479 --advertise-client-urls http://172.18.0.17:2479,http://localhost:2479 \
    --listen-peer-urls http://172.18.0.17:2480 --initial-advertise-peer-urls http://172.18.0.17:2480 \
    --initial-cluster etcd0=http://172.18.0.30:2380,etcd1=http://172.18.0.26:2480,etcd2=http://172.18.0.17:2480 --initial-cluster-token yj1918-etcd-cluster \
    --initial-cluster-state existing
Restart=always

[Install]
WantedBy=multi-user.target

master節點配置文件中的集羣成員添加node2節點,只需修改--initial-cluster參數選項

# vim /etc/systemd/sytem/etcd.service

ExecStart= ... \
	--initial-cluster etcd0=http://172.18.0.30:2380,etcd1=http://172.18.0.26:2480,etcd2=http://172.18.0.17:2480

同理node1節點配置文件中的集羣成員也添加node2節點,只需修改--initial-cluster參數選項

# vim /etc/systemd/sytem/etcd.service

ExecStart= ... \
	--initial-cluster etcd0=http://172.18.0.30:2380,etcd1=http://172.18.0.26:2480,etcd2=http://172.18.0.17:2480

3、啓動etcd服務

分別在master、node1、node2上執行同上的啓動步驟,但在啓動node2的時候啓動失敗,報如下錯誤

error validating peerURLs {ClusterID:b0f36b8e1c8349f4 Members:[&{ID:bd3b67d271b1097d RaftAttributes:{PeerURLs:[http://172.18.0.30:2380]} Attributes:{Name:etcd0 ClientURLs:[http://172.18.0.30:2379]

也就是說在驗證對端(master)的rul時出錯了,谷歌後找到解決方法,我們需要在啓動新節點之前,必須把新節點接入集羣,然後再啓動新節點

# 在master上添加新的節點成員(master機器操作)
$  /data/etcd-v2.3.7-linux-amd64/etcdctl member add node2 http://172.18.0.17:2480

# 重新啓動node2的etcd服務就可以成功了(node2機器操作)
$ systemctl restart etcd

# 查看集羣的成員(如我在node2上執行,如果監聽的不是默認端口2379則需要指定 --endpoints)
$ /data/etcd-v2.3.7-linux-amd64/etcdctl --endpoints http://172.18.0.17:2479 member list
bd3b67d271b1097d: name=etcd0 peerURLs=http://172.18.0.30:2380 clientURLs=http://172.18.0.30:2379,http://localhost:2379 isLeader=true
e20ddba3b692fe46: name=etcd2 peerURLs=http://172.18.0.17:2480 clientURLs=http://172.18.0.17:2479,http://localhost:2479 isLeader=false
e5ad2a6d82697b13: name=etcd1 peerURLs=http://172.18.0.26:2480 clientURLs=http://172.18.0.26:2479,http://localhost:2479 isLeader=false

寫在最後: etcd是集羣,我們get取數據的時候,怎麼保證數據的一致性和可靠性呢?etcd是通過類似選舉的方式,過半通過規則返回數據。比如集羣有三臺機器,如果兩臺機器的key-value都相同,即2/3過半,則返回數據。爲了保證數據的一致和可靠性,etcd集羣的機器數必須奇數且大於一個節點,並非偶數,更詳細可以查看:https://www.jianshu.com/p/5aed73b288f7

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章