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
- # systemctl start etcd
進行以下測試etcd的可用性:算法
- [root@etcd1 ~]# etcdctl set site www.361way.com
- www.361way.com
- [root@etcd1 ~]# etcdctl get site
- www.361way.com
從上面能夠看到能夠進行k/v值的設置和獲取。不過單機模式通常不多使用。數據庫
3、集羣模式說明
這裏的集羣模式是指徹底集羣模式,固然也能夠在單機上經過不一樣的端口,部署僞集羣模式,只是那樣作只適合測試環境,生產環境考慮到可用性的話須要將etcd實例分佈到不一樣的主機上,這裏集羣搭建有三種方式,分佈是靜態配置,etcd發現,dns發現。默認配置運行etcd,監聽本地的2379端口,用於與client端交互,監聽2380用於etcd內部交互。etcd啓動時,集羣模式下會用到的參數以下:後端
- --name
- etcd集羣中的節點名,這裏能夠隨意,可區分且不重複就行
- --listen-peer-urls
- 監聽的用於節點之間通訊的url,可監聽多個,集羣內部將經過這些url進行數據交互(如選舉,數據同步等)
- --initial-advertise-peer-urls
- 建議用於節點之間通訊的url,節點間將以該值進行通訊。
- --listen-client-urls
- 監聽的用於客戶端通訊的url,一樣能夠監聽多個。
- --advertise-client-urls
- 建議使用的客戶端通訊url,該值用於etcd代理或etcd成員與etcd節點通訊。
- --initial-cluster-token etcd-cluster-1
- 節點的token值,設置該值後集羣將生成惟一id,併爲每一個節點也生成惟一id,當使用相同配置文件再啓動一個集羣時,只要該token值不同,etcd集羣就不會相互影響。
- --initial-cluster
- 也就是集羣中全部的initial-advertise-peer-urls 的合集
- --initial-cluster-state new
- 新建集羣的標誌,初始化狀態使用 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:
- ./etcd --name etcd01 --initial-advertise-peer-urls http://192.168.122.51:2380 \
- --listen-peer-urls http://0.0.0.0:2380 \
- --listen-client-urls http://0.0.0.0:2379 \
- --advertise-client-urls http://192.168.122.51:2379 \
- --initial-cluster-token etcd-cluster-1 \
- --initial-cluster etcd01=http://192.168.122.51:2380,etcd02=http://192.168.122.52:2380,etcd03=http://192.168.122.53:2380 \
- --initial-cluster-state new
- #節點2
- ./etcd --name etcd02 --initial-advertise-peer-urls http://192.168.122.52:2380 \
- --listen-peer-urls http://0.0.0.0:2380 \
- --listen-client-urls http://0.0.0.0:2379 \
- --advertise-client-urls http://192.168.122.52:2379 \
- --initial-cluster-token etcd-cluster-1 \
- --initial-cluster etcd01=http://192.168.122.51:2380,etcd02=http://192.168.122.52:2380,etcd03=http://192.168.122.53:2380 \
- --initial-cluster-state new
- #節點3
- ./etcd --name etcd03 --initial-advertise-peer-urls http://192.168.122.53:2380 \
- --listen-peer-urls http://0.0.0.0:2380 \
- --listen-client-urls http://0.0.0.0:2379 \
- --advertise-client-urls http://192.168.122.53:2379 \
- --initial-cluster-token etcd-cluster-1 \
- --initial-cluster etcd01=http://192.168.122.51:2380,etcd02=http://192.168.122.52:2380,etcd03=http://192.168.122.53:2380 \
- --initial-cluster-state new
啓動成功後,可使用以下命令查看(啓動集羣后,將會進入集羣選舉狀態。看到isLeader爲true爲就是選舉出的leader節點):負載均衡
- [root@etcd1 ~]# etcdctl member list
- 692a38059558446: name=etcd03 peerURLs=http://192.168.122.53:2380 clientURLs=http://192.168.122.53:2379 isLeader=true
- 1b1ca7d3774cbbb9: name=etcd02 peerURLs=http://192.168.122.52:2380 clientURLs=http://192.168.122.52:2379 isLeader=false
- cb75efb850ec8c2a: name=etcd01 peerURLs=http://192.168.122.51:2380 clientURLs=http://192.168.122.51:2379 isLeader=false
- [root@etcd1 ~]# etcdctl cluster-health
- member 692a38059558446 is healthy: got healthy result from http://192.168.122.53:2379
- member 1b1ca7d3774cbbb9 is healthy: got healthy result from http://192.168.122.52:2379
- member cb75efb850ec8c2a is healthy: got healthy result from http://192.168.122.51:2379
若是使用yum包安裝的,須要修改etcd.conf配置文件以下:
- [root@etcd1 ~]# grep -v ^# /etc/etcd/etcd.conf
- ETCD_NAME=etcd01
- ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
- ETCD_LISTEN_PEER_URLS="http://0.0.0.0:2380"
- ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379"
- ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.122.51:2380"
- ETCD_INITIAL_CLUSTER="etcd01=http://192.168.122.51:2380,etcd02=http://192.168.122.52:2380,etcd03=http://192.168.122.53:2380"
- ETCD_INITIAL_CLUSTER_STATE="existing"
- ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster2"
- ETCD_ADVERTISE_CLIENT_URLS="http://192.168.122.51:2379"
修改完成後,還須要修改/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=/var/lib/etcd/
- EnvironmentFile=-/etc/etcd/etcd.conf
- User=etcd
- # set GOMAXPROCS to number of processors
- ExecStart=/bin/bash -c "GOMAXPROCS=$(nproc) /usr/bin/etcd \
- --name=\"${ETCD_NAME}\" \
- --data-dir=\"${ETCD_DATA_DIR}\" \
- --listen-peer-urls=\"${ETCD_LISTEN_PEER_URLS}\" \
- --advertise-client-urls=\"${ETCD_ADVERTISE_CLIENT_URLS}\" \
- --initial-cluster-token=\"${ETCD_INITIAL_CLUSTER_TOKEN}\" \
- --initial-cluster=\"${ETCD_INITIAL_CLUSTER}\" \
- --initial-cluster-state=\"${ETCD_INITIAL_CLUSTER_STATE}\" \
- --listen-client-urls=\"${ETCD_LISTEN_CLIENT_URLS}\""
- Restart=on-failure
- LimitNOFILE=65536
- [Install]
- WantedBy=multi-user.target
配置過程當中可能遇到的問題有兩個。
一、本地鏈接報錯
內容以下:
- Error: client: etcd cluster is unavailable or misconfigured
- error #0: dial tcp 127.0.0.1:2379: getsockopt: connection refused
- 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代理,在環境變量裏有以下配置:
- http_proxy=10.212.52.25:3128
- https_proxy=10.212.52.25:3128
- ftp_proxy=10.212.52.25:3128
- export http_proxy https_proxy ftp_proxy
發現將這部分結果註釋,從新啓動etcd服務就行了。
5、動態配置
靜態配置前提是在搭建集羣以前已經提早知道各節點的信息,而實際應用中可能存在預先並不知道各節點ip的狀況,這時可經過已經搭建的etcd來輔助搭建新的etcd集羣。首先須要在已經搭建的etcd中建立用於發現的url,命令以下:
- [root@etcd1 ~]# curl -X PUT http://192.168.122.51:2379/v2/keys/discovery/6c007a14875d53d9bf0ef5a6fc0257c817f0fb83/_config/size -d value=3
- {"action":"set","node":{"key":"/discovery/6c007a14875d53d9bf0ef5a6fc0257c817f0fb83/_config/size","value":"3","modifiedIndex":19,"createdIndex":19}}
若是沒有搭建好的etcd集羣用於註冊和發現,可以使用etcd公有服務來進行服務註冊發現。公有etcd服務上建立用於發現的url爲:
- [root@etcd1 ~]# curl https://discovery.etcd.io/new?size=3
- https://discovery.etcd.io/a6a292c79fff6d25ef09243e3dfd2043
在三個節點上啓動的命令分別以下:
- #節點1
- ./etcd --name infra0 --initial-advertise-peer-urls http://192.168.122.51:2380 \
- --listen-peer-urls http://0.0.0.0:2380 \
- --listen-client-urls http://0.0.0.0:2379 \
- --advertise-client-urls http://192.168.122.51:2379 \
- --discovery http://192.168.122.51:2379/v2/keys/discovery/6c007a14875d53d9bf0ef5a6fc0257c817f0fb83
- #節點2
- ./etcd --name infra1 --initial-advertise-peer-urls http://10.0.1.109:2380 \
- --listen-peer-urls http://0.0.0.0:2380 \
- --listen-client-urls http://0.0.0.0:2379 \
- --advertise-client-urls http://10.0.1.109:2379 \
- --discovery http://192.168.122.51:2379/v2/keys/discovery/6c007a14875d53d9bf0ef5a6fc0257c817f0fb83
- #節點3
- ./etcd --name infra2 --initial-advertise-peer-urls http://10.0.1.110:2380 \
- --listen-peer-urls http://0.0.0.0:2380 \
- --listen-client-urls http://0.0.0.0:2379 \
- --advertise-client-urls http://10.0.1.110:2379 \
- --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記錄
- $ dig +noall +answer SRV _etcd-server._tcp.example.com
- _etcd-server._tcp.example.com. 300 IN SRV 0 0 2380 infra0.example.com.
- _etcd-server._tcp.example.com. 300 IN SRV 0 0 2380 infra1.example.com.
- _etcd-server._tcp.example.com. 300 IN SRV 0 0 2380 infra2.example.com.
- $ dig +noall +answer SRV _etcd-client._tcp.example.com
- _etcd-client._tcp.example.com. 300 IN SRV 0 0 2379 infra0.example.com.
- _etcd-client._tcp.example.com. 300 IN SRV 0 0 2379 infra1.example.com.
- _etcd-client._tcp.example.com. 300 IN SRV 0 0 2379 infra2.example.com.
- $ dig +noall +answer infra0.example.com infra1.example.com infra2.example.com
- infra0.example.com. 300 IN A 10.0.1.111
- infra1.example.com. 300 IN A 10.0.1.109
- infra2.example.com. 300 IN A 10.0.1.110
而後啓動各個節點
- $ etcd --name infra0 \
- --discovery-srv example.com \
- --initial-advertise-peer-urls http://infra0.example.com:2380 \
- --initial-cluster-token etcd-cluster-1 \
- --initial-cluster-state new \
- --advertise-client-urls http://infra0.example.com:2379 \
- --listen-client-urls http://infra0.example.com:2379 \
- --listen-peer-urls http://infra0.example.com:2380
- $ etcd --name infra1 \
- --discovery-srv example.com \
- --initial-advertise-peer-urls http://infra1.example.com:2380 \
- --initial-cluster-token etcd-cluster-1 \
- --initial-cluster-state new \
- --advertise-client-urls http://infra1.example.com:2379 \
- --listen-client-urls http://infra1.example.com:2379 \
- --listen-peer-urls http://infra1.example.com:2380
- $ etcd --name infra2 \
- --discovery-srv example.com \
- --initial-advertise-peer-urls http://infra2.example.com:2380 \
- --initial-cluster-token etcd-cluster-1 \
- --initial-cluster-state new \
- --advertise-client-urls http://infra2.example.com:2379 \
- --listen-client-urls http://infra2.example.com:2379 \
- --listen-peer-urls http://infra2.example.com:2380
參考頁面:
github etcd集羣配置頁