etcd
环境设置¶
我在 Kubespray快速起步 成功部署了一个生产就绪的Kubernetes集群后,发现直接 Kubespray维护etcd 时由于环境变量配置问题无法直接访问和维护 etcd - 分布式kv存储 。所以借助 Kubespray 部署的 Systemd进程管理器 包装的 容器运行时(Container Runtimes) 运行 etcd - 分布式kv存储 ,梳理维护方法。
在
/etc/etcd.env
配置了 Systemd进程管理器 运行启动 etcd - 分布式kv存储 的执行参数,也包括了etcdctl
客户端运行参数:
# CLI settings
ETCDCTL_ENDPOINTS=https://127.0.0.1:2379
ETCDCTL_CACERT=/etc/ssl/etcd/ssl/ca.pem
ETCDCTL_KEY=/etc/ssl/etcd/ssl/admin-y-k8s-m-1-key.pem
ETCDCTL_CERT=/etc/ssl/etcd/ssl/admin-y-k8s-m-1.pem
执行以下脚本命令为自己构建一个环境变量:
# 从 /etc/etcd.env 中取出变量 ETCD_INITIAL_CLUSTER 并过滤出etcd的3个服务器IP地址
# 也就是将 ETCD_INITIAL_CLUSTER=etcd1=https://192.168.8.116:2380,etcd2=https://192.168.8.117:2380,etcd3=https://192.168.8.118:2380
# 转换成 192.168.8.116 192.168.8.117 192.168.8.118
# 可以使用 echo $ETCD_INITIAL_CLUSTER | awk -F'[= ,]' '{print $2, $4, $6}' | sed 's/https:\/\///g' | sed 's/:2380//g'
. /etc/etcd.env
echo ". /etc/etcd.env" >> ~/.bashrc
# 我发现还是通过sed转换更为简洁
var="ETCDCTL_ENDPOINTS=`echo $ETCD_INITIAL_CLUSTER | sed 's/etcd.=//g' | sed 's/:2380/:2379/g'`"
echo $var >> ~/.bashrc
echo "export ETCDCTL_ENDPOINTS ETCDCTL_CACERT ETCDCTL_KEY ETCDCTL_CERT" >> ~/.bashrc
echo "alias etcd_status='etcdctl --write-out=table endpoint status'" >> ~/.bashrc
此时执行
etcd_status
就能正确看到当前集群的etcd状态:
+----------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS |
+----------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| https://192.168.8.116:2379 | 3ff555f9837c69b9 | 3.5.6 | 7.8 MB | true | false | 16 | 2014832 | 2014832 | |
| https://192.168.8.117:2379 | 4a784b4a93b49575 | 3.5.6 | 7.9 MB | false | false | 16 | 2014832 | 2014832 | |
| https://192.168.8.118:2379 | cb79cdeb0f0fe1cb | 3.5.6 | 7.9 MB | false | false | 16 | 2014832 | 2014832 | |
+----------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+