ETCD集群安裝配置及簡單應用 老版本


一、環境准備

  • CentOS Linux release 7.3.1611 (Core) 
  • etcd-v3.2.6

二、ETCD下載

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

三、ETCD安裝配置

1.部署架構

  • 192.168.108.128 節點1
  • 192.168.108.129 節點2
  • 192.168.108.130 節點3

2.解壓安裝:

  1.  
    $ tar -zxvf  etcd-v3.2.6-linux-amd64.tar.gz -C /opt/
  2.  
    $ cd /opt
  3.  
    $ mv etcd-v3.2.6-linux-amd64  etcd-v3.2.6
  4.  
    # 創建etcd配置文件目錄
  5.  
    $ mkdir /etc/etcd          

3.創建etcd配置文件:

$ vi /etc/etcd/conf.yml

節點1,添加如下內容:

  1.  
    name: etcd-1
  2.  
    data-dir: /opt/etcd-v3.2.6/data
  3.  
    listen-client-urls: http://192.168.108.128:2379,http://127.0.0.1:2379
  4.  
    advertise-client-urls: http://192.168.108.128:2379,http://127.0.0.1:2379
  5.  
    listen-peer-urls: http://192.168.108.128:2380
  6.  
    initial-advertise-peer-urls: http://192.168.108.128:2380
  7.  
    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
  8.  
    initial-cluster-token: etcd-cluster-token
  9.  
    initial-cluster-state: new

節點2,添加如下內容:

  1.  
    name: etcd-2
  2.  
    data-dir: /opt/etcd-v3.2.6/data
  3.  
    listen-client-urls: http://192.168.108.129:2379,http://127.0.0.1:2379
  4.  
    advertise-client-urls: http://192.168.108.129:2379,http://127.0.0.1:2379
  5.  
    listen-peer-urls: http://192.168.108.129:2380
  6.  
    initial-advertise-peer-urls: http://192.168.108.129:2380
  7.  
    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
  8.  
    initial-cluster-token: etcd-cluster-token
  9.  
    initial-cluster-state: new

節點3,添加如下內容:

  1.  
    name: etcd-3
  2.  
    data-dir: /opt/etcd-v3.2.6/data
  3.  
    listen-client-urls: http://192.168.108.130:2379,http://127.0.0.1:2379
  4.  
    advertise-client-urls: http://192.168.108.130:2379,http://127.0.0.1:2379
  5.  
    listen-peer-urls: http://192.168.108.130:2380
  6.  
    initial-advertise-peer-urls: http://192.168.108.130:2380
  7.  
    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
  8.  
    initial-cluster-token: etcd-cluster-token
  9.  
    initial-cluster-state: new

4.更新etcd系統默認配置:

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

$ vi /etc/profile

在文件末尾追加:

export ETCDCTL_API=3

 使文件生效:

$ source /etc/profile

四、ETCD命令

$ cd  /opt/etcd-v3.2.6

1.查看版本信息:

  1.  
    $ ./etcdctl version
  2.  
     
  3.  
    etcdctl version: 3.2.6
  4.  
    API version: 3.2

2.啟動命令:

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

3.查看集群成員信息:

  1.  
    $ ./etcdctl member list
  2.  
     
  3.  
    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
  4.  
    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
  5.  
    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

4.查看集群狀態(Leader節點):

  1.  
    $ ./etcdctl cluster-health
  2.  
     
  3.  
    member 2618ce5cd761aa8e is healthy: got healthy result from http://127.0.0.1:2379
  4.  
    member 9c359d48a2f34938 is healthy: got healthy result from http://127.0.0.1:2379
  5.  
    member f3c45714407d68f3 is healthy: got healthy result from http://127.0.0.1:2379
  6.  
    cluster is healthy

5.HTTPS加密下使用etcdctl檢查集群健康狀況:

  1.  
    $ etcdctl --ca-file /etc/ssl/etcd/ca.pem --cert-file /etc/ssl/etcd/server1.pem --key-file /etc/ssl/etcd/server1-key.pem member list
  2.  
    $ etcdctl --ca-file /etc/ssl/etcd/ca.pem --cert-file /etc/ssl/etcd/server1.pem --key-file /etc/ssl/etcd/server1-key.pem cluster-health

五、ECTD讀寫操作

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

1.下面通過給message key設置Hello值示例:

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

2.讀取message的值:

  1.  
    $ ./etcdctl  get /message
  2.  
    Hello
  3.  
     
  4.  
    $ curl http://127.0.0.1:2379/v2/keys/message
  5.  
    { "action":"get","node":{"key":"/message","value":"Hello","modifiedIndex":9,"createdIndex":9}}

3.刪除message key:

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

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

4.查看所有key-value:

curl -s http://127.0.0.1:2379/v2/keys/?recursive=true

六、配置ETCD為啟動服務

1.編輯/usr/lib/systemd/system/etcd.service,添加下面內容:

  1.  
    [Unit]
  2.  
    Description=Etcd Server
  3.  
    After=network.target
  4.  
    After=network-online.target
  5.  
    Wants=network-online.target
  6.  
     
  7.  
    [Service]
  8.  
    Type=notify
  9.  
    WorkingDirectory=/opt/etcd-v3.2.6/
  10.  
    # User=etcd
  11.  
    ExecStart=/opt/etcd-v3.2.6/etcd --config-file=/etc/etcd/conf.yml
  12.  
    Restart=on-failure
  13.  
    LimitNOFILE=65536
  14.  
     
  15.  
    [Install]
  16.  
    WantedBy=multi-user.target

2.更新啟動:

  1.  
    systemctl daemon-reload
  2.  
    systemctl enable etcd
  3.  
    systemctl start etcd
  4.  
    systemctl restart etcd
  5.  
     
  6.  
    systemctl status etcd.service -l


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM