手動構(gòu)筑單機版K8s(五)創(chuàng)建單節(jié)點etcd

5.創(chuàng)建單節(jié)點etcd

kuberntes 系統(tǒng)使用 etcd 存儲所有數(shù)據(jù),本文介紹單節(jié)點etcd部署方式

TLS 認(rèn)證文件

需要為 etcd 集群創(chuàng)建加密通信的 TLS 證書,這里復(fù)用以前創(chuàng)建的 kubernetes 證書

確認(rèn)以下證書是否存在

$ ls /etc/kubernetes/ssl
ca.pem kubernetes-key.pem kubernetes.pem

下載etcd二進(jìn)制文件

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

創(chuàng)建 etcd 的 systemd unit 文件

在/usr/lib/systemd/system/目錄下創(chuàng)建文件etcd.service,內(nèi)容如下。

[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/
EnvironmentFile=-/etc/etcd/etcd.conf
ExecStart=/usr/local/bin/etcd \
  --name ${ETCD_NAME} \
  --cert-file=/etc/kubernetes/ssl/kubernetes.pem \
  --key-file=/etc/kubernetes/ssl/kubernetes-key.pem \
  --peer-cert-file=/etc/kubernetes/ssl/kubernetes.pem \
  --peer-key-file=/etc/kubernetes/ssl/kubernetes-key.pem \
  --trusted-ca-file=/etc/kubernetes/ssl/ca.pem \
  --peer-trusted-ca-file=/etc/kubernetes/ssl/ca.pem \
  --initial-advertise-peer-urls ${ETCD_INITIAL_ADVERTISE_PEER_URLS} \
  --listen-peer-urls ${ETCD_LISTEN_PEER_URLS} \
  --listen-client-urls ${ETCD_LISTEN_CLIENT_URLS},http://127.0.0.1:2379 \
  --advertise-client-urls ${ETCD_ADVERTISE_CLIENT_URLS} \
  --initial-cluster-token ${ETCD_INITIAL_CLUSTER_TOKEN} \
  --initial-cluster infra1=https://127.0.0.1:2380
  --initial-cluster-state new \
  --data-dir=${ETCD_DATA_DIR}
Restart=on-failure
RestartSec=5
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
  • 指定 etcd 的工作目錄為 /var/lib/etcd,數(shù)據(jù)目錄為 /var/lib/etcd,需在啟動服務(wù)前創(chuàng)建這個目錄,否則啟動服務(wù)的時候會報錯“Failed at step CHDIR spawning /usr/bin/etcd: No such file or directory”;
  • 為了保證通信安全,需要指定 etcd 的公私鑰(cert-file和key-file)、Peers 通信的公私鑰和 CA 證書(peer-cert-file、peer-key-file、peer-trusted-ca-file)、客戶端的CA證書(trusted-ca-file);
  • 創(chuàng)建 kubernetes.pem 證書時使用的 kubernetes-csr.json 文件的 hosts 字段包含所有 etcd 節(jié)點的IP,否則證書校驗會出錯;
  • --initial-cluster-state 值為 new 時,--name 的參數(shù)值必須位于 --initial-cluster 列表中;

環(huán)境變量配置文件/etc/etcd/etcd.conf

mkdir /etc/etcd
vi /etc/etcd/etcd.conf
# [member]
ETCD_NAME=infra1
ETCD_DATA_DIR="/var/lib/etcd"
ETCD_LISTEN_PEER_URLS="https://127.0.0.1:2380"
ETCD_LISTEN_CLIENT_URLS="https://127.0.0.1:2379"

#[cluster]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://127.0.0.1:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_ADVERTISE_CLIENT_URLS="https://127.0.0.1:2379"

啟動 etcd 服務(wù)

systemctl daemon-reload
systemctl enable etcd
systemctl start etcd
systemctl status etcd

注意:如果日志中出現(xiàn)連接異常信息,請確認(rèn)所有節(jié)點防火墻是否開放2379,2380端口。 以centos7為例:

firewall-cmd --zone=public --add-port=2380/tcp --permanent
firewall-cmd --zone=public --add-port=2379/tcp --permanent
firewall-cmd --reload

驗證服務(wù)

[root@k8s bin]# etcdctl \
  --ca-file=/etc/kubernetes/ssl/ca.pem \
  --cert-file=/etc/kubernetes/ssl/kubernetes.pem \
  --key-file=/etc/kubernetes/ssl/kubernetes-key.pem \
  cluster-health

2021-06-30 17:06:24.650133 I | warning: ignoring ServerName for user-provided CA for backwards compatibility is deprecated
2021-06-30 17:06:24.650546 I | warning: ignoring ServerName for user-provided CA for backwards compatibility is deprecated
member a874c87fd42044f is healthy: got healthy result from https://127.0.0.1:2379
cluster is healthy

結(jié)果最后一行為 cluster is healthy 時表示集群服務(wù)正常。

安裝步驟

  1. 事前準(zhǔn)備
  2. 創(chuàng)建TLS證書和秘鑰
  3. 安裝kubectl命令行工具
  4. 創(chuàng)建kubeconfig文件
  5. 創(chuàng)建高可用etcd集群
  6. 部署master節(jié)點
  7. 部署node節(jié)點
  8. 全局驗證
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容