AWS Load Balancer Controller

repository·main·Indexed 26 days ago

https://github.com/kubernetes-sigs/aws-load-balancer-controller

A Kubernetes controller that automates the management of AWS Elastic Load Balancers (ALB and NLB) by satisfying Kubernetes Ingress, Service, and Gateway API resources. Documentation covers installation via Helm, IAM Roles for Service Accounts (IRSA) configuration, high availability setup, and Gateway API conformance testing.

Tokens
103.4K
Snippets
194
Records
416
Agent score
82%

What's inside AWS Load Balancer Controller

  1. Overview of AWS Load Balancer Controller

    main

    AWS Load Balancer Controller is a Kubernetes controller used to manage AWS Elastic Load Balancers. It automates the provisioning of AWS load balancers based on Kubernetes resources:

    • Ingress resources: Provisions Application Load Balancers (ALB).
    • Service resources: Provisions Network Load Balancers (NLB).
    • Gateway resources: Provisions both Network Load Balancers (NLB) and Application Load Balancers (ALB).
  2. Understand Ingress Resource Provisioning

    main

    When an Ingress resource satisfying the controller's requirements is created, the controller automates the following AWS resource lifecycle:

    1. ALB Creation: Creates an Application Load Balancer (ELBv2), which can be internet-facing or internal. Subnets can be specified via annotations.
    2. Target Groups: Creates Target Groups for each unique Kubernetes service in the Ingress.
    3. Listeners: Creates Listeners for every port in the Ingress. Defaults to 80 or 443 if not specified. Certificates can be attached via annotations.
    4. Rules: Creates Listener Rules for each path to route traffic to the correct Kubernetes Service.

    Note: Avoid out-of-band modifications to these AWS resources. The controller manages these configurations and may revert manual changes during reconciliation. Use Ingress/Service annotations, controller flags, or IngressClassParams for configuration instead.

  3. Prerequisites for AWS Load Balancer Controller

    main

    Before installing, ensure the following requirements are met:

    Kubernetes Versions

    • Chart v1.5.0+: Kubernetes 1.22+
    • Chart v1.4.0+: Kubernetes 1.19+
    • Chart v1.2.0 - v1.3.3: Kubernetes 1.16-1.21
    • Chart v1.1.6 and earlier: Kubernetes 1.15

    Required Tools & Permissions

    • Helm v3
    • IAM permissions (via IRSA or attached to worker node IAM roles)

    Optional Dependencies

    • cert-manager: Required if setting enableCertManager: true. Requires cert-manager.io/v1 API version.
    • Prometheus Operator: Required if setting serviceMonitor.enabled: true to collect metrics.
  4. Manage AWS Global Accelerator via GlobalAccelerator CRD

    main

    The AWS Global Accelerator Controller allows you to declaratively manage AWS Global Accelerator resources using a single GlobalAccelerator Custom Resource Definition (CRD). This monolithic CRD manages the entire hierarchy, including the accelerator itself, listeners (with protocol and port ranges), endpoint groups (with region and traffic dial settings), and endpoints.

    Supported endpoint types include:

    • Service: Discovers NLBs from Service type LoadBalancer.
    • Ingress: Discovers ALBs from Ingress resources.
    • Gateway API: Discovers ALBs and NLBs from Gateway API resources.
    • EndpointID: Direct reference to an ELB ARN.

    The controller handles the full lifecycle (CRUD), automatic resource discovery, and status reporting (including ARNs, DNS names, and deployment state) back to the CRD.

  5. Gateway API Reconciliation Process

    main

    The controller uses a continuous reconciliation loop to align Gateway API objects with AWS infrastructure. The process follows these steps:

    1. API Monitoring: Monitors the Kubernetes API for changes to Gateway API resources.
    2. Queueing: Adds identified resources to an internal processing queue.
    3. Processing:
      • Verifies the GatewayClass is managed by the controller.
      • Maps the Gateway API definition to AWS resources (NLB/ALB, Listeners, Rules, Target Groups, etc.).
      • Compares desired state with actual AWS state and executes API calls to reconcile differences.
    4. Status Updates: Updates the status field of the Gateway resource with real-time feedback, including the load balancer's DNS name, ARN, and whether the resource is accepted and programmed.
  6. Prerequisites for AWS Global Accelerator Controller

    main

    Before installing the AWS Global Accelerator Controller, ensure the following requirements are met:

    1. AWS Partition: AWS Global Accelerator is only available in the commercial AWS partition. It is not available in AWS GovCloud (aws-us-gov) or AWS China (aws-cn).
    2. Kubernetes Version: Requires Kubernetes version 1.19 or later.
    3. AWS Load Balancer Controller Version: Requires version 2.17.0 or later.
    4. IAM Permissions: The controller's IAM role must include the Global Accelerator permissions.
  7. Subnet Auto-Discovery Process Overview

    main

    The AWS Load Balancer Controller (LBC) automatically discovers subnets for NLB and ALB creation through a three-step process:

    1. Candidate Subnet Determination:
      • If tag filters are specified (via IngressClassParams for Ingress), only matching subnets are candidates.
      • If no tag filters are specified, the controller looks for subnets with matching role tags or, for LBC version >= 2.12.1, subnets whose reachability (public/private) matches the LoadBalancer's schema.
    2. Subnet Filtering:
      • Cluster Tag Check: Subnets must match the cluster name via kubernetes.io/cluster/<cluster-name> (can be disabled via SubnetsClusterTagCheck flag).
      • IP Availability: Subnets with fewer than 8 available IP addresses are filtered out.
    3. Final Selection: The controller selects one subnet per availability zone based on priority (Cluster Tag match > Lexicographical Subnet ID).
  8. Enable Pod Readiness Gates for zero-downtime deployments

    main

    The AWS Load Balancer controller supports Pod readiness gates to ensure a pod is registered to the ALB/NLB and healthy before Kubernetes considers it 'Ready'. This prevents rolling updates from terminating old pods before new pods are actually capable of receiving traffic.

    Requirements:

    1. Target Type: This feature only works when using target-type: ip. It does not work with target-type: instance because the ALB is not aware of individual pod health in that mode.
    2. Namespace Labeling: You must apply the label elbv2.k8s.aws/pod-readiness-gate-inject: enabled to the pod's namespace.
    3. Resource Order: To ensure all pods receive the configuration, you must create the Ingress or Service and label the namespace before creating the pods/deployments.

    When active, the controller injects readiness gates with the prefix target-health.elbv2.k8s.aws into the pod spec during creation.

    $ kubectl create namespace readiness
    $ kubectl label namespace readiness elbv2.k8s.aws/pod-readiness-gate-inject=enabled
  9. Use Gateway API for L4 and L7 Routing

    main

    The controller supports the Kubernetes Gateway API to manage traffic routing. The type of Load Balancer provisioned depends on the route type:

    • L7 Routes (HTTPRoute, GRPCRoute): Provisioned using Application Load Balancers (ALB).
    • L4 Routes (TCPRoute, UDPRoute, TLSRoute): Provisioned using Network Load Balancers (NLB).

    For detailed configuration, prerequisites, and examples, refer to the Gateway API Guide.

  10. Install AWS Load Balancer Controller with IAM Roles for Service Accounts (IRSA)

    main
    If you are using IAM Roles for Service Accounts (IRSA), you must configure the Helm chart to use an existing service account instead of creating a new one. You must specify both serviceAccount.create=false and the exact serviceAccount.name of your pre-created service account (typically aws-load-balancer-controller).
  11. Enable MultiCluster Target Groups for ALB or NLB

    main

    By default, the AWS Load Balancer Controller assumes full control over target groups and de-registers any targets not currently in the cluster. To allow a target group to be shared across multiple Kubernetes clusters or to support arbitrary targets from other sources, you must enable MultiCluster mode using specific annotations.

    • For ALB (Ingress): Add the annotation alb.ingress.kubernetes.io/multi-cluster-target-group: "true" to your Ingress resource.
    • For NLB (Service): Add the annotation service.beta.kubernetes.io/aws-load-balancer-multi-cluster-target-group: "true" to your Service resource.

    When enabled, the controller takes a snapshot of the cluster state and stores it in a ConfigMap named aws-lbc-targets-$TARGET_GROUP_BINDING_NAME within the same namespace as your load balancer resources.

    # Example ALB Ingress with MultiCluster enabled
    ```yaml
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: echoserver
      namespace: echoserver
      annotations:
        alb.ingress.kubernetes.io/multi-cluster-target-group: "true"
        alb.ingress.kubernetes.io/scheme: internet-facing
        alb.ingress.kubernetes.io/tags: Environment=dev,Team=test
    spec:
      ingressClassName: alb
      rules:
        - http:
            paths:
              - path: /
                pathType: Exact
                backend:
                  service:
                    name: echoserver
                    port:
                      number: 80