How do I set up rolling updates with automated rollback on failure?
In this guide, we will set up rolling updates with automated rollback on failure for a Kubernetes deployment using Pulumi. Rolling updates allow you to update your application without downtime by gradually replacing old pods with new ones. If a failure occurs during the update, Kubernetes will automatically rollback to the previous stable state to ensure minimal disruption to your application’s availability.
Below is a Pulumi program written in TypeScript that demonstrates how to configure a Kubernetes deployment with rolling updates and automated rollback.
import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";
// Define the Kubernetes namespace
const namespace = new k8s.core.v1.Namespace("app-namespace", {
metadata: { name: "app-namespace" },
});
// Define the Kubernetes deployment
const appLabels = { app: "my-app" };
const deployment = new k8s.apps.v1.Deployment("app-deployment", {
metadata: {
namespace: namespace.metadata.name,
name: "app-deployment",
},
spec: {
replicas: 3,
selector: { matchLabels: appLabels },
strategy: {
type: "RollingUpdate",
rollingUpdate: {
maxSurge: "25%",
maxUnavailable: "25%",
},
},
template: {
metadata: { labels: appLabels },
spec: {
containers: [
{
name: "app-container",
image: "nginx:1.19.0", // Use your application image here
ports: [{ containerPort: 80 }],
livenessProbe: {
httpGet: {
path: "/",
port: 80,
},
initialDelaySeconds: 10,
periodSeconds: 5,
},
readinessProbe: {
httpGet: {
path: "/",
port: 80,
},
initialDelaySeconds: 10,
periodSeconds: 5,
},
},
],
},
},
minReadySeconds: 10,
revisionHistoryLimit: 3,
progressDeadlineSeconds: 600,
},
});
// Export the name of the deployment
export const deploymentName = deployment.metadata.name;
Key Points
- Namespace: A separate namespace is created for the application.
- Deployment: The deployment is configured with rolling updates strategy using
maxSurge
andmaxUnavailable
settings. - Probes: Liveness and readiness probes are defined to ensure the application is healthy before traffic is routed to it.
- Automated Rollback: Kubernetes will automatically rollback to the previous version if the new version fails to become ready within the specified
progressDeadlineSeconds
.
Summary
In this guide, we configured a Kubernetes deployment with rolling updates and automated rollback using Pulumi. This ensures that updates to your application are performed without downtime, and any failures during the update process will trigger an automatic rollback to maintain application availability.
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.