1. Answers
  2. Distributing Pods Evenly Across Zones with Pod Topology

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

  1. Kubernetes Provider: We’ll use the Pulumi Kubernetes provider to interact with the Kubernetes cluster.
  2. Namespace: We’ll create a namespace to isolate our resources.
  3. 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

  1. Namespace: We create a namespace named example-namespace to isolate our resources.
  2. Deployment: We create a Deployment named example-deployment with 6 replicas of an nginx container.
  3. Topology Spread Constraints: We configure topologySpreadConstraints to ensure that the pods are evenly distributed across different zones. The topologyKey is set to topology.kubernetes.io/zone, which is a common label used to denote zones in Kubernetes clusters.

Running the Program

  1. Ensure you have Pulumi installed and configured with access to your Kubernetes cluster.
  2. Save the code to a file, for example, index.ts.
  3. 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 up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up