✨Build a spring boot-based Java application using CICD✨

✨Build a spring boot-based Java application using CICD✨

Goal:
⚡️Build a spring boot-based Java application using Maven and deploy this application on Kubernetes using CICD
⚡️We accomplish this by adhering to the procedure outlined below, which integrates CI/CD implementation at each stage

Activities in this project:
🔸Build a Java application using Maven including configuration & installation
🔸Setup a sonar server locally and send the report of code execution(static code analysis) to sonarqube
🔸Build a docker image for the artifact and push it to dockerhub
🔸Automatically update the manifest repository or source code repository using a shell script
🔸Deploy the manifest automatically to a Kubernetes cluster using Argo CD

Step1:
🌟Set up an EC2 instance in your AWS account, utilizing Ubuntu with an instance type of t2.large to effectively utilize resources such as 2 CPUs and 8GB of memory, which are sufficient for this project
🌟 Check out below video by Abhishek Veermalla , if you have any doubts on how to create an EC2 instance.

🌟Checkout below GitHub repo URL where you can find code for this project
Note: It was created by Abhishek Veermalla – creator of this project)

Step2:
1) Let's move to the Jenkins part
Login to EC2 instance using ssh
command: ssh –i /Downloads/sakeena-aws-pem-file.pem ubuntu@ 100.24.255.244 (public IP)
2)Install Java as a prerequisite for Jenkins
sudo apt update
sudo apt install openjdk-11-jre

By default Jenkins server will be started by port 8080, when you try to access localhost 8080 it will not be started
so we have to configure the inbound traffic rules on EC2 instance( security rules which prevents the incoming traffic unless we enable the port for the EC2 instance)

we have selected All traffic here, but in a real-time environment restrict it as per your requirement.
To check if Jenkins is running use below
ps –ef | grep Jenkins

6)Now login to Jenkins using your ec2 instance ip address
eg: http://<instance ip address>:8080/

Cat the password as below or you can set username and password by your own

💠 Login to Jenkins using the password you got in the above command
now you are ready to use the Jenkins
💠 Now click on the new item and select pipeline and click on ok
💠Jenkins allows you to write your code in two ways, you can write under the script section in Jenkins or you can put your Jenkins file in the git repo where your source code is available
💠 Usually in real time, organizations will manage the Jenkins file in the same folder where your application code is available i.e.,. in Git repo

we have to give the Jenkins file path in your Jenkins configuration as below

🔷The purpose of the Jenkins file is to execute all the other tasks of the continuous integration.
🔷We now need to install the Docker Pipeline plugin, which already includes Maven, located at the following path
Dashboard -> manage Jenkins ->Plugins -> Available plugins -> Docker pipeline -> install without restart
🔷To install the sonar server, install the sonar service under the below path
Dashboard -> manage Jenkins -> Plugins -> Available Plugins ->SonarQubeScanner -> install without restart
🔷To install sonar sever on ec2 instance
use apt install unzip to install unzip package before downloading sonarqube
sudo su – sonarqube -> to get in to sonarqube folder
unzip * -> To extract the sonarqube folder
🔷Checkout below Readme file below where you have steps to install sonarqube
https://github.com/iam-veeramalla/Jenkins-Zero-To-Hero/blob/main/java-maven-sonar-argocd-helm-k8s/spring-boot-app/README.md
🔷By default sonar will be started on port 9000

💠To Authenticate Jenkins with sonar follow below:
Go to sonarqube -> my account-> security ->give token name as Jenkins and generate a token

Now copy the token, go to Jenkins -> click on Manage Jenkins ->credentials ->system ->global credentials -> add credentials -> add sonarqube authentication here

🔹Install Docker on your ec2-instance
command: sudo apt install docker.io
🔹Now grant permissions to all users like Jenkins, ubuntu etc to avoid any permission issues
command: usermod –aG docker jenkins
usermod –aG docker ubuntu
systemctl restart docker -> to restart docker
🔹Go back and restart your jenkins(it's a good practice to restart jenkins when any plugin is installed or any activity is done from our end)
eg: http://100.24.255.244:8080/restart
🔹Now We have to configure docker credentials and GitHub credentials inside jenkins
🔹To configure dockerhub cred:
Dashboard=>Manage jenkins -> Credentials -> system-> global credentials->adduser
The username will be your dockerhub username
ID is based on the name we have provided in the jenkins file as per the jenkins credential store
🔹To configure GitHub cred:
To get access key -> go to your GitHub account -> settings -> developer settings ->personal access tokens->Tokens(classic) ->generate new token ->generate new token(classic)-> copy token to your jenkins dashboard in same path(Dashboard=>Manage jenkins -> Credentials -> system-> global credentials)

Now restart jenkins as we did the above configuration to get the changes reflected
Run the build and check the console output :
Things we automated via Jenkins like below:
💠Create Container
💠 Checkout SCM
💠Should push dockerhub image
💠 Should create a sonarqube report
💠Should update the image on dockerhub with the build number automatically (shell script to update the manifest folder)
Stage1: Creating container, Jenkins has to start creating a container and executing the container

Stage 2:
Maven will create the jar file
here maven target(mvn clean package) is downloading a lot of packages and their dependencies and it will create a jar file, This step is important as all other steps based on the creation of this jar file

Stage 3: mvn sonar:sonar will be executed and pushes the report to sonarqube

Stage 4:
A docker image has to be created which will be configured in your jenkins file
with the same name, the image should get created on the docker hub

Stage 5:
The final step using shell script to update the manifest folder

👉Let's move on to the CD part now,
👉To install Kubectl follow the below documentation as per your OS
https://kubernetes.io/docs/tasks/tools/install-kubectl-linux/

👉To install Minikube use the below doc
https://minikube.sigs.k8s.io/docs/start/ Note: follow steps and documentation based on your OS

👉To install argo CD using operators( which will manage the lifecycle of Kubernetes controllers)
https://operatorhub.io/operator/argocd-operator
👉Run all commands sequentially as mentioned in the doc to install argocd

👉To check whether your Argocd operator is installed use the below command
kubectl get pods –n operators
minikube status

👉Use the below documentation to get started with argocd implementation
https://argocd-operator.readthedocs.io/en/latest/usage/basics/
To create an argocd controller, create a vi file with the below example(mentioned in the above doc )

👉Run the below commands to get the argocd workloads being created
Commands:
kubectl apply –f argocd-basic.yml
kubectl get pods (to check the status)

💎Now use the below command which is responsible for running the argocd ui on your browser
kubectl get svc
kubectl edit svc example-argocd-server (to change the type from ClusterIP to NodePort)
💎Below command can also be passed directly:
kubectl patch svc example-argocd-server -n argocd -p '{"spec": {"type": “NodePort"}}'

⭐️Now to execute it on the browser, use the minikube feature by executing the below command
minikube service list -> lists all the services exposed by minikube
minikube service example-argocd-server -> to expose specific service

⭐️In case if tunneling is not happening use the below command
kubectl port-forward svc/example-argocd-server -n default --address 0.0.0.0 8080:443
⭐️Tunneling in Minikube enables access to Kubernetes cluster services from the host machine by forwarding traffic through a secure channel. It facilitates seamless interaction with applications and services running within the cluster for development and testing purposes
⭐️Now you should be able to login argocd ui on your browser

⭐️The username is admin by default
And for a password, use below below command
kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d ; echo
Now click on the Create Application tab and start creating your first application

💠Click on create, you will see that the application will be automatically created using the kubernetes cluster as argocd will fetch your application info from the github repo

💠You can use the below command to check whether the application is deployed
kubectl get deploy