Automated Node.js CI/CD Pipeline with Jenkins, AWS EKS, and Kubernetes

Select the Instance type as T3 Medium. Kindly note that this is not a free instance but a paid one. So, as soon as you are finished working on this project, remember to de-provision or tear it down to avoid any additional costs. Create and download the key.

T3 Medium Instance and Jenkins-key

Allow HTTP and HTTPs Traffic from the Internet. Click on Launch EC2 Instance is now in Running State.

Since Jenkins works on Port 8080, configure the Security Group. Add Custom TCP Port 8080 to access Jenkins using the Public IP Address of the EC2 Instance. Inbound Security Group would be now modified for our Jenkins.

Install and Setup Jenkins

Follow the steps for installing Jenkins on the EC2 instance.

sudo apt update
sudo apt install openjdk-17-jre -y
java -version

curl -fsSL https://pkg.jenkins.io/debian/jenkins.io-2023.key | sudo tee \
  /usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
  https://pkg.jenkins.io/debian binary/ | sudo tee \
  /etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update
sudo apt-get install jenkins -y

sudo systemctl start jenkins
sudo systemctl enable jenkins

You can get the ec2-instance-public-ip-address from your AWS EC2 console page.

Edit the inbound traffic rule to only allow custom TCP port 8080

http://:<ec2-instance-public-ip-address>8080.

Install Java and Jenkins on Jenkins Server and configure port 8080 as we have done so many times in our previous blogs. You can follow my previous blogs: https://nahid0002.hashnode.dev/mastering-cicd-with-jenkins-building-and-deploying-applications-with-efficiency

Also you can follow jenkins officials documents: https://www.jenkins.io/doc/book/installing/linux/

Update visudo and assign administration privileges to jenkins user

Now we have installed the Jenkins on the EC2 instance. To interact with the Kubernetes cluster Jenkins will be executing the shell script with the Jenkins user, so the Jenkins user should have an administration(superuser) role assigned forehand.

Let's add jenkins user as an administrator and also ass NOPASSWD so that during the pipeline run it will not ask for root password.

Open the file /etc/sudoers in vi mode

sudo vi /etc/sudoers

Add the following line at the end of the file

jenkins ALL=(ALL) NOPASSWD: ALL

After adding the line save and quit the file with :wq!

Now we can use Jenkins as root user and for that run the following command

sudo su - jenkins

Install Docker

Remember, all these commands are run as a Jenkins user and not as Ubuntu user.

sudo apt install docker.io
docker --version
docker ps
sudo usermod -aG docker jenkins
sudo reboot

Install and Setup AWS CLI

Login to your console and enter these commands

sudo apt-get update
sudo apt install awscli -y
aws --version

You will see that AWS CLI is now installed

Goto Access Keys → Create Access Keys →Download CSV File. Remember to download the CSV File so that it can be in your downloads section.

Now, go to your AWS Console login, and type below command

aws configure

You will need to enter the details

Install and Setup Kubectl

Moving forward now we need to set up the kubectl also onto the EC2 instance where we set up the Jenkins in the previous steps.

Here is the command for installing kubectl

curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin
kubectl version

Latest version of kubectl download official Link: https://pwittrock.github.io/docs/tasks/tools/install-kubectl/

https://kubernetes.io/docs/tasks/tools/

Install and Setup eksctl

Here is the command for installing eksctl:

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
eksctl version

Download and extract the latest release of eksctl with the following command.

curl --silent --location "https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_$(uname -s)_amd64.tar.gz" | tar xz -C /tmp

Move the extracted binary to /usr/local/bin

sudo mv /tmp/eksctl /usr/local/bin

Test that your installation was successful with the following command.

eksctl version

You can refer to this URL to install eksctl : https://linux.how2shout.com/how-to-install-eksctl-cli-tool-on-ubuntu-linux/

Creating an Amazon EKS cluster using eksctl

Now in this step, we are going to create Amazon EKS cluster using eksctl

You need the following in order to run the eksctl command

  1. Name of the cluster : — eks1

  2. Version of Kubernetes : — version 1.24

  3. Region : — region us-east-1

  4. Nodegroup name/worker nodes : — nodegroup-name worker-nodes

  5. Node Type : — nodegroup-type t3.medium

  6. Number of nodes: — nodes 2

  7. Minimum Number of nodes: — nodes-min 2

  8. Maximum Number of nodes: — nodes-max 3

Here is the eksctl command. Now, sit back and relax as it will take time to make this cluster. It took me close to 20 minutes.

Command:

eksctl create cluster --name eks1 --version 1.24 --region us-east-1 --nodegroup-name worker-nodes --node-type t3.medium --nodes 2 --nodes-min 2 --nodes-max 3

Kindly note that it took me almost 20–25 minutes to get this installed and running.

If you get any error for not having sufficient data for mentioned availability zone then try it again.

If get this type of error then again aws configure

To verify whether the EKS cluster is working or not, you can go back to your AWS dashboard and check.

Add Docker and GitHub Credentials on Jenkins

Setup Docker Hub In Jenkins

You can set the docker credentials by going into -

Goto -> Jenkins -> Manage Jenkins -> Manage Credentials -> Stored scoped to jenkins -> global -> Add Credentials

Setup GitHub In Jenkins

Goto GitHub — > Setting — > Developer Settings — > Personal access tokens — > Tokens(Classic) — > Generate new token

Now, you can see all two credentials have been added

Build, deploy and test CI/CD pipeline

Now we can start writing out the Jenkins pipeline for deploying the Node.js Application into the Kubernetes Cluster.

Create new Pipeline: Goto Jenkins Dashboard or Jenkins home page click on New Item

Pipeline Name: Now enter Jenkins pipeline name and select Pipeline

Add pipeline script: Goto -> Configure and then pipeline section.

Copy the below script and paste it into Pipeline Script.

node {

    stage("Git Clone"){

        git credentialsId: 'GIT_HUB_CREDENTIALS', url: 'https://github.com/nahidkishore/node-todo-cicd.git', branch: 'main' 
    }

     stage("Build") {

       sh 'docker build . -t nahid0002/node-todo-test:latest'
       sh 'docker image list'

    }

    withCredentials([string(credentialsId: 'DOCKER_HUB_PASSWORD', variable: 'PASSWORD')]) {
        sh 'docker login -u nahid0002 -p $PASSWORD'
    }

    stage("Push Image to Docker Hub"){
        sh 'docker push nahid0002/node-todo-test:latest'
    }

    stage("kubernetes deployment"){
        sh 'kubectl apply -f deployment.yml'
    }
}

To set up Jenkins - GitHub Webhook

Jenkins GitHub Webhook is used to trigger the action whenever Developers commit something into the repository. It can automatically build and deploy applications.

Switch to your GitHub account, go to the “Settings” option. Here, select the “Webhooks” option and then click on the “Add Webhook”

It will provide you the blank fields to add the Payload URL where you will paste your Jenkins address, Content type, and other configurations.

Go to your Jenkins tab and copy the URL then paste it in the text field named “Payload URL“, as shown in the image below. Append the “/github-webhook/” at the end of the URL.

You completed Jenkins GitHub Webhook. Now for any commit in the GitHub repository, Jenkins will trigger the event specified

After pushing code

To check if the application is working or not, use this command

kubectl get deployments
kubectl get svc

You can access the rest endpoint from a browser using the EXTERNAL-IP address.

Clean up

Copy Deployment.yml file (From Github Repository) to EC2 server and run with below command.

kubectl delete -f deployment.yml

Delete EKS Cluster and instance from AWS Console.

We have successfully deployed our Node.js App in the Amazon EKS cluster using AWS EC2, Jenkins, Docker, Kubernetes, GitHub, and Webhook.

Thank you for reading this blog. If you found this blog helpful, please like, share, and follow me for more blog posts like this in the future.

— Happy Learning !!!

Let’s connect !!!

Linkedin

Medium

Github

Mail