In this article, we will guide you through setting up a continuous integration and continuous deployment (CI/CD) pipeline to deploy a Node.js application on an Amazon EKS Kubernetes cluster using Jenkins and GitHub Webhook. This setup ensures automated build and deployment whenever a commit is made to the source code repository.
Prerequisites
Before we begin, make sure you have the following:
AWS Account with EKS and other necessary services.
Jenkins server set up and running.
GitHub repository containing your Node.js application.
kubectl and awscli installed on Ec2 Instance.
Step 1: Create an EC2 Ubuntu Instance
Login to AWS Management Console:
- Go to AWS Management Console and sign in.
Launch an EC2 Instance:
Navigate to EC2 Dashboard, click "Launch Instance".
Select "Ubuntu Server 22.04 LTS".
Choose t2.micro for free tier or your preferred instance type.
Configure instance details, add storage, and tags.
Configure Security Group:
Add inbound rules for:
SSH: TCP, port 22, source 0.0.0.0/0
HTTP: TCP, port 80, source 0.0.0.0/0
HTTPS: TCP, port 443, source 0.0.0.0/0
TCP: TCP, port 8080, source 0.0.0.0/0
Launch and Connect:
Review and launch the instance.
Select or create a key pair and download it.
Connect to your instance via SSH:
ssh -i <your-key-pair.pem> ubuntu@<public-dns>
Step 2: Install Jenkins
Install Java:
sudo apt update sudo apt install openjdk-11-jdk -y
Add Jenkins Repository and Install:
sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \ https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key echo "deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc]" \ https://pkg.jenkins.io/debian-stable binary/ | sudo tee \ /etc/apt/sources.list.d/jenkins.list > /dev/null sudo apt-get update sudo apt-get install jenkins
Start Jenkins Service:
sudo systemctl start jenkins sudo systemctl enable jenkins
Sure! Here’s a shorter guide to creating an EC2 Ubuntu instance and installing Jenkins:
Step 1: Create an EC2 Ubuntu Instance
Login to AWS Management Console:
- Go to AWS Management Console and sign in.
Launch an EC2 Instance:
Navigate to EC2 Dashboard, click "Launch Instance".
Select "Ubuntu Server 20.04 LTS".
Choose
t2.micro
for free tier or your preferred instance type.Configure instance details, add storage, and tags.
Configure Security Group:
Add inbound rules for:
SSH: TCP, port 22, source 0.0.0.0/0
HTTP: TCP, port 80, source 0.0.0.0/0
HTTPS: TCP, port 443, source 0.0.0.0/0
Launch and Connect:
Review and launch the instance.
Select or create a key pair and download it.
Connect to your instance via SSH:
ssh -i <your-key-pair.pem> ubuntu@<public-dns>
Step 2: Install Jenkins
Update System Packages:
sudo apt update sudo apt upgrade -y
Install Java:
sudo apt install openjdk-11-jdk -y
Add Jenkins Repository:
shCopy codecurl -fsSL https://pkg.jenkins.io/debian/jenkins.io.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
Install Jenkins:
sudo apt update sudo apt install jenkins -y
Start Jenkins Service:
sudo systemctl start jenkins sudo systemctl enable jenkins
Step 3: Configure Jenkins
Access Jenkins:
- Navigate to
http://<public-dns>:8080
in your browser.
- Navigate to
Unlock Jenkins:
Retrieve initial admin password:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Enter this password in the web interface.
Install Suggested Plugins:
- Follow the prompts to install recommended plugins.
Create Admin User:
- Set up your admin user as prompted.
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.
Now we can use Jenkins as root user and for that run the following command -
sudo su - jenkins
Step 4: Install Docker
Install Docker:
The docker installation will be done by the Jenkins user because now it has root user privileges.
Add jenkins user to Docker group. Jenkins will be accessing the Docker for building the application Docker images, so we need to add the Jenkins user to the docker group.
sudo apt install docker.io docker --version docker ps sudo usermod -aG docker jenkins sudo reboot
Verify Docker Installation:
docker --version
Step 5: Install and Setup AWS CLI
Install AWS CLI:
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" sudo apt install unzip unzip awscliv2.zip sudo ./aws/install aws --version
Configure AWS CLI:
aws configure
- Enter your AWS Access Key ID, Secret Access Key, region, and output format as prompted.
Step 6: 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
Step 7: Install and Setup eksctl
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
Step 8: Creating an Amazon EKS cluster using eksctl
In this step, we will create an Amazon EKS cluster using eksctl
.
You need the following information to run the eksctl
command:
Name of the cluster : --name first-eks-cluster1
Version of Kubernetes : --version 1.25
Region : --region us-east-1
Nodegroup name/worker nodes : --nodegroup-name worker-nodes
Node Type : --nodegroup-type t2.micro
Number of nodes: --nodes 2
Here is the eksctl command -
eksctl create cluster --name first-eks-cluster1 --version 1.24 --region us-east-1 --nodegroup-name worker-nodes --node-type t2.micro --nodes 2
It took me 20 minutes to complete this EKS cluster. If you get any error for not having sufficient data for mentioned availability zone then try it again.
Verify the EKS kubernetes cluster on AWS Console.
You can go back to your AWS dashboard and look for Elastic Kubernetes Service -> Clusters
Step 9: Add Docker and GitHub Credentials on Jenkins
Step 1: Setup Docker Hub Secret Text in Jenkins
You can set the docker credentials by going into -
Goto -> Jenkins -> Manage Jenkins -> Credentials -> system->Global credentials (unrestricted) -> Add Credentials
Step 2. Setup GitHub Username and password into Jenkins
Now we add one more username and password for GitHub.
Goto -> Jenkins -> Manage Jenkins -> Credentials -> system->Global credentials (unrestricted) -> Add Credentials
Step 10: 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 into Pipeline Script.
node {
stage("Git Clone"){
git credentialsId: 'GIT_HUB_CREDENTIALS', url: 'https://github.com/rahulwath/Jenkins-CICD-with-Amazon-EKS.git', branch: 'main'
}
stage("Build") {
sh 'docker build . -t rahulwath/node-todo-test:latest'
sh 'docker image list'
}
withCredentials([string(credentialsId: 'DOCKER_HUB_PASSWORD', variable: 'PASSWORD')]) {
sh 'docker login -u rahulwath -p $PASSWORD'
}
stage("Push Image to Docker Hub"){
sh 'docker push rahulwath/node-todo-test:latest'
}
stage("kubernetes deployment"){
sh 'kubectl apply -f deployment.yml'
}
}
To set up Jenkins - GitHub Webhook
Now, go to the “Build Triggers” tab.
Here, choose the “GitHubhook trigger for GITScm pulling” option, which will listen for triggers from the given GitHub repository.
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 “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 configuration.
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.
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 from AWS Console.
Terminate EC2 Instance.
Conclusion
We have successfully deployed our Node.js App in Amazon EKS cluster using AWS EC2, Jenkins, Docker, Kubernetes, GitHub, Webhook.
If you have liked this project and my effort please share this and fork my Github repo for more practice.
Github Repo - https://github.com/rahulwath/Jenkins-CICD-with-Amazon-EKS