一、單實例etcd
1、啟動etcd服務
首先下載對應的etcd,然后進行解壓:
[root@localhost software]# tar -xzvf etcd-v3.3.10-linux-amd64.tar.gz -C /project/opt/
查看etcd server版本:
[root@localhost etcd-v3.3.10-linux-amd64]# ./etcd --version etcd Version: 3.3.10 Git SHA: 27fc7e2 Go Version: go1.10.4 Go OS/Arch: linux/amd64
啟動etcd服務:
[root@localhost etcd-v3.3.10-linux-amd64]# ./etcd 2021-02-11 20:02:00.850358 I | etcdmain: etcd Version: 3.3.10 2021-02-11 20:02:00.850419 I | etcdmain: Git SHA: 27fc7e2 ... 2021-02-11 20:02:02.176710 N | embed: serving insecure client requests on 127.0.0.1:2379, this is strongly discouraged!
可以看到etcd默認啟動在2379端口上。
2、客戶端測試
重新開一個終端,進行客戶端測試。
- 寫入鍵值對
# 通過put添加{foo:bar}鍵值對 [root@localhost etcd-v3.3.10-linux-amd64]# ETCDCTL_API=3 ./etcdctl --endpoints=localhost:2379 put foo bar OK
- 讀取鍵值
# 通過get讀取鍵值 [root@localhost etcd-v3.3.10-linux-amd64]# ETCDCTL_API=3 ./etcdctl --endpoints=localhost:2379 get foo foo bar
二、多實例etcd
1、配置與啟動
如何單機啟動多實例etcd,etcd server默認使用2380監聽集群中其它server的請求,但是如果啟動多個實例就需要監聽不同的端口。如果單價創建三個實例,其目錄如下:
# etcd_cluster目錄如下結構 [root@localhost etcd_cluster]# tree . ├── etcd1 │ ├── conf.yml │ └── data ├── etcd2 │ ├── conf.yml │ └── data └── etcd3 ├── conf.yml └── data 6 directories, 3 files
然后為三個實例各自創建三個配置文件:
- etcd1配置文件conf.yml
name: etcd1 data-dir: /project/opt/etcd_cluster/etcd1/data listen-client-urls: http://127.0.0.1:12379 advertise-client-urls: http://127.0.0.1:12379 listen-peer-urls: http://127.0.0.1:12380 initial-advertise-peer-urls: http://127.0.0.1:12380 initial-cluster: etcd1=http://127.0.0.1:12380,etcd2=http://127.0.0.1:22380,etcd3=http://127.0.0.1:32380 initial-cluster-token: etcd-cluster-1 initial-cluster-state: new
- etcd2配置文件conf.yml
name: etcd2 data-dir: /project/opt/etcd_cluster/etcd2/data listen-client-urls: http://127.0.0.1:22379 advertise-client-urls: http://127.0.0.1:22379 listen-peer-urls: http://127.0.0.1:22380 initial-advertise-peer-urls: http://127.0.0.1:22380 initial-cluster: etcd1=http://127.0.0.1:12380,etcd2=http://127.0.0.1:22380,etcd3=http://127.0.0.1:32380 initial-cluster-token: etcd-cluster-1 initial-cluster-state: new
- etcd3配置文件conf.yml
name: etcd3 data-dir: /project/opt/etcd_cluster/etcd3/data listen-client-urls: http://192.168.159.130:32379 advertise-client-urls: http://192.168.159.130:32379 listen-peer-urls: http://192.168.159.130:32380 initial-advertise-peer-urls: http://192.168.159.130:32380 initial-cluster: etcd1=http://192.168.159.130:12380,etcd2=http://192.168.159.130:22380,etcd3=http://192.168.159.130:32380 initial-cluster-token: etcd-cluster-1 initial-cluster-state: new
注意:配置文件中鍵值之間是有空格的。
此時可以通過etcd命令進行啟動:
# 啟動第一個實例 [root@localhost etcd-v3.3.10-linux-amd64]# ./etcd --config-file=/project/opt/etcd_cluster/etcd1/conf.yml # 啟動第二個實例 [root@localhost etcd-v3.3.10-linux-amd64]# ./etcd --config-file=/project/opt/etcd_cluster/etcd2/conf.yml # 啟動第三個實例 [root@localhost etcd-v3.3.10-linux-amd64]# ./etcd --config-file=/project/opt/etcd_cluster/etcd3/conf.yml
查看集群狀態:
[root@localhost etcd-v3.3.10-linux-amd64]# ./etcdctl --endpoints http://127.0.0.1:12379,http://127.0.0.1:22379,http://127.0.0.1:32379 member list 883d459774f37cba: name=etcd2 peerURLs=http://192.168.159.130:22380 clientURLs= isLeader=false aa9a2cb7a887f51b: name=etcd1 peerURLs=http://192.168.159.130:12380 clientURLs=http://127.0.0.1:12379 isLeader=true aab0ded7be6d8b05: name=etcd3 peerURLs=http://192.168.159.130:32380 clientURLs=http://192.168.159.130:32379 isLeader=false
2、客戶端測試
- 寫入數據
選擇任意節點向集群寫入數據。比如etcd1實例:
[root@localhost etcd-v3.3.10-linux-amd64]# ETCDCTL_API=3 ./etcdctl --endpoints 127.0.0.1:12379 put foo bar OK
- 容災能力
如果關閉etcd2實例,不影響數據的正常讀寫:
[root@localhost etcd-v3.3.10-linux-amd64]# ETCDCTL_API=3 ./etcdctl --endpoints 127.0.0.1:12379 put fo ba OK