Monday, November 21, 2022

Configuring Nginx Ingress Controller in Amazon EKS Cluster: A Step-by-Step Guide

Introduction:

Amazon Elastic Kubernetes Service (Amazon EKS) provides a robust platform for managing containerized applications, and one essential component for enabling external access to multiple Kubernetes services is the Ingress Controller. In this blog post, we'll explore the setup of the Nginx Ingress Controller in an Amazon EKS cluster. Before diving into the configuration, let's ensure we have the necessary prerequisites in place.

Prerequisites:

  1. AWS CLI Installation:

    • Install AWS CLI on your local machine (compatible with Windows and Linux).
  2. Kubectl Installation:

    • Install kubectl on your local machine to interact with your Kubernetes cluster.
  3. Docker Installation:

    • Install Docker on your local machine to build and manage containers.

Nginx Ingress Setup in Kubernetes:

Step 1: Connect to AWS EKS Cluster

bash
$ aws configure

Follow the prompts to enter your AWS Access Key ID, Secret Access Key, and default region.

bash
$ kubectl get ns

Verify the EKS cluster connection by listing the available namespaces.

Step 2: Clone Nginx Ingress Repository

bash
$ git clone https://github.com/nginxinc/kubernetes-ingress.git
$ cd kubernetes-ingress/deployments/

Step 3: Deploy Nginx Ingress Controller

bash
$ kubectl apply -f common/ns-and-sa.yaml
$ kubectl apply -f common/default-server-secret.yaml
$ kubectl apply -f common/nginx-config.yaml
$ kubectl apply -f rbac/rbac.yaml

# If Kubernetes version is >= 1.18, create an NGINX Ingress class
$ kubectl apply -f common/ingress-class.yaml

Note: Update the apiVersion in common/ingress-class.yaml if needed.

Step 4: Deploy Nginx Ingress Controller (Fixed CrashLoopBackOff Issue)

bash
$ kubectl apply -f common/crds/k8s.nginx.org_virtualserverroutes.yaml
$ kubectl apply -f common/crds/k8s.nginx.org_globalconfigurations.yaml
$ kubectl apply -f common/crds/k8s.nginx.org_policies.yaml
$ kubectl apply -f common/crds/k8s.nginx.org_transportservers.yaml
$ kubectl apply -f common/crds/k8s.nginx.org_virtualservers.yaml

# Deploy the Ingress Controller
$ kubectl apply -f deployment/nginx-ingress.yaml

Step 5: Apply Configuration for Load Balancer

bash
$ kubectl apply -f service/loadbalancer-aws-elb.yaml
$ kubectl get svc --namespace=nginx-ingress

Verify the successful deployment, and note the LoadBalancer's external DNS for external access.

Common Error and Solution:

bash
$ kubectl apply -f common/ingress-class.yaml

NOTE: When applying this, you may encounter the following error:

vbnet
error: resource mapping not found for name: "nginx" namespace: "" from "common/ingress-class.yaml": no matches for kind "IngressClass" in version "networking.k8s.io/v1beta1"

Solution: Update the apiVersion: networking.k8s.io/v1 in common/ingress-class.yaml file and reapply it; it will work.

bash
$ kubectl apply -f deployment/nginx-ingress.yaml

NOTE: I Applied successfully but got a CrashLoopBackOff error.

bash
nginx-ingress nginx-ingress-5b95958c76-lcjtz 0/1 CrashLoopBackOff 4 (72s ago) 2m42s

When I checked logs/Events, I saw see below error:

less
main.go:300] Error when getting IngressClass nginx: the server could not find the requested resource

By seeing this, I understood some resources were still missing. So when I was browsing, I found that I missed the CRD creation for the ingress controller.

SOLUTION:

As I mentioned, I installed the below files, and after a successful deployment, it worked fine.

bash

   $ kubectl apply -f common/crds/k8s.nginx.org_virtualserverroutes.yaml 

   $ kubectl apply -f common/crds/k8s.nginx.org_globalconfigurations.yaml 

   $ kubectl apply -f common/crds/k8s.nginx.org_policies.yaml 

   $ kubectl apply -f common/crds/k8s.nginx.org_transportservers.yaml 

   $ kubectl apply -f common/crds/k8s.nginx.org_virtualservers.yaml


Error: CrashLoopBackOff for Nginx Ingress Controller Pods

If you encounter a CrashLoopBackOff error for the Nginx Ingress Controller Pods, check the logs/events for the following error:

bash
main.go:300] Error when getting IngressClass nginx: the server could not find the requested resource

Solution:

Update the apiVersion: networking.k8s.io/v1 in common/ingress-class.yaml file, and reapply it.

Conclusion:

Configuring the Nginx Ingress Controller in an Amazon EKS cluster is a crucial step for enabling external access to your Kubernetes services. By following this step-by-step guide, addressing potential errors, and applying the provided solutions, you can successfully deploy and manage your Ingress Controller. This enhances the scalability and accessibility of your containerized applications. For more details, refer to the official NGINX documentation

Featured Post

Ansible Tool Introduction

                                                                                                                                    Next ...