How do I implement blue-green deployments on AKS?
In this guide, we will implement blue-green deployments on Azure Kubernetes Service (AKS) using Pulumi. Blue-green deployment is a technique that reduces downtime and risk by running two identical production environments, only one of which (let’s say blue) serves live production traffic. The other environment (green) is staged with the new version of the application. Once the new version is verified, traffic is switched from blue to green.
We will:
- Create an AKS cluster.
- Deploy the blue environment with the current version of the application.
- Deploy the green environment with the new version of the application.
- Use a Kubernetes Service to switch traffic between the blue and green environments.
import * as pulumi from "@pulumi/pulumi";
import * as azure from "@pulumi/azure-native";
import * as k8s from "@pulumi/kubernetes";
// Create an Azure Resource Group
const resourceGroup = new azure.resources.ResourceGroup("resourceGroup", {
location: "WestUS",
});
// Create an AKS Cluster
const cluster = new azure.containerservice.ManagedCluster("aksCluster", {
resourceGroupName: resourceGroup.name,
location: resourceGroup.location,
agentPoolProfiles: [{
count: 3,
maxPods: 110,
mode: "System",
name: "agentpool",
osType: "Linux",
type: "VirtualMachineScaleSets",
vmSize: "Standard_DS2_v2",
}],
dnsPrefix: resourceGroup.name,
linuxProfile: {
adminUsername: "aksuser",
ssh: {
publicKeys: [{
keyData: "<your-ssh-public-key>",
}],
},
},
servicePrincipalProfile: {
clientId: "<your-client-id>",
secret: "<your-client-secret>",
},
enableRBAC: true,
});
// Export the KubeConfig
export const kubeConfig = pulumi.all([cluster.name, resourceGroup.name]).apply(([name, rgName]) =>
azure.containerservice.listManagedClusterUserCredentials({
resourceGroupName: rgName,
resourceName: name,
}).then(credentials => Buffer.from(credentials.kubeconfigs[0].value, "base64").toString("utf-8"))
);
// Create a Kubernetes provider using the KubeConfig
const k8sProvider = new k8s.Provider("k8sProvider", {
kubeconfig: kubeConfig,
});
// Deploy the Blue environment
const blueDeployment = new k8s.apps.v1.Deployment("blue-deployment", {
metadata: { name: "blue-deployment" },
spec: {
selector: { matchLabels: { app: "blue-app" } },
replicas: 3,
template: {
metadata: { labels: { app: "blue-app" } },
spec: {
containers: [{
name: "blue-app",
image: "nginx:1.14.2",
ports: [{ containerPort: 80 }],
}],
},
},
},
}, { provider: k8sProvider });
// Deploy the Green environment
const greenDeployment = new k8s.apps.v1.Deployment("green-deployment", {
metadata: { name: "green-deployment" },
spec: {
selector: { matchLabels: { app: "green-app" } },
replicas: 3,
template: {
metadata: { labels: { app: "green-app" } },
spec: {
containers: [{
name: "green-app",
image: "nginx:1.16.0",
ports: [{ containerPort: 80 }],
}],
},
},
},
}, { provider: k8sProvider });
// Create a Kubernetes Service to switch traffic
const service = new k8s.core.v1.Service("app-service", {
metadata: { name: "app-service" },
spec: {
selector: { app: "blue-app" }, // Initially point to the blue deployment
ports: [{ port: 80, targetPort: 80 }],
type: "LoadBalancer",
},
}, { provider: k8sProvider });
// Export the service's IP address
export const serviceIP = service.status.loadBalancer.ingress[0].ip;
Key Points
- Resource Group: Created to hold all Azure resources.
- AKS Cluster: A managed Kubernetes cluster on Azure.
- Deployments: Two separate deployments for blue and green environments.
- Kubernetes Service: Used to switch traffic between blue and green deployments.
Summary
We have implemented a blue-green deployment strategy on AKS using Pulumi. Initially, we created an AKS cluster and deployed the blue environment. We then deployed the green environment and used a Kubernetes Service to switch traffic between the two environments. This setup minimizes downtime and allows for seamless application updates.
Deploy this code
Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.
Sign upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.