使用 Velero 备份 & 恢复 K8s 集群

使用 Velero 备份 k8s 集群


获取 Velero 安装命令,开源项目地址: Velero

参考文档: 官方文档


创建 velero namespace

kubectl create ns velero

使用storageclass为Minio 创建PVC

---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: minio-data
  namespace: velero
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi
  storageClassName: "test-nas"
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: minio-config
  namespace: velero
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi
  storageClassName: "test-nas"

部署 Minio 对象存储

---
apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: velero
  name: minio
  labels:
    component: minio
spec:
  strategy:
    type: Recreate
  selector:
    matchLabels:
      component: minio
  template:
    metadata:
      labels:
        component: minio
    spec:
      volumes:
      - name: storage
        persistentVolumeClaim:
          claimName: minio-data
      - name: config
        persistentVolumeClaim:
          claimName: minio-config
      containers:
      - name: minio
        image: minio/minio:latest
        imagePullPolicy: IfNotPresent
        args:
        - server
        - /storage
        - --config-dir=/config
        env:
        - name: MINIO_ACCESS_KEY
          value: "minio"
        - name: MINIO_SECRET_KEY
          value: "minio123"
        ports:
        - containerPort: 9000
        volumeMounts:
        - name: storage
          mountPath: "/storage"
        - name: config
          mountPath: "/config"

---
apiVersion: v1
kind: Service
metadata:
  namespace: velero
  name: minio
  labels:
    component: minio
spec:
  # ClusterIP is recommended for production environments.
  # Change to NodePort if needed per documentation,
  # but only if you run Minio in a test/trial environment, for example with Minikube.
  type: NodePort
  ports:
    - port: 9000
      targetPort: 9000
      protocol: TCP
  selector:
    component: minio

---
apiVersion: batch/v1
kind: Job
metadata:
  namespace: velero
  name: minio-setup
  labels:
    component: minio
spec:
  template:
    metadata:
      name: minio-setup
    spec:
      restartPolicy: OnFailure
      volumes:
      - name: config
        emptyDir: {}
      containers:
      - name: mc
        image: minio/minio-mc:latest
        imagePullPolicy: IfNotPresent
        command:
        - /bin/sh
        - -c
        - "mc --config-dir=/config config host add velero http://minio:9000 minio minio123 && mc --config-dir=/config mb -p velero/velero"
        volumeMounts:
        - name: config
          mountPath: "/config"

创建 minio credentials-velero 认证文件

cat >> credentials-velero << EOF
[default]
aws_access_key_id = minio
awk_secret_access_key = minio123
EOF

使用 Velero 命令创建Velero 容器


./velero install --provider aws --plugins velero/velero-plugin-for-aws:v1.2.1 --bucket velero --secret-file ./credentials-velero --use-volume-snapshots=false --backup-location-config region=minio,s3ForcePathStyle="true",s3Url=http://minio.velero.svc:9000 --image velero/velero:v1.8.1-rc.1 --plugins velero/velero-plugin-for-aws:v1.2.1

创建定时备份

velero create schedule cluster-backup --schedule="@every 24h" --ttl 240h0m0s
# 查看备份任务
velero get schedule
# 恢复集群备份
velero restore create --from-backup cluster-backup
velero restore get
# 清理备份任务
velero backup delete cluster-backup
# 删除 velero
kubectl delete namespace/velero clusterrolebinding/velero
kubectl delete crds -l component=velero
kubectl delete -f examples/nginx-app/base.yaml

你可能感兴趣的:(使用 Velero 备份 & 恢复 K8s 集群)