Velero 笔记

Velero 的一些笔记

Posted by Ksd on December 21, 2024

Minio plugin

启动 Minio:

 docker run -itd    -p 31900:9000    -p 31901:9001    --name minio    -v /opt/minio/data:/data    -e "MINIO_ROOT_USER=admin"    -e "MINIO_ROOT_PASSWORD=Rancher@123"    minio/minio:RELEASE.2023-04-28T18-11-17Z server /data --console-address ":9001"

安装 velero:

velero install \
    --provider aws \
    --plugins velero/velero-plugin-for-aws:v1.11.0 \
    --bucket velero \
    --secret-file ./credentials-velero \
    --use-volume-snapshots=false \
    --features=EnableCSI \
    --backup-location-config region=cn-sy-1,s3ForcePathStyle="true",s3Url=http://10.201.170.127:31900

基于 longhorn 做夸集群备份恢复(保留块存储数据)

只恢复 nginx-example 命名空间的数据作为测试。

安装 Longhorn

A/B 集群分别安装 Longhorn,并且配置相同的 Backup Target ,本例使用的是 minio,截图如下:

A/B 集群分别安装 velero

需要使用 --features=EnableCSI 启用 CSI 支持:

velero install \
    --provider aws \
    --plugins velero/velero-plugin-for-aws:v1.11.0 \
    --bucket velero \
    --secret-file ./credentials-velero \
    --use-volume-snapshots=false \
    --features=EnableCSI \
    --backup-location-config region=cn-sy-1,s3ForcePathStyle="true",s3Url=http://10.201.170.127:31900

A/B 集群创建 VolumeSnapshotClass

# VolumeSnapshotClass.yaml
kind: VolumeSnapshotClass
apiVersion: snapshot.storage.k8s.io/v1
metadata:
  name: longhorn
driver: driver.longhorn.io
deletionPolicy: Delete

A 集群创建示例业务

A 集群:

# with-pv.yaml
---
apiVersion: v1
kind: Namespace
metadata:
  name: nginx-example
  labels:
    app: nginx

---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: nginx-logs
  namespace: nginx-example
  labels:
    app: nginx
spec:
  # Optional:
  # storageClassName: longhorn
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 50Mi

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: nginx-example
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
      annotations:
        pre.hook.backup.velero.io/container: fsfreeze
        pre.hook.backup.velero.io/command: '["/sbin/fsfreeze", "--freeze", "/var/log/nginx"]'
        post.hook.backup.velero.io/container: fsfreeze
        post.hook.backup.velero.io/command: '["/sbin/fsfreeze", "--unfreeze", "/var/log/nginx"]'
    spec:
      volumes:
        - name: nginx-logs
          persistentVolumeClaim:
           claimName: nginx-logs
      containers:
      - image: nginx:1.17.6
        name: nginx
        ports:
        - containerPort: 80
        volumeMounts:
          - mountPath: "/var/log/nginx"
            name: nginx-logs
            readOnly: false
      - image: ubuntu:bionic
        name: fsfreeze
        securityContext:
          privileged: true
        volumeMounts:
          - mountPath: "/var/log/nginx"
            name: nginx-logs
            readOnly: false
        command:
          - "/bin/bash"
          - "-c"
          - "sleep infinity"


---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: nginx
  name: my-nginx
  namespace: nginx-example
spec:
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: nginx
  type: NodePort

创建备份

A 集群:

velero backup create nginx-example-all
velero backup describe nginx-example-all # 查看备份状态

可以在 longhorn UI 中看见新创建了一个备份,这个备份是通过 velero 创建:

恢复到 B 集群

B 集群执行:

velero get backups # 确认 A 集群的备份在 B 集群能看见

恢复 nginx-example namespace 的数据:

velero restore create --from-backup nginx-example-all --include-namespaces nginx-example

最后,可以在 B 集群查看 pv pvc pod 等数据,而且 volume 的数据是从 A 集群通过备份在 B 集群自动创建的。