介绍:
ETCD是CoreOS团队2013年6月发起的开源项目,已为CNCF的毕业项目,是一款高可用的键值数据库。采用raft协议作为一致性算法,基于go语言实现。以下基于V3.5的介绍。
其特点有:
- 简单性:使用标准HTTP工具(如curl)读取和写入值
- 观测性:可持续watch key的变化,做出相应的响应
- 高可用:分布式集群,解决单点故障
- 完全复制:每个节点都是一份完整的的存档
- 安全:带有客户端验证的TLS
- 一致性:每次读取都会返回垮多主机的最新写入
背景:
ETCD作为kubernetes集群后端存储的分布式键值数据库,保存整个集群的状态信息,因此,对应etcd的备份和优化显得至关重要。
参数优化:
配置文件详解
vim /etc/systemd/system/etcd.service
[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 #数据存放目录
ExecStart=/opt/kube/bin/etcd \ ##二进制执行路径
--name=etcd-192.168.2.131 \
--cert-file=/etc/kubernetes/ssl/etcd.pem \
--key-file=/etc/kubernetes/ssl/etcd-key.pem \
--peer-cert-file=/etc/kubernetes/ssl/etcd.pem \
--peer-key-file=/etc/kubernetes/ssl/etcd-key.pem \
--trusted-ca-file=/etc/kubernetes/ssl/ca.pem \
--peer-trusted-ca-file=/etc/kubernetes/ssl/ca.pem \
--initial-advertise-peer-urls=https://192.168.2.131:2380 \ #广播自身的服务端口
--listen-peer-urls=https://192.168.2.131:2380 \ #集群通讯端口
--listen-client-urls=https://192.168.2.131:2379,http://127.0.0.1:2379 \ #客户端访问地址
--advertise-client-urls=https://192.168.2.131:2379 \ #广播客户端地址
--initial-cluster-token=etcd-cluster-0 \ #集群token
--initial-cluster=etcd-192.168.2.131=https://192.168.2.131:2380,etcd-192.168.2.132=https://192.168.2.132:2380,etcd-192.168.2.133=https://192.168.2.133:2380 \ #集群所有节点地址信息
--initial-cluster-state=new \ #新建集群为new,已存在的为existing
--data-dir=/var/lib/etcd \ #数据存放路径
--wal-dir= \ #快照日志路径
--snapshot-count=50000 \ #最大快照次数,指定有多少事务被提交时,触发截取快照保存到磁盘,释放wal日志,默认值100000
--auto-compaction-retention=1 \ #首次压缩周期为1小时,后续压缩周期为当前值的10%,也就是每隔6分钟压缩一次
--auto-compaction-mode=periodic \ #周期性压缩
--max-request-bytes=10485760 \ #请求的最大字节数,默认一个key为1.5M,官方推荐最大为10M
--quota-backend-bytes=8589934592 #磁盘存储空间大小限制,默认为2G,超过8G会有告警信息
Restart=always
RestartSec=15
LimitNOFILE=65536
OOMScoreAdjust=-999
[Install]
WantedBy=multi-user.target
参数优化项
了解了上面提到的参数含义,接下来就可以做优化了
- max-request-bytes=10485760 ;请求的最大字节数,默认一个key为1.5M,官方推荐最大为10M。避免出现请求过大导致无法写入的情况
- –quota-backend-bytes=8589934592 ;磁盘存储空间大小限制,默认为2G,超过8G会有告警信息,按照默认值,当存储空间达到2G后,会报存储空间已满,无法写入的告警
- 集群碎片整理,进行压缩操作之后,旧的revision被清理,会产生内部的碎片,内部碎片是指空闲状态的,能被etcd使用但是仍然消耗存储空间的磁盘空间,去碎片化实际上是释放磁盘空间。
注意:碎片整理会阻塞对etcd的读写操作,大量数据的defrag建议逐台进行,以免影响集群稳定性,一般在业务低峰期执行此操作
etcdctl defrag endpoint #本机碎片整理
etcdctl defrag --endpoints=https://192.168.2.132:2379 --cacert=/etc/kubernetes/ssl/ca.pem --cert=/etc/kubernetes/ssl/etcd.pem --key=/etc/kubernetes/ssl/etcd-key.pem #指定节点碎片处理
etcdctl defrag --cluster --endpoints=https://192.168.2.131:2379 --cacert=/etc/kubernetes/ssl/ca.pem --cert=/etc/kubernetes/ssl/etcd.pem --key=/etc/kubernetes/ssl/etcd-key.pem #集群碎片处理
备份恢复
WAL是write ahead log(预写日志)的缩写,顾名思义,就是在执行真正的写操作之前先写一个日志,预写日志。
wal: 存放预写式日志,最大的作用是记录了整个数据变化的全部历程。在etcd中,所有数据的修改在提交前,都要先写入到WAL中。
V3版本数据备份
etcdctl snapshot save /data/backup/snapshot.db
ezctl backup ClusterName #使用kubeasz安装的集群可以使用此命令做备份,参照:https://github.com/easzlab/kubeasz/blob/master/docs/op/cluster_restore.md
V3版本数据恢复
etcdctl snapshot restore /data/backup/snapshot.db --data-dir=/app/etcd-dir #将数据恢复到一个新创建的目录中
ezctl restore ClusterName #使用kubeasz安装的集群可以使用此命令做恢复,恢复期间会停止停止kube-apiserver/controller-manager/scheduler/kubelet/kube-proxy服务
状态查看
export NODE_IPS="192.168.2.131 192.168.2.132 192.168.2.133"
root@ubuntu01:/app/etcd-dir# for ip in ${NODE_IPS}; do etcdctl --endpoints=https://${ip}:2379 --cacert=/etc/kubernetes/ssl/ca.pem --cert=/etc/kubernetes/ssl/etcd.pem --key=/etc/kubernetes/ssl/etcd-key.pem endpoint health; done
https://192.168.2.131:2379 is healthy: successfully committed proposal: took = 10.651582ms
https://192.168.2.132:2379 is healthy: successfully committed proposal: took = 74.296019ms
https://192.168.2.133:2379 is healthy: successfully committed proposal: took = 14.587119ms
节点详细信息查看:
root@ubuntu01:/app/etcd-dir/member# for ip in ${NODE_IPS}; do etcdctl --write-out=table endpoint status --endpoints=https://${ip}:2379 --cacert=/etc/kubernetes/ssl/ca.pem --cert=/etc/kubernetes/ssl/etcd.pem --key=/etc/kubernetes/ssl/etcd-key.pem; done
+----------------------------+-----------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS |
+----------------------------+-----------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| https://192.168.2.131:2379 | 2afd23759ae3787 | 3.5.4 | 1.8 MB | false | false | 48 | 64986 | 64986 | |
+----------------------------+-----------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
+----------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS |
+----------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| https://192.168.2.132:2379 | feb3bcf2733c5b7a | 3.5.4 | 1.8 MB | false | false | 48 | 64986 | 64986 | |
+----------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
+----------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS |
+----------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| https://192.168.2.133:2379 | 2d06aa5780fbce84 | 3.5.4 | 1.8 MB | true | false | 48 | 64986 | 64986 | |
+----------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+