DEV Community

Cover image for Kubernetes Effortless Migration Using Velero
Shuhaib K K
Shuhaib K K

Posted on

Kubernetes Effortless Migration Using Velero

Introduction

In the dynamic world of cloud-native applications, Kubernetes has emerged as the go-to container orchestration platform. As organizations embrace the benefits of Kubernetes, there comes a time when migrating clusters becomes a necessity. However, the process of migrating Kubernetes clusters can be complex and error-prone without the right tools.
Enter Velero, an open-source tool that simplifies the migration process by ensuring data consistency, preserving application state, and enabling seamless cluster transitions. In this blog post, we'll explore the power of Velero and how it can help you migrate Kubernetes clusters with ease.

In our case we will be using GKE as a source and destination cluster.

Setup Velero

First you need to setup velero in your local or cloudshell in GCP.

  • Download velero binary from the following link

  • Copy velero binary to bin directory

sudo cp velero /usr/local/bin
Enter fullscreen mode Exit fullscreen mode
Velero Cluster Setup
  • Create a GCS Bucket
BUCKET=<backup-bucket>
gsutil mb gs://$BUCKET
Enter fullscreen mode Exit fullscreen mode
  • Create a Service account with for velero
gcloud iam service-accounts create velero --display-name “Velero service account”
Enter fullscreen mode Exit fullscreen mode
  • Set the $EMAIL variable to match its email value.
EMAIL=$(gcloud iam service-accounts list --filter=”displayName:Velero service account” --format ‘value(email)’)
Enter fullscreen mode Exit fullscreen mode
  • Store the project id to variable PROJECT_ID
PROJECT_ID=$(gcloud config get-value project)
Enter fullscreen mode Exit fullscreen mode
  • Attach necessary permissions to the service account.
ROLE_PERMISSIONS=(
 compute.disks.get
 compute.disks.create
 compute.disks.createSnapshot
 compute.snapshots.get
 compute.snapshots.create
 compute.snapshots.useReadOnly
 compute.snapshots.delete
 compute.zones.get
)

gcloud iam roles create velero.server \ 
--project $PROJECT_ID \ 
--title “Velero Server” \
--permissions “$(IFS=”,”; echo “${ROLE_PERMISSIONS[*]}”)”

gcloud projects add-iam-policy-binding $PROJECT_ID \
--member serviceAccount:$EMAIL \
--role projects/$PROJECT_ID/roles/velero.server


gsutil iam ch serviceAccount:$SERVICE_ACCOUNT_EMAIL:objectAdmin gs://${BUCKET}
Enter fullscreen mode Exit fullscreen mode
  • Install Velero into the source cluster and start the deployment. This will create a namespace called velero.
velero install \ 
--provider gcp \ 
--plugins velero/velero-plugin-for-gcp:v1.7.0 \
--bucket $BUCKET \
--secret-file ./service-account.json

Enter fullscreen mode Exit fullscreen mode

For more details on GCP plugin you can visit this link.

Create Velero Backup

Login to source GKE cluster.

gcloud container clusters get-credentials source-cluster — zone <my-zone> --project <my-project-id>
Enter fullscreen mode Exit fullscreen mode
velero backup create <backup-name>
Enter fullscreen mode Exit fullscreen mode

you can even exclude namespaces or even backup only specific namespaces. Please visit here for more details.

Some examples for filtering resources
     velero backup create <backup-name> --include-namespaces <namespace>
Enter fullscreen mode Exit fullscreen mode
     velero backup create <backup-name> --include-resources deployments
Enter fullscreen mode Exit fullscreen mode

To list backups

velero backup get
Enter fullscreen mode Exit fullscreen mode

Describe backup

velero backup describe <backup-name>
Enter fullscreen mode Exit fullscreen mode

Create Velero Restore

  • Login to Destination Cluster
gcloud container clusters get-credentials destination-cluster --zone <my-zone> --project <my-project-id>
Enter fullscreen mode Exit fullscreen mode
  • Install velero plugin on the cluster by executing below command.
velero install \ 
--provider gcp \ 
--plugins velero/velero-plugin-for-gcp:v1.7.0 \
--bucket $BUCKET \
--secret-file ./service-account.json
Enter fullscreen mode Exit fullscreen mode
  • Create a manual restore from backup
velero restore create <restore-name> --from-backup <backup-name>
Enter fullscreen mode Exit fullscreen mode
  • To retrieve restores
velero restore get
Enter fullscreen mode Exit fullscreen mode
  • To describe and retrieve restore logs
velero restore describe <restore-name>

velero restore logs <restore-name>
Enter fullscreen mode Exit fullscreen mode
  • Verify all the resources whether it got properly deployed on the destination cluster.

This is how we managed to migrated our Kubernetes clusters from different region or zones.

Top comments (2)

Collapse
 
chindu_grey profile image
Chindu.⚫️ | chindu_grey.lens 🌿

Superb document. As someone new to Kubernetes , this seems to be really helpful for me

Collapse
 
rashidpokkat profile image
rashidpokkat

It is a great article😊😊😊, easy to understand.