etcd in Kubernetes: Distributed Configuration Management
Aditya Joshi
Senior Software Engineer @ Walmart | Blockchain, Hyperledger, Kubernetes | Lead Dev Advocate @Hyperledger | CKS | CKA | CKAD
In the world of container orchestration, Kubernetes has emerged as the de facto standard for managing and scaling containerized applications. Behind the scenes, Kubernetes relies on a distributed key-value store called etcd to maintain crucial cluster state information and provide reliable configuration management. In this article, we will explore the role of etcd in Kubernetes and understand its significance in ensuring the resilience and consistency of the Kubernetes cluster.
What is?etcd?
etcd is an open-source distributed key-value store that is designed to be highly available, consistent, and fault-tolerant. It is built on the Raft consensus algorithm, which ensures that data consistency is maintained across a cluster of nodes. etcd is written in Go and was developed by CoreOS, later becoming an integral part of the Kubernetes ecosystem.
Key Features of?etcd
Role of etcd in Kubernetes:
In Kubernetes, etcd acts as the central brain of the cluster. It stores and manages all essential cluster state information, such as:
How etcd Ensures Reliability in Kubernetes:
Running etcd?Cluster
etcd cluster using Docker Compose is a convenient way to set up a local development environment for testing and experimentation. Here’s how you can create a simple etcd cluster using Docker Compose:
x-variables
flag_initial_cluster_token: &flag_initial_cluster_token '--initial-cluster-token=mys3cr3ttok3n'
common_settings: &common_settings
image: quay.io/coreos/etcd:v3.5.9
entrypoint: /usr/local/bin/etcd
ports:
- 2379
services:
etcd-1:
<<: *common_settings
command:
- '--name=etcd-1'
- '--initial-advertise-peer-urls=https://etcd-1:2380'
- '--listen-peer-urls=https://0.0.0.0:2380'
- '--listen-client-urls=https://0.0.0.0:2379'
- '--advertise-client-urls=https://etcd-1:2379'
- '--heartbeat-interval=250'
- '--election-timeout=1250'
- '--initial-cluster=etcd-1=https://etcd-1:2380,etcd-2=https://etcd-2:2380,etcd-3=https://etcd-3:2380'
- '--initial-cluster-state=new'
- *flag_initial_cluster_token
volumes:
- etcd1:/etcd_data
etcd-2:
<<: *common_settings
command:
- '--name=etcd-2'
- '--initial-advertise-peer-urls=https://etcd-2:2380'
- '--listen-peer-urls=https://0.0.0.0:2380'
- '--listen-client-urls=https://0.0.0.0:2379'
- '--advertise-client-urls=https://etcd-2:2379'
- '--heartbeat-interval=250'
- '--election-timeout=1250'
- '--initial-cluster=etcd-1=https://etcd-1:2380,etcd-2=https://etcd-2:2380,etcd-3=https://etcd-3:2380'
- '--initial-cluster-state=new'
- *flag_initial_cluster_token
volumes:
- etcd2:/etcd_data
etcd-3:
<<: *common_settings
command:
- '--name=etcd-3'
- '--initial-advertise-peer-urls=https://etcd-3:2380'
- '--listen-peer-urls=https://0.0.0.0:2380'
- '--listen-client-urls=https://0.0.0.0:2379'
- '--advertise-client-urls=https://etcd-3:2379'
- '--heartbeat-interval=250'
- '--election-timeout=1250'
- '--initial-cluster=etcd-1=https://etcd-1:2380,etcd-2=https://etcd-2:2380,etcd-3=https://etcd-3:2380'
- '--initial-cluster-state=new'
- *flag_initial_cluster_token
volumes:
- etcd3:/etcd_data
volumes:
etcd1:
etcd2:
etcd3::
start the etcd cluster using docker-compose
$ docker-compose -f docker-compose.yaml up -d
check if all the containers are in a healthy state
领英推荐
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3a4e906e81a6 quay.io/coreos/etcd:v3.5.9 "/usr/local/bin/etcd…" 8 minutes ago Up 8 minutes 2380/tcp, 0.0.0.0:49164->2379/tcp, :::49164->2379/tcp etcd-cluster-etcd-3-1
d3e82f47075b quay.io/coreos/etcd:v3.5.9 "/usr/local/bin/etcd…" 8 minutes ago Up 8 minutes 2380/tcp, 0.0.0.0:49165->2379/tcp, :::49165->2379/tcp etcd-cluster-etcd-2-1
15a0857e99b5 quay.io/coreos/etcd:v3.5.9 "/usr/local/bin/etcd…" 8 minutes ago Up 8 minutes 2380/tcp, 0.0.0.0:49166->2379/tcp, :::49166->2379/tcp etcd-cluster-etcd-1-1
Then, you can run the etcdctl command to check the cluster health:
$ docker exec -it etcd-cluster-etcd-3-1 etcdctl member list
88d11e2649dad027, started, etcd-2, https://etcd-2:2380, https://etcd-2:2379, false
b8c6addf901e4e46, started, etcd-1, https://etcd-1:2380, https://etcd-1:2379, false
c3697a4fd7a20dcd, started, etcd-3, https://etcd-3:2380, https://etcd-3:2379, false
$ docker exec -it etcd-cluster-etcd-1-1 etcdctl put secret password
2. To read data from etcd, you use the get command:
$ docker exec -it etcd-cluster-etcd-2-1 etcdctl get secret
secret
password
3. To delete a key-value pair from etcd, you use the del command:
$ docker exec -it etcd-cluster-etcd-2-1 etcdctl del secret
1
Conclusion
In Kubernetes, etcd serves as a critical component responsible for maintaining the reliable state and configuration management of the entire cluster. Its distributed key-value store, combined with the Raft consensus algorithm, guarantees strong consistency, high availability, and fault tolerance. Thanks to etcd’s robustness, Kubernetes can efficiently manage containerized applications at scale, ensuring seamless orchestration and smooth user experiences.
As Kubernetes continues to evolve and grow in popularity, etcd will remain an indispensable pillar in the foundation of distributed systems, empowering administrators and developers to deploy and manage applications confidently in today’s dynamic and demanding cloud-native landscape.
Reference