1. 程式人生 > >ETCD叢集安裝配置及簡單應用

ETCD叢集安裝配置及簡單應用

環境配置


CentOS Linux release 7.3.1611 (Core) 

etcd-v3.2.6


192.168.108.128 節點1

192.168.108.129 節點2

192.168.108.130 節點3


ETCD下載


https://github.com/coreos/etcd/releases/download/v3.2.6/etcd-v3.2.6-linux-amd64.tar.gz


ETCD安裝配置

  • 解壓安裝:

$ tar -zxvf  etcd-v3.2.6-linux-amd64.tar.gz -C /opt/

$ cd /opt

$ mv etcd-v3.2.6-linux-amd64  etcd-v3.2.6

$ mkdir /etc/etcd           # 建立etcd配置檔案目錄

  • 建立etcd配置檔案:

$ vi /etc/etcd/conf.yml

節點1,新增如下內容:

name: etcd-1
data-dir: /opt/etcd-v3.2.6/data
listen-client-urls: http://192.168.108.128:2379,http://127.0.0.1:2379
advertise-client-urls: http://192.168.108.128:2379,http://127.0.0.1:2379
listen-peer-urls: http://192.168.108.128:2380
initial-advertise-peer-urls: http://192.168.108.128:2380
initial-cluster: etcd-1=http://192.168.108.128:2380,etcd-2=http://192.168.108.129:2380,etcd-3=http://192.168.108.130:2380
initial-cluster-token: etcd-cluster-token
initial-cluster-state: new


節點2,新增如下內容:

name: etcd-2
data-dir: /opt/etcd-v3.2.6/data
listen-client-urls: http://192.168.108.129:2379,http://127.0.0.1:2379
advertise-client-urls: http://192.168.108.129:2379,http://127.0.0.1:2379
listen-peer-urls: http://192.168.108.129:2380
initial-advertise-peer-urls: http://192.168.108.129:2380
initial-cluster: etcd-1=http://192.168.108.128:2380,etcd-2=http://192.168.108.129:2380,etcd-3=http://192.168.108.130:2380
initial-cluster-token: etcd-cluster-token
initial-cluster-state: new

節點3,新增如下內容:

name: etcd-3
data-dir: /opt/etcd-v3.2.6/data
listen-client-urls: http://192.168.108.130:2379,http://127.0.0.1:2379
advertise-client-urls: http://192.168.108.130:2379,http://127.0.0.1:2379
listen-peer-urls: http://192.168.108.130:2380
initial-advertise-peer-urls: http://192.168.108.130:2380
initial-cluster: etcd-1=http://192.168.108.128:2380,etcd-2=http://192.168.108.129:2380,etcd-3=http://192.168.108.130:2380
initial-cluster-token: etcd-cluster-token
initial-cluster-state: new

  • 更新etcd系統預設配置:

當前使用的是etcd v3版本,系統預設的是v2,通過下面命令修改配置。

$ vi /etc/profile

在末尾追加

export ETCDCTL_API=3

$ source /etc/profile


ETCD命令


$ cd  /opt/etcd-v3.2.6

  • 建立etcd配置檔案:

$ ./etcdctl version

etcdctl version: 3.2.6
API version: 3.2

  • 啟動命令:

$ ./etcd --config-file=/etc/etcd/conf.yml

  • 檢視叢集成員資訊:

$ ./etcdctl member list

2618ce5cd761aa8e: name=etcd-3 peerURLs=http://192.168.108.130:2380 clientURLs=http://127.0.0.1:2379,http://192.168.108.130:2379 isLeader=false
9c359d48a2f34938: name=etcd-1 peerURLs=http://192.168.108.128:2380 clientURLs=http://127.0.0.1:2379,http://192.168.108.128:2379 isLeader=false
f3c45714407d68f3: name=etcd-2 peerURLs=http://192.168.108.129:2380 clientURLs=http://127.0.0.1:2379,http://192.168.108.129:2379 isLeader=true

  • 檢視叢集狀態(Leader節點):

$ ./etcdctl cluster-health

member 2618ce5cd761aa8e is healthy: got healthy result from http://127.0.0.1:2379
member 9c359d48a2f34938 is healthy: got healthy result from http://127.0.0.1:2379
member f3c45714407d68f3 is healthy: got healthy result from http://127.0.0.1:2379
cluster is healthy


ECTD讀寫操作


基於HTTP協議的API使用起來比較簡單,這裡主要通過etcdctl和curl兩種方式來做簡單介紹。

下面通過給message key設定Hello值示例:

$ ./etcdctl set /message Hello

Hello

$ curl -X PUT http://127.0.0.1:2379/v2/keys/message -d value="Hello"
{"action":"set","node":{"key":"/message","value":"Hello","modifiedIndex":4,"createdIndex":4}}


讀取message的值:

$ ./etcdctl  get /message
Hello

$ curl http://127.0.0.1:2379/v2/keys/message
{"action":"get","node":{"key":"/message","value":"Hello","modifiedIndex":9,"createdIndex":9}}


刪除message key:

$ ./etcdctl  rm  /message

$ curl -X DELETE http://127.0.0.1:2379/v2/keys/message
{"action":"delete","node":{"key":"/message","modifiedIndex":10,"createdIndex":9},"prevNode":{"key":"/message","value":"Hello","modifiedIndex":9,"createdIndex":9}}


說明:因為是叢集,所以message在其中一個節點建立後,在叢集中的任何節點都可以查詢到。


配置ETCD為啟動服務


編輯/usr/lib/systemd/system/etcd.service,新增下面內容:

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

[Service]
Type=notify
WorkingDirectory=/opt/etcd-v3.2.6/
# User=etcd
ExecStart=/opt/etcd-v3.2.6/etcd --config-file=/etc/etcd/conf.yml
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

更新啟動:

systemctl daemon-reload
systemctl enable etcd
systemctl start etcd
systemctl restart etcd

systemctl status etcd.service -l


參考文件


https://coreos.com/etcd/docs/latest/getting-started-with-etcd.html