https://etcd.io/docs/v3.5/op-guide/container/

安装

  1. export NODE1=192.168.1.21
  2. docker volume create --name etcd-data
  3. export DATA_DIR="etcd-data"
  4. REGISTRY=quay.io/coreos/etcd
  5. docker run -d \
  6. -p 2379:2379 \
  7. -p 2380:2380 \
  8. --volume=${DATA_DIR}:/etcd-data \
  9. --name etcd ${REGISTRY}:latest \
  10. /usr/local/bin/etcd \
  11. --data-dir=/etcd-data --name node1 \
  12. --initial-advertise-peer-urls http://${NODE1}:2380 --listen-peer-urls http://0.0.0.0:2380 \
  13. --advertise-client-urls http://${NODE1}:2379 --listen-client-urls http://0.0.0.0:2379 \
  14. --initial-cluster node1=http://${NODE1}:2380

访问

  1. # API 版本为 3
  2. export ETCDCTL_API=3
  3. ENDPOINTS=127.0.0.1:2379
  4. etcdctl --endpoints=$ENDPOINTS member list

使用

  1. # 简单使用
  2. etcdctl --endpoints=$ENDPOINTS put foo "Hello World!"
  3. etcdctl --endpoints=$ENDPOINTS get foo
  4. etcdctl --endpoints=$ENDPOINTS --write-out="json" get foo
  5. # 前缀匹配
  6. etcdctl --endpoints=$ENDPOINTS put web1 value1
  7. etcdctl --endpoints=$ENDPOINTS put web2 value2
  8. etcdctl --endpoints=$ENDPOINTS put web3 value3
  9. etcdctl --endpoints=$ENDPOINTS get web --prefix
  10. # 删除
  11. etcdctl --endpoints=$ENDPOINTS put key myvalue
  12. etcdctl --endpoints=$ENDPOINTS del key
  13. etcdctl --endpoints=$ENDPOINTS put k1 value1
  14. etcdctl --endpoints=$ENDPOINTS put k2 value2
  15. etcdctl --endpoints=$ENDPOINTS del k --prefix
  16. # 交互式操作
  17. etcdctl --endpoints=$ENDPOINTS put user1 bad
  18. etcdctl --endpoints=$ENDPOINTS txn --interactive
  19. compares:
  20. value("user1") = "bad"
  21. success requests (get, put, delete):
  22. del user1
  23. failure requests (get, put, delete):
  24. put user1 good
  25. # 租约
  26. etcdctl --endpoints=$ENDPOINTS lease grant 300
  27. # lease 2be7547fbc6a5afa granted with TTL(300s)
  28. etcdctl --endpoints=$ENDPOINTS put sample value --lease=2be7547fbc6a5afa
  29. etcdctl --endpoints=$ENDPOINTS get sample
  30. etcdctl --endpoints=$ENDPOINTS lease keep-alive 2be7547fbc6a5afa
  31. etcdctl --endpoints=$ENDPOINTS lease revoke 2be7547fbc6a5afa
  32. # or after 300 seconds
  33. etcdctl --endpoints=$ENDPOINTS get sample
  34. # 锁
  35. etcdctl --endpoints=$ENDPOINTS lock mutex1
  36. # another client with the same name blocks
  37. etcdctl --endpoints=$ENDPOINTS lock mutex1
  38. # leader 选举
  39. etcdctl --endpoints=$ENDPOINTS elect one p1
  40. # another client with the same name blocks
  41. etcdctl --endpoints=$ENDPOINTS elect one p2
  42. # etcd 状态
  43. etcdctl --write-out=table --endpoints=$ENDPOINTS endpoint status
  44. # 备份 etcd
  45. ENDPOINTS=$HOST_1:2379
  46. etcdctl --endpoints=$ENDPOINTS snapshot save my.db
  47. etcdctl --write-out=table --endpoints=$ENDPOINTS snapshot status my.db

Kubernetes etcd 备份

https://kubernetes.io/zh/docs/tasks/administer-cluster/configure-upgrade-etcd/

  1. kubectl -n kube-system get pods etcd-name -o=jsonpath='{.spec.containers[0].command}' | jq
  2. ETCDCTL_API=3 etcdctl snapshot save <backup-file-location> \
  3. --endpoints=https://127.0.0.1:2379 \
  4. --cacert=<trusted-ca-file> \
  5. --cert=<cert-file> \
  6. --key=<key-file>
  7. # 进入容器
  8. kubectl -n kube-system exec -it etcd-vm-12-3-centos -- sh
  9. ETCDCTL_API=3 etcdctl snapshot save /tmp/etcdBackup.db \
  10. --endpoints=https://10.0.12.3:2379 \
  11. --cacert=/etc/kubernetes/pki/etcd/ca.crt \
  12. --cert=/etc/kubernetes/pki/etcd/server.crt \
  13. --key=/etc/kubernetes/pki/etcd/server.key
  14. # 查看状态
  15. ETCDCTL_API=3 \
  16. etcdctl --write-out=table snapshot status /tmp/etcdBackup.db

image.png

恢复

说明: 如果集群中正在运行任何 API 服务器,则不应尝试还原 etcd 的实例。相反,请按照以下步骤还原 etcd:

  • 停止 所有 API 服务实例
  • 在所有 etcd 实例中恢复状态
  • 重启所有 API 服务实例

我们还建议重启所有组件(例如 kube-scheduler、kube-controller-manager、kubelet),以确保它们不会 依赖一些过时的数据。请注意,实际中还原会花费一些时间。 在还原过程中,关键组件将丢失领导锁并自行重启。

  1. export ETCDCTL_CACERT=/etc/kubernetes/pki/etcd/ca.crt
  2. export ETCDCTL_CERT=/etc/kubernetes/pki/etcd/server.crt
  3. export ETCDCTL_KEY=/etc/kubernetes/pki/etcd/server.key
  4. export ETCDCTL_API=3
  5. etcdctl snapshot restore /tmp/etcdBackup.db