Configuring support for AWS Elastic Load Balancers

Earlier in this chapter, when you defined your Kubernetes configuration for the todobackend application, you created a service for the todobackend application of type LoadBalancer. We discussed that the implementation details of the load balancer are specific to the platform that your Kubernetes cluster is deployed to, and in the case of Docker Desktop, Docker provides their own load balancer component that allows the service to be exposed to the local network interface on your development machine.

When using EKS, the good news is that you don't need to do anything to support services of type LoadBalancer your EKS cluster will automatically create and associate an AWS Elastic Load Balancer with each service endpoint, with the AmazonEKSClusterPolicy managed policy granting the required IAM permissions for this. 

Kubernetes does allow you configure vendor-specific features of the LoadBalancer type by configuring annotations, which are a metadata property that will be understood by a given vendor on their target platform and ignored if deploying on a different platform, such as your local Docker Desktop environment. You can read more about these annotations at https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services-service-types, and the following example demonstrates adding several annotations that are specific to the AWS Elastic Load Balancer to the service definition within the todobackend/k8s/app/deployment.yaml file:

apiVersion: v1
kind: Service
metadata:
name: todobackend
annotations:
service.beta.kubernetes.io/aws-load-balancer-backend-protocol: "http"
service.beta.kubernetes.io/aws-load-balancer-connection-draining-enabled: "true"
service.beta.kubernetes.io/aws-load-balancer-connection-draining-timeout: "60"
spec:
selector:
app: todobackend
ports:
- protocol: TCP
port: 80
targetPort: 8000
type: LoadBalancer
---
...
...

In the preceding example, we added the following annotations:

One important point to note is that the annotations expect every value to be a string value, so ensure that you quote boolean values such as "true" and "false", as well as any numeric values such as "60", as demonstrated in the preceding code.