1. Answers
  2. Create An Kubernetes.apps/v1.Deployment Resource

Create an Kubernetes.apps/V1.Deployment Resource

Introduction

In this guide, we will create a Kubernetes Deployment resource using Pulumi in TypeScript. The kubernetes.apps/v1.Deployment resource allows you to manage and deploy containerized applications in a Kubernetes cluster. We will define the deployment configuration, including the container image, replicas, and other necessary settings.

Step-by-Step Explanation

Step 1: Set Up Your Pulumi Project

  1. Ensure you have Pulumi installed. If not, follow the installation guide.
  2. Create a new Pulumi project: pulumi new typescript.
  3. Install the Pulumi Kubernetes provider: npm install @pulumi/kubernetes.

Step 2: Define the Deployment Resource

Create a new TypeScript file (e.g., index.ts) and add the following code to define the Kubernetes Deployment resource:

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

const appLabels = { app: "nginx" };
const deployment = new k8s.apps.v1.Deployment("nginx-deployment", {
    metadata: { name: "nginx-deployment" },
    spec: {
        selector: { matchLabels: appLabels },
        replicas: 2,
        template: {
            metadata: { labels: appLabels },
            spec: {
                containers: [{
                    name: "nginx",
                    image: "nginx:1.14.2",
                    ports: [{ containerPort: 80 }],
                }],
            },
        },
    },
});

Step 3: Deploy the Resource

  1. Run pulumi up to preview and deploy the changes.
  2. Confirm the deployment to create the Kubernetes resources.

Conclusion

In this guide, we created a Kubernetes Deployment resource using Pulumi in TypeScript. This deployment will manage and deploy an NGINX container with two replicas. You can customize the deployment configuration to suit your application’s requirements. For more information, refer to the Pulumi Kubernetes documentation.

Full Code Example

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

const appLabels = { app: "nginx" };
const deployment = new k8s.apps.v1.Deployment("nginx-deployment", {
    metadata: { name: "nginx-deployment" },
    spec: {
        selector: { matchLabels: appLabels },
        replicas: 2,
        template: {
            metadata: { labels: appLabels },
            spec: {
                containers: [{
                    name: "nginx",
                    image: "nginx:1.14.2",
                    ports: [{ containerPort: 80 }],
                }],
            },
        },
    },
});

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