Created by: Aly Ghazal
The purpose of this project is to Deploy a simple Nodejs app in K8s cluster using Jenkins deployed in the same K8s cluster too
and for This we will use AWS Infrastructure to deploy our application using Terraform
-
we will create this infra
- a VPC
- at least one public subnet and 2 private subnets
- Internet Gateway & NAT Gateway
- one Bastion host
- one EKS cluster with a Node group
To deploy this infra we enter those commands in terminal
cd ./Terraform-Iaac terraform init terraform apply -auto-aprove
(Note): in this project we will use Bastion host to install Docker daemon in worker nodes because we EKS latest version "1.24" and it don't have Docker engine like the previous versions
-
Build and push custom jenkins image with Kubectl and docker client to Dockerhub
cd ./JenkinsInK8s docker login docker build . -f Dockerfile -t <your-username-in-dockerhub>/jenkins:v1.0 docker push <your-username-in-dockerhub>/jenkins:v1.0
-
connect to EKS created cluster
aws eks --region <region-of-deployed-cluster> update-kubeconfig --name <cluster-name>
-
install docker engine in worker nodes with Bastion host using ansible
-
edit ip address and path of the private key of the bastion host and worker nodes in Ansible-files/inventory.ini
-
edit ip address and path of the private key of bastion host in ~/.ssh/config
-
apply this playbook with this command
cd /Ansible-files ansible-playbook playbook.yml -i inventory.ini
-
-
deploy our custom jenkins in our EKS cluster
cd /JenkinsInK8s kubectl apply -f . kubectl apply -f . #in case the first apply didn't run will
then to get url (External IP address) of the exposed jenkins pod we run this command
kubectl get svc -n jenkins-ns
and using this url in our browser the result will be like that
Link: http://a9dba2356b242471da8c86755eac08ad-497230415.eu-central-1.elb.amazonaws.com:8080/
output
-
Add our dockerhub and github credintials in Jenkins agent
-
create pipeline with the provided jenkinsfile which
- clone app and it's deployment files from Aly-Ghazal/nodejs-app-deployment-from-jenkins
- build image for it
- push it to container registery (Dockerhub)
- apply deployment files using kubectl tool
-
start building pipeline and access console output to get link of the deployed app
Link: http://ace073eef09be4a209ae118a9f14064d-1816240542.eu-central-1.elb.amazonaws.com:3000/
output
-
create new pipeline in jenkins to delete app deployment using jenkinsfile-RemoveAPP
-
then in your local machine in JenkinsInK8s
cd /JenkinsInK8s kubectl delete -f .
-
in Terraform-Iaac
cd /Terraform-Iaac terraform destroy
- we can use an older version of EKS cluster to use docker engine without the need of install it by bastion host OR keep using the same version but instead of using Docker we can use a tool like Kaniko to build images for our app it lack some features that docker have like caching built images but it still more secure than using a bastion host to install docker engine
- instead of using dockerhub we can use private container registry like Amazon ECR