创建高可用 etcd 集群

kuberntes 系统使用 etcd 存储所有数据,本文档介绍部署一个三节点高可用 etcd 集群的步骤,这三个节点复用 kubernetes master 机器,分别命名为test-001.jimmysong.iotest-002.jimmysong.iotest-003.jimmysong.io

  • test-001.jimmysong.io:172.20.0.113
  • test-002.jimmysong.io:172.20.0.114
  • test-003.jimmysong.io:172.20.0.115

TLS 认证文件

需要为 etcd 集群创建加密通信的 TLS 证书,这里复用以前创建的 kubernetes 证书

  1. cp ca.pem kubernetes-key.pem kubernetes.pem /etc/kubernetes/ssl
  • kubernetes 证书的 hosts 字段列表中包含上面三台机器的 IP,否则后续证书校验会失败;

下载二进制文件

https://github.com/coreos/etcd/releases 页面下载最新版本的二进制文件

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

或者直接使用yum命令安装:

  1. yum install etcd

若使用yum安装,默认etcd命令将在/usr/bin目录下,注意修改下面的etcd.service文件中的启动命令地址为/usr/bin/etcd

创建 etcd 的 systemd unit 文件

在/usr/lib/systemd/system/目录下创建文件etcd.service,内容如下。注意替换IP地址为你自己的etcd集群的主机IP。

  1. [Unit]
  2. Description=Etcd Server
  3. After=network.target
  4. After=network-online.target
  5. Wants=network-online.target
  6. Documentation=https://github.com/coreos
  7. [Service]
  8. Type=notify
  9. WorkingDirectory=/var/lib/etcd/
  10. EnvironmentFile=-/etc/etcd/etcd.conf
  11. ExecStart=/usr/local/bin/etcd \
  12. --name ${ETCD_NAME} \
  13. --cert-file=/etc/kubernetes/ssl/kubernetes.pem \
  14. --key-file=/etc/kubernetes/ssl/kubernetes-key.pem \
  15. --peer-cert-file=/etc/kubernetes/ssl/kubernetes.pem \
  16. --peer-key-file=/etc/kubernetes/ssl/kubernetes-key.pem \
  17. --trusted-ca-file=/etc/kubernetes/ssl/ca.pem \
  18. --peer-trusted-ca-file=/etc/kubernetes/ssl/ca.pem \
  19. --initial-advertise-peer-urls ${ETCD_INITIAL_ADVERTISE_PEER_URLS} \
  20. --listen-peer-urls ${ETCD_LISTEN_PEER_URLS} \
  21. --listen-client-urls ${ETCD_LISTEN_CLIENT_URLS},http://127.0.0.1:2379 \
  22. --advertise-client-urls ${ETCD_ADVERTISE_CLIENT_URLS} \
  23. --initial-cluster-token ${ETCD_INITIAL_CLUSTER_TOKEN} \
  24. --initial-cluster infra1=https://172.20.0.113:2380,infra2=https://172.20.0.114:2380,infra3=https://172.20.0.115:2380 \
  25. --initial-cluster-state new \
  26. --data-dir=${ETCD_DATA_DIR}
  27. Restart=on-failure
  28. RestartSec=5
  29. LimitNOFILE=65536
  30. [Install]
  31. WantedBy=multi-user.target
  • 指定 etcd 的工作目录为 /var/lib/etcd,数据目录为 /var/lib/etcd,需在启动服务前创建这个目录,否则启动服务的时候会报错“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);
  • 创建 kubernetes.pem 证书时使用的 kubernetes-csr.json 文件的 hosts 字段包含所有 etcd 节点的IP,否则证书校验会出错;
  • --initial-cluster-state 值为 new 时,--name 的参数值必须位于 --initial-cluster 列表中;

完整 unit 文件见:etcd.service

环境变量配置文件/etc/etcd/etcd.conf

  1. # [member]
  2. ETCD_NAME=infra1
  3. ETCD_DATA_DIR="/var/lib/etcd"
  4. ETCD_LISTEN_PEER_URLS="https://172.20.0.113:2380"
  5. ETCD_LISTEN_CLIENT_URLS="https://172.20.0.113:2379"
  6. #[cluster]
  7. ETCD_INITIAL_ADVERTISE_PEER_URLS="https://172.20.0.113:2380"
  8. ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
  9. ETCD_ADVERTISE_CLIENT_URLS="https://172.20.0.113:2379"

这是172.20.0.113节点的配置,其他两个etcd节点只要将上面的IP地址改成相应节点的IP地址即可。ETCD_NAME换成对应节点的infra1/2/3。

启动 etcd 服务

  1. mv etcd.service /usr/lib/systemd/system/
  2. systemctl daemon-reload
  3. systemctl enable etcd
  4. systemctl start etcd
  5. systemctl status etcd

在所有的 kubernetes master 节点重复上面的步骤,直到所有机器的 etcd 服务都已启动。

注意:如果日志中出现连接异常信息,请确认所有节点防火墙是否开放2379,2380端口。以centos7为例:

  1. firewall-cmd --zone=public --add-port=2380/tcp --permanent
  2. firewall-cmd --zone=public --add-port=2379/tcp --permanent
  3. firewall-cmd --reload

验证服务

在任一 kubernetes master 机器上执行如下命令:

  1. $ etcdctl \
  2. --ca-file=/etc/kubernetes/ssl/ca.pem \
  3. --cert-file=/etc/kubernetes/ssl/kubernetes.pem \
  4. --key-file=/etc/kubernetes/ssl/kubernetes-key.pem \
  5. cluster-health
  6. 2017-04-11 15:17:09.082250 I | warning: ignoring ServerName for user-provided CA for backwards compatibility is deprecated
  7. 2017-04-11 15:17:09.083681 I | warning: ignoring ServerName for user-provided CA for backwards compatibility is deprecated
  8. member 9a2ec640d25672e5 is healthy: got healthy result from https://172.20.0.115:2379
  9. member bc6f27ae3be34308 is healthy: got healthy result from https://172.20.0.114:2379
  10. member e5c92ea26c4edba0 is healthy: got healthy result from https://172.20.0.113:2379
  11. cluster is healthy

结果最后一行为 cluster is healthy 时表示集群服务正常。

更多资料

关于如何在etcd中查看kubernetes的数据,请参考使用etcdctl访问kuberentes数据