Skip to main content

AWS — Deploying React App With NGINX on EKS

A step by step guide with an example project


  • Example Project
  • Prerequisites
  • Dockerize the Project
  • Pushing Docker Image To ECR
  • Create a Cluster and Worker Nodes
  • Configure kubectl to use Cluster
  • Deploy Kubernetes Objects On AWS EKS Cluster
  • Summary
  • Conclusion

Example Project

Example Project
// clone the project
git clone https://github.com/bbachi/react-nginx-example.git// install React dependencies and start
cd my-app
npm install
npm start


Prerequisites

  • If you are new to React please go through the below link on how to develop and build the React project with the NGINX web server.

Docker Essentials

Kubernetes Essentials

AWS account

  • AWS account setup: AWS offers a free tier for one year here is the link to set it up.
  • Once you set it up you have a root account. It’s not a best practice to use your root account to do any tasks instead you should create an IAM group that has permissions for administrator access and add a user to it and log in with that user.
  • Install AWS CLI
  • Configure AWS CLI for the user you just created above. You should use this command aws configure and it will ask for access key id and secret key.
log in with user credentials
  • You have to install Docker for Desktop (whatever your OS is). Please follow this link to install Docker on your laptop. Once installed you can check the Docker info or version with the following commands.
docker info
docker --version

Dockerize the Project

FROM node:10 AS ui-build
WORKDIR /usr/src/app
COPY my-app/ ./my-app/
RUN cd my-app && npm install && npm run build


FROM nginx:alpine

#!/bin/sh

COPY ./.nginx/nginx.conf /etc/nginx/nginx.conf

## Remove default nginx index page
RUN rm -rf /usr/share/nginx/html/*

# Copy from the stahg 1
COPY --from=ui-build /usr/src/app/my-app/build/ /usr/share/nginx/html

EXPOSE 4200 80

ENTRYPOINT ["nginx", "-g", "daemon off;"]

Dockerfile
// build the image
docker build -t react-nginx-ui .// run the image
docker run -d --name react-nginx-webapp -p 80:80 react-nginx-ui// list the image you just built
docker images// list the container
docker ps

Pushing Docker Image To ECR

AWS console

creating repository
repository

AWS CLI

aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin aws_account_id.dkr.ecr.us-east-1.amazonaws.com
Authenticating to ECR
aws ecr create-repository \     
--repository-name frontend/web-app \
--image-scanning-configuration scanOnPush=
true \
--image-tag-mutability IMMUTABLE \
--region
us-east-2
Creating repository
Repository

Tagging your local Docker image and Pushing

docker tag react-nginx-ui:latest 864227929192.dkr.ecr.us-east-2.amazonaws.com/frontend/web-app:v1
// list the images
docker images// push the image
docker push 864227929192.dkr.ecr.us-east-2.amazonaws.com/frontend/web-app:v1
pushing Docker image
Docker image with tag v1

Create a Cluster and Worker Nodes

  • We need to create an AWS EKS cluster with AWS console, SDK, or AWS CLI.
  • Create a worker node group that registers with EKS Cluster
  • When your cluster is ready, you can configure kubectl to communicate with your cluster.
  • Deploy and manage your applications on the cluster

Cluster Creation

eks_cluster_role
Creating Cluster
Cluster Created

Create Worker Nodes

NodeInstanceRole
Node Group is active

Configure kubectl to use Cluster

  • We need to install kubectl on our machine, follow this guide to install depending on your OS.
  • The next thing we need to do is to install an aws-iam-authenticator. Follow this guide. We need this to authenticate the cluster and it uses the same user as AWS CLI is authenticated with.
  • Use the AWS CLI update-kubeconfig command to create or update your kubeconfig for your cluster. Here region-code is us-east-2 and cluster_name is frontend_clutser
aws eks --region region-code update-kubeconfig --name cluster_name
connected to the cluster
// get the service
kubectl get nodes// get the current context
kubectl config current-context
connected to cluster

Deploy Kubernetes Objects On AWS EKS Cluster

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: nginx-webapp
  name: nginx-webapp
spec:
  replicas: 5
  selector:
    matchLabels:
      app: nginx-webapp
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx-webapp
    spec:
      containers:
      - image: 864227929192.dkr.ecr.us-east-2.amazonaws.com/frontend/web-app:v1
        name: webapp
        imagePullPolicy: Always
        resources: {}
        ports:
          - containerPort: 80 
status: {}

---

apiVersion: v1
kind: Service
metadata:
  name: nginx-webapp
  labels:
    run: nginx-webapp
spec:
  ports:
  - port: 80
    protocol: TCP
  selector:
    app: nginx-webapp
  type: LoadBalancer

kubectl create -f manifest.yml
// list the deployment
kubectl get deploy// list the pods
kubectl get po// list the service
kubectl get svc
all objects are deployed

Summary

  • Amazon Elastic Kubernetes Service (Amazon EKS) is a managed service that makes it easy for you to run Kubernetes on AWS without needing to stand up or maintain your own Kubernetes control plane.
  • You need to create an AWS Account as a prerequisite.
  • It’s not a best practice to use your root account to do any tasks instead you should create an IAM group that has permissions for administrator access and add a user to it and log in with that user.
  • You should use this command aws configure with access key and secret key.
  • Amazon EKS is a managed service that makes it easy for you to run Kubernetes on AWS.
  • Amazon Elastic Container Registry (ECR) is a fully-managed Docker container registry that makes it easy for developers to store, manage, and deploy Docker container images.
  • Amazon ECR is integrated with Amazon Elastic Container Service (ECS), simplifying your development to production workflow.
  • Amazon ECS works with any Docker registry such as Docker Hub, etc.
  • You have to follow these steps to run apps on the Kubernetes cluster: we need to create an AWS EKS cluster with AWS console, SDK, or AWS CLI. Create a worker node group that registers with EKS Cluster, when your cluster is ready, you can configure kubectl to communicate with your cluster, Deploy and manage your applications on the cluster
Conclusion