1. Using kubernetes deployment with node-group

    TypeScript

    In Kubernetes, a Deployment is a resource that provides declarative updates to applications. A Deployment allows you to describe an application’s life cycle, such as which images to use for the app, the number of pods to run, and the way to update them, among other aspects.

    When working with a managed Kubernetes service, such as Amazon EKS, Azure AKS, or Google GKE, you often have the concept of node groups or node pools. These are groups of machines, often virtual machines, that form the worker nodes for the Kubernetes cluster.

    Node groups allow you to run different types of workloads on different machines that may have varying characteristics such as CPU, memory, or GPUs, and you can label or taint these node groups accordingly. Then, you can use node selectors, affinity, and anti-affinity rules in your Deployment to schedule pods on specific node groups.

    Here, I'll show you how to define a Kubernetes Deployment with a nodeSelector in Pulumi to target a specific node group. This Pulumi program will be written in TypeScript.

    Before we start with the Pulumi program, you'll need to have Pulumi installed and configured for TypeScript. You should also have kubectl configured to interact with your Kubernetes cluster.

    The Pulumi Kubernetes provider will use the kubeconfig file to interact with your Kubernetes cluster. Make sure that the kubeconfig file is set up correctly and is either located in the default location (~/.kube/config) or the path to it is specified in the KUBECONFIG environment variable.

    Below is a TypeScript program that defines a Kubernetes Deployment intended to be scheduled on a specific node group. The nodes in this group are labeled with "node-group=backend", and the Deployment's nodeSelector is used to match this label.

    import * as kubernetes from "@pulumi/kubernetes"; // Create a Kubernetes Namespace const ns = new kubernetes.core.v1.Namespace("app-ns", { metadata: { name: "app-namespace", }, }); // Define a Kubernetes Deployment const appLabels = { app: "myapp" }; const deployment = new kubernetes.apps.v1.Deployment("app-deploy", { metadata: { namespace: ns.metadata.name, labels: appLabels, }, spec: { replicas: 2, selector: { matchLabels: appLabels }, template: { metadata: { labels: appLabels, }, spec: { containers: [{ name: "myapp-container", image: "myapp:latest", }], // Use `nodeSelector` to target the Deployment at a specific node group. nodeSelector: { "node-group": "backend" // Make sure this label exists on your node group }, }, }, }, }); // Export the Deployment name export const deploymentName = deployment.metadata.name;

    In the program above, a new Namespace is created to logically separate our resources within the cluster. You might choose to omit this if you are deploying into an existing Namespace.

    The Deployment is defined with a spec that includes:

    • replicas: The desired number of replicas for the pods.
    • selector: A label selector for targeting the pods that belong to the Deployment.
    • template: Defines the pod templates. Includes metadata to apply labels and a spec that details the containers to be run and the nodeSelector.
    • nodeSelector: A selector that specifies a label of nodes where you want the pods to be scheduled. In this example, it's targeting nodes labeled with node-group=backend.

    The label node-group=backend should match the label on the nodes of your desired node group. If you have labeled your nodes with a different key-value pair, you will need to change the nodeSelector accordingly.

    If you apply this Pulumi program, it will create a Deployment in the specified namespace, and the pods will only be scheduled on the nodes that have the label node-group=backend.

    Once you have the Pulumi program ready, you can deploy it using the Pulumi CLI:

    pulumi up

    This will prompt you to review the changes and approve them, after which Pulumi will apply the configurations and create the resources in your Kubernetes cluster.

    Remember, this is a simple introduction to Deployment and nodeSelector. Kubernetes provides even more granular controls such as affinities, anti-affinities, and taints and tolerations to control pod scheduling.