Table of contents
Description:
Deploy an application Kubernetes using Jenkins for CI-CD pipeline, EKS for automated cluster configuration, and terraform for provisioning of the infrastructure.
Project Code:
https://github.com/harshitsahu2311/terraform-k8s-project.git
Prerequisites:
AWS for EC2, EKS, S3 bucket creation
Linux terminal for CLI execution
Install Terraform
wget -O - https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list sudo apt update && sudo apt install terraform
Install AWS CLI
sudo apt install unzip -y curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" unzip awscliv2.zip sudo ./aws/install
Ensure you have configured your AWS creds with CLI using
aws configure
.Create a bucket with name “
jenkins-terraform-kubernetes-nginx
“ inap-south-1
region.Create a key-pair with name “
jenkins-server-demo
“ inap-south-1
region.
Procedure:
Clone the Project
git clone https://github.com/harshitsahu2311/terraform-k8s-project.git
You will find multiple files in it, let’s deep dive to explain each one
vpc.tf
—> provisioning of VPC in awsvariables.tf
—> all the variables are initialized hereterraform.tfvars
—> all the variables which are above initialized are assigned with values in this fileserver.tf
—> Code for the provisioning of EC2 in the subnet of VPCsecurity.tf
—> Provisioning of security group with allow port for SSH and Jenkins (8080)route.tf
—> Creation of route table for the routes of subnetprovider.tf
—> In this file required providers are mentioned like aws with the specific regionoutputs.tf
—> This will give the public-ip of the EC2 instancebackend.tf
—> this will ensure that the state file of terraform (terraform.tfstate) is stored in the bucketJenkins-script.sh
—> it has code to install git, Jenkins, Kubectl, AWS CLI.
Let’s create the infrastructure in AWS
Run the command to initialize the terraform backend
terraform init
Check the code of terraform with this command
terraform validate
Plan the action which terraform will do
terraform plan
Apply the changes which you have planned
terraform apply --auto-approve
After that you will receive this type of output:
- Then access the created server using
ssh -i "jenkins-server-demo" ubuntu@<public-ip>
- Check whether all the services which we have listed in the script are installed and running
git --version
java -version
jenkins --version
terraform -version
kubectl version --client
sudo systemctl status jenkins
If anything is not installed, then install it manually and update the server also.
Now access the Jenkins with the
public-ip:8080
and configure itInstall plugins:
Pipeline: Stage-view
After that go to manage Jenkins and configure your credentials of AWS and GitHub with these formats
SECRET TEXT - AWS_ACCESS_KEY_ID
SECRET TEXT - AWS_SECRET_ACCESS_KEY
USERNAME WITH PASSWORD - GITHUB
Now create job with pipeline type
Discard old build
Choose Pipeline Script with SCM
SCM: git
Repository URL: paste your repository URL
Credentials: choose your GitHub cred
Branch: main
Script Path: Jenkinsfile
Apply > Save
Click on Build Now to build the job
Congratulations you have successfully deployed your application in EKS Cluster using fully automated Jenkins pipeline.
If you get errors like
Bucket not found then check whether their exist bucket with same name as mentioned in the prerequisite.
Also ensure that AWS CLI is installed and configured in your provisioned instance.
Create a Bastion host in public subnet and access the server in private subnet using NAT gateway and then check by
docker ps
container is running or not.If running congrats otherwise troubleshoot it.
After your project completion uncomment the
delete EKS cluster script
from the Jenkins pipeline code and run the Jenkins job again.After that from your local delete the instance and VPC also using
terraform destroy
command.
Now you have completed the project also and no bill also came. So, all your work is done except one which is clicking the subscribe button below. 👇👇👇👇