CodeBlanks Logo Icon CodeBlanks

What is etcd?

Learn that etcd stores cluster state.

What is etcd?

etcd is a distributed, consistent key-value database holding the whole state of the cluster (the desired and actual state of every object). It ensures consistency with the Raft consensus algorithm: a write becomes final when a majority (quorum) of members confirm it. That's why etcd is run with an odd number of members (3, 5).

3 members -> quorum 2 -> survives the loss of 1 member
5 members -> quorum 3 -> survives the loss of 2 members
4 members -> quorum 3 -> still survives the loss of only 1

Only kube-apiserver connects to etcd directly; all other components talk through the API server.

Looking inside etcd

Kubernetes objects are stored in etcd under the keys /registry/<type>/<namespace>/<name>. They can be inspected with etcdctl (on a control-plane node, with certificates):

ETCDCTL_API=3 etcdctl --endpoints=https://127.0.0.1:2379 \
  --cacert=/etc/kubernetes/pki/etcd/ca.crt \
  --cert=/etc/kubernetes/pki/etcd/server.crt \
  --key=/etc/kubernetes/pki/etcd/server.key \
  get /registry/deployments/default --prefix --keys-only
/registry/deployments/default/web
/registry/deployments/default/api

Values are encoded with Protobuf; Secrets are unreadable only if encryption has been configured.

Practice etcd health

Write two commands (with the certificate flags from the card, each on one line): query the health with etcdctl ... endpoint health; list the members with etcdctl ... member list -w table. Flags: --endpoints=https://127.0.0.1:2379, --cacert=/etc/kubernetes/pki/etcd/ca.crt, --cert=/etc/kubernetes/pki/etcd/server.crt, --key=/etc/kubernetes/pki/etcd/server.key.

Starter code

terminal
# buraya yaz

Sample solution

terminal
etcdctl --endpoints=https://127.0.0.1:2379 --cacert=/etc/kubernetes/pki/etcd/ca.crt --cert=/etc/kubernetes/pki/etcd/server.crt --key=/etc/kubernetes/pki/etcd/server.key endpoint health
etcdctl --endpoints=https://127.0.0.1:2379 --cacert=/etc/kubernetes/pki/etcd/ca.crt --cert=/etc/kubernetes/pki/etcd/server.crt --key=/etc/kubernetes/pki/etcd/server.key member list -w table

Expected output

https://127.0.0.1:2379 is healthy: successfully committed proposal: took = 4.2ms
+------------------+---------+-------+---------------------------+
|        ID        | STATUS  | NAME  |        PEER ADDRS         |
+------------------+---------+-------+---------------------------+
| 8e9e05c52164694d | started | cp-1  | https://192.168.49.2:2380 |
+------------------+---------+-------+---------------------------+

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What happens if 2 members are lost in a 3-member etcd cluster?

  • Quorum is lost; the cluster accepts no writes Correct answer
  • It keeps working normally
  • The remaining member becomes leader and writes
  • The data is deleted

Explanation: Existing workloads keep running but no changes can be made.

2 List only the keys.

Question
etcdctl get /registry/pods/default --prefix ____
Answer
etcdctl get /registry/pods/default --prefix --keys-only

Explanation: --keys-only doesn't print the values.

3 Match.

Term Meaning
etcd Cluster state
Raft Consensus algorithm
quorum Majority
/registry/ Key prefix

All lessons in Expert