1. Using kubernetes apps with deployment

    TypeScript

    To deploy applications on Kubernetes using Pulumi, you often utilize the Deployment resource from the Kubernetes API. This resource allows you to define the desired state of your application deployments, including the container images to use, the number of replicas, and other configurations like environment variables, mounted volumes, and readiness/liveness probes.

    The Deployment resource is managed under the apps/v1 API group in Kubernetes. In Pulumi, the Kubernetes provider packages this functionality into types and classes that you can use to specify the deployment in your code.

    Here’s how a basic Pulumi TypeScript program to create a deployment on Kubernetes might look:

    1. Setup: You should already have a Kubernetes cluster running and have configured your Pulumi environment to communicate with your cluster. You can do this by setting up kubeconfig to point to your cluster.

    2. Define the Deployment: You define a deployment in Pulumi using classes from the @pulumi/kubernetes package. Specifically, you would use the apps/v1/Deployment class to create a new deployment.

    3. Specify the Deployment Configuration: You will need to specify the number of replicas, the container image, and other configuration details.

    The program below deploys a simple nginx application with two replicas:

    import * as k8s from "@pulumi/kubernetes"; // Creating a Kubernetes Deployment for an nginx application. const appName = "nginx"; const appLabels = { app: appName }; const deployment = new k8s.apps.v1.Deployment(appName, { metadata: { name: appName }, spec: { replicas: 2, // Specifies that we want two instances of nginx to be running. selector: { matchLabels: appLabels }, template: { metadata: { labels: appLabels }, spec: { containers: [{ name: appName, image: "nginx:latest", // Use the latest nginx image from Docker Hub. ports: [{ name: "http", containerPort: 80 }] // Expose port 80 of the container. }], }, }, }, }); // Export the Deployment name export const deploymentName = deployment.metadata.name;

    Explanation:

    • import * as k8s from "@pulumi/kubernetes";: This line imports the Pulumi Kubernetes package which contains the necessary classes and functions to work with Kubernetes resources.

    • const appName = "nginx";: We declare a constant for the application name, which is used to label our resources and can help with querying them later.

    • const appLabels = { app: appName };: Here we define a set of labels that will be used to select and manage the pods that belong to the deployment.

    • new k8s.apps.v1.Deployment(appName, {...});: This block creates a new Kubernetes deployment. The Deployment object takes two arguments, the name of the resource and a spec object describing the deployment.

    • metadata: { name: appName }: Defines the metadata for the deployment, primarily its name.

    • spec: { ... }: The spec outlines the specification of the deployment. This includes the desired number of replicas, a selector to match labels, and a template for the pod configuration.

    • replicas: 2: Indicates the desired number of replicas of the application that should be running.

    • selector: { matchLabels: appLabels }: This selector is used by the Deployment controller to manage and scale the application pods. The labels in the selector must match the labels of the pods.

    • template: { ... }: Specifies the template for creating the pods. Labels, container image name and tag, and the container port to expose are all defined here.

    • ports: [{ name: "http", containerPort: 80 }]: We're defining which port the container should expose; the nginx server in the container listens on port 80 by default.

    • export const deploymentName = deployment.metadata.name;: Finally, we export the name of the deployment. This is useful for retrieving the name of the created deployment when the Pulumi program is run.

    After you write the code, you can deploy your application to the Kubernetes cluster by executing the Pulumi CLI commands pulumi up. This will prompt an interactive confirmation where you can see the proposed changes before they are applied. Once confirmed, Pulumi will apply the deployment specification to your cluster, and your nginx application will be deployed.