Lesson 78 · Section 7: Security
Lesson 78: The persistent key/value store: keeping etcd safe
Why etcd is the most sensitive part of the cluster, how mutual TLS and a separate certificate authority limit who can reach it, which files on the node give the same access, and how to check and repair all of it. · 16 min read · 4 min video
Official competencies this lesson covers
Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.
Why the store needs protecting
You have met etcd three times already: as a key-value store, as the place where Kubernetes keeps its state, and
through etcdctl. This lesson is in the Security section because of one consequence of that design. Everything you
are learning to configure here, such as certificate requests, kubeconfig users, roles and role bindings, service
accounts and Secrets, is in the end data in etcd.
That makes etcd the one place where all of the cluster's protection can be skipped at once.
Parts:
- User or Pod (token or certificate)
- kube-apiserver (authn, RBAC, admission)
- etcd (every object and Secret)
- etcd client cert (signed by the etcd CA)
- etcdctl (straight to port 2379)
Connections:
- User or Pod to kube-apiserver: request
- kube-apiserver to etcd: allowed
- etcd client cert to etcdctl: used by
- etcdctl to etcd: no RBAC, no audit
A request that comes through the API server is authenticated, checked against RBAC, passed through admission and written to the audit log. A client that connects to etcd directly meets none of this. etcd does not know what a Role is. It sees keys and values, and it answers whoever it has let in.
| With direct access to etcd, someone can | Because |
|---|---|
| Read every Secret in every namespace | Secrets are stored as values, in clear text unless encryption at rest is on |
| Read service account data and configuration of every workload | Every API object is a key under /registry |
Give themselves cluster-admin | A ClusterRoleBinding is just one more key that can be written |
| Delete the cluster | etcdctl del --prefix /registry needs no confirmation from the API server |
| Leave no trace | Audit logging happens in the API server, which was never asked |
What protects it
There is no single setting called "etcd security". There are several ways to the data, and each one has its own lock.
Parts:
- Client port 2379 (client cert required)
- kube-apiserver (can encrypt Secrets)
- Peer port 2380 (peer cert required)
- etcd (all cluster state)
- /var/lib/etcd (mode 700, root only)
- pki/etcd/*.key (the CA key mints access)
- Snapshot files (a full copy of the data)
Connections:
- Client port 2379 to etcd
- kube-apiserver to etcd
- Peer port 2380 to etcd
- /var/lib/etcd to etcd
- pki/etcd/*.key to etcd
- Snapshot files to etcd
| Way in | Lock | Where you check it |
|---|---|---|
| Client port 2379 | TLS, and a client certificate signed by the etcd CA | --client-cert-auth, --trusted-ca-file in etcd.yaml |
| Peer port 2380 | TLS, and a peer certificate signed by the etcd CA | --peer-client-cert-auth, --peer-trusted-ca-file |
| The network path to both ports | Listen addresses and a firewall: only control plane nodes need to reach etcd | --listen-client-urls, host firewall rules |
The data directory /var/lib/etcd | File permissions: mode 700, owned by root on a kubeadm cluster | stat /var/lib/etcd |
The private keys in /etc/kubernetes/pki | File permissions: mode 600, owned by root | ls -l /etc/kubernetes/pki/etcd |
| Snapshot files | Whatever you do with them: they hold everything the database holds | Where your backups are written and copied |
| Any copy of the data | Encryption at rest, done by the API server before it writes | --encryption-provider-config in kube-apiserver.yaml |
Notice that only the first two locks are cryptographic. The rest depend on who can log in to a control plane node and who can read your backups. That is why "who has root on the control plane" and "who can read etcd" are the same question.
How the API server and etcd trust each other
Parts:
- kube-apiserver (the client)
- etcd (--client-cert-auth)
- Client certificate (apiserver-etcd-client)
- etcd CA (pki/etcd/ca.crt)
- Server certificate (pki/etcd/server.crt)
Connections:
- kube-apiserver and etcd: TLS :2379
- etcd CA to Client certificate: signs
- etcd CA to Server certificate: signs
- Client certificate to kube-apiserver: shown by
- Server certificate to etcd: shown by
The connection uses mutual TLS. In ordinary TLS only the server proves who it is. Here both sides do:
- The API server connects to
https://127.0.0.1:2379. - etcd shows its server certificate (
pki/etcd/server.crt). The API server checks it against the CA named in--etcd-cafile. This stops the API server from sending the cluster's data to something that only pretends to be etcd. - etcd asks for a client certificate, because it runs with
--client-cert-auth=true. The API server showsapiserver-etcd-client.crt. etcd checks it against the CA named in--trusted-ca-file. - Only when both checks pass is the encrypted connection set up. Everything after that, including every Secret that travels between the two, is encrypted in transit.
These are the certificates kubeadm creates for etcd, all signed by the etcd CA:
File under /etc/kubernetes/pki | Used by | For |
|---|---|---|
etcd/ca.crt, etcd/ca.key | Everything below | The etcd certificate authority |
etcd/server.crt, .key | etcd | Serving clients on port 2379 |
etcd/peer.crt, .key | etcd | Talking to other members on port 2380, in both directions |
etcd/healthcheck-client.crt, .key | etcdctl, health checks | A client pair with no other job |
apiserver-etcd-client.crt, .key | kube-apiserver | The API server's client pair (one folder up, next to its other certificates) |
And these are the flags that put them to work:
# /etc/kubernetes/manifests/etcd.yaml
- --cert-file=/etc/kubernetes/pki/etcd/server.crt
- --key-file=/etc/kubernetes/pki/etcd/server.key
- --client-cert-auth=true
- --trusted-ca-file=/etc/kubernetes/pki/etcd/ca.crt
- --peer-cert-file=/etc/kubernetes/pki/etcd/peer.crt
- --peer-key-file=/etc/kubernetes/pki/etcd/peer.key
- --peer-client-cert-auth=true
- --peer-trusted-ca-file=/etc/kubernetes/pki/etcd/ca.crt
# /etc/kubernetes/manifests/kube-apiserver.yaml
- --etcd-servers=https://127.0.0.1:2379
- --etcd-cafile=/etc/kubernetes/pki/etcd/ca.crt
- --etcd-certfile=/etc/kubernetes/pki/apiserver-etcd-client.crt
- --etcd-keyfile=/etc/kubernetes/pki/apiserver-etcd-client.key
Why etcd has its own certificate authority
With --client-cert-auth, the question etcd asks a client is only this: is your certificate signed by a CA I
trust? In a Kubernetes cluster there are no user names or roles behind that question. Every certificate that
passes gets full read and write access.
Now imagine etcd trusted the cluster CA, pki/ca.crt. That CA signs the certificate of every kubelet, of the
scheduler, of the controller manager, and of every user you create with the Certificates API. Each of them could
then open etcd and read every Secret. A separate CA that signs only a handful of certificates keeps the list of
"who may talk to etcd" short enough to read in one ls.
Under the hood · Does etcd have users and roles of its own?
It does. etcd can be run with its own authentication, with users, roles and permissions on key ranges, and with
--client-cert-auth it can take the user name from the certificate's common name. Kubernetes does not use any of
it, and the Kubernetes documentation states that etcd authentication is not planned for Kubernetes. The API server
needs the whole /registry tree anyway, and it is meant to be the only client. So in a cluster the model stays
simple: the etcd CA decides who gets in, and whoever gets in can do everything.
Under the hood · What is still reachable without a certificate
- The metrics port. kubeadm starts etcd with
--listen-metrics-urls=http://127.0.0.1:2381. It is plain HTTP, but it listens on localhost only and serves health and metrics, not keys. The Pod's probes use it. - The client port from other machines. kubeadm lists the node's own address in
--listen-client-urls, next to127.0.0.1, so port 2379 is reachable from the network. A caller still needs a certificate, but a firewall that admits only the control plane nodes to 2379 and 2380 removes the chance to try. - The peer port. Members replicate everything to each other on 2380, so it carries the same data. It is protected by the peer flags, the same way the client port is protected by the client flags. On a cluster with a single member nobody connects to it, and the flags are set all the same.
Protection at rest: files, snapshots and encryption
TLS protects data while it travels. It does nothing for data that sits on a disk.
The data directory. /var/lib/etcd holds the database file. Anyone who can copy that folder has the cluster's
state, and can read it offline on their own machine with no certificate at all. kubeadm creates it with mode 700
for root, and that is the whole protection.
Snapshots. A snapshot made with etcdctl snapshot save is the same database in one file. It usually leaves the
node: to a backup server, to object storage, sometimes to a laptop. Treat it like the root password of the
cluster. Write it to a folder only root can read, and encrypt it wherever it is stored.
Encryption at rest. The last layer makes a stolen copy less useful. The API server can encrypt chosen
resources, normally Secrets, before it writes them to etcd, using keys from a file named in
--encryption-provider-config. etcd never sees the clear text and never has the key. The lesson
Encrypting Secret Data at Rest in the Application Lifecycle Management section shows how to set it up. What
matters here is where it fits:
| Layer | Protects against | Does not protect against |
|---|---|---|
| Mutual TLS, separate etcd CA | Other machines and components connecting to etcd | Someone with root on a control plane node |
| File permissions on the node | Ordinary users on the node | Root, or a Pod with a hostPath mount |
| Encryption at rest | Reading Secrets from a stolen disk, data directory or snapshot | Anyone who also has the encryption key file, or who can ask the API server |
| RBAC | API users reading Secrets they should not see | Anyone who gets around the API server |
Using it
Example · Audit how etcd is protected, in one minute
A task asks whether etcd accepts unauthenticated clients, or which CA it trusts. Read it, do not guess. On the control plane node:
sudo grep -E 'cert-auth|trusted-ca|listen-(client|peer|metrics)' /etc/kubernetes/manifests/etcd.yaml
- --client-cert-auth=true
- --listen-client-urls=https://127.0.0.1:2379,https://192.168.56.10:2379
- --listen-metrics-urls=http://127.0.0.1:2381
- --listen-peer-urls=https://192.168.56.10:2380
- --peer-client-cert-auth=true
- --peer-trusted-ca-file=/etc/kubernetes/pki/etcd/ca.crt
- --trusted-ca-file=/etc/kubernetes/pki/etcd/ca.crt
What to look for: both cert-auth flags are true, both CA flags name the etcd CA, and the client and peer
URLs start with https. Then the API server's half and the files:
sudo grep -- '--etcd-' /etc/kubernetes/manifests/kube-apiserver.yaml
sudo stat -c '%a %U %n' /var/lib/etcd /etc/kubernetes/pki/etcd/*.key
700 root /var/lib/etcd
600 root /etc/kubernetes/pki/etcd/ca.key
600 root /etc/kubernetes/pki/etcd/healthcheck-client.key
600 root /etc/kubernetes/pki/etcd/peer.key
600 root /etc/kubernetes/pki/etcd/server.key
Example · Prove that etcd turns strangers away
First try without a client certificate. The CA is given, so etcdctl trusts the server, but it has nothing to
show:
sudo etcdctl --cacert=/etc/kubernetes/pki/etcd/ca.crt endpoint health
https://127.0.0.1:2379 is unhealthy: failed to commit proposal: context deadline exceeded
Error: unhealthy cluster
etcd is not unhealthy. It refused the handshake, and etcdctl kept retrying until it ran out of time. Now a pair
signed by the wrong CA, the cluster's own:
sudo etcdctl --cacert=/etc/kubernetes/pki/etcd/ca.crt \
--cert=/etc/kubernetes/pki/apiserver-kubelet-client.crt \
--key=/etc/kubernetes/pki/apiserver-kubelet-client.key endpoint health
The result is the same, even though that certificate makes the API server a full administrator towards every kubelet. For etcd it comes from an unknown CA. Finally, a pair from the etcd CA:
sudo etcdctl --cacert=/etc/kubernetes/pki/etcd/ca.crt \
--cert=/etc/kubernetes/pki/etcd/healthcheck-client.crt \
--key=/etc/kubernetes/pki/etcd/healthcheck-client.key endpoint health
https://127.0.0.1:2379 is healthy: successfully committed proposal: took = 9.1ms
To test a certificate without connecting at all, ask openssl whether the etcd CA signed it:
sudo openssl verify -CAfile /etc/kubernetes/pki/etcd/ca.crt /etc/kubernetes/pki/apiserver-etcd-client.crt
/etc/kubernetes/pki/apiserver-etcd-client.crt: OK
Example · See what a reader of etcd gets
Create a Secret, then read it the way someone with etcd access would, without going through the API:
kubectl -n shop create secret generic db-password --from-literal=password=S3cr3t-Value
kubectl -n kube-system exec etcd-controlplane -- etcdctl \
--cacert=/etc/kubernetes/pki/etcd/ca.crt \
--cert=/etc/kubernetes/pki/etcd/healthcheck-client.crt \
--key=/etc/kubernetes/pki/etcd/healthcheck-client.key \
get /registry/secrets/shop/db-password | strings | grep -i -A1 password
db-password
--
password
S3cr3t-Value
No RBAC rule was consulted, and the audit log has no entry for this read. If the value had been encrypted at rest,
the output would instead begin with k8s:enc: and the name of the provider, followed by unreadable bytes.
Notice also what made this possible: the right to exec into a Pod in kube-system. Keep that right, and the
right to create Pods there, for cluster administrators only.
Example · Repair an API server that etcd no longer accepts
kubectl answers The connection to the server 192.168.56.10:6443 was refused. On the control plane node:
sudo crictl ps -a --name 'kube-apiserver|etcd'
CONTAINER IMAGE STATE NAME ATTEMPT
3f1c0a9d2b7e 6f2d1e0c4a5b Exited kube-apiserver 7
b82e4d6f0a13 a9e1f3c5d7b2 Running etcd 0
etcd runs, the API server keeps exiting. Its log says why:
sudo crictl logs 3f1c0a9d2b7e 2>&1 | tail -5
... "transport: authentication handshake failed: tls: failed to verify certificate: x509: certificate signed by unknown authority"
The API server does not trust the certificate etcd shows, so --etcd-cafile is the suspect:
sudo grep -- '--etcd-' /etc/kubernetes/manifests/kube-apiserver.yaml
- --etcd-cafile=/etc/kubernetes/pki/ca.crt
- --etcd-certfile=/etc/kubernetes/pki/apiserver-etcd-client.crt
- --etcd-keyfile=/etc/kubernetes/pki/apiserver-etcd-client.key
- --etcd-servers=https://127.0.0.1:2379
The CA is the cluster's, not etcd's. Change the path to /etc/kubernetes/pki/etcd/ca.crt, save, and the kubelet
starts a new API server within a minute. Watch it with sudo crictl ps --name kube-apiserver.
The mirror image of this fault looks different. If the API server's client pair is wrong or expired, the API
server log shows a handshake failure with bad certificate, and the etcd log (crictl logs on the etcd
container) shows a rejected connection from 127.0.0.1. Then check --etcd-certfile and --etcd-keyfile, and
the dates with sudo kubeadm certs check-expiration.
Tricks
Common mistakes
- Believing RBAC protects etcd. RBAC decides what the API server will do for a caller. It has no effect on a
client that reaches port 2379 or reads
/var/lib/etcd. - Pointing an etcd flag at the cluster CA.
--etcd-cafile=/etc/kubernetes/pki/ca.crtstops the API server with "certificate signed by unknown authority".--trusted-ca-file=/etc/kubernetes/pki/ca.crton etcd is worse: it locks the API server out and lets every component certificate in. - Turning off
--client-cert-authto make a command work. The command works, and so does everybody else's. Pass the three TLS flags toetcdctlinstead. - Forgetting the peer flags. Members send each other the full data on port 2380. Peer TLS needs its own
--peer-*flags. - Treating base64 as protection. Without encryption at rest, a Secret is readable in etcd, in the data directory and in every snapshot.
- Guarding the cluster but not the backups. A snapshot on a shared drive gives away what all the certificates were protecting.
- Letting workloads mount control plane paths. A Pod with a
hostPathto/etc/kubernetes/pkior/var/lib/etcdon a control plane node has etcd.
Recap
- All of the API server's checks (authentication, RBAC, admission, audit) sit in front of etcd. Direct access to etcd skips them all.
- The API server and etcd use mutual TLS on port 2379. etcd accepts only certificates signed by its own CA, and every accepted certificate has full access.
- The flags are
--client-cert-auth,--trusted-ca-fileand the--peer-*set on etcd, and--etcd-cafile,--etcd-certfile,--etcd-keyfileon the API server. - The data directory, the private keys and every snapshot give the same access as the network. Access to a control plane node is access to etcd.
- Encryption at rest, set up on the API server, keeps Secrets unreadable in stolen copies of the data.
Official docs
Quiz: check what you learned
5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.
Sign in to take the quiz and keep your score.
Practise: try it on a real cluster
A hands-on lab for this lesson. You get goals, not steps: any solution that reaches them passes.
Talk to the cluster's etcd with etcdctl: identify the member, find the key behind an object, read what etcd really stores for a Secret, and ask the server about itself.
Investigate · Level 2 · standard · target 20 min
Discussion
Learners ask and answer questions here. Sign in to read and join the discussion.