How do I distribute pods evenly across zones with Pod Topology?
To distribute pods evenly across zones in a Kubernetes cluster, you can use the topologySpreadConstraints
feature in your Pod or Deployment specification. This feature allows you to specify how your pods should be spread across different topology domains, such as zones or nodes, to ensure high availability and fault tolerance.
In this example, we’ll create a Kubernetes Deployment with a topologySpreadConstraints
configuration to distribute the pods evenly across different zones. We’ll use Pulumi with the Kubernetes provider to define and deploy this infrastructure.
Explanation
- Kubernetes Provider: We’ll use the Pulumi Kubernetes provider to interact with the Kubernetes cluster.
- Namespace: We’ll create a namespace to isolate our resources.
- Deployment: We’ll create a Deployment with a
topologySpreadConstraints
configuration to distribute pods across zones.
Code
Below is the Pulumi program written in TypeScript that accomplishes this:
import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";
// Create a namespace
const namespace = new k8s.core.v1.Namespace("example-namespace", {
metadata: {
name: "example-namespace",
},
});
// Create a Deployment with topologySpreadConstraints
const deployment = new k8s.apps.v1.Deployment("example-deployment", {
metadata: {
namespace: namespace.metadata.name,
},
spec: {
replicas: 6,
selector: {
matchLabels: {
app: "example-app",
},
},
template: {
metadata: {
labels: {
app: "example-app",
},
},
spec: {
containers: [
{
name: "nginx",
image: "nginx:latest",
},
],
topologySpreadConstraints: [
{
maxSkew: 1,
topologyKey: "topology.kubernetes.io/zone",
whenUnsatisfiable: "DoNotSchedule",
labelSelector: {
matchLabels: {
app: "example-app",
},
},
},
],
},
},
},
});
// Export the namespace name and deployment name
export const namespaceName = namespace.metadata.name;
export const deploymentName = deployment.metadata.name;
Explanation of the Code
- Namespace: We create a namespace named
example-namespace
to isolate our resources. - Deployment: We create a Deployment named
example-deployment
with 6 replicas of annginx
container. - Topology Spread Constraints: We configure
topologySpreadConstraints
to ensure that the pods are evenly distributed across different zones. ThetopologyKey
is set totopology.kubernetes.io/zone
, which is a common label used to denote zones in Kubernetes clusters.
Running the Program
- Ensure you have Pulumi installed and configured with access to your Kubernetes cluster.
- Save the code to a file, for example,
index.ts
. - Run
pulumi up
to deploy the resources to your Kubernetes cluster.
This program will create a namespace and a deployment with pods distributed evenly across zones, ensuring high availability and fault tolerance.
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.