This article will discuss creating an Nginx Ingress controller with an AWS Classic Load Balancer. An Nginx Ingress controller is a way to route external traffic to multiple services within a Kubernetes cluster. On the other hand, an AWS Classic Load Balancer is a type of load balancer used to distribute incoming traffic across multiple instances in the AWS ecosystem. Together, these two tools can create a highly available and scalable infrastructure for your applications.
To create an NGINX ingress controller with an AWS Classic Load Balancer (CLB) on an EKS cluster, you can follow these steps:
1. First, we must create a cluster in EKS by running the command below. For creating the cluster I will use eksctl. Here is the command
# for ARM systems, set ARCH to: `arm64`, `armv6` or `armv7`
ARCH=amd64
PLATFORM=$(uname -s)_$ARCH
curl -sLO "https://github.com/eksctl-io/eksctl/releases/latest/download/eksctl_$PLATFORM.tar.gz"
# (Optional) Verify checksum
curl -sL "https://github.com/eksctl-io/eksctl/releases/latest/download/eksctl_checksums.txt" | grep $PLATFORM | sha256sum --check
tar -xzf eksctl_$PLATFORM.tar.gz -C /tmp && rm eksctl_$PLATFORM.tar.gz
sudo mv /tmp/eksctl /usr/local/bin
create cluster.yml file with the following data. ( change it based on your needs )
apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig
metadata:
name: atiqur-cluster
region: ap-southeast-1
nodeGroups:
- name: ng-spot-1
instanceType: t3.large
desiredCapacity: 2
Then run this command to create the cluster
eksctl create cluster -f cluster.yml
Alternatively, you can create a cluster with this one-line command ( Simpler version )
eksctl create cluster --name=cluster-1 --version=1.31
The EKS cluster creation will take around 5-10 mins. Once the cluster is created then copy the cluster name and run the following command to update the “kubectl” with cluster information
aws eks update-kubeconfig --region {aws-region-name} --name {eks-cluster-name}
Before you run, replace the region-name and cluster-name parameters with your current region and cluster name.
Create a service account and role binding for the ingress controller. This will allow the ingress controller to interact with the Kubernetes API server.
kubectl create serviceaccount nginx-ingress-controller
kubectl create clusterrolebinding nginx-ingress-controller --clusterrole=cluster-admin --serviceaccount=default:nginx-ingress-controller
2. Create a namespace for the ingress controller
kubectl create namespace ingress-nginx
3. Use Helm to install the ingress controller
First, install Helm. The instructions for installing Helm on AWS are given here.
Then add the repo of the nginx ingress controller.
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update
Install a Network Load balancer. This is the latest Load balancer from AWS
helm install nginx-ingress ingress-nginx/ingress-nginx --namespace ingress-nginx --set controller.service.annotations."service\.beta\.kubernetes\.io/aws-load-balancer-type"=nlb
4. Verify that the ingress controller is running by using the “kubectl get pods
-n ingress-nginx” command and checking for pods with the name nginx-ingress-controller-*
.
5 Here is one sample deployment file that will create one service and link it with the Nginx ingress in a path.
nano deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-app
labels:
app: test-app
spec:
replicas: 3
selector:
matchLabels:
app: test-app
template:
metadata:
labels:
app: test-app
spec:
containers:
- name: test-app
image: nginx:latest
ports:
- containerPort: 80
resources:
limits:
cpu: 100m
memory: 128Mi
requests:
cpu: 50m
memory: 64Mi
---
apiVersion: v1
kind: Service
metadata:
name: atiq-service
spec:
type: NodePort
ports:
- port: 80
selector:
app: test-app
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: test-app-ingress
annotations:
nginx.ingress.kubernetes.io/ssl-redirect: "false"
nginx.ingress.kubernetes.io/use-regex: "true"
nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
ingressClassName: nginx
rules:
- http:
paths:
- path: /atiq(/|$)(.*)
pathType: Prefix
backend:
service:
name: atiq-service
port:
number: 80
Run the following command to deploy the container to eks
kubectl apply -f deployment.yml
Conclusion
Following the steps outlined in this article, you should now have a working Nginx Ingress controller and AWS Classic Load Balancer set up for your Kubernetes cluster. This setup will allow you to easily route external traffic to multiple services within your cluster while providing high availability and scalability for your applications. However, it’s worth noting that this is just one way to set up a load balancer, and different situations may call for various solutions. However, the concept, the process, and the tools are the same and can be used in other setups.