etcd集羣搭建

1、etcd簡介與應用場景

etcd 是一個分佈式一致性k-v存儲系統,可用於服務註冊發現與共享配置,具備如下優勢:一、簡單 : 相比於晦澀難懂的paxos算法,etcd基於相對簡單且易實現的raft算法實現一致性,並經過gRPC提供接口調用;二、安全:支持TLS通訊,並能夠針對不一樣的用戶進行對key的讀寫控制;三、高性能:10,000 /秒的寫性能。其主要應用於服務註冊發現以及共享配置。node

一、 服務註冊與發現 

  • 服務啓動後向etcd註冊,並上報本身的監聽的端口以及當前的權重因子等信息,且對該信息設置ttl值。
  • 服務在ttl的時間內週期性上報權重因子等信息。
  • client端調用服務時向etcd獲取信息,進行調用,同時監聽該服務是否變化(經過watch方法實現)。
  • 當新增服務時watch方法監聽到變化,將服務加入代用列表,當服務掛掉時ttl失效,client端檢測到變化,將服務踢出調用列表,從而實現服務的動態擴展。
  • 另外一方面,client端經過每次變化獲取到的權重因子來進行client端的加權調用策略,從而保證後端服務的負載均衡。

二、共享配置

通常服務啓動時須要加載一些配置信息,如數據庫訪問地址,鏈接配置,這些配置信息每一個服務都差很少,若是經過讀取配置文件進行配置會存在要寫多份配置文件,且每次更改時這些配置文件都要更改,且更改配置後,須要重啓服務後才能生效,這些無疑讓配置極不靈活,若是將配置信息放入到etcd中,程序啓動時進行加載並運行,同時監聽配置文件的更改,當配置文件發生更改時,自動將舊值替換新值,這樣無疑簡化程序配置,更方便於服務部署。git

2、單機模式運行

默認在centos7的yum源裏已集成了etcd包,能夠經過yum進行安裝。也能夠去github上下載二進制包:https://github.com/coreos/etcd/tags,這裏我選擇的yum直接安裝的。啓用etcd服務命令以下:github

 

  1. # systemctl start etcd

進行以下測試etcd的可用性:算法

 

  1. [root@etcd1 ~]# etcdctl set site www.361way.com
  2. www.361way.com
  3. [root@etcd1 ~]# etcdctl get site
  4. www.361way.com

從上面能夠看到能夠進行k/v值的設置和獲取。不過單機模式通常不多使用。數據庫

3、集羣模式說明

這裏的集羣模式是指徹底集羣模式,固然也能夠在單機上經過不一樣的端口,部署僞集羣模式,只是那樣作只適合測試環境,生產環境考慮到可用性的話須要將etcd實例分佈到不一樣的主機上,這裏集羣搭建有三種方式,分佈是靜態配置,etcd發現,dns發現。默認配置運行etcd,監聽本地的2379端口,用於與client端交互,監聽2380用於etcd內部交互。etcd啓動時,集羣模式下會用到的參數以下:後端

 

  1. --name
  2. etcd集羣中的節點名,這裏能夠隨意,可區分且不重複就行
  3. --listen-peer-urls
  4. 監聽的用於節點之間通訊的url,可監聽多個,集羣內部將經過這些url進行數據交互(如選舉,數據同步等)
  5. --initial-advertise-peer-urls
  6. 建議用於節點之間通訊的url,節點間將以該值進行通訊。
  7. --listen-client-urls
  8. 監聽的用於客戶端通訊的url,一樣能夠監聽多個。
  9. --advertise-client-urls
  10. 建議使用的客戶端通訊url,該值用於etcd代理或etcd成員與etcd節點通訊。
  11. --initial-cluster-token etcd-cluster-1
  12. 節點的token值,設置該值後集羣將生成惟一id,併爲每一個節點也生成惟一id,當使用相同配置文件再啓動一個集羣時,只要該token值不同,etcd集羣就不會相互影響。
  13. --initial-cluster
  14. 也就是集羣中全部的initial-advertise-peer-urls 的合集
  15. --initial-cluster-state new
  16. 新建集羣的標誌,初始化狀態使用 new,創建以後改此值爲 existing

主機規劃以下:centos

name IP
etcd01 192.168.122.51
etcd02 192.168.122.52
etcd03 192.168.122.53

注意,這裏的name是etcd集羣裏使用的名字,不是主機名,固然和主機名一致也是不要緊的。安全

4、靜態模式

若是隻是測試,這裏建議使用二進制包進行測試。由於源碼包編譯的,使用etcd命令執行時加上的參數會被配置文件/etc/etcd/etcd.conf覆蓋。直接二進制包的不會,若是是現網使用yum包就比較推薦了。分別在三個節點上使用以下命令啓動:bash

 

  1. #節點1:
  2. ./etcd --name etcd01 --initial-advertise-peer-urls http://192.168.122.51:2380 \
  3. --listen-peer-urls http://0.0.0.0:2380 \
  4. --listen-client-urls http://0.0.0.0:2379 \
  5. --advertise-client-urls http://192.168.122.51:2379 \
  6. --initial-cluster-token etcd-cluster-1 \
  7. --initial-cluster etcd01=http://192.168.122.51:2380,etcd02=http://192.168.122.52:2380,etcd03=http://192.168.122.53:2380 \
  8. --initial-cluster-state new
  9. #節點2
  10. ./etcd --name etcd02 --initial-advertise-peer-urls http://192.168.122.52:2380 \
  11. --listen-peer-urls http://0.0.0.0:2380 \
  12. --listen-client-urls http://0.0.0.0:2379 \
  13. --advertise-client-urls http://192.168.122.52:2379 \
  14. --initial-cluster-token etcd-cluster-1 \
  15. --initial-cluster etcd01=http://192.168.122.51:2380,etcd02=http://192.168.122.52:2380,etcd03=http://192.168.122.53:2380 \
  16. --initial-cluster-state new
  17. #節點3
  18. ./etcd --name etcd03 --initial-advertise-peer-urls http://192.168.122.53:2380 \
  19. --listen-peer-urls http://0.0.0.0:2380 \
  20. --listen-client-urls http://0.0.0.0:2379 \
  21. --advertise-client-urls http://192.168.122.53:2379 \
  22. --initial-cluster-token etcd-cluster-1 \
  23. --initial-cluster etcd01=http://192.168.122.51:2380,etcd02=http://192.168.122.52:2380,etcd03=http://192.168.122.53:2380 \
  24. --initial-cluster-state new

啓動成功後,可使用以下命令查看(啓動集羣后,將會進入集羣選舉狀態。看到isLeader爲true爲就是選舉出的leader節點):負載均衡

 

  1. [root@etcd1 ~]# etcdctl member list
  2. 692a38059558446: name=etcd03 peerURLs=http://192.168.122.53:2380 clientURLs=http://192.168.122.53:2379 isLeader=true
  3. 1b1ca7d3774cbbb9: name=etcd02 peerURLs=http://192.168.122.52:2380 clientURLs=http://192.168.122.52:2379 isLeader=false
  4. cb75efb850ec8c2a: name=etcd01 peerURLs=http://192.168.122.51:2380 clientURLs=http://192.168.122.51:2379 isLeader=false
  5. [root@etcd1 ~]# etcdctl cluster-health
  6. member 692a38059558446 is healthy: got healthy result from http://192.168.122.53:2379
  7. member 1b1ca7d3774cbbb9 is healthy: got healthy result from http://192.168.122.52:2379
  8. member cb75efb850ec8c2a is healthy: got healthy result from http://192.168.122.51:2379

若是使用yum包安裝的,須要修改etcd.conf配置文件以下:

 

  1. [root@etcd1 ~]# grep -v ^# /etc/etcd/etcd.conf
  2. ETCD_NAME=etcd01
  3. ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
  4. ETCD_LISTEN_PEER_URLS="http://0.0.0.0:2380"
  5. ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379"
  6. ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.122.51:2380"
  7. ETCD_INITIAL_CLUSTER="etcd01=http://192.168.122.51:2380,etcd02=http://192.168.122.52:2380,etcd03=http://192.168.122.53:2380"
  8. ETCD_INITIAL_CLUSTER_STATE="existing"
  9. ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster2"
  10. ETCD_ADVERTISE_CLIENT_URLS="http://192.168.122.51:2379"

修改完成後,還須要修改/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. [Service]
  7. Type=notify
  8. WorkingDirectory=/var/lib/etcd/
  9. EnvironmentFile=-/etc/etcd/etcd.conf
  10. User=etcd
  11. # set GOMAXPROCS to number of processors
  12. ExecStart=/bin/bash -c "GOMAXPROCS=$(nproc) /usr/bin/etcd \
  13. --name=\"${ETCD_NAME}\" \
  14. --data-dir=\"${ETCD_DATA_DIR}\" \
  15. --listen-peer-urls=\"${ETCD_LISTEN_PEER_URLS}\" \
  16. --advertise-client-urls=\"${ETCD_ADVERTISE_CLIENT_URLS}\" \
  17. --initial-cluster-token=\"${ETCD_INITIAL_CLUSTER_TOKEN}\" \
  18. --initial-cluster=\"${ETCD_INITIAL_CLUSTER}\" \
  19. --initial-cluster-state=\"${ETCD_INITIAL_CLUSTER_STATE}\" \
  20. --listen-client-urls=\"${ETCD_LISTEN_CLIENT_URLS}\""
  21. Restart=on-failure
  22. LimitNOFILE=65536
  23. [Install]
  24. WantedBy=multi-user.target

配置過程當中可能遇到的問題有兩個。

一、本地鏈接報錯

內容以下:

 

  1. Error: client: etcd cluster is unavailable or misconfigured
  2. error #0: dial tcp 127.0.0.1:2379: getsockopt: connection refused
  3. error #1: dial tcp 127.0.0.1:4001: getsockopt: connection refused

若是出現如上的錯誤,是由於ETCD_LISTEN_CLIENT_URLS參數沒有配置http://127.0.0.1:2379而致使的,因此這裏我使用了0.0.0.0表明了監控全部地址。

二、context deadline exceeded

集羣啓動後,在經過etcdctl member list查看結果時,一直返回這個。後來發現和我使用的環境有關,由於測試不能上外網,這裏配置了一個squid代理,在環境變量裏有以下配置:

 

  1. http_proxy=10.212.52.25:3128
  2. https_proxy=10.212.52.25:3128
  3. ftp_proxy=10.212.52.25:3128
  4. export http_proxy https_proxy ftp_proxy

發現將這部分結果註釋,從新啓動etcd服務就行了。

5、動態配置

靜態配置前提是在搭建集羣以前已經提早知道各節點的信息,而實際應用中可能存在預先並不知道各節點ip的狀況,這時可經過已經搭建的etcd來輔助搭建新的etcd集羣。首先須要在已經搭建的etcd中建立用於發現的url,命令以下:

 

  1. [root@etcd1 ~]# curl -X PUT http://192.168.122.51:2379/v2/keys/discovery/6c007a14875d53d9bf0ef5a6fc0257c817f0fb83/_config/size -d value=3
  2. {"action":"set","node":{"key":"/discovery/6c007a14875d53d9bf0ef5a6fc0257c817f0fb83/_config/size","value":"3","modifiedIndex":19,"createdIndex":19}}

若是沒有搭建好的etcd集羣用於註冊和發現,可以使用etcd公有服務來進行服務註冊發現。公有etcd服務上建立用於發現的url爲:

 

  1. [root@etcd1 ~]# curl https://discovery.etcd.io/new?size=3
  2. https://discovery.etcd.io/a6a292c79fff6d25ef09243e3dfd2043

在三個節點上啓動的命令分別以下:

 

  1. #節點1
  2. ./etcd --name infra0 --initial-advertise-peer-urls http://192.168.122.51:2380 \
  3. --listen-peer-urls http://0.0.0.0:2380 \
  4. --listen-client-urls http://0.0.0.0:2379 \
  5. --advertise-client-urls http://192.168.122.51:2379 \
  6. --discovery http://192.168.122.51:2379/v2/keys/discovery/6c007a14875d53d9bf0ef5a6fc0257c817f0fb83
  7. #節點2
  8. ./etcd --name infra1 --initial-advertise-peer-urls http://10.0.1.109:2380 \
  9. --listen-peer-urls http://0.0.0.0:2380 \
  10. --listen-client-urls http://0.0.0.0:2379 \
  11. --advertise-client-urls http://10.0.1.109:2379 \
  12. --discovery http://192.168.122.51:2379/v2/keys/discovery/6c007a14875d53d9bf0ef5a6fc0257c817f0fb83
  13. #節點3
  14. ./etcd --name infra2 --initial-advertise-peer-urls http://10.0.1.110:2380 \
  15. --listen-peer-urls http://0.0.0.0:2380 \
  16. --listen-client-urls http://0.0.0.0:2379 \
  17. --advertise-client-urls http://10.0.1.110:2379 \
  18. --discovery http://192.168.122.51:2379/v2/keys/discovery/6c007a14875d53d9bf0ef5a6fc0257c817f0fb83

 

一樣,若是使用yum 包安裝的,須要修改etcd.conf文件和etcd.service文件。

6、DNS發現

這個配置方法我沒測試過,這裏直接拿官方給的樣例列出下。

 

dns 發現主要經過dns服務來記錄集羣中各節點的域名信息,各節點到dns服務中獲取相互的地址信息,從而創建集羣。etcd各節點經過--discovery-serv配置來獲取域名信息,節點間將獲取如下域名下的各節點域名,
  • _etcd-server-ssl._tcp.example.com
  • _etcd-server._tcp.example.com
若是_etcd-server-ssl._tcp.example.com下有值表示節點間將使用ssl協議,相反則使用非ssl。
  • _etcd-client._tcp.example.com
  • _etcd-client-ssl._tcp.example.com

另外一方面,client端將獲取以上域名下的節點域名,用於client端與etcd通訊,ssl與非ssl的使用取決於以上那個域名下有值。

 

建立dns記錄

  1. $ dig +noall +answer SRV _etcd-server._tcp.example.com
  2. _etcd-server._tcp.example.com. 300 IN SRV 0 0 2380 infra0.example.com.
  3. _etcd-server._tcp.example.com. 300 IN SRV 0 0 2380 infra1.example.com.
  4. _etcd-server._tcp.example.com. 300 IN SRV 0 0 2380 infra2.example.com.
  5. $ dig +noall +answer SRV _etcd-client._tcp.example.com
  6. _etcd-client._tcp.example.com. 300 IN SRV 0 0 2379 infra0.example.com.
  7. _etcd-client._tcp.example.com. 300 IN SRV 0 0 2379 infra1.example.com.
  8. _etcd-client._tcp.example.com. 300 IN SRV 0 0 2379 infra2.example.com.
  9. $ dig +noall +answer infra0.example.com infra1.example.com infra2.example.com
  10. infra0.example.com. 300 IN A 10.0.1.111
  11. infra1.example.com. 300 IN A 10.0.1.109
  12. infra2.example.com. 300 IN A 10.0.1.110

而後啓動各個節點

  1. $ etcd --name infra0 \
  2. --discovery-srv example.com \
  3. --initial-advertise-peer-urls http://infra0.example.com:2380 \
  4. --initial-cluster-token etcd-cluster-1 \
  5. --initial-cluster-state new \
  6. --advertise-client-urls http://infra0.example.com:2379 \
  7. --listen-client-urls http://infra0.example.com:2379 \
  8. --listen-peer-urls http://infra0.example.com:2380
  9. $ etcd --name infra1 \
  10. --discovery-srv example.com \
  11. --initial-advertise-peer-urls http://infra1.example.com:2380 \
  12. --initial-cluster-token etcd-cluster-1 \
  13. --initial-cluster-state new \
  14. --advertise-client-urls http://infra1.example.com:2379 \
  15. --listen-client-urls http://infra1.example.com:2379 \
  16. --listen-peer-urls http://infra1.example.com:2380
  17. $ etcd --name infra2 \
  18. --discovery-srv example.com \
  19. --initial-advertise-peer-urls http://infra2.example.com:2380 \
  20. --initial-cluster-token etcd-cluster-1 \
  21. --initial-cluster-state new \
  22. --advertise-client-urls http://infra2.example.com:2379 \
  23. --listen-client-urls http://infra2.example.com:2379 \
  24. --listen-peer-urls http://infra2.example.com:2380

參考頁面:

github etcd集羣配置頁

相關文章
相關標籤/搜索