1. Using kubernetes elbv2.k8s.aws with operator

    TypeScript

    To use Amazon's Elastic Load Balancing Version 2 (ELBv2) with an AWS Kubernetes cluster using Pulumi, we need to define and set up the necessary resources to facilitate the load balancing for our Kubernetes deployments.

    Before we begin, it's important to understand that Kubernetes interacts with ELBv2 through the AWS Load Balancer Controller, which is an operator that manages AWS load balancers for a Kubernetes cluster. You also need to have an existing EKS (Elastic Kubernetes Service) cluster running on AWS or create a new one.

    Here's a detailed breakdown of what you need to do:

    1. Create an EKS Cluster: First, you'll need an EKS cluster where you'll deploy your Kubernetes workloads. We'll use Pulumi to declare and create an EKS cluster if you don't already have one.

    2. Install the AWS Load Balancer Controller: You have to install the AWS Load Balancer Controller to your EKS cluster. This controller will watch for Kubernetes Ingress resources and Service resources that require an AWS Load Balancer and will manage those resources accordingly.

    3. Define an Ingress resource or Service of type LoadBalancer: In your Kubernetes manifest files, you will define either an Ingress resource or a Service with type: LoadBalancer. The AWS Load Balancer Controller will detect these manifest and provision an ELBv2 load balancer.

    Below is a program written in TypeScript that showcases the creation of an EKS cluster and outlines the setup of the AWS Load Balancer Controller. Remember, you will need to have your AWS credentials configured for Pulumi to interact with your AWS account.

    import * as pulumi from '@pulumi/pulumi'; import * as aws from '@pulumi/aws'; import * as k8s from '@pulumi/kubernetes'; import * as eks from '@pulumi/eks'; // Create an EKS cluster. const cluster = new eks.Cluster('my-cluster', {}); // Export the cluster's kubeconfig. export const kubeconfig = cluster.kubeconfig; // Define the Helm chart for the AWS Load Balancer Controller. const awsLoadBalancerControllerChart = new k8s.helm.v3.Chart('aws-load-balancer-controller', { chart: 'aws-load-balancer-controller', version: '1.2.3', // specify the version that is compatible with your cluster namespace: 'kube-system', // it's recommended to run the controller in the kube-system namespace fetchOpts: { repo: 'https://aws.github.io/eks-charts', }, values: { clusterName: cluster.eksCluster.name, serviceAccount: { // Assuming service account is already created and annotated with the IAM role // If not, you'll need to create it with the correct IAM role annotations. create: false, name: 'aws-load-balancer-controller', // the name of your service account }, }, }, { provider: cluster.provider }); // To use the load balancer controller, you would define a Kubernetes Service of type LoadBalancer or an Ingress. // For this example, we'll create a simple NGINX deployment and a LoadBalancer Service for it. // NGINX deployment const appLabels = { app: 'nginx' }; const nginxDeployment = new k8s.apps.v1.Deployment('nginx-deployment', { spec: { selector: { matchLabels: appLabels }, replicas: 1, template: { metadata: { labels: appLabels }, spec: { containers: [ { name: 'nginx', image: 'nginx:1.14.2', ports: [{ containerPort: 80 }], }, ], }, }, }, }, { provider: cluster.provider }); // NGINX LoadBalancer Service const nginxService = new k8s.core.v1.Service('nginx-svc', { metadata: { labels: nginxDeployment.metadata.labels }, spec: { type: 'LoadBalancer', ports: [{ port: 80, targetPort: 80 }], selector: appLabels, }, }, { provider: cluster.provider }); // Export the Service's LoadBalancer address. export const nginxServiceAddress = nginxService.status.apply(status => status.loadBalancer.ingress[0].hostname);

    This program does the following:

    • Creates an EKS Cluster: It uses Pulumi's EKS package to create a new EKS cluster and exports the kubeconfig which allows you to interact with your cluster with tools like kubectl.
    • Installs the AWS Load Balancer Controller: We're using a Helm chart to install the AWS Load Balancer Controller in the kube-system namespace.
    • Deploys an NGINX example application: Creates a deployment with a simple NGINX instance to serve as an example application.
    • Creates a LoadBalancer Service: Declares a LoadBalancer Service to expose the NGINX deployment. The AWS Load Balancer Controller will automatically create an ELB for this Service.

    After the Pulumi program runs and provisions your resources, you should have an EKS cluster running with the AWS Load Balancer Controller. When you apply the NGINX Service of type LoadBalancer, it will create an ELBv2 instance to handle incoming traffic to your services. The resulting ELB DNS name is exported as nginxServiceAddress.

    Please replace '1.2.3' with the version number of the AWS Load Balancer Controller Helm chart compatible with your Kubernetes version. Also, ensure that the service account for the AWS Load Balancer Controller is properly set up with the necessary IAM roles and annotations; you might need additional configuration for that part based on your AWS setup.