Using kubernetes node-group with discovery.k8s.io
TypeScriptCreating a Kubernetes node group is a way to manage a set of nodes within a cluster that have similar configuration. When using Pulumi, we interact with Kubernetes resources using Pulumi's Kubernetes provider, which allows us to declare our cluster's desired state with code.
We can use Pulumi to create node groups by defining the necessary resources and configuring them to meet specific requirements such as labels, taints, or instance types. If you also want to interact with
discovery.k8s.io
to discover services within the Kubernetes cluster, you'll typically configure EndpointSlices as part of the service discovery process, which is automatically managed by Kubernetes when you run services.Below is a TypeScript program using Pulumi to create an EKS cluster and an associated node group. EKS (Amazon Elastic Kubernetes Service) provides a simplified way to run Kubernetes on AWS without needing to install and operate your cluster's control plane.
The program will perform the following actions:
- Create an EKS cluster using the Pulumi EKS package, which is a simplified higher-level abstraction over AWS resources.
- Define a managed node group within the cluster.
- Configure labels and taints for the node group to properly categorize and schedule workloads.
import * as eks from "@pulumi/eks"; // Create an EKS cluster with the desired configuration. const cluster = new eks.Cluster("my-cluster", { instanceType: "t2.medium", // Specify the instance type for worker nodes. desiredCapacity: 2, // Desired number of worker nodes. minSize: 1, // Minimum number of worker nodes. maxSize: 3, // Maximum number of worker nodes. storageClasses: "gp2", // The storage class to use for the cluster's persistent volumes. deployDashboard: false, // Indicate whether the Kubernetes dashboard should be deployed. }); // Create a managed node group with specific settings. const nodeGroup = new eks.NodeGroup("my-node-group", { cluster: cluster, // Reference to the created cluster. instanceType: "t2.large", // Override the instance type for nodes in this group. desiredCapacity: 3, // Desired number of nodes in the node group. minSize: 1, // Minimum number of nodes. maxSize: 5, // Maximum number of nodes. labels: { // Define the labels for nodes in the node group. environment: "production", workloadType: "stateless", }, taints: [ // Define taints to control pod scheduling for nodes in the node group. { key: "workloadType", value: "stateless", effect: "NoSchedule", }, ], // Additional configurations can be set here such as disk size, AMI version, etc. }, { provider: cluster.provider, // Use the provider that was created with the cluster. }); // Export the cluster's kubeconfig, which can be used to access the cluster. export const kubeconfig = cluster.kubeconfig;
In the above program:
- We import the
@pulumi/eks
package which contains helpers for working with EKS. - We create an EKS cluster using the
eks.Cluster
class. Various properties likeinstanceType
,desiredCapacity
,minSize
,maxSize
, anddeployDashboard
are set according to our cluster needs. Adjust these based on your workload requirements and AWS resource availability. - We then define a managed node group using the
eks.NodeGroup
class, linked to the previously created cluster. Here, we can specify specific configurations for this group of nodes, overriding the cluster-wide settings if necessary. - Labels and taints are added to the node group configuration for applying organizational structure and scheduling policies.
- Lastly, we export the
kubeconfig
value. This is the configuration required to access the cluster withkubectl
or other Kubernetes tooling. Keep this secure, as it provides administrative access to your cluster.
Remember, ensure you have the AWS provider configured with sufficient permissions to create these resources and that you have Pulumi CLI installed and set up to run the program.
To apply this Pulumi program:
- Save the code into a file named
index.ts
. - Run
pulumi up
in the same directory to create the resources. Pulumi will call the AWS API and set up everything for your EKS cluster and node group according to the specified configuration. - Review the proposed changes and confirm them by selecting
yes
. - Once complete, the output will provide you with a
kubeconfig
to interact with your new Kubernetes cluster.
Handle the
kubeconfig
with care, as it provides administrative access to your Kubernetes cluster. Store it securely and provide it only to authorized users and systems.