操bb国片四区三区,亚洲一片内射无码,91短视频福利导航,蜜桃臀成人免费在线视频观看

快速搭建高可用 ETCD 集群

2021-11-03 15:06:23 shuai.chang

睿智創(chuàng)新RAIZ,一體化IT服務提供商

本文介紹了etcd集群的工作原理和高可用技術細節(jié)。

etcd 簡介

coreos 開發(fā)的分布式服務系統(tǒng),內部采用 raft 協(xié)議作為一致性算法。作為服務發(fā)現(xiàn)系統(tǒng),有以下的特點:

  • 簡單:安裝配置簡單,而且提供了 HTTP API 進行交互,使用也很簡單

  • 安全:支持 SSL 證書驗證

  • 快速:根據(jù)官方提供的 benchmark 數(shù)據(jù),單實例支持每秒 2k+ 讀操作

  • 可靠:采用 raft 算法,實現(xiàn)分布式系統(tǒng)數(shù)據(jù)的可用性和一致性

etcd 目前默認使用 2379 端口提供 HTTP API 服務,2380 端口和 peers(集群內其他節(jié)點)通信

雖然 etcd 也支持單點部署,但是在生產(chǎn)環(huán)境中推薦集群方式部署,一般 etcd 節(jié)點數(shù)會選擇 3、5、7。etcd 會保證所有的節(jié)點都會保存數(shù)據(jù),并保證數(shù)據(jù)的一致性和正確性。

工作原理

每個 etcd cluster 都是有若干個 member 組成的,每個 member 是一個獨立運行的 etcd 實例,單臺機器上可以運行多個 member。

在正常運行的狀態(tài)下,集群中會有一個 leader,其余的 member 都是 followers。leader 向 followers 同步日志,保證數(shù)據(jù)在各個 member 都有副本。leader 還會定時向所有的 member 發(fā)送心跳報文,如果在規(guī)定的時間里 follower 沒有收到心跳,就會重新進行選舉。

客戶端所有的請求都會先發(fā)送給 leader,leader 向所有的 followers 同步日志,等收到超過半數(shù)的確認后就把該日志存儲到磁盤,并返回響應客戶端。

每個 etcd 服務有三大主要部分組成:raft 實現(xiàn)、WAL 日志存儲、數(shù)據(jù)的存儲和索引。WAL 會在本地磁盤(就是之前提到的 --data-dir)上存儲日志內容(wal file)和快照(snapshot)。

集群規(guī)劃

nameIPHOSTNAME
etcd01192.168.255.131master1
etcd02192.168.255.132master2
etcd03192.168.255.133master3

安裝

在安裝和啟動 etcd 服務的時候,各個節(jié)點需要知道集群中其他節(jié)點的信息(一般是 ip 和 port 信息)。根據(jù)你是否可以提前知道每個節(jié)點的 ip,有幾種不同的啟動方案:

  • 靜態(tài)配置:在啟動 etcd server 的時候,通過 --initial-cluster 參數(shù)配置好所有的節(jié)點信息

  • etcd動態(tài)發(fā)現(xiàn):通過已有的etcd集群作為數(shù)據(jù)交互點,然后在擴展新的集群時實現(xiàn)通過已有集群進行服務發(fā)現(xiàn)的機制。比如官方提供的:discovery.etcd.io

  • DNS動態(tài)發(fā)現(xiàn): 通過DNS查詢方式獲取其他節(jié)點地址信息。

一般生產(chǎn)環(huán)境都是使用靜態(tài)配置,我們這里也采用這種方法。并配置 SSL來保證通信安全。

1、安裝cfssl

wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64  
wget https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64 
 
chmod +x cfssl_linux-amd64 cfssljson_linux-amd64 
 
mv cfssl_linux-amd64 /usr/local/bin/cfssl 
mv cfssljson_linux-amd64 /usr/local/bin/cfssljson 

2、創(chuàng)建ca證書,客戶端,服務端,節(jié)點之間的證書

Etcd屬于server ,etcdctl 屬于client,二者之間通過http協(xié)議進行通信。

  • ca證書 自己給自己簽名的權威證書,用來給其他證書簽名

  • server證書 etcd的證書

  • client證書 客戶端,比如etcdctl的證書

  • peer證書 節(jié)點與節(jié)點之間通信的證書

1) 創(chuàng)建目錄

mkdir -p /etc/etcd/pki  
cd /etc/etcd/pki 
cfssl print-defaults config > ca-config.json 
cfssl print-defaults csr > ca-csr.json 

2) 創(chuàng)建ca證書

修改ca-config.json

server auth表示client可以用該ca對server提供的證書進行驗證

client auth表示server可以用該ca對client提供的證書進行驗證


    "signing": { 
        "default": { 
            "expiry": "43800h" 
        }, 
        "profiles": { 
            "server": { 
                "expiry": "43800h"
                "usages": [ 
                    "signing"
                    "key encipherment"
                    "server auth"
                    "client auth" 
                ] 
            }, 
            "client": { 
                "expiry": "43800h"
                "usages": [ 
                    "signing"
                    "key encipherment"
                    "client auth" 
                ] 
            }, 
            "peer": { 
                "expiry": "43800h"
                "usages": [ 
                    "signing"
                    "key encipherment"
                    "server auth"
                    "client auth" 
                ] 
            } 
        } 
    } 

創(chuàng)建證書簽名請求ca-csr.json


    "CN": "etcd"
    "key": { 
        "algo": "rsa"
        "size": 2048 
    } 

生成CA證書和私鑰

# cfssl gencert -initca ca-csr.json | cfssljson -bare ca  
# ls ca* 
ca-config.json ca.csr ca-csr.json ca-key.pem ca.pem 

3) 生成客戶端證書

vim client.json


    "CN": "client"
    "key": { 
        "algo": "ecdsa"
        "size": 256 
    } 

生成

cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=client client.json  | cfssljson -bare client -  
# ls ca* 
ca-config.json ca.csr ca-csr.json ca-key.pem ca.pem client-key.pem client.pem 

4) 生成server,peer證書

vim etcd.json


    "CN": "etcd"
    "hosts": [ 
        "127.0.0.1"
        "192.168.255.131"
        "192.168.255.132"
        "192.168.255.133" 
    ], 
    "key": { 
        "algo": "ecdsa"
        "size": 256 
    }, 
    "names": [ 
        { 
            "C": "CN"
            "L": "SH"
            "ST": "SH" 
        } 
    ] 

cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=server etcd.json | cfssljson -bare server  
 
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=peer etcd.json | cfssljson -bare peer 

3、安裝etcd二進制文件

wget https://github.com/coreos/etcd/releases/download/v3.1.5/etcd-v3.1.5-linux-amd64.tar.gz  
tar -xvf etcd-v3.1.5-linux-amd64.tar.gz 
mv etcd-v3.1.5-linux-amd64/etcd* /usr/local/bin 

4、service配置文件

vim /usr/lib/systemd/system/etcd.service, 三臺機器配置不一樣,需要替換為相應的IP和name。


[Unit] 
Description=Etcd Server 
After=network.target 
After=network-online.target 
Wants=network-online.target 
Documentation=https://github.com/coreos 
 
[Service] 
Type=notify 
WorkingDirectory=/var/lib/etcd 
ExecStart=/usr/local/bin/etcd \ 
--data-dir=/var/lib/etcd \ 
--name=master1 \ 
--cert-file=/etc/etcd/pki/server.pem \ 
--key-file=/etc/etcd/pki/server-key.pem \ 
--trusted-ca-file=/etc/etcd/pki/ca.pem \ 
--peer-cert-file=/etc/etcd/pki/peer.pem \ 
--peer-key-file=/etc/etcd/pki/peer-key.pem \ 
--peer-trusted-ca-file=/etc/etcd/pki/ca.pem \ 
--listen-peer-urls=https://192.168.255.131:2380 \ 
--initial-advertise-peer-urls=https://192.168.255.131:2380 \ 
--listen-client-urls=https://192.168.255.131:2379,http://127.0.0.1:2379 \ 
--advertise-client-urls=https://192.168.255.131:2379 \ 
--initial-cluster-token=etcd-cluster-0 \ 
--initial-cluster=master1=https://192.168.255.131:2380,master2=https://192.168.255.132:2380,master3=https://192.168.255.133:2380 \ 
--initial-cluster-state=new \ 
--heartbeat-interval=250 \ 
--election-timeout=2000 
Restart=on-failure 
RestartSec=5 
LimitNOFILE=65536 
 
[Install] 
WantedBy=multi-user.target 

配置參數(shù)的含義

--name:方便理解的節(jié)點名稱,默認為 default,在集群中應該保持唯一,可以使用 hostname  
--data-dir:服務運行數(shù)據(jù)保存的路徑,默認為 ${name}.etcd 
 
--snapshot-count:指定有多少事務(transaction)被提交時,觸發(fā)截取快照保存到磁盤 
--heartbeat-interval:leader 多久發(fā)送一次心跳到 followers。默認值是 100ms 
--eletion-timeout:重新投票的超時時間,如果 follow 在該時間間隔沒有收到心跳包,會觸發(fā)重新投票,默認為 1000 ms 
 
--listen-peer-urls:和集群內其他節(jié)點通信的地址, http://ip:2380,如果有多個,使用逗號分隔。需要所有節(jié)點都能夠訪問,所以不要使用 localhost! 
--listen-client-urls:節(jié)點與客戶端通信的地址,比如 http://ip:2379,http://127.0.0.1:2379,客戶端會連接到這里和 etcd 交互 
--advertise-client-urls:對外通告的該節(jié)點客戶端監(jiān)聽地址,http://ip:2379,這個值會通知集群中其他節(jié)點 
 
--initial-advertise-peer-urls:節(jié)點與其他節(jié)點通信的地址,會通告給集群的其他成員。這個地址用來傳輸集群數(shù)據(jù)。因此這個地址必須是可以被集群中所有的成員訪問http://ip:2380 
 
--initial-cluster:集群中所有節(jié)點的信息,格式為 node1=http://ip1:2380,node2=http://ip2:2380,…。注意:這里的 node1 是節(jié)點的 --name 指定的名字;后面的 ip1:2380 是 --initial-advertise-peer-urls 指定的值 
--initial-cluster-state:新建集群的時候,這個值為 new;假如已經(jīng)存在的集群,這個值為 existing 
--initial-cluster-token:創(chuàng)建集群的 token,這個值每個集群保持唯一。這樣的話,如果你要重新創(chuàng)建集群,即使配置和之前一樣,也會再次生成新的集群和節(jié)點 uuid;否則會導致多個集群之間的沖突,造成未知的錯誤 
 
所有以--initial 開頭的配置都是在 bootstrap(引導) 集群的時候才會用到,后續(xù)節(jié)點重啟時會被忽略。 

5、創(chuàng)建存放etcd數(shù)據(jù)的目錄,啟動 etcd


mkdir /var/lib/etcd 
 
systemctl daemon-reload && systemctl enable etcd && systemctl start etcd && systemctl status etcd 

6、驗證是否成功

在任意一臺機器(無論是不是集群節(jié)點,前提是需要有etcdctl工具和ca證書,server證書)上執(zhí)行如下命令:


[root@master1] /etc/etcd/pki$ etcdctl --ca-file=/etc/etcd/pki/ca.pem --cert-file=/etc/etcd/pki/server.pem --key-file=/etc/etcd/pki/server-key.pem --endpoints=https://192.168.255.131:2379 cluster-health 
2019-01-27 20:41:26.909601 I | warning: ignoring ServerName for user-provided CA for backwards compatibility is deprecated 
2019-01-27 20:41:26.910165 I | warning: ignoring ServerName for user-provided CA for backwards compatibility is deprecated 
member 5d7a44f5c39446c1 is healthy: got healthy result from https://192.168.255.132:2379 
member e281e4e43dceb752 is healthy: got healthy result from https://192.168.255.133:2379 
member ea5e4f12ed162d4b is healthy: got healthy result from https://192.168.255.131:2379 
cluster is healthy 

如果你沒有指定證書,會報如下錯誤

client: etcd cluster is unavailable or misconfigured; error #0: x509: certificate signed by unknown authority  

查看集群成員

[root@master1] /etc/etcd/pki$ etcdctl --ca-file=/etc/etcd/pki/ca.pem --cert-file=/etc/etcd/pki/server.pem --key-file=/etc/etcd/pki/server-key.pem --endpoints=https://192.168.255.131:2379 member list  
2019-01-27 22:58:46.914338 I | warning: ignoring ServerName for user-provided CA for backwards compatibility is deprecated 
5d7a44f5c39446c1: name=master2 peerURLs=https://192.168.255.132:2380 clientURLs=https://192.168.255.132:2379 isLeader=false 
e281e4e43dceb752: name=master3 peerURLs=https://192.168.255.133:2380 clientURLs=https://192.168.255.133:2379 isLeader=false 
ea5e4f12ed162d4b: name=master1 peerURLs=https://192.168.255.131:2380 clientURLs=https://192.168.255.131:2379 isLeader=true 

etcd的使用

  • HTTP API

etcd 對外通過 HTTP API 對外提供服務,這種方式方便測試(通過 curl 或者其他工具就能和 etcd 交互),也很容易集成到各種語言中(每個語言封裝 HTTP API 實現(xiàn)自己的 client 就行)。

  • etcdctl 命令行工具

除了 HTTP API 外,etcd 還提供了 etcdctl 命令行工具和 etcd 服務交互。這個命令行用 go 語言編寫,也是對 HTTP API 的封裝,日常使用起來也更容易。

總結

etcd 默認只保存 1000 個歷史事件,所以不適合有大量更新操作的場景,這樣會導致數(shù)據(jù)的丟失。etcd 典型的應用場景是配置管理和服務發(fā)現(xiàn),這些場景都是讀多寫少的。

相比于 zookeeper,etcd 使用起來要簡單很多。不過要實現(xiàn)真正的服務發(fā)現(xiàn)功能,etcd 還需要和其他工具(比如 registrator、confd 等)一起使用來實現(xiàn)服務的自動注冊和更新。一般etcd大多用于配置管理的場景,比如kubernetes。

目前 etcd 還沒有圖形化的工具。


我要咨詢