# Create an EKS Cluster with Nginx Application on AWS

## Introduction

Amazon Elastic Kubernetes Service (Amazon EKS) is a managed Kubernetes service that makes it easy for you to run Kubernetes on AWS and on-premises. Kubernetes is an open-source system for automating deployment, scaling, and management of containerized applications. Amazon EKS is certified Kubernetes-conformant, so existing applications that run on upstream Kubernetes are compatible with Amazon EKS.

Amazon EKS automatically manages the availability and scalability of the Kubernetes control plane nodes responsible for scheduling containers, managing application availability, storing cluster data, and other key tasks.

In this blog, I will step you through the process of deploying an nginx web server on EKS cluster.

## Pre-requisites

* AWS Account
    

## Step 1: Creating an IAM User with Admin Permissions

* Go to `IAM > Users` and click on "Add users"
    
* Specify the username for example `k8s-admin`, enable Programmatic access, and hit Next.
    
* Click on `Attach existing policies directly` and select the `AdministratorAccess` policy.
    
* Proceed till the end and click on `Create user`
    
* Copy both Access key ID and Secret access key and save them for later use.
    

## Step 2: Launching an EC2 Instance and Configuring the required CLI Tools

In this step, we will start by creating an EC2 instance, updating the AWS CLI version, and configuring the AWS CLI using the credentials of the user created in the previous step. We will then install both `eksctl` and `kubectl` on that EC2 instance. This instance will act as an admin workstation to run all administrative commands from.

* Go to `EC2` service and click on `Launch instance`.
    
* Follow below instructions:
    
    * Set Name: `k8s-admin`
        
    * Select AMI: `Amazon Linux 2 AMI`
        
    * Keep the default Instance Type `t2.micro`
        
    * Create new key pair with you preferred name and download it (Note: Select `.ppk` format if you are using Putty)
        
    * Edit `Network Settings` and enable `Auto-assign public IP`
        
    * Keep other settings as is and click `Launch instance`
        
* Wait till the Status check turns into `2/2 checks passed` and then click on `Connect`. Choose `EC2 Instance Connect` and press on `Connect` button to access the EC2 instance terminal.
    
* Follow the instructions from the AWS documentation [here](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) or follow the below steps to update the AWS CLI version:
    
    * Download the latest AWS CLI version
        

```yaml
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
```

* Unzip the installer
    

```yaml
unzip awscliv2.zip
```

* Run following to update the current version:
    

```yaml
sudo ./aws/install --bin-dir /usr/bin --install-dir /usr/local/aws-cli --update
```

* Verify version is updated
    

```yaml
aws --version
```

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1665239652272/XRT5oQv8r.png align="left")

* Run `aws configure` and set below values:
    
    * AWS Acsess Key ID and AWS Secret Access Key from previous step
        
    * Default Region name: `us-east-1`
        
    * Default output format: `json`
        
* Install `kubectl` with following commands:
    

```yaml
curl -o kubectl https://amazon-eks.s3.us-west-2.amazonaws.com/1.16.8/2020-04-16/bin/linux/amd64/kubectl
chmod +x ./kubectl
mkdir -p $HOME/bin && cp ./kubectl $HOME/bin/kubectl && export PATH=$PATH:$HOME/bin
```

* Ensure `kubectl` is installed
    

```yaml
kubectl version --short --client
```

* Install and configure `eksctl`
    

```yaml
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/bin
```

* Ensure `eksctl` is installed
    

```yaml
eksctl version
```

## Step 3: Provisioning an EKS Cluster

* Provision an EKS cluster with three worker nodes in `us-east-1`
    

```yaml
eksctl create cluster --name dev --region us-east-1 --nodegroup-name standard-workers --node-type t3.medium --nodes 3 --nodes-min 1 --nodes-max 4 --managed
```

It will take 10–15 minutes since it's provisioning the control plane and worker nodes, attaching the worker nodes to the control plane, and creating the VPC, security group, and Auto Scaling group.

* After the process is fully completed, check the cluster status by running the following command
    

```yaml
eksctl get cluster
```

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1665247108704/nnvKOVb6S.png align="left")

* Enable connecting to the cluster
    

```yaml
aws eks update-kubeconfig --name dev --region us-east-1
```

## Step 4: Creating an Nginx Deployment on the EKS Cluster

* Install Git
    

```yaml
sudo yum install -y git
```

* Download the configuration files
    

```yaml
git clone https://github.com/noweder/EKS-with-Nginx.git
```

* Move to the directory with configuration files
    

```yaml
cd EKS-with-Nginx
```

* Create the Load Balancer service
    

```yaml
kubectl apply -f ./nginx-svc.yaml
```

* Check the Load Balancer service status and copy the external DNS hostname of the load balancer, and paste it into a text file for later use
    

```yaml
kubectl get service
```

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1665247260114/GuVGLtSoY.png align="left")

* Create the deployment of 3 pods (3 Nginx containers)
    

```yaml
kubectl apply -f ./nginx-deployment.yaml
```

* Check the deployment status
    

```yaml
kubectl get deployment
```

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1665248435232/f_VNptwIZ.png align="left")

* View the created pods
    

```yaml
kubectl get pod
```

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1665248485198/5MDxP-ivx.png align="left")

* View the ReplicaSets
    

```yaml
kubectl get rs
```

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1665248525780/X5yXCmdF9.png align="left")

* View the EC2 worker nodes
    

```yaml
kubectl get node
```

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1665248552849/QG-SPiTBo.png align="left")

* Finally, you can access the Nginx application using the load balancer DNS address previously copied on a web browser, or by running the below command in CLI replacing `<LOAD_BALANCER_DNS_HOSTNAME>` with your specific Load Balancer DNS address
    

```yaml
curl <LOAD_BALANCER_DNS_HOSTNAME>
```

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1665247715181/TmuNujfyp.png align="left")

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1665247771294/VX4o97j00.png align="left")

If you get above results, it means that you have successfully accessed the Nginx application running in container which is deployed in the EKS cluster and exposed to the internet via the load balancer service.

Thank you for reading!
