Disaster Recovery (DR) is an essential strategy in modern Kubernetes-based architectures to ensure business continuity and safeguard data integrity. This implementation demonstrates how to synchronize 100 Persistent Volume Claims (PVCs), distributed across namespaces in an Azure Kubernetes Service (AKS) cluster, from the primary region (UK South) to the secondary region (UK West). Leveraging Azure DevOps (ADO) pipelines, Azure Storage, and automation scripts, we achieve a scalable, secure, and efficient DR solution.
This guide explores the architecture, implementation, challenges encountered, and the strategies used to overcome them using standard DevOps and cloud-native practices.
Challenge: Managing 100 clients, each deployed in separate namespaces, with unique PVCs and data requirements.
Solution:
pvc_mappings.yaml
) to create a one-to-one relationship between source and target PVCs.Challenge: Ensuring consistent replication of PVC data to the secondary region with minimal latency and overhead.
Solution:
Challenge: Protecting sensitive data during replication across regions.
Solution:
Challenge: Avoiding data discrepancies, incomplete transfers, or failures during sync.
Solution:
azcopy
, a high-performance data transfer tool, to synchronize blob data efficiently.The DR setup involves:
pvc_mappings.yaml
) for source and target PVCs.azcopy sync
commands to replicate data between storage accounts.The Azure DevOps pipeline (azure-pipelines.yml
) automates the sync process with the following key sections:
Triggers and Scheduling: Defines branch-based triggers and a 15-minute sync interval using cron:
Secure SAS Token Retrieval: Uses the Azure Key Vault task to fetch the SAS token securely:
Sync Execution:
Executes the sync-pvcs.sh
script, passing the SAS token as an argument:
The pvc_mappings.yaml
serves as a configuration layer to map source PVCs (primary region) to target PVCs (secondary region).
Example YAML Structure:
Scalability: New mappings can be added without modifying the script logic.
The sync-pvcs.sh
script orchestrates the data sync process:
All configurations (pipeline, PVC mappings) are defined declaratively.
This solution demonstrates a scalable and robust DR mechanism for Kubernetes workloads using AKS and Azure DevOps. With automated PVC syncing, secure data handling, and regular monitoring, it achieves minimal downtime and data loss in the event of a regional outage. This architecture can be further extended for larger-scale Kubernetes deployments or integrated with other cloud services for enhanced DR capabilities.
#!/bin/bashsas_token=$1mappings_file="pvc_mappings.yaml"echo "Reading PVC mappings from $mappings_file..."if [[ ! -f "$mappings_file" ]]; thenecho "ERROR: PVC mappings file not found!"exit 1fifor mapping in $(cat $mappings_file | grep -v "^#" | awk '{print $1}'); dosource_pvc=$(echo $mapping | cut -d':' -f1)target_pvc=$(echo $mapping | cut -d':' -f2)echo "Syncing $source_pvc to $target_pvc..."azcopy sync \"https://primary.blob.core.windows.net/$source_pvc?$sas_token" \"https://secondary.blob.core.windows.net/$target_pvc?$sas_token" \--recursiveif [ $? -ne 0 ]; thenecho "ERROR: Sync failed for $source_pvc to $target_pvc"fidone
yamlCopy codepvcMappings:pvc-11a239c8-3a14-434b-b9bb-e9f6945f01bf: pvc-517fae97-3c99-42a4-9c08-85d886e0bd5d
yamlCopy code- script: |chmod +x sync-pvcs.sh./sync-pvcs.sh $(SASToken) displayName: "Run PVC Sync Script"
yamlCopy code- task: AzureKeyVault@2inputs:azureSubscription: 'AzureServiceConnection'KeyVaultName: 'MyKeyVault'SecretsFilter: 'SASToken'RunAsPreJob: true
yamlCopy codetrigger:branches:include:- mainschedules:- cron: "*/15 * * * *" # Run every 15 minutesdisplayName: "15-Min PVC Sync"branches:include:- mainalways: true
# Trigger pipeline on push to 'main' branch or on scheduletrigger:branches:include:- main# Schedule to run the pipeline every hour (you can adjust this)schedules:- cron: "*/15 * * * *" # Sync every 15 minutesdisplayName: "15-Min PVC Sync"branches:include:- mainalways: truepool:name: Livcast-prod # Use Ubuntu-based agent; modify if you want to use anothersteps:# Step 1: Fetch the SAS Token from Azure Key Vault- task: AzureKeyVault@1displayName: 'Fetch the token'inputs:azureSubscription: 'ADO-TF-SP-Livcast-PROD'KeyVaultName: 'kv-Livcast-prod'SecretsFilter: 'pvc-sas-token'RunAsPreJob: true# Step 2: Check if AzCopy is installed, else install it- script: |if ! command -v azcopy &> /dev/nullthenecho "AzCopy not found. Installing AzCopy..."wget -O azcopy.tar https://aka.ms/downloadazcopy-v10-linuxtar -xf azcopy.tar --strip-components=1sudo mv ./azcopy /usr/bin/azcopyelseecho "AzCopy already installed."fidisplayName: 'Ensure AzCopy is Installed'# Step 3: Check if yq is installed, else install it- script: |if ! command -v yq &> /dev/nullthenecho "yq not found. Installing yq..."sudo wget https://github.com/mikefarah/yq/releases/download/v4.9.8/yq_linux_amd64 -O /usr/bin/yqsudo chmod +x /usr/bin/yqelseecho "yq already installed."fidisplayName: 'Ensure yq is Installed'# Step 4: Convert line endings of sync-pvcs.sh- script: |echo "Converting line endings of sync-pvcs.sh..."sed -i 's/\r$//' sync-pvcs.shdisplayName: 'Convert Line Endings to Unix'- script: |echo "Fetching SAS token from Key Vault..."sas_token=$(echo $PVC_SAS_TOKEN) # This retrieves the SAS token from Key Vaultecho "Using SAS Token: $sas_token"echo "Starting PVC sync process for 100 clients..."set -e # Exit script on error# Run your sync script, passing the SAS token as an argumentbash ./sync-pvcs.sh $sas_token# Check the exit status and log the resultif [ $? -eq 0 ]; thenecho "PVC sync process completed successfully."elseecho "ERROR: PVC sync process failed!" >&2exit 1fidisplayName: 'Sync PVC Data with SAS from Key Vault'env:PVC_SAS_TOKEN: $(pvc-sas-token) # Make sure the token is passed as an environment variablecontinueOnError: false # Pipeline should fail on error
pvcMappings:pvc-11a239c8-3a14-434b-b9bb-e9f6945f01bf: pvc-517fae97-3c99-42a4-9c08-85d886e0bd5d# Add the rest of your 100 PVC mappings
#!/bin/bash# Accept the SAS token as an argumentsas_token=$1# Load the PVC mappings from YAML filemappings_file="pvc_mappings.yaml"# sas_token="?sv=2022-11-02&ss=f&srt=sco&sp=rwdlc&se=2024-10-31T14:31:21Z&st=2024-10-04T06:31:21Z&spr=https&sig=5b7OmzE4bE4ZTOaLmUjbdYEiBc6lqlTAtauRrk9WJ8s%3D"echo "Reading PVC mappings from $mappings_file..."# Check if the mappings file existsif [[ ! -f "$mappings_file" ]]; thenecho "ERROR: PVC mappings file not found!"exit 1fi# Read the PVC mappings from the YAML file under the 'pvcMappings' keydeclare -A pvc_mappingswhile IFS= read -r line; doif [[ "$line" =~ ^[[:space:]]*pvc- ]]; thenprimary_pvc=$(echo $line | cut -d ":" -f1 | xargs) # Get the primary PVC IDsecondary_pvc=$(echo $line | cut -d ":" -f2 | xargs) # Get the secondary PVC IDpvc_mappings[$primary_pvc]=$secondary_pvcfidone < <(yq eval '.pvcMappings' "$mappings_file")echo "Loaded PVC mappings:"for primary_pvc in "${!pvc_mappings[@]}"; doecho "$primary_pvc -> ${pvc_mappings[$primary_pvc]}"done# Debugging: Check if mappings are loadedif [ ${#pvc_mappings[@]} -eq 0 ]; thenecho "No PVC mappings loaded! Please check the format of pvc_mappings.yaml."exit 1fi# Loop through all primary PVCs and sync to their corresponding secondary PVCsfor primary_pvc in "${!pvc_mappings[@]}"; dosecondary_pvc=${pvc_mappings[$primary_pvc]}echo "Syncing data from primary PVC ($primary_pvc) to secondary PVC ($secondary_pvc)..."# Sync data from primary PVC (file share) to secondary PVC (file share) using the same SAS tokenazcopy copy "https://primary.file.core.windows.net/$primary_pvc$sas_token" \"https://primary.file.core.windows.net/$secondary_pvc$sas_token" \--recursive \--overwrite=true \--force-if-read-only=true \--log-level INFO \--output-type textif [ $? -eq 0 ]; thenecho "Sync successful for $primary_pvc to $secondary_pvc"elseecho "ERROR: Sync failed for $primary_pvc to $secondary_pvc"exit 1fidoneecho "PVC sync process completed."
Thank you for reading our comprehensive guide on "Building Private Azure Infrastructure with Terraform" We hope you found it insightful and valuable. If you have any questions, need further assistance, or are looking for expert support in setting up and managing your Azure infrastructure, our team is here to help!
Reach out to us for Your Azure Infrastructure Needs:
🌐 Website: https://blog.prometheanz.com
📧 Email: [email protected]
Happy Terraforming!
Copyright © 2024 PrometheanTech. All Rights Reserved.