k8s安裝之etcd備份還原yaml

etcd備份還原方案,這種比較高級。html

使用docker,自動化處理。node

若是單節點備份,ETCD_ENDPOINTS一個便可。docker

若是多節點恢復,依次執行恢復腳本便可。api

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: backup
  namespace: kube-system
spec:
  # activeDeadlineSeconds: 100
  schedule: "0 0 * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: backup
            # Same image as in /etc/kubernetes/manifests/etcd.yaml
            image: harbor.xxx.cn/3rd_part/k8s.gcr.io/etcd:3.3.10
            env:
            - name: ETCDCTL_API
              value: "3"
            - name: ETCD_ENDPOINTS
              value: "https://1.1:2379,https://1.2:2379,https://1.3:2379"
            command: ["/bin/sh"]
            args: ["-c", "etcdctl --endpoints=${ETCD_ENDPOINTS} --cacert=/etc/kubernetes/pki/etcd/ca.crt --cert=/etc/kubernetes/pki/etcd/healthcheck-client.crt --key=/etc/kubernetes/pki/etcd/healthcheck-client.key snapshot save /backup/etcd-ha-snapshot-$(date +%Y-%m-%d_%H:%M:%S_%Z).db"]
            volumeMounts:
            - mountPath: /etc/kubernetes/pki/etcd
              name: etcd-certs
              readOnly: true
            - mountPath: /backup
              name: backup
          restartPolicy: OnFailure
          nodeSelector:
            node-role.kubernetes.io/master: ""
          tolerations:
          - key: "node-role.kubernetes.io/master"
            effect: "NoSchedule"
          hostNetwork: true
          volumes:
          - name: etcd-certs
            hostPath:
              path: /etc/kubernetes/pki/etcd
              type: DirectoryOrCreate
          - name: backup
            hostPath:
              path: /tmp/etcd_backup/
              type: DirectoryOrCreate

主要參考url:url

http://www.javashuo.com/article/p-bkborffv-gp.htmlspa

至關於把c,etcd單節點數據恢復操做三次便可。但要注意,在做數據恢復以前,千萬必定確定要停掉整個etcd集羣的服務!!!!切記切記切記!!!!而且要平時或是找一個相似環境演練,做到膽大心細!!!rest

    若是已有備份數據,在只有etcd數據損壞的下,可根據如下步驟進行恢復。code

1, 將/etc/kubernetes/manifests/ kube-apiserver.yaml文件裏的鏡像版本更改,中止kube-api server服務。server

2, 將/etc/kubernetes/manifests/ etcd.yaml文件裏的鏡像版本更改,中止etcd server服務。htm

3, 運行以下命令,將損壞的數據文件移至其它地方。

mv /var/lib/etcd/* /tmp/

4, 運行如下命令,以臨時docker運行的方式,將數據從備份裏恢復到/var/lib/etcd/。

docker run --rm \

    -v '/tmp:/backup' \

    -v '/var/lib/etcd:/var/lib/etcd' \

    --env ETCDCTL_API=3 \

    'k8s.gcr.io/etcd-amd64:3.1.12' \

/bin/sh -c "etcdctl snapshot restore '/backup/etcd-snapshot-xxx_UTC.db' ; mv /default.etcd/member/ /var/lib/etcd/"

[上面的命令中,假定咱們已將待還原數據放置於/tmp/目錄下]

5, 改回/etc/kubernetes/manifests/kube-apiserver.yaml文件裏的鏡像版本,恢復etcd server服務。

6, 改回/etc/kubernetes/manifests/etcd.yaml文件裏的鏡像版本,恢復kube-api server服務。

相關文章
相關標籤/搜索