1、etcd介紹:java
ETCD 是一個高可用的分佈式鍵值數據庫,可用於服務發現。ETCD 採用 raft 一致性算法,基於 Go 語言實現。etcd做爲一個高可用鍵值存儲系統,天生就是爲集羣化而設計的。因爲Raft算法在作決策時須要多數節點的投票,因此etcd通常部署集羣推薦奇數個節點,推薦的數量爲三、5或者7個節點構成一個集羣。node
實際上,etcd做爲一個受到Zookeeper與doozer啓發而催生的項目,除了擁有與之相似的功能外,更具備如下4個特色{![引自Docker官方文檔]}。linux
3、etcd關鍵詞彙git
4、etcd應用場景:github
一、服務發現:算法
服務發現(Service Discovery)要解決的是分佈式系統中最多見的問題之一,即在同一個分佈式集羣中的進程或服務如何才能找到對方並創建鏈接。從本質上說,服務發現就是想要了解集羣中是否有進程在監聽udp或tcp端口,而且經過名字就能夠進行查找和鏈接。數據庫
二、消息發佈與訂閱:安全
在分佈式系統中,最爲適用的組件間通訊方式是消息發佈與訂閱機制。具體而言,即構建一個配置共享中心,數據提供者在這個配置中心發佈消息,而消息使用者則訂閱他們關心的主題,一旦相關主題有消息發佈,就會實時通知訂閱者。經過這種方式能夠實現分佈式系統配置的集中式管理與實時動態更新服務器
key TTL
功能能夠確保機器狀態是實時更新的。三、負載均衡:架構
在分佈式系統中,爲了保證服務的高可用以及數據的一致性,一般都會把數據和服務部署多份,以此達到對等服務,即便其中的某一個服務失效了,也不影響使用。這樣的實現雖然會致使必定程度上數據寫入性能的降低,可是卻能實現數據訪問時的負載均衡。由於每一個對等服務節點上都存有完整的數據,因此用戶的訪問流量就能夠分流到不一樣的機器上。
四、分佈式通知與協調:
這裏討論的分佈式通知與協調,與消息發佈和訂閱有些類似。二者都使用了etcd中的Watcher機制,經過註冊與異步通知機制,實現分佈式環境下不一樣系統之間的通知與協調,從而對數據變動進行實時處理。實現方式一般爲:不一樣系統都在etcd上對同一個目錄進行註冊,同時設置Watcher監控該目錄的變化(若是對子目錄的變化也有須要,能夠設置成遞歸模式),當某個系統更新了etcd的目錄,那麼設置了Watcher的系統就會收到通知,並做出相應處理。
五、分佈式鎖:
由於etcd使用Raft算法保持了數據的強一致性,某次操做存儲到集羣中的值必然是全局一致的,因此很容易實現分佈式鎖。鎖服務有兩種使用方式,一是保持獨佔,二是控制時序。
CompareAndSwap
)的API。經過設置prevExist
值,能夠保證在多個節點同時建立某個目錄時,只有一個成功,而該用戶便可認爲是得到了鎖。POST
動做,這樣etcd會自動在目錄下生成一個當前最大的值爲鍵,存儲這個新的值(客戶端編號)。同時還可使用API按順序列出全部當前目錄下的鍵值。此時這些鍵的值就是客戶端的時序,而這些鍵中存儲的值能夠是表明客戶端的編號。5、搭建集羣
主機信息:
角色 | 系統 | 節點 |
master | CentOS-7 | 192.168.10.5 |
node-1 | CentOS-7 | 192.168.10.6 |
node-2 | CentOS-7 | 192.168.10.7 |
配置阿里epel源:
mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
安裝配置存儲etcd(高可用):
一、在三臺安裝etcd(集羣)、作高可用
[root@node1 ~]# yum -y install etcd [root@node2 ~]# yum -y install etcd [root@master ~]# yum -y install etcd [root@master ~]# etcd --version etcd Version: 3.2.22 Git SHA: 1674e68 Go Version: go1.9.4 Go OS/Arch: linux/amd64
2 配置文件修改
master節點
[root@master ~]# egrep -v "^$|^#" /etc/etcd/etcd.conf
ETCD_DATA_DIR="/var/lib/etcd/default.etcd" #etcd數據保存目錄 ETCD_LISTEN_PEER_URLS="http://192.168.10.5:2380" #集羣內部通訊使用的URL ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379" #供外部客戶端使用的url ETCD_NAME="etcd01" #etcd實例名稱 ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.10.5:2380" #廣播給集羣內其餘成員訪問的URL ETCD_ADVERTISE_CLIENT_URLS="http://0.0.0.0:2379" #廣播給外部客戶端使用的url ETCD_INITIAL_CLUSTER="etcd01=http://192.168.10.5:2380,etcd02=http://192.168.10.6:2380,etcd03=http://192.168.10.7:2380" #初始集羣成員列表 ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster" #集羣的名稱 ETCD_INITIAL_CLUSTER_STATE="new" #初始集羣狀態,new爲新建集羣
node-1
[root@node1 etcd]# egrep -v "^$|^#" /etc/etcd/etcd.conf ETCD_DATA_DIR="/var/lib/etcd/default.etcd" ETCD_LISTEN_PEER_URLS="http://192.168.10.6:2380" ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379" ETCD_NAME="etcd02" ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.10.6:2380" ETCD_ADVERTISE_CLIENT_URLS="http://0.0.0.0:2379" ETCD_INITIAL_CLUSTER="etcd01=http://192.168.10.5:2380,etcd02=http://192.168.10.6:2380,etcd03=http://192.168.10.7:2380" ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster" ETCD_INITIAL_CLUSTER_STATE="exist"
node-2
[root@node2 ~]# egrep -v "^$|^#" /etc/etcd/etcd.conf ETCD_DATA_DIR="/var/lib/etcd/default.etcd" ETCD_LISTEN_PEER_URLS="http://192.168.10.7:2380" ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379" ETCD_NAME="etcd03" ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.10.7:2380" ETCD_ADVERTISE_CLIENT_URLS="http://0.0.0.0:2379" ETCD_INITIAL_CLUSTER="etcd01=http://192.168.10.5:2380,etcd02=http://192.168.10.6:2380,etcd03=http://192.168.10.7:2380" ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster" ETCD_INITIAL_CLUSTER_STATE="exist"
分別進行啓動:
[root@master etcd]# systemctl start etcd [root@master etcd]# systemctl status etcd ● etcd.service - Etcd Server Loaded: loaded (/usr/lib/systemd/system/etcd.service; disabled; vendor preset: disabled) Active: active (running) since Tue 2019-01-08 17:28:34 CST; 2h 32min ago Main PID: 114077 (etcd) Tasks: 7 Memory: 48.7M CGroup: /system.slice/etcd.service └─114077 /usr/bin/etcd --name=etcd01 --data-dir=/var/lib/etcd/default.etcd --listen-client-urls=http://0.0.0.0:2379 Jan 08 20:00:08 master etcd[114077]: 4c5d727d37966a87 became candidate at term 16 Jan 08 20:00:08 master etcd[114077]: 4c5d727d37966a87 received MsgVoteResp from 4c5d727d37966a87 at term 16 Jan 08 20:00:08 master etcd[114077]: 4c5d727d37966a87 [logterm: 11, index: 44] sent MsgVote request to 315fd62e577c4037 at term 16 Jan 08 20:00:08 master etcd[114077]: 4c5d727d37966a87 [logterm: 11, index: 44] sent MsgVote request to f617da66fb9b90ad at term 16 Jan 08 20:00:08 master etcd[114077]: 4c5d727d37966a87 received MsgVoteResp from f617da66fb9b90ad at term 16 Jan 08 20:00:08 master etcd[114077]: 4c5d727d37966a87 [quorum:2] has received 2 MsgVoteResp votes and 0 vote rejections Jan 08 20:00:08 master etcd[114077]: 4c5d727d37966a87 became leader at term 16 Jan 08 20:00:08 master etcd[114077]: raft.node: 4c5d727d37966a87 elected leader 4c5d727d37966a87 at term 16 Jan 08 20:00:09 master etcd[114077]: the clock difference against peer 315fd62e577c4037 is too high [4.285950016s > 1s] Jan 08 20:00:09 master etcd[114077]: the clock difference against peer f617da66fb9b90ad is too high [4.120954945s > 1s]
[root@master etcd]# netstat -tunlp|grep etcd tcp 0 0 192.168.10.5:2380 0.0.0.0:* LISTEN 114077/etcd tcp6 0 0 :::2379 :::* LISTEN 114077/etcd
三、etc集羣測試
集羣數據操做命令:
查看集羣節點
[root@node1 etcd]# etcdctl member list 315fd62e577c4037: name=etcd03 peerURLs=http://192.168.10.7:2380 clientURLs=http://0.0.0.0:2379 isLeader=false 4c5d727d37966a87: name=etcd01 peerURLs=http://192.168.10.5:2380 clientURLs=http://0.0.0.0:2379 isLeader=true f617da66fb9b90ad: name=etcd02 peerURLs=http://192.168.10.6:2380 clientURLs=http://0.0.0.0:2379 isLeader=false
查看集羣健康狀態:
[root@master etcd]# etcdctl cluster-health
member 315fd62e577c4037 is healthy: got healthy result from http://0.0.0.0:2379
member 4c5d727d37966a87 is healthy: got healthy result from http://0.0.0.0:2379
member f617da66fb9b90ad is healthy: got healthy result from http://0.0.0.0:2379
cluster is healthy
設置鍵值:
在一個節點設置值
[root@master etcd]# etcdctl set /test/key "test kubernetes" test kubernetes
在另外一個節點獲取值
[root@node1 etcd]# etcdctl get /test/key
test kubernetes
/路徑/key
dir
和key
不存在,該命令會建立對應的項更新鍵值:
[root@master etcd]# etcdctl update /test/key "test kubernetes cluster" test kubernetes cluster
[root@node1 etcd]# etcdctl get /test/key test kubernetes cluster
刪除鍵值:
[root@master etcd]# etcdctl rm /test/key PrevNode.Value: test kubernetes cluster
當鍵不存在時,會報錯 [root@node1 etcd]# etcdctl get /test/key Error: 100: Key not found (/test/key) [15]
etcd幫助:
[root@master etcd]# etcdctl help NAME: etcdctl - A simple command line client for etcd. WARNING: Environment variable ETCDCTL_API is not set; defaults to etcdctl v2. Set environment variable ETCDCTL_API=3 to use v3 API or ETCDCTL_API=2 to use v2 API. USAGE: etcdctl [global options] command [command options] [arguments...] VERSION: 3.2.22 COMMANDS: backup backup an etcd directory cluster-health check the health of the etcd cluster mk make a new key with a given value mkdir make a new directory rm remove a key or a directory rmdir removes the key if it is an empty directory or a key-value pair get retrieve the value of a key ls retrieve a directory set set the value of a key setdir create a new directory or update an existing directory TTL update update an existing key with a given value updatedir update an existing directory watch watch a key for changes exec-watch watch a key for changes and exec an executable member member add, remove and list subcommands user user add, grant and revoke subcommands role role add, grant and revoke subcommands auth overall auth controls help, h Shows a list of commands or help for one command
參考文檔:
https://github.com/etcd-io/etcd
https://yq.aliyun.com/articles/623228
https://blog.csdn.net/hxpjava1/article/details/78275995