K8s Etcd备份与恢复

kubenetes使用Etcd数据库存储集群中的数据,安全起见,一定要备份.

kubeadm 方式部署:

备份

ETCDCTL_API=3 etcdctl snapshot save /opt/etcd.db \
--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

#### 演示:
[root@k8s-master-1 ~]# ETCDCTL_API=3 etcdctl snapshot save /opt/etcd.db \
> --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
Snapshot saved at /opt/etcd.db

恢复

# 1 先暂停kube-apiserver和etcd容器:
mv /etc/kubernetes/manifests /etc/kubernetes/manifests.bak
mv /var/lib/etcd /var/lib/etcd.bak

# 2 恢复etcd数据
ETCDCTL_API=3 etcdctl snapshot restore \
/opt/etcd.db --data-dir=/var/lib/etcd 

# 3 启动kube-apiserver 和 etcd容器:
mv /etc/kubernetes/manifests.bak /etc/kubernetes/manifests


#### 演示:
[root@k8s-master-1 ~]# mv /etc/kubernetes/manifests /etc/kubernetes/manifests.bak
[root@k8s-master-1 ~]# mv /var/lib/etcd /var/lib/etcd.bak

[root@k8s-master-1 ~]# ETCDCTL_API=3 etcdctl snapshot restore /opt/etcd.db \
--data-dir=/var/lib/etcd 
2023-01-18 07:01:42.935583 I | mvcc: restore compact to 7026586
2023-01-18 07:01:42.942566 I | etcdserver/membership: added member 8e9e05c52164694d [http://localhost:2380] to cluster cdf818194e3a8c32

[root@k8s-master-1 ~]# mv /etc/kubernetes/manifests.bak /etc/kubernetes/manifests

验证

#### 验证(等待一会儿让容器启动):
[root@k8s-master-1 ~]# kubectl get node,deployment,svc,pod -o wide
NAME                STATUS   ROLES           AGE   VERSION   INTERNAL-IP     EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION           CONTAINER-RUNTIME
node/k8s-master-1   Ready    control-plane   55d   v1.25.1   192.168.3.101   <none>        CentOS Linux 7 (Core)   3.10.0-1160.el7.x86_64   docker://20.10.21
node/k8s-node-1     Ready    <none>          55d   v1.25.1   192.168.3.104   <none>        CentOS Linux 7 (Core)   3.10.0-1160.el7.x86_64   docker://20.10.21
node/k8s-node-2     Ready    <none>          55d   v1.25.1   192.168.3.105   <none>        CentOS Linux 7 (Core)   3.10.0-1160.el7.x86_64   docker://20.10.21
node/k8s-node-3     Ready    <none>          55d   v1.25.1   192.168.3.106   <none>        CentOS Linux 7 (Core)   3.10.0-1160.el7.x86_64   docker://20.10.21

NAME                      READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS                          IMAGES                                              SELECTOR
deployment.apps/counter   3/3     3            3           21d   count,count-log-1,count-log-2,ngs   busybox:1.28,busybox:1.28,busybox:1.28,nginx:1.17   app=ng
deployment.apps/www       1/1     1            1           43m   nginx                               nginx                                               app=www

NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)           AGE   SELECTOR
service/counter      NodePort    10.104.53.36     <none>        80:31946/TCP      21d   app=ng
service/java-demo    NodePort    10.97.29.88      <none>        8080:32390/TCP    45d   app=java-demo
service/kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP           55d   <none>
service/memcached    NodePort    10.98.147.155    <none>        11211:32466/TCP   24d   app_env_stage=dev
service/nginx-d      NodePort    10.104.101.166   <none>        80:30280/TCP      24d   app_env_stage=dev
service/redis        NodePort    10.111.151.222   <none>        6379:32040/TCP    24d   app_env_stage=dev

NAME                           READY   STATUS    RESTARTS   AGE     IP               NODE         NOMINATED NODE   READINESS GATES
pod/counter-765fd887f9-4m9ml   4/4     Running   0          12d     10.244.140.104   k8s-node-2   <none>           <none>
pod/counter-765fd887f9-5qkz2   4/4     Running   0          7d22h   10.244.140.110   k8s-node-2   <none>           <none>
pod/counter-765fd887f9-qjlwz   4/4     Running   0          12d     10.244.140.106   k8s-node-2   <none>           <none>
pod/www-7d6cb45fbb-9zk2r       1/1     Running   0          43m     10.244.109.115   k8s-node-1   <none>           <none>

你可能感兴趣的:(k8s,kubernetes,kubernetes,etcd)