In this project I've tried setting up a CI/CD pipeline. I've created a basic web app which exposes an api(login), whenever there'a a request, it sends a name and details in json format. Here'a a list of tools which I used:
- Docker: For contenatization of applications.
- Minikube: For setting up local K8s cluster.
- AWS EC2: For setting up a Jenkins server.
- Jenkins: For setting up CI part.
- ArgoCD: For the CD part following GitOps principles.
This is how the workflow looks like:
Containerize the app using Dockerfile given in the project repo.
docker build -t prady0t/pipeline .
Now try to run it.
docker run -it prady0t/pipeline
App should be running at port 3001.
Push the image to the Dockerhub registery. (if not authenticated, first do docker login)
docker push prady0t/pipeline
Setup a Jenkins server on AWS EC2. Set inbould traffic to Anywhere
Login to your AWS account and create a new EC2 instance. Now follow these steps to setup Jenkins on EC2 instance : https://www.jenkins.io/doc/book/installing/linux/#debianubuntu. (You would have to install Java first). Now in the terminal
systemctl status jenkins
Check Jenkins should be in the running state. Copy paste IP of the instance followed by :8080 (default port at which jenkins run) in your browser. You should have a unlock Jenkins page.
Now go to your instance's terminal and do :
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Use this as a password to unlock Jenkins. Download suggested pluggins.
Setup credentials for Github (using secret text, Github token as secret) and Dockerhub (using username and password).
Create a pipeline project in Jenkins.Select these as per given below:
Save
Setting up docker on AWS instance. Why do we need that?(I learned it the hard way) We are running single machine where Jenkins master and slave are same machine.
We want nodes to have docker installed so taht we can build images with Jenkins. We would also need a Docker pipeline
plugin.
Go to aws console and type these commands:
sudo apt-get install docker.io -y
sudo systemctl start docker
Check status:
systemctl status docker
Now we have to give jenkins user and ubuntu user permission to acces docker daemon:
sudo su -
usermod -aG docker jenkins
usermod -aG docker ubuntu
systemctl restart docker
Recent debuging, also add :
sudo chmod 777 /var/run/docker.sock
Also restart jenkins.
Now add webhook to github repo. A webhook is basically Jenkins server url followed by //github-webhook/
:
Now hit build
on Jenkins:
With this we are done with our CI part.
Setup ArgoCD cluster on minikube. Just follow all the steps from the docs -> https://argo-cd.readthedocs.io/en/stable/getting_started/
Go to localhost:8080
Again for initial login, follow the docs.
Create a new app with these settings:
Click Create
Go to the terminal and enter:
kubectl get pods
You will see it automatically created pods which were mentioned in the manifest files!
With this we are also done with our CD part.