How do I set up a pod disruption budget in Kubernetes?
This guide will show you how to set up a Pod Disruption Budget (PDB) in Kubernetes using Pulumi. A PDB ensures that a certain number or percentage of pods in a deployment remain available during voluntary disruptions such as node drains or updates.
Below is a complete Pulumi program written in TypeScript to create a Pod Disruption Budget for a set of pods labeled app: myapp
. This PDB specifies that at least one pod must be available at all times.
import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";
// Define the namespace
const namespace = new k8s.core.v1.Namespace("my-namespace", {
metadata: { name: "my-namespace" },
});
// Define the Pod Disruption Budget
const pdb = new k8s.policy.v1.PodDisruptionBudget("myapp-pdb", {
metadata: {
name: "myapp-pdb",
namespace: namespace.metadata.name,
},
spec: {
selector: {
matchLabels: {
app: "myapp",
},
},
minAvailable: 1, // Ensure at least 1 pod is always available
},
});
// Export the namespace and PDB names
export const namespaceName = namespace.metadata.name;
export const pdbName = pdb.metadata.name;
Key Points
- Namespace: We create a Kubernetes namespace to isolate resources.
- Pod Disruption Budget: We define a PDB that ensures at least one pod labeled
app: myapp
remains available during disruptions. - Selectors: The PDB uses label selectors to target specific pods.
Summary
In this guide, we set up a Pod Disruption Budget in Kubernetes using Pulumi and TypeScript. This ensures that at least one pod remains available during voluntary disruptions, helping 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.