一 部署ETCD集羣
1.1 安裝ETCD
etcd 是基於 Raft 的分佈式 key-value 存儲系統,由 CoreOS 開發,經常使用於服務發現、共享配置以及併發控制(如 leader 選舉、分佈式鎖等)。kubernetes 使用 etcd 存儲全部運行數據。
1 etcd 是基於 Raft 的分佈式 key-value 存儲系統,由 CoreOS 開發,經常使用於服務發現、共享配置以及併發控制(如 leader 選舉、分佈式鎖等)。kubernetes 使用 etcd 存儲全部運行數據。
2 [root@k8smaster01 ~]# cd /opt/k8s/work
3 [root@k8smaster01 work]# wget https://github.com/coreos/etcd/releases/download/v3.3.13/etcd-v3.3.13-linux-amd64.tar.gz
4 [root@k8smaster01 work]# tar -xvf etcd-v3.3.13-linux-amd64.tar.gz
1.2 分發ETCD
1 [root@k8smaster01 ~]# cd /opt/k8s/work
2 [root@k8smaster01 work]# source /opt/k8s/bin/environment.sh
3 [root@k8smaster01 work]# for master_ip in ${MASTER_IPS[@]}
4 do
5 echo ">>> ${master_ip}"
6 scp etcd-v3.3.13-linux-amd64/etcd* root@${master_ip}:/opt/k8s/bin
7 ssh root@${master_ip} "chmod +x /opt/k8s/bin/*"
8 done
1.3 建立etcd證書和密鑰
1 [root@k8smaster01 ~]# cd /opt/k8s/work
2 [root@k8smaster01 work]# cat > etcd-csr.json <<EOF
3 {
4 "CN": "etcd",
5 "hosts": [
6 "127.0.0.1",
7 "172.24.8.71",
8 "172.24.8.72",
9 "172.24.8.73"
10 ],
11 "key": {
12 "algo": "rsa",
13 "size": 2048
14 },
15 "names": [
16 {
17 "C": "CN",
18 "ST": "Shanghai",
19 "L": "Shanghai",
20 "O": "k8s",
21 "OU": "System"
22 }
23 ]
24 }
25 EOF
26 #建立etcd的CA證書請求文件
解釋:
hosts 字段指定受權使用該證書的 etcd 節點 IP 或域名列表,須要將 etcd 集羣的三個節點 IP 都列在其中。
1 [root@k8smaster01 ~]# cd /opt/k8s/work
2 [root@k8smaster01 work]# cfssl gencert -ca=/opt/k8s/work/ca.pem \
3 -ca-key=/opt/k8s/work/ca-key.pem -config=/opt/k8s/work/ca-config.json \
4 -profile=kubernetes etcd-csr.json | cfssljson -bare etcd #生成CA密鑰(ca-key.pem)和證書(ca.pem)
1.4 分發證書和私鑰
1 [root@k8smaster01 ~]# cd /opt/k8s/work
2 [root@k8smaster01 work]# source /opt/k8s/bin/environment.sh
3 [root@k8smaster01 work]# for master_ip in ${MASTER_IPS[@]}
4 do
5 echo ">>> ${master_ip}"
6 ssh root@${master_ip} "mkdir -p /etc/etcd/cert"
7 scp etcd*.pem root@${master_ip}:/etc/etcd/cert/
8 done
1.5 建立etcd的systemd
1 [root@k8smaster01 ~]# cd /opt/k8s/work
2 [root@k8smaster01 work]# source /opt/k8s/bin/environment.sh
3 [root@k8smaster01 work]# cat > etcd.service.template <<EOF
4 [Unit]
5 Description=Etcd Server
6 After=network.target
7 After=network-online.target
8 Wants=network-online.target
9 Documentation=https://github.com/coreos
10
11 [Service]
12 Type=notify
13 WorkingDirectory=${ETCD_DATA_DIR}
14 ExecStart=/opt/k8s/bin/etcd \\
15 --data-dir=${ETCD_DATA_DIR} \\
16 --wal-dir=${ETCD_WAL_DIR} \\
17 --name=##MASTER_NAME## \\
18 --cert-file=/etc/etcd/cert/etcd.pem \\
19 --key-file=/etc/etcd/cert/etcd-key.pem \\
20 --trusted-ca-file=/etc/kubernetes/cert/ca.pem \\
21 --peer-cert-file=/etc/etcd/cert/etcd.pem \\
22 --peer-key-file=/etc/etcd/cert/etcd-key.pem \\
23 --peer-trusted-ca-file=/etc/kubernetes/cert/ca.pem \\
24 --peer-client-cert-auth \\
25 --client-cert-auth \\
26 --listen-peer-urls=https://##MASTER_IP##:2380 \\
27 --initial-advertise-peer-urls=https://##MASTER_IP##:2380 \\
28 --listen-client-urls=https://##MASTER_IP##:2379,http://127.0.0.1:2379 \\
29 --advertise-client-urls=https://##MASTER_IP##:2379 \\
30 --initial-cluster-token=etcd-cluster-0 \\
31 --initial-cluster=${ETCD_NODES} \\
32 --initial-cluster-state=new \\
33 --auto-compaction-mode=periodic \\
34 --auto-compaction-retention=1 \\
35 --max-request-bytes=33554432 \\
36 --quota-backend-bytes=6442450944 \\
37 --heartbeat-interval=250 \\
38 --election-timeout=2000
39 Restart=on-failure
40 RestartSec=5
41 LimitNOFILE=65536
42
43 [Install]
44 WantedBy=multi-user.target
45 EOF
解釋:
WorkingDirectory、--data-dir:指定工做目錄和數據目錄爲 ${ETCD_DATA_DIR},需在啓動服務前建立這個目錄;
--wal-dir:指定 wal 目錄,爲了提升性能,通常使用 SSD 或者和 --data-dir 不一樣的磁盤;
--name:指定節點名稱,當 --initial-cluster-state 值爲 new 時,--name 的參數值必須位於 --initial-cluster 列表中;
--cert-file、--key-file:etcd server 與 client 通訊時使用的證書和私鑰;
--trusted-ca-file:簽名 client 證書的 CA 證書,用於驗證 client 證書;
--peer-cert-file、--peer-key-file:etcd 與 peer 通訊使用的證書和私鑰;
--peer-trusted-ca-file:簽名 peer 證書的 CA 證書,用於驗證 peer 證書。
1.6 修改systemd相應地址
1 [root@k8smaster01 ~]# cd /opt/k8s/work
2 [root@k8smaster01 work]# source /opt/k8s/bin/environment.sh
3 [root@k8smaster01 work]# for (( i=0; i < 3; i++ ))
4 do
5 sed -e "s/##MASTER_NAME##/${MASTER_NAMES[i]}/" -e "s/##MASTER_IP##/${MASTER_IPS[i]}/" etcd.service.template > etcd-${MASTER_IPS[i]}.service
6 done
1.7 分發etcd systemd
1 [root@k8smaster01 ~]# cd /opt/k8s/work
2 [root@k8smaster01 work]# source /opt/k8s/bin/environment.sh
3 [root@k8smaster01 work]# for master_ip in ${MASTER_IPS[@]}
4 do
5 echo ">>> ${master_ip}"
6 scp etcd-${master_ip}.service root@${master_ip}:/etc/systemd/system/etcd.service
7 done
二 啓動並驗證
2.1 啓動ETCD
1 [root@k8smaster01 ~]# cd /opt/k8s/work
2 [root@k8smaster01 work]# source /opt/k8s/bin/environment.sh
3 [root@k8smaster01 work]# for master_ip in ${MASTER_IPS[@]}
4 do
5 echo ">>> ${master_ip}"
6 ssh root@${master_ip} "mkdir -p ${ETCD_DATA_DIR} ${ETCD_WAL_DIR}"
7 ssh root@${master_ip} "systemctl daemon-reload && systemctl enable etcd && systemctl restart etcd " &
8 done
2.2 檢查ETCD啓動
1 [root@k8smaster01 ~]# cd /opt/k8s/work
2 [root@k8smaster01 work]# source /opt/k8s/bin/environment.sh
3 [root@k8smaster01 work]# for master_ip in ${MASTER_IPS[@]}
4 do
5 echo ">>> ${master_ip}"
6 ssh root@${master_ip} "systemctl status etcd|grep Active"
7 done
2.3 驗證服務狀態
1 [root@k8smaster01 ~]# cd /opt/k8s/work
2 [root@k8smaster01 work]# source /opt/k8s/bin/environment.sh
3 [root@k8smaster01 work]# for master_ip in ${MASTER_IPS[@]}
4 do
5 echo ">>> ${master_ip}"
6 ETCDCTL_API=3 /opt/k8s/bin/etcdctl \
7 --endpoints=https://${master_ip}:2379 \
8 --cacert=/etc/kubernetes/cert/ca.pem \
9 --cert=/etc/etcd/cert/etcd.pem \
10 --key=/etc/etcd/cert/etcd-key.pem endpoint health
11 done
2.4 查看ETCD當前leader
1 [root@k8smaster01 ~]# source /opt/k8s/bin/environment.sh
2 [root@k8smaster01 ~]# ETCDCTL_API=3 /opt/k8s/bin/etcdctl \
3 -w table --cacert=/etc/kubernetes/cert/ca.pem \
4 --cert=/etc/etcd/cert/etcd.pem \
5 --key=/etc/etcd/cert/etcd-key.pem \
6 --endpoints=${ETCD_ENDPOINTS} endpoint status
如上所示,當前ETCD集羣的leader爲172.24.8.71。