一、etcd 概述
etcd 是一个高可用、配置性、实时、分布式的键值存储系统,由 CoreOS 开发,是 CNCF 主要项目之一。在 Kubernetes 中,etcd 是集群的数据库,存储着所有管理性信息,如 Pod 、Service 、Deployment 等。
二、etcd 的功能
-
支持分布式配置管理
-
支持事务(Transactions)
-
提供事件通知(Watch)
-
支持 TTL 与秒级失效
-
支持 gRPC 和 HTTP 接口
三、Kubernetes 中 etcd 的场景应用
-
存储 Kubernetes 的全部状态数据
-
管理集群的配置信息
-
做为控制面板组件之间的数据同步基础
四、etcd 的架构
etcd 是基于 Raft 协议实现的分布式调度系统,通过带有主从组成的集群,确保数据的一致性。
-
Leader:控制数据写入,同步至 Follower
-
Follower:读播事件,有效性验证
-
Raft 协议:确保进程在分布式环境下达成协议
五、etcd 常用命令
5.1 查看版本
etcdctl version
5.2 初始化 etcd 集群
etcd --name s1 \
--initial-advertise-peer-urls http://192.168.1.1:2380 \
--listen-peer-urls http://192.168.1.1:2380 \
--listen-client-urls http://192.168.1.1:2379 \
--advertise-client-urls http://192.168.1.1:2379 \
--initial-cluster s1=http://192.168.1.1:2380 \
--initial-cluster-state new \
--initial-cluster-token etcd-cluster-1
5.3 提供配置查询
etcdctl get /registry/pods --prefix --keys-only
5.4 规范化接口访问
curl http://127.0.0.1:2379/v2/keys/
5.5 存储数据
etcdctl put mykey "hello etcd"
5.6 获取数据
etcdctl get mykey
5.7 删除键
etcdctl del mykey
5.8 列出所有键
etcdctl get "" --prefix --keys-only
5.9 数据备份
etcdctl snapshot save snapshot.db
5.10 恢复数据
etcdctl snapshot restore snapshot.db \
--data-dir /var/lib/etcd-from-backup
5.11 查看环境信息
etcdctl endpoint status --write-out=table
5.12 规范化格式输出
etcdctl get / --prefix --write-out=json
六、实际应用场景
6.1 集群性强高可用性需求
通过部署多个 etcd 节点,实现高可用的分布式数据库,确保数据不丢失,可高效息息地接收、传播状态变化。
6.2 积极监控 etcd 状态
通过 Prometheus 、Grafana 等工具进行监控,最重要的是 etcd 的操作性能、延迟、剩余集群组元状态。
6.3 数据备份与恢复
可以通过 etcdctl snapshot save 和 restore 实现数据完整备份和恢复,是数据安全与维护的基本技能。
七、结论
etcd 是 Kubernetes 最核心的组件之一,它确保了数据的持久性、一致性和可用性。熟悉 etcd 的架构和应用,是理解和维护 Kubernetes 集群的基础。