Deployment Of NodeJs on EKS using Fargate

Step 1: Creating Ec2 instance

  • Create an EC2 instance using aws console (ubuntu t2.micro) and download the private key
  • Name the private key file ec2-control.pem.
  • Move that file to home folder.
    mv <path to the private key file> ~
  • Restrict all of the permissions of that file except read.
    chmod 400 ~/ec2-control.pem
  • SSH into ec2 instance using that private key file.
    ssh -i ~/ec2-control.pem ubuntu@<ip>
    Note: ip is the IPv4 address of Ec2 instance. This can be copied from the aws console.

Step 2: Installing Dependencies in the the VM.

  • In the EC2 install aws-cli, eksctl, kubectl and helm.
  • Log into aws-cli using IAM user (using IAM user with restricted permissions is recommended)
    aws configure

Step 3: Creating cluster and configuring kubectl

Step 4: Creating IAM Role and Service Account for Load Balancer

  • Create IAM OIDC provider:

    eksctl utils associate-iam-oidc-provider \
    --region ${AWS_REGION} \
    --cluster ${CLUSTER_NAME} \
    --approve
  • Download IAM policy for load-balancer (excluding us-east)

    curl -o iam_policy.json https://raw.githubusercontent.com/kubernetes-sigs/aws-load-balancer-controller/v2.4.1/docs/install/iam_policy.json
  • Create a policy called AWSLoadBalancerControllerIAMPolicy

    aws iam create-policy \
    --policy-name AWSLoadBalancerControllerIAMPolicy \
    --policy-document file://iam_policy.json
  • Create IAM service account

    eksctl create iamserviceaccount \
    --cluster=${CLUSTER_NAME} \
    --namespace=kube-system \
    --name=aws-load-balancer-controller \
    --role-name "AmazonEKSLoadBalancerControllerRole" \
    --attach-policy-arn=arn:aws:iam::${ACCOUNT_ID}:policy/AWSLoadBalancerControllerIAMPolicy \
    --approve
  • Annotate your service account

    kubectl annotate serviceaccount -n kube-system aws-load-balancer-controller \
    eks.amazonaws.com/sts-regional-endpoints=true

Step 5: Installing Load-Balancer using helm

  • Add the eks-charts repository.
    helm repo add eks https://aws.github.io/eks-charts
  • Update local repo
    helm repo update
  • Install the AWS Load Balancer Controller
    helm install aws-load-balancer-controller eks/aws-load-balancer-controller \
    -n kube-system \
    --set clusterName=cluster-name \
    --set serviceAccount.create=false \
    --set serviceAccount.name=aws-load-balancer-controller  \
    --set region=region-code
    --set vpcId=vpc-xxxxxxxx
  • Verify that controller is installed successfully
    kubectl get deployment -n kube-system aws-load-balancer-controller
    Output
    NAME                           READY   UP-TO-DATE   AVAILABLE   AGE
    aws-load-balancer-controller   2/2     2            2           34s

Step 6: Create Config-Map yaml file

  • Create config-map.yml file
    nano config-map.yml
  • Copy the contents of config-map.yml, write your environment variables under data section save the file (Ctrl+X).
  • Apply the file using kubectl.
    kubectl apply -f config-map.yml

Step 7: Create Service and Deployment

  • Create a file named nginx.yml and open it in text editor.
    nano nginx.yml
  • Copy the contents of nginx.yml and make necessary changes according to your application.
  • Apply this file using kubectl
    kubectl apply -f nginx.yml
  • Check the service and deployments using kubectl.
    kubectl get <service/deployment> --all-namespaces
    For a particular namespace
    kubectl get <service/deployment> -namespace <namespace>

Step 8: Create Ingress Controller

  • Create ingress.yml and copy the contents of ingress.yml in it.
    nano ingress.yml
  • Apply this ingress file using kubectl.
    kubectl apply -f ingress.yml
  • Check the ingress controller is running and get the url for the application.
    kubectl get ingress --all-namespaces
    Output
    NAMESPACE   NAME              CLASS    HOSTS   ADDRESS                                                                 PORTS   AGE
    default     ingress-backend   <none>   *       k8s-default-ingressb-2d7bfa3e15-213467671.eu-west-3.elb.amazonaws.com   80      2d10h