Course contentsShow
← Docker, Kubernetes & CI/CD
Lesson 431 of 8026. Kubernetes Traffic and ScalingFree lesson

Routing External Traffic via Custom Load Balancer Annotations

Apply provider-specific annotations to service manifests to fine-tune cloud load balancer behavior, including subnets, protocol settings, and traffic distribution algorithms.

In this lesson, you will learn how to fine-tune your cloud-provided load balancers by applying provider-specific annotations to your Kubernetes Service manifests.

When you create a type: LoadBalancer service, Kubernetes asks your cloud provider (like AWS, GCP, or Azure) to provision an external load balancer. By default, this is a "one-size-fits-all" configuration. However, real-world production environments often require specific behaviors, such as placing the load balancer in a private subnet, forcing the use of a specific TLS policy, or choosing a custom traffic distribution algorithm (like Least Request instead of Round Robin).

To achieve this, we use the metadata.annotations field in the Service manifest. These annotations are essentially key-value pairs that the cloud-controller-manager reads to override default provisioning settings.

Applying Custom Annotations

Imagine you are running on AWS and need to ensure your Load Balancer is internal-only and utilizes a specific security group. You would define your service manifest like this:

apiVersion: v1
kind: Service
metadata:
  name: my-app-service
  annotations:
    service.beta.kubernetes.io/aws-load-balancer-internal: "true"
    service.beta.kubernetes.io/aws-load-balancer-security-groups: "sg-12345678"
    service.beta.kubernetes.io/aws-load-balancer-target-group-attributes: "stickiness.enabled=true"
spec:
  selector:
    app: my-app
  type: LoadBalancer
  ports:
    - port: 80
      targetPort: 8080

In this example, the annotations instruct the cloud controller to bypass public IP assignment and strictly configure the load balancer’s target group behaviors.

Why this matters

These annotations bridge the gap between abstract Kubernetes declarations and the granular capabilities of your cloud provider. Without them, you would have to manually configure the load balancer in your cloud console, which creates "configuration drift"—a major hurdle in CI/CD pipelines. By keeping these settings in your manifest, you ensure the infrastructure is provisioned exactly as the application requires every time the deployment runs.

Key Takeaway: Custom annotations allow you to declare provider-specific infrastructure requirements directly in your Kubernetes manifests, ensuring that cloud-native load balancers are configured programmatically and consistently.