Sub topic 10
Exposing Services Through ALB and NLB
This chapter assumes you can write Deployments and Services, you know what an ALB (Application Load Balancer) and an NLB (Network Load Balancer) are, and you have a cluster from the earlier chapters. You also need a working pod networking setup, because the load balancers in this chapter send traffic straight to pod IPs.
What the AWS Load Balancer Controller does
Kubernetes does not create AWS load balancers by itself in a useful way. The AWS Load Balancer Controller is a component you install in the cluster. It watches for Ingress and Service objects and creates the matching AWS resources.
- An Ingress with
ingressClassName: albbecomes an Application Load Balancer. The ALB works at layer 7 (HTTP and HTTPS), so it can route by host name and path. - A Service of type LoadBalancer with the controller's annotations becomes a Network Load Balancer. The NLB works at layer 4 (TCP, UDP, TLS), so it fits non-HTTP protocols and cases that need static IP addresses.
The controller needs AWS permissions to create load balancers, target groups, listeners and security group rules. Give it those through IRSA or EKS Pod Identity, as covered in the previous chapter on pod permissions. It also needs to discover the subnets it should use, which is the next section.
Subnet tags and the traffic path
The controller picks subnets by tag. If the tags are missing or wrong, the load balancer is not created, or it lands in the wrong subnets.
| Tag | Value | Meaning |
|---|---|---|
| `kubernetes.io/role/elb` | `1` | Public subnet, used for internet-facing load balancers |
| `kubernetes.io/role/internal-elb` | `1` | Private subnet, used for internal load balancers |
Clusters created with eksctl normally get these tags on their subnets. If you bring your own VPC, add them yourself. Each load balancer needs subnets in at least two Availability Zones.
The diagram shows how a request reaches a pod when the target type is ip.
Instance targets and IP targets
A target group holds the destinations the load balancer sends traffic to. The controller supports two target types.
| Target type | What gets registered | Path of a request |
|---|---|---|
| `instance` | Worker nodes, on the Service's NodePort | Load balancer, node, kube-proxy, pod (possibly on another node) |
| `ip` | Pod IPs directly | Load balancer, pod |
With ip targets there is no extra hop through a NodePort, and the load balancer sees real pod health. This works because the Amazon VPC CNI gives each pod a real VPC IP address. It also works with Fargate pods, which have no NodePort path. With instance targets, the Service must be of type NodePort or LoadBalancer.
For most new clusters, choose ip.
Creating an ALB with an Ingress
First deploy the web Deployment and Service from the earlier chapter. Then create this Ingress:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: web
annotations:
alb.ingress.kubernetes.io/scheme: internet-facing
alb.ingress.kubernetes.io/target-type: ip
spec:
ingressClassName: alb
rules:
- http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: web
port:
number: 80The two annotations carry the AWS-specific choices:
scheme: internet-facingputs the ALB in public subnets. The default isinternal, which puts it in private subnets and gives it no public access. People often forget this and wonder why the address does not answer from the internet.target-type: ipregisters pod IPs, as described above.
Apply it and wait for the address:
kubectl apply -f ingress.yaml
kubectl get ingress webThe ADDRESS column stays empty for a short time, then shows the ALB's DNS name. DNS and target registration take another minute or two, so a request sent immediately may fail.
Useful ALB annotations
The controller reads many annotations. These are the ones you will use most.
| Annotation | Purpose |
|---|---|
| `alb.ingress.kubernetes.io/listen-ports` | Which listeners to create, for example `[{"HTTP":80},{"HTTPS":443}]` |
| `alb.ingress.kubernetes.io/certificate-arn` | ACM certificate for HTTPS |
| `alb.ingress.kubernetes.io/ssl-redirect` | Redirect HTTP to HTTPS on the given port |
| `alb.ingress.kubernetes.io/healthcheck-path` | Path the target group health check requests |
| `alb.ingress.kubernetes.io/group.name` | Share one ALB between several Ingress objects |
Sharing an ALB with group.name is a cost control. Each ALB has an hourly charge, so one ALB serving ten services costs far less than ten ALBs.
The health check matters more than it looks. The default checks / and expects an HTTP 200 response. If your application returns a redirect or a 404 on /, the targets are marked unhealthy and the ALB returns 503 errors. Set healthcheck-path to an endpoint that returns 200, and success-codes if you need to accept other status codes.
Creating an NLB with a Service
For TCP or UDP traffic, or when you need a fixed IP per Availability Zone, use a Service of type LoadBalancer handled by the controller:
apiVersion: v1
kind: Service
metadata:
name: web-nlb
annotations:
service.beta.kubernetes.io/aws-load-balancer-type: external
service.beta.kubernetes.io/aws-load-balancer-nlb-target-type: ip
service.beta.kubernetes.io/aws-load-balancer-scheme: internet-facing
spec:
type: LoadBalancer
selector:
app: web
ports:
- port: 80
targetPort: 80Setting aws-load-balancer-type to external tells Kubernetes to leave the Service to the AWS Load Balancer Controller. Without it, the older built-in cloud provider code may create a Classic Load Balancer or a legacy NLB instead, with different behavior. Check which one you got by looking at the load balancer type in the EC2 console or with aws elbv2 describe-load-balancers.
An NLB preserves the client source IP when the target type is ip, which an ALB does not. An ALB passes the original address in the X-Forwarded-For header.
Choosing between ALB and NLB
| Need | Choose |
|---|---|
| Path or host-based routing, many services behind one address | ALB |
| HTTP features such as redirects, WAF, OIDC authentication | ALB |
| Non-HTTP protocols, UDP, or very high connection rates | NLB |
| Static IP addresses per Availability Zone | NLB |
| Lowest added latency | NLB |
Security groups and common failures
The controller manages security groups for you. It creates one for the load balancer and adds a rule to the node or pod security group that allows traffic from the load balancer to the target port. If you manage security groups yourself, that rule must exist or health checks time out.
Most problems fall into a short list.
- No ADDRESS on the Ingress. Run
kubectl describe ingress weband look at the events. Typical causes are missing subnet tags, a missing or incorrect IAM permission for the controller, or the controller not running. Check its logs withkubectl logs -n kube-system deployment/aws-load-balancer-controller. - Targets unhealthy. The health check path returns a non-200 code, the container port differs from
targetPort, or a security group blocks traffic from the load balancer. - 503 from the ALB. The target group has no healthy targets, often because the Service selector matches no pods. Run
kubectl get endpoints webto confirm it lists pod IPs. - Dropped requests during deployments. A pod can be removed while the load balancer still sends it traffic. Add a short
preStopsleep to the container and consider the controller's pod readiness gates, which hold a pod as not ready until the load balancer reports its target healthy. - Wrong scheme. An
internalload balancer cannot be reached from the internet. Changing the scheme annotation on an existing load balancer may force the controller to replace it, so the DNS name changes.
The Service type ClusterIP that you used earlier is not needed for ip targets to work, but the Ingress backend must still reference a Service, because that is how the controller finds the pods through the Service's endpoints.
Exercise
You create an Ingress with ingressClassName: alb and target-type: ip but no scheme annotation. The ALB is created, yet requests from your laptop time out. What is the likely cause, and how do you fix it?
Show answer
The scheme annotation defaults to internal, so the ALB sits in private subnets and has no public route. Add alb.ingress.kubernetes.io/scheme: internet-facing and make sure the public subnets carry the kubernetes.io/role/elb tag. Because the scheme of an existing load balancer generally cannot be changed in place, the controller may create a new ALB with a new DNS name.
Exercise
Your ALB returns 503 and the target group shows all targets unhealthy. The application answers 200 on /healthz and 404 on /. What do you change?
Show answer
Set alb.ingress.kubernetes.io/healthcheck-path: /healthz on the Ingress. The default health check requests / and expects 200, so the 404 marks every target unhealthy. Also confirm with kubectl get endpoints web that the Service has pod IPs.