Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 66

Exposing API Gateway with AWS Load Balancer Ingress

This lesson is built with resources from youtube.com, oneuptime.com and 1 more site.

Welcome to the next lesson on deploying our Blog Platform to Kubernetes!

In our previous session, we successfully deployed all our backend microservices—Auth, Blog, and the API Gateway—to our EKS cluster. Each service is running in its own Deployment and is discoverable through an internal ClusterIP Service. However, our application is still completely sealed off from the outside world. Today, we will change that.

Our goal is to expose our API Gateway to external traffic using a Kubernetes Ingress and an AWS Application Load Balancer (ALB). This is the final and crucial step to making our backend accessible over the internet.

Learning Goals for This Lesson

  • Understand the roles of a Kubernetes Ingress and the AWS Load Balancer Controller.
  • Write an Ingress manifest to route external traffic to our gateway-service.
  • Apply the manifest and verify that an AWS ALB is provisioned and correctly configured.
  • Learn how to debug common issues related to Ingress and the Load Balancer Controller.

MERN Stack Comparison

In the MERN world, when you deploy a backend API, you need a way to route public traffic to your Node.js process. You might put a reverse proxy like NGINX in front of it or use a managed service like AWS Elastic Load Balancing (ELB) or Vercel's routing layer. You configure this proxy with rules, for instance, "all traffic to api.myblog.com should go to my server running on port 3000."

A Kubernetes Ingress is the declarative, Kubernetes-native way to define these rules. You write a YAML file that says, "I want traffic for path / to go to my gateway-service." A special piece of software called an Ingress Controller then reads this file and automatically configures the underlying infrastructure (in our case, an AWS Application Load Balancer) to make it happen.



1. Understanding Ingress and the AWS Load Balancer Controller

Before we write any code, let's clarify the key components.

  • Ingress: A Kubernetes resource that defines rules for routing external HTTP/S traffic to services within the cluster. It can handle path-based routing, host-based routing, and TLS termination.
  • Ingress Controller: A program running in your cluster that watches for Ingress resources and uses them to configure a load balancer. It's the engine that implements the Ingress rules.
  • AWS Load Balancer Controller: The specific Ingress Controller we use for EKS. It integrates directly with AWS APIs. When it sees an Ingress resource, it automatically provisions and configures an AWS Application Load Balancer (ALB) for you.

This architecture is powerful because you manage your cloud infrastructure directly from your Kubernetes manifests, keeping everything version-controlled and in one place.

AWS ALB and Kubernetes Ingress Controller Integration
This diagram shows exactly what we're building. The AWS Load Balancer Controller (1) watches for Ingress resources. When we create one, it provisions an AWS ALB (2) and configures it with rules (5) to forward traffic from the internet to the correct Kubernetes Service (our `gateway-service`).

To understand the benefits of this controller-based approach over older methods, it's worth watching a short introduction.

AWS Load Balancer Controller Tutorial (TLS): AWS EKS Kubernetes Tutorial - Part 6

The first couple of minutes of this video from Anton Putra provide an excellent explanation of why the AWS Load Balancer Controller exists and the advantages it offers over the legacy, in-tree Kubernetes controller.

Watch from the beginning until 02:24. Focus on understanding the limitations of the old system (slow release cycle, NodePort inefficiency) and the benefits of the new controller (independent releases, direct-to-pod routing).



2. Creating the Ingress Manifest

Now, let's write the manifest that will expose our gateway-service.

In your kube directory, create a new file named ingress.yaml.




# kube/ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: gateway-ingress
  annotations:



    # Specifies that the AWS Load Balancer Controller should handle this Ingress
    kubernetes.io/ingress.class: "alb" 



    # Creates a public, internet-facing Load Balancer
    alb.ingress.kubernetes.io/scheme: internet-facing



    # Routes traffic directly to Pod IPs, which is more efficient
    alb.ingress.kubernetes.io/target-type: ip
spec:
  rules:



    # Rule for all HTTP traffic
    - http:
        paths:



          # A catch-all rule for any path
          - path: /
            pathType: Prefix
            backend:
              service:



                # The name of the Service to route to
                name: gateway-service



                # The port of the Service to route to
                port:
                  number: 80

Let's break down the most important parts:

  • metadata.annotations: This is where the magic happens for the AWS Load Balancer Controller.

    • kubernetes.io/ingress.class: "alb": This annotation is deprecated in favor of spec.ingressClassName, but is still widely used and tells Kubernetes that the alb controller is responsible for this Ingress.
    • alb.ingress.kubernetes.io/scheme: internet-facing: This provisions a public ALB with a public IP address. The alternative is internal for private traffic within your VPC.
    • alb.ingress.kubernetes.io/target-type: ip: This is a key optimization. It instructs the ALB to register the private IPs of your gateway-service Pods directly as targets. This bypasses an extra network hop through the Kubernetes kube-proxy, resulting in lower latency and better performance.
  • spec.rules: This defines the routing logic.

    • We create a single rule that matches any path (path: /, pathType: Prefix).
    • The backend specifies that all matching traffic should be sent to the Service named gateway-service on its port 80.

For a clear, concise text reference on Ingress manifests and their annotations, the following article is very helpful.

How To Expose Multiple Applications on Amazon EKS Using a ...

The blog post 'How To Expose Multiple Applications on Amazon EKS...' provides a practical walkthrough. The section on creating the Ingress file is particularly relevant.

Read part 'C) Finally, let’s define our Ingress' in the article. Compare their Ingress manifest with ours. Note the similarities in annotations and the spec.rules structure. This reinforces the common patterns for defining an ALB Ingress.



3. Apply, Verify, and Test

It's time to bring our application online.

Prerequisite: The AWS Load Balancer Controller must be installed in your cluster. If you followed the standard eksctl cluster creation process, it may already be there. You can check with:

kubectl get deployment -n kube-system aws-load-balancer-controller

If the command returns a deployment, you're good to go. If not, you will need to install it. The videos referenced provide detailed instructions on this one-time setup.

  1. Apply the Ingress Manifest:
    From your project's root directory, run:

    kubectl apply -f kube/ingress.yaml
    

    Output should be ingress.networking.k8s.io/gateway-ingress created.

  2. Check the Ingress Status:
    The controller will now start provisioning the ALB in AWS. This can take a few minutes. You can monitor its progress with:

    kubectl get ingress gateway-ingress
    

    Initially, the ADDRESS column will be empty. Wait and run the command again. Once provisioning is complete, you will see the public DNS name of your new ALB:

    NAME              CLASS    HOSTS   ADDRESS                                                                  PORTS   AGE
    gateway-ingress   <none>   *       k8s-default-gatewayi-xxxxxxxx-xxxxxxxx.us-east-1.elb.amazonaws.com   80      2m
    

    Copy this address. This is the public entry point to your entire application!

  3. Test the Endpoint:
    Let's assume your API Gateway routes requests to /api/blogs to the blog-service. You can now test this publicly using curl:

    curl http://<your-alb-address>/api/blogs
    

    If everything is configured correctly, you should receive a response (likely an empty list [] if your database is empty) from your blog-service, served through the API Gateway and the AWS Load Balancer.

Congratulations! Your microservices application is now live on the internet.



4. Debugging-First: When the Ingress Address Stays Empty

One of the most common problems is applying an Ingress manifest and seeing the ADDRESS field remain empty indefinitely.

  • Cause: This almost always means the AWS Load Balancer Controller is not running, has crashed, or lacks the correct IAM permissions to create resources in your AWS account.

  • How to Debug:

    1. Check the Controller Logs: This is your primary source of truth. The controller runs in the kube-system namespace.

      kubectl logs -n kube-system deployment/aws-load-balancer-controller
      

      Look for any error messages. You might see AccessDenied errors if IAM permissions are wrong, or other errors related to misconfiguration.

    2. Describe the Ingress: Get the events associated with your Ingress resource.

      kubectl describe ingress gateway-ingress
      

      Scroll to the bottom Events section. You might see an error message directly attached to the Ingress, giving you a clue. For example: Error syncing load balancer: failed to ensure load balancer: ...

    3. Check Controller Pod Status: Make sure the controller pod itself is running.

      kubectl get pods -n kube-system | grep aws-load-balancer-controller
      

      If it's CrashLoopBackOff or has 0/1 in the READY column, you need to debug the pod itself using kubectl describe pod ... in the kube-system namespace.

The "How to Use AWS Load Balancer Controller" article has an excellent section on troubleshooting that lists these commands and more. It's a great page to bookmark.

How to Use AWS Load Balancer Controller for ALB and NLB on EKS

The 'Monitoring and Troubleshooting' section of this article by OneUptime is a concise and valuable reference for debugging.

Review the commands listed under 'Monitoring and Troubleshooting'. You don't need to run them all now, but familiarize yourself with what they do. kubectl logs and kubectl describe ingress are your first go-to commands for any Ingress-related issue.



Conclusion

In this lesson, we successfully bridged the gap between our internal Kubernetes cluster and the public internet. By creating a single Ingress manifest, we instructed the AWS Load Balancer Controller to provision and configure a powerful Application Load Balancer, routing all external traffic to our API Gateway.

Key Takeaways:

  • An Ingress resource is Kubernetes' standard way to define HTTP/S routing rules.
  • The AWS Load Balancer Controller acts on Ingress resources to manage AWS ALBs automatically.
  • Annotations on the Ingress manifest are used to configure specific behaviors of the ALB (e.g., internet-facing, ip target type).
  • The ADDRESS field of kubectl get ingress provides the public DNS name for your application.
  • When debugging, always start by checking the logs of the aws-load-balancer-controller.

Preview of the Next Lesson:
Our application is now fully deployed and functional. However, what happens when things go wrong inside a pod? In the next lesson, we will focus entirely on debugging running applications in Kubernetes. We'll intentionally introduce some common errors and learn how to use kubectl logs, kubectl describe pod, and kubectl exec to systematically diagnose and fix them, honing the critical skills you'll need in a production environment.