# Backup and Restore a Kubernetes Cluster with 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

* มีสิทธิ์เข้าถึง Kubernetes cluster เวอร์ชัน 1.16 ขึ้นไป และต้องเปิดใช้งาน DNS และ container networking หากต้องการดูเวอร์ชัน Kubernetes ที่รองรับ สามารถดูได้ที่ [compatibility matrix ของ Velero](https://github.com/vmware-tanzu/velero#velero-compatibility-matrix)
* ติดตั้ง `kubectl` ไว้ในเครื่อง local เรียบร้อยแล้ว
* Bucket ที่จะใช้เก็บข้อมูลของ NIPA Cloud และ access key, secret key ของ bucket โดยมีสิทธิ์ขั้นต่ำที่จำเป็นดังนี้
  * `s3:ListBucket` ให้ Velero อ่านไฟล์ใน bucket
  * `s3:GetBucketLocation` ใช้ตรวจสอบ region/location ของ bucket
  * `s3:PutObject` อัปโหลด backup file
  * `s3:GetObject` ดาวน์โหลดข้อมูลเพื่อ restore
  * `s3:DeleteObject` ลบ backup ที่หมดอายุ
  * `s3:AbortMultipartUpload` หยุดการอัปโหลดที่ไม่สมบูรณ์
* ขั้นตอนการสร้าง sub-users และ bucket สามารถดูได้จาก
  * <https://app.gitbook.com/o/b4jKLRni8sRoSESA9D6d/s/q2BYmWTMiaonrEL8QBP2/~/changes/483/storage/object-storage-s3/create-an-object-storage-sub-user>
  * <https://app.gitbook.com/o/b4jKLRni8sRoSESA9D6d/s/q2BYmWTMiaonrEL8QBP2/~/changes/483/storage/object-storage-s3/create-an-object-storage-bucket>

### วิธีติดตั้งและใช้งาน 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/>

```bash
brew install velero
```

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

```bash
velero version
```

***

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

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

1. ดาวน์โหลดไฟล์ `kubeconfig` จากหน้า Rancher dashboard หรือผู้ดูแลระบบ
2. เปิดโปรแกรม [Lens](https://k8slens.dev/)
3. ไปที่เมนู **Add Cluster** → เลือก **Browse** แล้วเลือกไฟล์ `kubeconfig`
4. กด **Add Cluster** เพื่อเชื่อมต่อ
5. ใช้ command ผ่าน Lens Terminal

&#x20;![](https://t9003005656.p.clickup-attachments.com/t9003005656/b65fd9ff-cf11-49a0-b132-ef46ef1b2964/image.png)

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

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

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

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

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

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

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

```bash
export KUBECONFIG=~/kubeconfig
```

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

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

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

```bash
kubectl get nodes
```

![](https://t9003005656.p.clickup-attachments.com/t9003005656/d78357b0-5d7e-4f8c-b455-d99520b5d3fa/image.png)

***

#### 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)

```bash
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**

```bash
velero backup-location get
```

![](https://t9003005656.p.clickup-attachments.com/t9003005656/a98a8ede-137f-46af-84b0-4e5787044c21/image.png)

***

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

การทดสอบของเอกสารนี้จะทำการ backup และ restore resources ของ namespace `test-backup` ภายใน cluster เดียวกัน โดยมีขั้นตอนดังนี้

&#x20;  1\. การสร้าง namspace และ resources ต่างๆ ภายใน namespace

&#x20;  2\. ทำการ velero backup namespace ทั้งหมด

&#x20;  3\. ลบ namespace ทิ้งเพื่อทดสอบ restore

&#x20;  4\. ทำการ velero restore และตรวจสอบ namespace resourcesสร้างไฟล์ test-backup.yaml

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

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

```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

```powershell
kubectl apply -f test-backup.yaml
```

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

```bash
kubectl get pods -n test-backup
```

![](https://t9003005656.p.clickup-attachments.com/t9003005656/2906e3d2-e50d-4866-bb51-c039e985d0a3/image.png)

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

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

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

```
kubectl get pods -n test-backup
```

![](https://t9003005656.p.clickup-attachments.com/t9003005656/af854ed7-f5a9-40a8-85fd-9d6a75d9ecd0/image.png) จะได้ชื่อ pod เช่น `test-ubuntu-xxxxx`

```sql
kubectl exec -it -n test-backup test-ubuntu-xxxxx -- bash
```

1. สร้างไฟล์ทดสอบใน path ที่ mount PVC

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

ตรวจสอบ

```bash
ls -l /mnt
cat /mnt/test.txt
```

![](https://t9003005656.p.clickup-attachments.com/t9003005656/0a816625-7236-4bc6-a926-0e550e10c191/image.png)

1. ออกจาก Pod ด้วยคำสั่ง `exit` หลังจากนั้นให้ทำการ `velero backup create` ตามปกติ

**4.4 สร้าง Backup**

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

ตรวจสอบ Backup

```
velero backup get
```

![](https://t9003005656.p.clickup-attachments.com/t9003005656/36854fa1-c371-48f2-ba52-32c282c758e3/image.png)

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

```
kubectl delete ns test-backup
```

![](https://t9003005656.p.clickup-attachments.com/t9003005656/01784de6-5f43-4dcd-aec6-7b38fb95a0c0/image.png)

**4.6 Restore จาก Backup**

```
velero restore create --from-backup ubuntu-backup
```

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

```
velero restore get
```

![](https://t9003005656.p.clickup-attachments.com/t9003005656/1d1e8a10-224c-45fe-9b08-b4d873a5283a/image.png)&#x20;

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

```
kubectl get all -n test-backup
```

![](https://t9003005656.p.clickup-attachments.com/t9003005656/86cbb67d-f12a-4898-9b0b-c2ad96725515/image.png)

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

&#x20;![](https://t9003005656.p.clickup-attachments.com/t9003005656/c288706c-6a59-43bb-8007-5f0350c1f7ca/image.png)

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

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

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

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

&#x20;![](https://t9003005656.p.clickup-attachments.com/t9003005656/e8e723aa-cbfb-4589-9e2a-ea3396aa997a/image.png)

#### สรุป

* ติดตั้ง Velero Client
* เชื่อมต่อ Cluster ด้วย Lens หรือ Terminal โดยใช้ kubeconfig
* ติดตั้ง Velero Server และตรวจสอบ Backup Location
* ทดสอบสำรองข้อมูล (Backup) และกู้คืน (Restore)

## Tips for Cluster Migration

Velero Doc for Cluster migration : <https://velero.io/docs/v1.8/migration-case/>

***

![](https://t9003005656.p.clickup-attachments.com/t9003005656/4df9c5d7-b693-4b78-9ce6-61fce488038d/image.png)

จากภาพหากต้องการ migration ข้อมูลไปอีก cluster จำเป็นจะต้องติดตั้ง velero และเชื่อมต่อ Bucket เดียวกันในทั้งสอง cluster เพื่อ backup และ restore

**❗ know issue** Velero ต้องถูกติดตั้งใน namespace เดียวกัน ทั้งใน Cluster ต้นทางและปลายทาง

#### คำสั่งที่จำเป็นสำหรับการ backup/restore

ถ้าต้องการ backup ทุก namespace (หรือทั้ง cluster) ไม่ต้องระบุ flag `--include-namespaces`โดย default หากไม่ระบุ filter ใดๆ Velero จะทำการ backup ทรัพยากรทั้งหมดใน cluster อยู่แล้ว รวมถึง cluster-scoped resources เช่น `CRDs`, `ClusterRole`, `PersistentVolumes`

**คำสั่ง backup**

```
velero backup create <your-backup-name>
```

**คำสั่งสำหรับตรวจสอบจาก cluster2 ว่ามองเห็น backup ที่ได้สร้างไว้หรือไม่ (หมายเหตุ โดยปกติ Velero จะใช้เวลา 1 นาที ในการซิงค์ข้อมูล (sync interval) ดังนั้นคุณอาจจะต้องรอสักครู่หลังจาก Backup)**

```
velero backup describe <your-backup-name>
```

**คำสั่ง restore**

```
velero restore create --from-backup <your-backup-name>
```

**คำสั่งดูสถานะโดยรวมของการ Restore: ใช้คำสั่ง `velero restore get` เพื่อดูรายการและสถานะเบื้องต้น (เช่น `Completed`, `Failed`)**

```bash
velero restore get
```

ดูรายละเอียดของการ Restore: นำชื่อของ Restore ที่ได้จากคำสั่งก่อนหน้า มาใช้กับคำสั่ง `describe` เพื่อดูรายละเอียดเชิงลึก, ข้อผิดพลาด (Errors), หรือคำเตือน (Warnings) ที่เกิดขึ้น

```
velero restore describe <ชื่อ-restore-ที่ได้จากคำสั่ง-get>
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs-epc.gitbook.io/ncs-documents/tutorial/kubernetes-on-nipa-cloud-space-with-rancher/backup-and-restore-a-kubernetes-cluster-with-velero.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
