> For the complete documentation index, see [llms.txt](https://close.gitbook.io/yun-wei-bi-ji/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://close.gitbook.io/yun-wei-bi-ji/kubernetes/etcd/cronjob-zi-yuan-kong-zhi-qi-jin-xing-ding-shi-bei-fen.md).

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

### **CronJob资源控制器进行定时备份** <a href="#cronjob-e8-b5-84-e6-ba-90-e6-8e-a7-e5-88-b6-e5-99-a8-e8-bf-9b-e8-a1-8c-e5-ae-9a-e6-97-b6-e5-a4-87-e4" id="cronjob-e8-b5-84-e6-ba-90-e6-8e-a7-e5-88-b6-e5-99-a8-e8-bf-9b-e8-a1-8c-e5-ae-9a-e6-97-b6-e5-a4-87-e4"></a>

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

```bash
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资源清单:** <a href="#e5-88-9b-e5-bb-bacronjob-e8-b5-84-e6-ba-90-e6-b8-85-e5-8d-95-3a" id="e5-88-9b-e5-bb-bacronjob-e8-b5-84-e6-ba-90-e6-b8-85-e5-8d-95-3a"></a>

```bash
kubectl apply -f etcd-database-backup.yaml
```

### **查看詳情** <a href="#e6-9f-a5-e7-9c-8b-e8-a9-b3-e6-83-85" id="e6-9f-a5-e7-9c-8b-e8-a9-b3-e6-83-85"></a>

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

<figure><img src="/files/fbZBa29KPkn2XTUD1yez" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/gzYQPzvAVRDhisGGdP0B" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/FMvzmLWf1nvx9kUkoRDd" alt=""><figcaption></figcaption></figure>
