Simplifying Blue-Green Deployments with Terraform on Kubernetes — Part 3
Terraform on Azure: A Case Study with Kubernetes 1.29 and Advanced Deployment Strategies
Introduction
For junior cloud engineers looking to leverage the powerful combination of Terraform, Azure DevOps, and Kubernetes, this case study provides a comprehensive guide to setting up an Azure environment. This setup will utilize the latest features from Kubernetes 1.29, including enhanced deployment techniques like canary and blue-green deployments, integrated within an Azure DevOps pipeline.
Understanding the Tools
Terraform is an open-source tool for building, changing, and versioning infrastructure safely and efficiently. Azure DevOps provides developer services for support teams to plan work, collaborate on code development, and build and deploy applications. Kubernetes 1.29, known as Mandala, offers advanced deployment options and improved ecosystem connectivity, making it ideal for complex deployment strategies.
Setting Up the Azure Environment with Terraform
Creating the Azure Resource Group: A resource group is a container that holds related resources for an Azure solution. Here, Terraform is used to define and manage this group:
resource "azurerm_resource_group" "rg" {
name = "K8sResources"
location = "East US"
}
Provisioning an AKS Cluster:
Azure Kubernetes Service (AKS) simplifies deploying a managed Kubernetes cluster in Azure by handling critical tasks like health monitoring and maintenance.
resource "azurerm_kubernetes_cluster" "aks" {
name = "myAKSCluster"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
dns_prefix = "myakscluster"
default_node_pool {
name = "default"
node_count = 3
vm_size = "Standard_DS2_v2"
}
identity {
type = "SystemAssigned"
}
}
Configuring Kubernetes Provider:
Once the AKS cluster is provisioned, configure the Kubernetes provider to interact with it:
provider "kubernetes" {
host = azurerm_kubernetes_cluster.aks.kube_config.0.host
client_certificate = base64decode(azurerm_kubernetes_cluster.aks.kube_config.0.client_certificate)
client_key = base64decode(azurerm_kubernetes_cluster.aks.kube_config.0.client_key)
cluster_ca_certificate = base64decode(azurerm_kubernetes_cluster.aks.kube_config.0.cluster_ca_certificate)
}
Implementing Deployment Strategies in Kubernetes
Blue-Green Deployment: Blue-green deployment is an effective strategy for zero-downtime deployments and instant rollback capability.
module "blue_deployment" {
source = "./modules/kubernetes-deployment"
name = "nginx-blue"
replicas = 3
version = "1.19"
color = "blue"
}
module "green_deployment" {
source = "./modules/kubernetes-deployment"
name = "nginx-green"
replicas = 3
version = "1.20"
color = "green"
}
Traffic switching can be managed by updating the Kubernetes service to direct traffic between blue and green deployments based on testing results.
Canary Deployment: Canary deployments allow rolling out changes to a small subset of users before rolling them out to the entire base.
resource "kubernetes_deployment" "nginx_canary" {
metadata {
name = "nginx-canary"
}
spec {
replicas = 1
selector {
match_labels = {
app = "nginx"
track = "canary"
}
}
template {
metadata {
labels {
app = "nginx"
track = "canary"
}
}
spec {
containers {
image = "nginx:1.20"
name = "nginx"
}
}
}
}
}
Integration with Azure DevOps
Setting up CI/CD pipelines in Azure DevOps to automate the deployment processes using Terraform configurations ensures consistent and reliable deployments:
- Pipeline Setup: Create a YAML pipeline in Azure DevOps that triggers Terraform scripts based on Git commits or pull requests. This pipeline would include steps for Terraform initialization, planning, and applying.
- Monitoring and Rollbacks: Integrate monitoring tools such as Azure Monitor or Prometheus to track application performance. Use Terraform to roll back to previous versions if anomalies are detected.
Conclusion
This case study demonstrates how to effectively use Terraform with Azure and Kubernetes. This setup not only leverages modern infrastructure practices but also provides flexibility, scalability, and robustness needed for modern cloud applications.