CronJob资源控制器进行定时备份

CronJob资源控制器进行定时备份

这里设置了三个 etcd 地址来进行备份,逻辑上只备份一个地址上的数据即可,如果后期出 etcd 数据不一样的情况,可使用同时间段 key 最多的那个备份恢复

cat > etcd-database-backup.yaml <<'EOF'
apiVersion: batch/v1
kind: CronJob 
metadata:
  name: etcd-database-backup
  annotations:
    descript: "etcd数据库定时备份"
spec:
  schedule: "*/5 * * * *"   # 表示每5分钟运行一次
  jobTemplate:
    spec:
      template:
        spec:           
          containers:    
          - name: etcdctl
            image: registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.5.5-0
            env:
            - name: ETCDCTL_API
              value: "3"
            - name: ETCDCTL_CACERT
              value: "/data/kube/ssl/ca.pem"
            - name: ETCDCTL_CERT
              value: "/data/kube/ssl/etcd.pem"
            - name: ETCDCTL_KEY
              value: "/data/kube/ssl/etcd-key.pem"
            command:
            - /bin/sh 
            - -c
            - |
              export RAND=$RANDOM
              etcdctl --endpoints=https://192.168.182.129:2379 snapshot save /backup/etcd-129-${RAND}-snapshot.db
              etcdctl --endpoints=https://192.168.182.130:2379 snapshot save /backup/etcd-130-${RAND}-snapshot.db
              etcdctl --endpoints=https://192.168.182.132:2379 snapshot save /backup/etcd-132-${RAND}-snapshot.db
            volumeMounts: 
            - name: "pki"
              mountPath: "/data/kube/ssl"
            - name: "backup"
              mountPath: "/backup"
            imagePullPolicy: IfNotPresent
          volumes:
          - name: "pki"
            hostPath: 
              path: "/data/kube/ssl"
              type: "DirectoryOrCreate"
          - name: "backup"
            hostPath: 
              path: "/data/kube/backups"  # 数据备份目录
              type: "DirectoryOrCreate"
          nodeSelector:  # 将Pod绑定在主节点之中,否则只能将相关证书放在各个节点能访问的nfs共享存储中,如果固定某一個master节点,那需要多筛选器结合
            node-role.kubernetes.io/master: ""

          tolerations:   # 容忍度
          - key: "node-role.kubernetes.io/master"
            operator: "Exists"
            effect: "NoSchedule"  # operator: "Exists" , 忽略 effect 值
          restartPolicy: Never
EOF

创建cronjob资源清单:

kubectl apply -f etcd-database-backup.yaml

查看詳情

kubectl logs -f pod/etcd-database-backup-xxxx-xxx

Last updated