04.部署 etcd 集群

etcd 是基于 Raft 的分布式 key-value 存储系统,由 CoreOS 开发,常用于服务发现、共享配置以及并发控制(如 leader 选举、分布式锁等)。kubernetes 使用 etcd 存储所有运行数据。

本文档介绍部署一个三节点高可用 etcd 集群的步骤:

  • 下载和分发 etcd 二进制文件;
  • 创建 etcd 集群各节点的 x509 证书,用于加密客户端(如 etcdctl) 与 etcd 集群、etcd 集群之间的数据流;
  • 创建 etcd 的 systemd unit 文件,配置服务参数;
  • 检查集群工作状态;

etcd 集群各节点的名称和 IP 如下:

  • kube-node1:172.27.129.105
  • kube-node2:172.27.129.111
  • kube-node3:172.27.129.112

下载和分发 etcd 二进制文件

https://github.com/coreos/etcd/releases 页面下载最新版本的发布包:

  1. wget https://github.com/coreos/etcd/releases/download/v3.3.7/etcd-v3.3.7-linux-amd64.tar.gz
  2. tar -xvf etcd-v3.3.7-linux-amd64.tar.gz

分发二进制文件到集群所有节点:

  1. source /opt/k8s/bin/environment.sh
  2. for node_ip in ${NODE_IPS[@]}
  3. do
  4. echo ">>> ${node_ip}"
  5. scp etcd-v3.3.7-linux-amd64/etcd* k8s@${node_ip}:/opt/k8s/bin
  6. ssh k8s@${node_ip} "chmod +x /opt/k8s/bin/*"
  7. done

创建 etcd 证书和私钥

创建证书签名请求:

  1. cat > etcd-csr.json <<EOF
  2. {
  3. "CN": "etcd",
  4. "hosts": [
  5. "127.0.0.1",
  6. "172.27.129.105",
  7. "172.27.129.111",
  8. "172.27.129.112"
  9. ],
  10. "key": {
  11. "algo": "rsa",
  12. "size": 2048
  13. },
  14. "names": [
  15. {
  16. "C": "CN",
  17. "ST": "BeiJing",
  18. "L": "BeiJing",
  19. "O": "k8s",
  20. "OU": "4Paradigm"
  21. }
  22. ]
  23. }
  24. EOF
  • hosts 字段指定授权使用该证书的 etcd 节点 IP 或域名列表,这里将 etcd 集群的三个节点 IP 都列在其中;

生成证书和私钥:

  1. cfssl gencert -ca=/etc/kubernetes/cert/ca.pem \
  2. -ca-key=/etc/kubernetes/cert/ca-key.pem \
  3. -config=/etc/kubernetes/cert/ca-config.json \
  4. -profile=kubernetes etcd-csr.json | cfssljson -bare etcd
  5. ls etcd*

分发生成的证书和私钥到各 etcd 节点:

  1. source /opt/k8s/bin/environment.sh
  2. for node_ip in ${NODE_IPS[@]}
  3. do
  4. echo ">>> ${node_ip}"
  5. ssh root@${node_ip} "mkdir -p /etc/etcd/cert && chown -R k8s /etc/etcd/cert"
  6. scp etcd*.pem k8s@${node_ip}:/etc/etcd/cert/
  7. done

创建 etcd 的 systemd unit 模板文件

  1. source /opt/k8s/bin/environment.sh
  2. cat > etcd.service.template <<EOF
  3. [Unit]
  4. Description=Etcd Server
  5. After=network.target
  6. After=network-online.target
  7. Wants=network-online.target
  8. Documentation=https://github.com/coreos
  9. [Service]
  10. User=k8s
  11. Type=notify
  12. WorkingDirectory=/var/lib/etcd/
  13. ExecStart=/opt/k8s/bin/etcd \\
  14. --data-dir=/var/lib/etcd \\
  15. --name=##NODE_NAME## \\
  16. --cert-file=/etc/etcd/cert/etcd.pem \\
  17. --key-file=/etc/etcd/cert/etcd-key.pem \\
  18. --trusted-ca-file=/etc/kubernetes/cert/ca.pem \\
  19. --peer-cert-file=/etc/etcd/cert/etcd.pem \\
  20. --peer-key-file=/etc/etcd/cert/etcd-key.pem \\
  21. --peer-trusted-ca-file=/etc/kubernetes/cert/ca.pem \\
  22. --peer-client-cert-auth \\
  23. --client-cert-auth \\
  24. --listen-peer-urls=https://##NODE_IP##:2380 \\
  25. --initial-advertise-peer-urls=https://##NODE_IP##:2380 \\
  26. --listen-client-urls=https://##NODE_IP##:2379,http://127.0.0.1:2379 \\
  27. --advertise-client-urls=https://##NODE_IP##:2379 \\
  28. --initial-cluster-token=etcd-cluster-0 \\
  29. --initial-cluster=${ETCD_NODES} \\
  30. --initial-cluster-state=new
  31. Restart=on-failure
  32. RestartSec=5
  33. LimitNOFILE=65536
  34. [Install]
  35. WantedBy=multi-user.target
  36. EOF
  • User:指定以 k8s 账户运行;
  • WorkingDirectory--data-dir:指定工作目录和数据目录为 /var/lib/etcd,需在启动服务前创建这个目录;
  • --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 证书;

为各节点创建和分发 etcd systemd unit 文件

替换模板文件中的变量,为各节点创建 systemd unit 文件:

  1. source /opt/k8s/bin/environment.sh
  2. for (( i=0; i < 3; i++ ))
  3. do
  4. sed -e "s/##NODE_NAME##/${NODE_NAMES[i]}/" -e "s/##NODE_IP##/${NODE_IPS[i]}/" etcd.service.template > etcd-${NODE_IPS[i]}.service
  5. done
  6. ls *.service
  • NODE_NAMES 和 NODE_IPS 为相同长度的 bash 数组,分别为节点名称和对应的 IP;

分发生成的 systemd unit 文件:

  1. source /opt/k8s/bin/environment.sh
  2. for node_ip in ${NODE_IPS[@]}
  3. do
  4. echo ">>> ${node_ip}"
  5. ssh root@${node_ip} "mkdir -p /var/lib/etcd && chown -R k8s /var/lib/etcd"
  6. scp etcd-${node_ip}.service root@${node_ip}:/etc/systemd/system/etcd.service
  7. done
  • 必须先创建 etcd 数据目录和工作目录;
  • 文件重命名为 etcd.service;

完整 unit 文件见:etcd.service

启动 etcd 服务

  1. source /opt/k8s/bin/environment.sh
  2. for node_ip in ${NODE_IPS[@]}
  3. do
  4. echo ">>> ${node_ip}"
  5. ssh root@${node_ip} "systemctl daemon-reload && systemctl enable etcd && systemctl restart etcd &"
  6. done
  • etcd 进程首次启动时会等待其它节点的 etcd 加入集群,命令 systemctl start etcd 会卡住一段时间,为正常现象。

检查启动结果

  1. source /opt/k8s/bin/environment.sh
  2. for node_ip in ${NODE_IPS[@]}
  3. do
  4. echo ">>> ${node_ip}"
  5. ssh k8s@${node_ip} "systemctl status etcd|grep Active"
  6. done

确保状态为 active (running),否则查看日志,确认原因:

  1. $ journalctl -u etcd

验证服务状态

部署完 etcd 集群后,在任一 etc 节点上执行如下命令:

  1. source /opt/k8s/bin/environment.sh
  2. for node_ip in ${NODE_IPS[@]}
  3. do
  4. echo ">>> ${node_ip}"
  5. ETCDCTL_API=3 /opt/k8s/bin/etcdctl \
  6. --endpoints=https://${node_ip}:2379 \
  7. --cacert=/etc/kubernetes/cert/ca.pem \
  8. --cert=/etc/etcd/cert/etcd.pem \
  9. --key=/etc/etcd/cert/etcd-key.pem endpoint health
  10. done

预期输出:

  1. https://172.27.129.105:2379 is healthy: successfully committed proposal: took = 2.192932ms
  2. https://172.27.129.111:2379 is healthy: successfully committed proposal: took = 3.546896ms
  3. https://172.27.129.112:2379 is healthy: successfully committed proposal: took = 3.013667ms

输出均为 healthy 时表示集群服务正常。