Geavanceerd
Zoeken inInstallatiegidsen
Zo zoek je
  • Plak een foutmelding tussen aanhalingstekens, bijv. "permission denied", om de oplossingstabel te vinden.
  • Kies een gebied en een deel van de gids (installeren, eerste instellingen, dagelijks gebruik, beveiliging, oplossingen) om direct naar de juiste stap te gaan.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

Gids 59etcd · Kubernetes

Install and set up an etcd cluster

etcd is a small, strongly consistent key-value store. It is the brain of every Kubernetes cluster (all objects live in it) and is also used by Patroni, CoreDNS, APISIX and many other systems for configuration and leader election. This guide builds a 3-node etcd cluster with TLS on RHEL or Ubuntu from the official release, runs it with systemd, and covers the most important operations: health, backup and restore, compaction and defragmentation, and adding or replacing members.

  • 9 Lessen
  • 13 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

59.1

Know it before you install

etcd uses the Raft protocol: the members elect a leader; a write is done when a majority (quorum) agrees. With 3 members, 1 may fail; with 5, 2 may fail. Always use an odd number. etcd is very sensitive to disk latency (fsync) — use SSDs.

ItemValue
Filesetcd, etcdctl (client), etcdutl (offline tools) from github.com/etcd-io/etcd/releases
Config/etc/etcd/etcd.conf.yml
Data/var/lib/etcd/
Ports2379/tcp (clients), 2380/tcp (peers)
Serviceetcd.service (you write it)
In kubeadm clustersetcd runs as a static Pod on control plane nodes (/etc/kubernetes/manifests/etcd.yaml)

59.2

Install on RHEL and Ubuntu

On each of the 3 nodes (etcd1 10.0.3.21, etcd2 .22, etcd3 .23):

EVER=v3.6.5
curl -fsSLO https://github.com/etcd-io/etcd/releases/download/$EVER/etcd-$EVER-linux-amd64.tar.gz
tar -xzf etcd-$EVER-linux-amd64.tar.gz
install -m 0755 etcd-$EVER-linux-amd64/etcd etcd-$EVER-linux-amd64/etcdctl etcd-$EVER-linux-amd64/etcdutl /usr/local/bin/
useradd --system --no-create-home --shell /sbin/nologin etcd
mkdir -p /etc/etcd/pki /var/lib/etcd
chown etcd: /var/lib/etcd && chmod 700 /var/lib/etcd
etcd --version

59.3

Certificates

etcd holds everything (in Kubernetes: all Secrets) — always use TLS with client certificates. Make a small CA and one certificate per node (with its name and IP in the SANs), for example with openssl or cfssl:

openssl req -x509 -newkey rsa:4096 -nodes -days 3650 -subj "/CN=etcd-ca" -keyout ca.key -out ca.crt
openssl req -newkey rsa:2048 -nodes -subj "/CN=etcd1" -keyout etcd1.key -out etcd1.csr
openssl x509 -req -in etcd1.csr -CA ca.crt -CAkey ca.key -CAcreateserial -days 825 -out etcd1.crt -extfile <(printf "subjectAltName=DNS:etcd1,IP:10.0.3.21,IP:127.0.0.1\nextendedKeyUsage=serverAuth,clientAuth")

Copy ca.crt, the node's .crt and .key to /etc/etcd/pki/ on that node (chown etcd:; keys 600). Keep ca.key offline.

59.4

First configuration: the cluster file

On etcd1 (change name, IPs and cert names on the other nodes):

/etc/etcd/etcd.conf.yml
name: etcd1
data-dir: /var/lib/etcd
listen-peer-urls: https://10.0.3.21:2380
listen-client-urls: https://10.0.3.21:2379,https://127.0.0.1:2379
initial-advertise-peer-urls: https://10.0.3.21:2380
advertise-client-urls: https://10.0.3.21:2379
initial-cluster: etcd1=https://10.0.3.21:2380,etcd2=https://10.0.3.22:2380,etcd3=https://10.0.3.23:2380
initial-cluster-state: new
initial-cluster-token: etcd-prod-1
client-transport-security:
  cert-file: /etc/etcd/pki/etcd1.crt
  key-file: /etc/etcd/pki/etcd1.key
  trusted-ca-file: /etc/etcd/pki/ca.crt
  client-cert-auth: true
peer-transport-security:
  cert-file: /etc/etcd/pki/etcd1.crt
  key-file: /etc/etcd/pki/etcd1.key
  trusted-ca-file: /etc/etcd/pki/ca.crt
  client-cert-auth: true
quota-backend-bytes: 8589934592
auto-compaction-mode: periodic
auto-compaction-retention: "1h"
SettingWhy
initial-cluster (same on all)All members know each other at the first start.
initial-cluster-state: newFirst creation. Use existing when a member joins a running cluster.
client-cert-auth: trueOnly clients with a certificate from your CA may connect.
quota-backend-bytes (8 GiB)The database size limit; at the limit etcd raises a NOSPACE alarm.
auto-compaction-*Old key versions are removed every hour, so the DB does not grow forever.
/etc/systemd/system/etcd.service
[Unit]
Description=etcd
After=network-online.target
 
[Service]
User=etcd
ExecStart=/usr/local/bin/etcd --config-file /etc/etcd/etcd.conf.yml
Restart=on-failure
LimitNOFILE=65536
 
[Install]
WantedBy=multi-user.target
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.3.0/24" port port="2379-2380" protocol="tcp" accept'
firewall-cmd --reload
systemctl daemon-reload
systemctl enable --now etcd

Start all three within a short time (the first waits for the others).

59.5

Daily use: health, backup and maintenance

Make the client settings short with environment variables:

export ETCDCTL_ENDPOINTS=https://10.0.3.21:2379,https://10.0.3.22:2379,https://10.0.3.23:2379
export ETCDCTL_CACERT=/etc/etcd/pki/ca.crt ETCDCTL_CERT=/etc/etcd/pki/etcd1.crt ETCDCTL_KEY=/etc/etcd/pki/etcd1.key
etcdctl endpoint health --cluster
etcdctl endpoint status --cluster -w table
etcdctl member list -w table
etcdctl alarm list
etcdctl put /app/config/mode "maintenance"
etcdctl get /app/config --prefix

Backup (from one healthy member) and restore (offline, with etcdutl):

etcdctl --endpoints=https://127.0.0.1:2379 snapshot save /backup/etcd-$(date +%F-%H%M).db
etcdutl snapshot status /backup/etcd-2026-11-17-0200.db -w table
systemctl stop etcd
etcdutl snapshot restore /backup/etcd-2026-11-17-0200.db --name etcd1 --initial-cluster etcd1=https://10.0.3.21:2380,etcd2=https://10.0.3.22:2380,etcd3=https://10.0.3.23:2380 --initial-advertise-peer-urls https://10.0.3.21:2380 --data-dir /var/lib/etcd-restored

(Restore on every member with its own --name, point data-dir to the restored folder, then start all.) Defragment one member at a time after big deletes, to give disk space back:

etcdctl --endpoints=https://10.0.3.22:2379 defrag
etcdctl alarm disarm

Real uses at work:

  • The datastore of Kubernetes (stacked in kubeadm, or an external cluster like this one).
  • Configuration and leader election for Patroni (PostgreSQL HA), APISIX, CoreDNS and your own services.
  • Regular snapshots are your cluster disaster recovery.

59.6

Make it safe

Security checklist:

  • TLS for clients and peers with client-cert-auth: true; CA key offline.
  • Firewall 2379/2380 to cluster members and API servers only.
  • Encrypted snapshots, stored off the nodes, with restore tests.
  • Odd member count (3 or 5), on separate hosts/zones, on SSDs.
  • Monitor: leader changes, etcd_disk_wal_fsync_duration_seconds, DB size, alarms.
  • In Kubernetes also encrypt Secrets at rest (API server EncryptionConfiguration) — etcd stores them in plain text otherwise.

59.7

Update and remove

Upgrade one member at a time (one minor version at a time): stop, replace the binaries, start, check endpoint health, then the next. Take a snapshot first. Remove a member from a running cluster:

etcdctl member remove 8e9e05c52164694d
systemctl disable --now etcd
rm -rf /var/lib/etcd

59.8

Fix common problems

ProblemCauseFix
etcdserver: mvcc: database space exceeded / alarm NOSPACEDB reached quota-backend-bytes.Compact (etcdctl compact REV), defrag each member, alarm disarm; raise the quota if needed.
Many leader changes, apply request took too longSlow disk or overloaded node.SSDs; check fsync latency; no other heavy I/O on the node.
x509: certificate is valid for …, not …The cert SANs miss the name/IP used.New certificate with all names and IPs.
Cluster has no leader / requests time outQuorum lost (2 of 3 down).Bring members back; if impossible, restore from a snapshot.
A rebuilt member will not join: cluster ID mismatchOld data folder or wrong initial-cluster-state.member remove + member add, empty data dir, start with initial-cluster-state: existing.
context deadline exceeded from etcdctlWrong endpoint, firewall, or missing client certs.Check ETCDCTL_* variables and port 2379.

59.9

Next steps

1Why does an etcd cluster have 3 or 5 members, not 4?

Writes need a majority. 4 members tolerate only 1 failure, the same as 3, but cost more.

2etcd raised a NOSPACE alarm. What are the steps?

Compact, defragment every member, then etcdctl alarm disarm (and raise the quota if needed).

Alle commando's in dit hoofdstuk