How to create an Nginx ingress controller with aws classic load balancer in 2023

You are currently viewing How to create an Nginx ingress controller with aws classic load balancer in 2023

This article will discuss how to create 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. An AWS Classic Load Balancer, on the other hand, is a type of load balancer that is 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. At first, we need to create a cluster in EKS by running the below command. For creating the cluster I will use eksctl. Here is the command

curl --silent --location "https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_$(uname -s)_amd64.tar.gz" | tar xz -C /tmp
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: us-east-1

nodeGroups:
  - name: ng-spot-1
    instanceType: mixed
    instancesDistribution:
      instanceTypes: ["t3.large"] # List of instance types
      maxPrice: 0.05 # Maximum price for spot instances, in USD
      onDemandBaseCapacity: 0
      spotInstancePools: 2 # Number of spot instance pools
    minSize: 1
    maxSize: 4
    desiredCapacity: 2
    volumeSize: 20

Then run this command to create the cluster

eksctl create cluster -f cluster.yml

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 to install 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

Then install the Nginx-ingress controller with Classic load balancer.

helm install nginx-ingress ingress-nginx/ingress-nginx --namespace ingress-nginx --set controller.service.annotations."service\.beta\.kubernetes\.io/aws-load-balancer-type"=classic

Alternatively, you can install a Network Load balancer.

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.

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"
    kubernetes.io/ingress.class: 'nginx'
    nginx.ingress.kubernetes.io/use-regex: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  rules:
  - http:
      paths:
      - path: /atiq(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: atiq-service
            port:
              number: 80

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 also 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 different solutions. But the concept, the process, and the tools are the same and can be used in other setups as well.

Atiqur Rahman

I am MD. Atiqur Rahman graduated from BUET and is an AWS-certified solutions architect. I have successfully achieved 6 certifications from AWS including Cloud Practitioner, Solutions Architect, SysOps Administrator, and Developer Associate. I have more than 8 years of working experience as a DevOps engineer designing complex SAAS applications.

Leave a Reply