Velero

How To Backup and Restore a Kubernetes Cluster on NIPA Cloud Object Storage Using Velero

บทความสอนการติดตั้งและใช้งาน Velero สำหรับสำรองและกู้คืน Kubernetes Cluster บน NIPA Cloud


Overview

Velero (ชื่อเดิม Heptio Ark) เป็นเครื่องมือสำหรับ Backup และ Restore ทรัพยากร (Resources) ต่าง ๆ ใน Kubernetes Cluster รวมถึง Persistent Volumes คุณสามารถใช้งาน Velero ได้ทั้งบน Cloud Provider หรือ On-premises โดย Velero ช่วยให้คุณสามารถ

  • Backup ข้อมูลของ Cluster และ Restore ได้ในกรณีที่ข้อมูลสูญหาย

  • Migrate ทรัพยากรของ Cluster ไปยัง Cluster อื่น

  • Replicate Cluster Production ไปยัง Cluster สำหรับ Development และ Testing

  • มีฟีเจอร์ Schedule Backups ที่ให้คุณสามารถตั้งเวลาสำรองข้อมูลได้ตามที่ต้องการ โดยใช้รูปแบบ cron expression

  • มีฟีเจอร์ Backup Hook ที่ให้คุณสามารถรันคำสั่งบางอย่างภายใน container ของ pod ก่อนหรือหลังกระบวนการสำรองข้อมูลได้ Velero ประกอบด้วยส่วนประกอบหลักดังนี้

  • Server ที่รันอยู่บน Kubernetes Cluster

  • Command-line Client ที่รันบนเครื่อง Local

Prerequisites

วิธีติดตั้งและใช้งาน Velero เพื่อสำรองและกู้คืน Kubernetes Cluster บน NIPA Cloud

1. ติดตั้ง Velero Client

Velero Client คือเครื่องมือที่ใช้สั่งงานกับ Velero Server บน Kubernetes Cluster สามารถเลือกติดตั้งได้ตามระบบปฏิบัติการที่ใช้ Velero Docs Installation : https://velero.io/docs/v1.16/basic-install/

brew install velero

ตรวจสอบเวอร์ชันหลังติดตั้ง

velero version

2. วิธีเข้าใช้งาน Cluster

2.1 นำไฟล์ kubeconfig มาใส่ใน Lens

  1. ดาวน์โหลดไฟล์ kubeconfig จากหน้า Rancher dashboard หรือผู้ดูแลระบบ

  2. เปิดโปรแกรม Lens

  3. ไปที่เมนู Add Cluster → เลือก Browse แล้วเลือกไฟล์ kubeconfig

  4. กด Add Cluster เพื่อเชื่อมต่อ

  5. ใช้ command ผ่าน Lens Terminal

2.2 วิธีเข้าใช้งาน Kubernetes ผ่าน Terminal

  1. ดาวน์โหลดไฟล์ kubeconfig

ดาวน์โหลดไฟล์ kubeconfig จากหน้า Rancher dashboard หรือจากผู้ดูแลระบบ

  1. นำไฟล์ kubeconfig ไปไว้ในเครื่อง

ตัวอย่างเช่น เก็บไว้ที่โฟลเดอร์ ~ (home directory) หรือโฟลเดอร์ที่ต้องการ

  1. ตั้งค่า Environment Variable เพื่อใช้งาน kubeconfig

ใช้คำสั่งนี้ใน terminal เพื่อชี้ไปยังไฟล์ kubeconfig ที่ดาวน์โหลดมา

export KUBECONFIG=~/kubeconfig

หมายเหตุ: ให้เปลี่ยน ~/kubeconfig เป็น path ที่ตรงกับไฟล์ของคุณ

  1. ทดสอบการเชื่อมต่อ Kubernetes Cluster

ใช้คำสั่งนี้เพื่อตรวจสอบว่าเชื่อมต่อสำเร็จ

kubectl get nodes

3. ติดตั้ง Velero Server และตรวจสอบ Backup Location

3.1 เตรียม secret-file ./credentials-velero สำหรับ Access เข้า Object Storage

[default]
aws_access_key_id = YOUR_ACCESS_KEY
aws_secret_access_key = YOUR_SECRET_KEY

3.2 ติดตั้ง Velero Server

ตัวอย่างการติดตั้งบน Kubernetes Cluster (เช่น ใช้ S3-compatible storage)

velero install \
  --provider aws \
  --plugins velero/velero-plugin-for-aws:v1.0.0 \
  --bucket <ชื่อ bucket บน NIPA Cloud> \
  --secret-file ./credentials-velero \
  --backup-location-config region=nipa-cloud,s3ForcePathStyle="true",s3Url=https://s3-bkk.nipa.cloud \
  --default-volumes-to-fs-backup \
  --use-volume-snapshots=true \
  --use-node-agent
  • --provider aws กำหนดให้ Velero ใช้ปลั๊กอินสำหรับ AWS S3 API (หรือ S3-compatible) เช่น NIPA Ceph Rados Gateway

  • --plugins velero/velero-plugin-for-aws:v1.0.0 โหลด plugin สำหรับการเชื่อมต่อ S3 API (รวมถึง S3-compatible storage) เพื่อใช้กับ --provider aws

  • --bucket <velero-bucket-name> ระบุชื่อ bucket ที่จะใช้สำหรับเก็บ backup/restore (ต้องสร้างไว้ล่วงหน้า)

  • --secret-file ./credentials-velero ระบุ path ของไฟล์ที่เก็บ AWS Access Key และ Secret Key สำหรับเชื่อมต่อ S3 (ในรูปแบบไฟล์ config แบบ AWS SDK)

  • --backup-location-config กำหนดการเชื่อมต่อเพิ่มเติมสำหรับ bucket เช่น:

    • region=nipa-cloud ชื่อ region ที่ใช้ (ไม่จำเป็นต้องตรงกับ AWS จริงๆ)

    • s3ForcePathStyle=true บังคับให้ใช้ path-style S3 URL (สำคัญมากกับ Ceph หรือ NIPA)

    • s3Url=https://s3-bkk.nipa.cloud ชี้ไปยัง endpoint ของ NIPA Object Storage

    • --default-volumes-to-fs-backup บอกให้ Velero ใช้วิธี filesystem backup แทน snapshot ถ้า PVC ไม่สามารถ snapshot ได้ (เช่นใน Ceph ที่ไม่มี CSI snapshot controller)

    • --use-volume-snapshots=true พยายามใช้ snapshot (ผ่าน CSI plugin) ถ้าสามารถทำได้ (ถ้าใช้ Ceph ที่มี CSI + snapshotter จะได้ benefit จากข้อนี้)

    • --use-node-agent สั่งให้ Velero ติดตั้ง node-agent daemonset เพื่อให้สามารถ backup PVC แบบ filesystem ได้แบบกระจายตาม node แทนการใช้ Restic แบบเดิม (มีประสิทธิภาพดีกว่า และเป็นแนวทางใหม่ของ Velero)

3.2 ตรวจสอบ Backup Location

velero backup-location get

4. ทดสอบการ Backup และ Restore (จาก YAML ด้านล่าง)

4.1 Apply Deployment และ Resource ที่ใช้ทดสอบ

สร้างไฟล์ test-backup.yaml

# Copyright 2017 the Velero contributors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

---
apiVersion: v1
kind: Namespace
metadata:
  name: test-backup
  labels:
    app: nginx

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: volume-a
  namespace: test-backup
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
  storageClassName: standard-ssd
---
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    backup.velero.io/backup-volumes: vol-xbqnt
  generation: 2
  labels:
    app: ubuntu
  name: test-ubuntu
  namespace: test-backup

spec:
  replicas: 1
  selector:
    matchLabels:
      app: ubuntu
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: ubuntu
      namespace: test-backup
    spec:
      containers:
        - command:
            - /bin/tail
            - '-f'
          image: ubuntu:24.04
          imagePullPolicy: Always
          name: ubuntu
          volumeMounts:
            - mountPath: /mnt/
              name: vol-xbqnt
      volumes:
        - name: vol-xbqnt
          persistentVolumeClaim:
            claimName: volume-a
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: ubuntu
  name: my-nginx
  namespace: test-backup
spec:
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: ubuntu
  type: ClusterIP

test-backup.yaml ประกอบด้วย:

  • Namespace: test-backup

  • PVC: volume-a

  • Deployment: test-ubuntu ที่ใช้ ubuntu:24.04

  • Volume Mount /mnt และ PVC ที่ชื่อ vol-xbqnt

  • Service: my-nginx ชนิด ClusterIP run command kubectl

kubectl apply -f test-backup.yaml

4.2 ตรวจสอบว่า Pod สร้างสำเร็จ:

kubectl get pods -n test-backup

4.3 ขั้นตอนสร้างไฟล์ใน PVC (ผ่าน Pod ที่ Mount PVC อยู่)

มีเข้าไปทดสอบสร้างไฟล์ ไว้บน pvc ด้วย จะได้ทดสอบว่ามีการ backup ไฟล์ใน volume ไปด้วยหรือไม่

  1. เข้าไปยัง Pod ที่รันอยู่

kubectl get pods -n test-backup

จะได้ชื่อ pod เช่น test-ubuntu-xxxxx

kubectl exec -it -n test-backup test-ubuntu-xxxxx -- bash
  1. สร้างไฟล์ทดสอบใน path ที่ mount PVC

cd /mnt
echo "this is backup test" > test.txt
mkdir subdir
echo "nested file" > subdir/nested.txt

ตรวจสอบ

ls -l /mnt
cat /mnt/test.txt
  1. ออกจาก Pod ด้วยคำสั่ง exit หลังจากนั้นให้ทำการ velero backup create ตามปกติ

4.4 สร้าง Backup

velero backup create ubuntu-backup --include-namespaces test-backup

ตรวจสอบ Backup

velero backup get

4.5 ลบ namespace เพื่อลอง restore

kubectl delete ns test-backup

4.6 Restore จาก Backup

velero restore create --from-backup ubuntu-backup

ตรวจสอบว่า Restore สำเร็จ

velero restore get

ตรวจสอบว่า Restore สำเร็จ

kubectl get all -n test-backup

ตรวจสอบข้อมูลใน Volume

4.7 ตรวจสอบ log หากเกิดปัญหา

velero backup logs ubuntu-backup #ดูได้จาก velero backup get
velero restore logs <ชื่อ restore> #ดูได้จาก velero restore get

4.8 เข้าไปตรวจสอบข้อมูลที่ Object Storage Client

ภาพจากโปรแกรม Cyberduck

สรุป

  • ติดตั้ง Velero Client

  • เชื่อมต่อ Cluster ด้วย Lens หรือ Terminal โดยใช้ kubeconfig

  • ติดตั้ง Velero Server และตรวจสอบ Backup Location

  • ทดสอบสำรองข้อมูล (Backup) และกู้คืน (Restore)

Last updated

Was this helpful?