1. Answers
  2. Creating a Kubernetes Deployment Patch

How do I create a Kubernetes deployment patch?

To create a Kubernetes deployment patch, you can use Pulumi to define and apply a patch to an existing Kubernetes deployment. This patch can update various attributes of the deployment, such as the number of replicas or container images.

Below is a Pulumi program written in TypeScript that demonstrates how to create and apply a patch to a Kubernetes deployment:

import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";

// Define the existing deployment to be patched
const existingDeployment = new k8s.apps.v1.Deployment("existing-deployment", {
    metadata: {
        name: "my-deployment",
        namespace: "default",
    },
    spec: {
        replicas: 3,
        selector: {
            matchLabels: {
                app: "my-app",
            },
        },
        template: {
            metadata: {
                labels: {
                    app: "my-app",
                },
            },
            spec: {
                containers: [
                    {
                        name: "my-container",
                        image: "nginx:1.14.2",
                    },
                ],
            },
        },
    },
});

// Define the patch to be applied
const deploymentPatch = new k8s.apps.v1.DeploymentPatch("deployment-patch", {
    metadata: {
        name: "my-deployment",
        namespace: "default",
    },
    spec: {
        replicas: 5, // Update the number of replicas
        template: {
            spec: {
                containers: [
                    {
                        name: "my-container",
                        image: "nginx:1.16.1", // Update the container image
                    },
                ],
            },
        },
    },
}, { dependsOn: existingDeployment });

export const deploymentName = existingDeployment.metadata.name;
export const patchedReplicas = deploymentPatch.spec.replicas;
export const patchedImage = deploymentPatch.spec.template.spec.containers[0].image;

Key Points:

  • Existing Deployment: The code defines an existing Kubernetes deployment named “my-deployment” in the “default” namespace.
  • Patch Definition: A deployment patch is defined using DeploymentPatch to update the number of replicas and the container image of the existing deployment.
  • Dependencies: The patch depends on the existing deployment to ensure it is applied correctly.

Summary:

In this example, we created a Kubernetes deployment patch using Pulumi. The patch updated the number of replicas and the container image of an existing deployment. By using the DeploymentPatch resource, we can easily manage and apply changes to Kubernetes deployments in a declarative manner.

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