[kubernetes]-k8s定时备份单节点etcd及故障恢复

导语:前场经常非法关机导致单机的etcd损坏。每次都重装k8s虽然比较简单,但是会相对耗时。

思路:通过cronjob类型去定时备份(cronjob备份可以确保每次备份的时候k8s状态是ok的),通过可执行文件在宿主机上进行恢复。

docker pull bitnami/etcd:3.5

cronjob.yaml如下

不用root去备份会报权限问题。

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  namespace: kube-system
  name: etcd-backup
spec:
  schedule: "0 20 * * *"
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            backup: "etcd"
        spec:
          containers:
          - name: etcd-backup
            image: bitnami/etcd:3.5
            command:
            - sh
            - -c
            - "etcdctl --endpoints 100.100.100.100:2379  \
               --cert=/etc/kubernetes/pki/etcd/server.crt \
               --key=/etc/kubernetes/pki/etcd/server.key  \
               --cacert=/etc/kubernetes/pki/etcd/ca.crt \
               snapshot save   /backup/$(date +%Y%m%d_%H%M%S)_snapshot.db && echo etcd backup sucess"
            #- "export ETCDCTL_API=3; etcdctl --endpoints $ENDPOINT snapshot save /backup/$(date +%Y%m%d_%H%M%S)_snapshot.db; echo etcd backup sucess"
            #ETCDCTL_DEBUG=true etcdctl --endpoints $ENDPOINT snapshot save /tmp/test2_snapshot.db
#            env:
#            - name: ENDPOINT
#              value: "127.0.0.1:2379"
#            securityContext:
#              privileged: true
            securityContext:
              runAsUser: 0
              runAsGroup: 0
              privileged: true
            volumeMounts:
              - name: backup
                mountPath: /backup
              - name: etcd
                mountPath: /etc/kubernetes
                readOnly: true  # 设置为只读
              - mountPath: /etc/localtime
                name: lt-config
              - mountPath: /etc/timezone
                name: tz-config
          restartPolicy: OnFailure
          hostNetwork: true
          nodeSelector:
            kubernetes.io/os: linux
            #node-role.kubernetes.io/control-plane: 'true'
            #node-role.kubernetes.io/control-plane: 'true'
          volumes:
            - name: backup
              persistentVolumeClaim:
                claimName: etcd-pvc
            - name: etcd
              hostPath:
                path: /etc/kubernetes
                type: ''
            - name: lt-config
              hostPath:
                path: /etc/localtime
            - name: tz-config
              hostPath:
                path: /etc/timezone

立即运行cronjob

kubectl -n kube-system  create job --from=cronjob/etcd-backup  manual-etcd-backup

恢复测试

ETCDCTL_API=3 etcdctl snapshot restore /srv/123.db  --name  ubutnuw2204 --initial-cluster ubutnuw2204=https://100.100.100.100:2380 --initial-advertise-peer-urls=https://100.100.100.100:2380 --data-dir /var/lib/etcd

把命令写到脚本里做了下测试,恢复成功

image-20240423144956349

https://liujinye.gitbook.io/openshift-docs/etcd/k8s-1.22-shi-yong-cronjob-bei-fen-etcd

https://developer.aliyun.com/article/704295

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爷来辣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值