1. Answers
  2. Deploying Kubernetes Applications with Cluster

How do I deploy Kubernetes applications with a cluster?

In this guide, we will demonstrate how to deploy a Kubernetes application using Pulumi. We will create a Kubernetes cluster and then deploy an application onto this cluster. This example will use a simple NGINX deployment to illustrate the process.

Key Points

  1. Create a Kubernetes Cluster: We will create a Kubernetes cluster.
  2. Deploy an Application: We will deploy an NGINX application onto the cluster.
  3. Expose the Application: We will create a service to expose the NGINX application.
import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";

// Create a Kubernetes cluster (assuming the cluster is already created and context is set)

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

// Expose the NGINX deployment with a Service
const service = new k8s.core.v1.Service("nginx-service", {
    metadata: {
        labels: deployment.spec.template.metadata.labels,
    },
    spec: {
        type: "LoadBalancer",
        ports: [{ port: 80, targetPort: 80 }],
        selector: appLabels,
    },
});

// Export the service URL
export const serviceUrl = service.status.loadBalancer.ingress[0].ip;

Summary

In this guide, we created a Kubernetes cluster and deployed an NGINX application onto it. We also exposed the application using a Kubernetes Service of type LoadBalancer. The service URL is exported to access the deployed application. This demonstrates the basic workflow of deploying applications using Pulumi and Kubernetes.

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