1. Using kubernetes yaml with deployment

    TypeScript

    When you want to deploy a Kubernetes application using YAML in Pulumi, you typically have two main resources from the Pulumi Kubernetes provider: ConfigFile and ConfigGroup. These resources allow you to apply YAML manifests to a Kubernetes cluster.

    • ConfigFile: This resource is used to deploy a single YAML configuration file to Kubernetes. You would use this if you have a single YAML file describing your Kubernetes resources.

    • ConfigGroup: This resource allows you to apply multiple YAML files or inlined YAML to a Kubernetes cluster. This would be your choice if you have a directory with several Kubernetes manifest files or want to specify resources directly in your Pulumi code using YAML strings.

    Below is a simple TypeScript program that demonstrates how to use a ConfigGroup resource to deploy a Kubernetes deployment defined in inline YAML. The deployment creates a simple Nginx server with one replica.

    import * as k8s from "@pulumi/kubernetes"; // Define our nginx deployment using inline YAML. const nginxDeployment = ` apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment labels: app: nginx spec: replicas: 1 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.14.2 ports: - containerPort: 80 `; // Create a ConfigGroup resource using the inline YAML. const nginxDeploymentResource = new k8s.yaml.ConfigGroup("nginx-deployment-resource", { yaml: nginxDeployment, // Pass the inline YAML to the ConfigGroup resource }); // Export the name and resource version of the deployment. export const deploymentName = nginxDeploymentResource.getResource("apps/v1/Deployment", "nginx-deployment").metadata.name; export const deploymentResourceVersion = nginxDeploymentResource.getResource("apps/v1/Deployment", "nginx-deployment").metadata.resourceVersion;

    In this program:

    • We import the Kubernetes package from Pulumi.
    • We define a simple Nginx deployment configuration in a string literal using YAML syntax. This deployment will ensure that one Nginx pod is always running.
    • We create a new ConfigGroup object, passing the inline YAML to its yaml property. The ConfigGroup resource will handle applying this configuration to the Kubernetes cluster.
    • We export two values using Pulumi's export: the name of the deployment and the resource version. These are properties of the Kubernetes Deployment resource and can be used for further operations or inspections.

    This program assumes that you have already configured Pulumi for your Kubernetes cluster and that you are authenticated to the cluster where you want to deploy the application.

    Note that with Pulumi, you can also use more expressive constructs to define Kubernetes resources, which can provide better abstractions and error checking than raw YAML. However, when you have existing YAML files or want to transition gradually, using ConfigGroup and ConfigFile can be a beneficial approach.