Backup and Restore Kubernetes Configurations
Kubernetes configurations such as deployments, services, config maps, and secrets are critical components of your cluster. Backing them up and restoring them ensures you can recover from issues like accidental deletions, failed upgrades, or cluster migrations. This guide provides a detailed bash script to automate the backup and restore of Kubernetes configurations.
Key Features of the Script for Backup and restore Kubernetes configurations
- Backup all Kubernetes resources (deployments, services, config maps, secrets, etc.) for a specified namespace or all namespaces.
- Save backups in organized directories with timestamps.
- Restore configurations from a specific backup file.
- Provide options to verify the backup and restoration process.
Script: kubernetes cluster backup and restore
Below is the bash script with detailed explanations and examples for each step.
#!/bin/bash
# Kubernetes Backup and Restore Script
# Author: [Your Name]
# Date: [Date]
# Version: 1.0
# Icons for visual feedback
CHECK="\u2714" # ✔
CROSS="\u274C" # ✘
INFO="\u2139" # ℹ
# Define backup directory
BACKUP_DIR="./k8s_backup"
# Function: Check if kubectl is installed
function check_kubectl() {
if ! command -v kubectl &> /dev/null; then
echo -e "${CROSS} Error: kubectl is not installed. Please install kubectl to use this script."
exit 1
fi
echo -e "${CHECK} kubectl is installed."
}
# Function: Backup Kubernetes resources
function backup_k8s() {
local namespace=$1
local timestamp=$(date +"%Y%m%d%H%M%S")
local backup_path="$BACKUP_DIR/$timestamp"
mkdir -p "$backup_path"
echo -e "${INFO} Starting backup for namespace: ${namespace:-all namespaces}"
if [[ -z "$namespace" ]]; then
kubectl get all --all-namespaces -o yaml > "$backup_path/all_resources.yaml"
kubectl get configmaps --all-namespaces -o yaml > "$backup_path/configmaps.yaml"
kubectl get secrets --all-namespaces -o yaml > "$backup_path/secrets.yaml"
else
kubectl get all -n "$namespace" -o yaml > "$backup_path/all_resources.yaml"
kubectl get configmaps -n "$namespace" -o yaml > "$backup_path/configmaps.yaml"
kubectl get secrets -n "$namespace" -o yaml > "$backup_path/secrets.yaml"
fi
if [[ $? -eq 0 ]]; then
echo -e "${CHECK} Backup completed successfully. Files saved to $backup_path"
else
echo -e "${CROSS} Error: Backup failed."
exit 1
fi
}
# Function: Restore Kubernetes resources
function restore_k8s() {
local backup_path=$1
if [[ ! -d "$backup_path" ]]; then
echo -e "${CROSS} Error: Backup directory $backup_path does not exist."
exit 1
fi
echo -e "${INFO} Starting restoration from backup: $backup_path"
kubectl apply -f "$backup_path/all_resources.yaml"
kubectl apply -f "$backup_path/configmaps.yaml"
kubectl apply -f "$backup_path/secrets.yaml"
if [[ $? -eq 0 ]]; then
echo -e "${CHECK} Restoration completed successfully."
else
echo -e "${CROSS} Error: Restoration failed."
exit 1
fi
}
# Function: List available backups
function list_backups() {
echo -e "${INFO} Available backups in $BACKUP_DIR:"
ls -1 "$BACKUP_DIR"
}
# Main Menu
echo "=== Kubernetes Backup and Restore Script ==="
check_kubectl
echo "Choose an option:"
echo "1) Backup Kubernetes configurations"
echo "2) Restore Kubernetes configurations"
echo "3) List available backups"
echo "4) Exit"
read -p "Enter your choice: " choice
case $choice in
1)
read -p "Enter namespace to backup (leave blank for all namespaces): " namespace
backup_k8s "$namespace"
;;
2)
list_backups
read -p "Enter the backup directory to restore from: " backup_dir
restore_k8s "$BACKUP_DIR/$backup_dir"
;;
3)
list_backups
;;
4)
echo -e "${CHECK} Exiting script."
exit 0
;;
*)
echo -e "${CROSS} Invalid choice. Exiting."
exit 1
;;
esac
Step-by-Step Explanation
1. Checking Prerequisites
The script checks if kubectl is installed:
if ! command -v kubectl &> /dev/null; then
If kubectl is not found, the script exits with an error message.
2. Backup Kubernetes Configurations
The backup_k8s function:
- Creates a timestamped backup directory.
- Retrieves Kubernetes configurations using
kubectl getcommands. - Saves the output in YAML format.
Backup all namespaces:
kubectl get all --all-namespaces -o yaml > all_resources.yaml
Backup specific namespaces:
kubectl get all -n <namespace> -o yaml > all_resources.yaml
It also backs up config maps and secrets separately for finer control.
3. Restore Kubernetes Configurations
The restore_k8s function applies the backup files:
kubectl apply -f <file.yaml>
This recreates all resources from the YAML backup files.
4. List Available Backups
The script lists available backups in the backup directory:
ls -1 "$BACKUP_DIR"
5. Interactive Menu
The script provides an easy-to-use menu with options to:
- Backup configurations.
- Restore configurations.
- List available backups.
Examples
Backup All Namespaces
- Run the script:
./k8s_backup_restore.sh - Choose the backup option:
Choose an option: 1) Backup Kubernetes configurations 2) Restore Kubernetes configurations 3) List available backups 4) Exit Enter your choice: 1 - Leave the namespace blank for all namespaces:
Enter namespace to backup (leave blank for all namespaces): ✔ Backup completed successfully. Files saved to ./k8s_backup/20250109090000
Restore from a Backup
- Run the script:
./k8s_backup_restore.sh - Choose the restore option:
Choose an option: 1) Backup Kubernetes configurations 2) Restore Kubernetes configurations 3) List available backups 4) Exit Enter your choice: 2 - Select a backup directory:
Available backups in ./k8s_backup: 20250109090000 Enter the backup directory to restore from: 20250109090000 ℹ Starting restoration from backup: ./k8s_backup/20250109090000 ✔ Restoration completed successfully.
Scenarios
Scenario 1: Regular Backups
Schedule this script to run daily using a cron job:
- Open crontab:
crontab -e - Add the cron job:
0 2 * * * /path/to/k8s_backup_restore.sh 1>>/var/log/k8s_backup.log 2>&1
Scenario 2: Cluster Migration
Use this script to backup configurations from one cluster and restore them in another:
- Backup configurations from the source cluster.
- Switch to the target cluster using
kubectl config use-context. - Restore configurations.
Technical Details
- Resource Backup:
kubectl get allfetches deployments, pods, services, etc.kubectl get configmapsandkubectl get secretsensure additional configuration data is preserved.
- Restoration Process:
- YAML files retain the original state, making restoration seamless.
kubectl applyensures resources are created or updated.
- Namespace Management:
- Specifying namespaces prevents unnecessary resources from being backed up.
- Directory Structure: Backups are timestamped for easy identification and rollback.
Benefits of This Approach
- Reliability: Ensures critical Kubernetes configurations are always recoverable.
- Flexibility: Supports namespace-specific and all-namespace backups.
- Automation-Friendly: Can be easily integrated with CI/CD pipelines or cron jobs.
Reference Doc
- kubernetes Documentation
Official Kubernetes documentation, including guides on managing resources and usingkubectl. - Backing Up and Restoring with Velero
Velero is a popular open-source tool for backup and recovery of Kubernetes clusters. - Kubernetes Disaster Recovery Strategies
Google’s guide on disaster recovery for Kubernetes applications. - kubectl Command Cheat Sheet
A quick reference forkubectlcommands, helpful for creating backups and restores. - Cluster Data Management with etcd
Information about etcd, the backend store for Kubernetes. - How to Configure Kubernetes on Ubuntu
