1. Answers
  2. Deploying Kubernetes Applications with Cluster

How Do I Deploy Kubernetes Applications With a Cluster?

Introduction

This guide provides a step-by-step process for deploying a Kubernetes application using Pulumi. The objective is to create a Kubernetes cluster and deploy an application onto this cluster. We will use a simple NGINX deployment to illustrate the deployment process.

Key Points

  1. Create a Kubernetes Cluster: Set up a Kubernetes cluster where the application will be deployed.
  2. Deploy an Application: Deploy an NGINX application onto the cluster.
  3. Expose the Application: Create a service to expose the NGINX application to external traffic.

Step-by-Step Deployment

  1. Set Up the Kubernetes Cluster:

    • Ensure that you have a Kubernetes cluster created and that your context is set to interact with it.
  2. Define and Deploy the NGINX Application:

    • Define the NGINX deployment by specifying the number of replicas and the container details.
    • Deploy the application using Pulumi, which will handle the creation of the necessary Kubernetes resources.
  3. Expose the Application:

    • Create a Kubernetes Service of type LoadBalancer to expose the NGINX application.
    • This service will route external traffic to the 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 successfully created a Kubernetes cluster and deployed an NGINX application onto it. We exposed the application using a Kubernetes Service configured as a LoadBalancer, allowing external access to the application. This process demonstrates the basic workflow for deploying applications using Pulumi and Kubernetes, highlighting the ease of managing infrastructure as code.

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