- General Info
- Technologies
- Setup
- Process -CI/CD pipeline
- Screenshots
The aim of this project is to create a CI/CD pipeline using Jenkins to implement a website deployment on Kubernetes.
First thing is to write a Docker file where we can run Apache (httpd) webserver with sample free template from (https://templatemo.com/tm-554-ocean-vibes) and copy to html directory and expose port 80. Similarly, write Kubernetes deployment & service yml file and Ansible-playbook files. Pull these file from Kubernetes sources file repo to Jenkins workspace and copy onto Ansible server, then build the Docker image from there and upload to DockerHub. Following that, using Ansible playbook, copy the Kubernetes deployment file and deploy it on Kubernetes cluster, this will initiate the pods creation in Kubernetes. In order to see the IP and port of the running deployment and service on, execute another Ansible playbook which will execute a bash script on Kubernetes and display the ip and port of the running deployment, so we can see it from web browser.
- Bash Scripting
- Github
- Jenkins
- Docker
- Ansible
- Kubernetes
As per the aim of this project, we need 3 VM instances,here we are using AWS EC2 instances.
- Jenkins server
- Ansible server
- Kubernetes server
Install Ansible on ansible server for deployment and install minikube to run Kubernetes cluster in order run the Kubernetes deployment.
#Update packages
sudo yum update -y
#Install ansible
sudo amazon-linux-extras install ansible2 -y
#Check version
ansible –version
On top ansible, we also need Docker to build docker image, for installation guide follow my docker-setup repo
On the second VM instance, install Kubernetes to setup Kubernetes, please refer my Kubernetes installation guide on here
Similarly, on the third instance, install Jenkins for Continuous Integration and Continuous Deployment. To setup Jenkins please refer my Jenkins installation guide on here
Project Source file - refer here
Now that we have all the resources, we can start writing the codes for the project.
-
Dockerfile
-
Kubernetes Deployment & Service file in yml format
-
Ansible playbook to copy the file and deploy on Kubernetes Cluster
-
Ansible playbook to run a shell script on Kubernetes server to display the ip & port where our website deployment is running so we can view it on browser
In order to automate the whole CI/CD environment, we will have to establish the connections between servers and do some ground work. Since we are going to connect to our ansible server, we will have to install “SSH-Agent” plugin and make the connection, please refer my guide to Jenkins connections repo
We should also establish connection between Ansible server and Kubernetes server via ssh
ssh-keygen
#goto “./.ssh” and copy public id
cat ./.ssh/id_rsa.pub
#Then onto Kubernetes server under ./.ssh directory
vi authorized_keys
#paste the ansible server’s public id and save it
Plan is to build Jenkins CI/CD pipeline, with environmental variables so we can pass in our credentials as a secret - docker login password and AWS IP as “secret text”. DockerHub password = PASS and AWS IP = AWS_IP
CI/CD Stages refer here
git branch: 'main', url: 'https://github.com/nav-InverseInfinity/docker-webservice.git'
scp /var/lib/jenkins/workspace/kubernetes_deployment/Docker* ec2-user@$AWS_IP:~/ansible/
ssh ec2-user@$AWS_IP "cd ~/ansible/ && docker build . -t $JOB_NAME:v1.$BUILD_ID"
ssh ec2-user@$AWS_IP "cd ~/ansible/ && docker tag $JOB_NAME:v1.$BUILD_ID inverseinfinity/$JOB_NAME:latest"
ssh ec2-user@$AWS_IP "cd ~/ansible/ && docker login -u inverseinfinity -p $PASS"
ssh ec2-user@$AWS_IP "cd ~/ansible/ && docker push inverseinfinity/$JOB_NAME:latest"
ssh ec2-user@$AWS_IP "cd ~/ansible/ && docker image rm inverseinfinity/$JOB_NAME:latest $JOB_NAME:v1.$BUILD_ID "
scp /var/lib/jenkins/workspace/kubernetes_deployment/*.yml ec2-user@$AWS_IP:~/ansible/
ssh ec2-user@$AWS_IP "cd ~/ansible/ && ansible webserver"
ssh ec2-user@$AWS_IP "cd ~/ansible/ && ansible-playbook kubernetes-playbook.yml"
This will successfully deploy and start the kuubernetes pods, thus the website will be up & running, however to see the ip and port of the running container n Kubernetes cluster we will have to run a bash script on Kubernetes server to find the ip and port.
scp /var/lib/jenkins/workspace/kubernetes_deployment/kube-ip-port.sh ec2-user@$AWS_IP:~/ansible/
ssh ec2-user@$AWS_IP "cd ~/ansible/ && chmod +x kube-ip-port.sh && ansible-playbook kube_ip_port-playbook.yml"