Medium - Com - @21harsh12 - Devsecops Devops Project Deploying A Petshop Java Based Application With Ci CD Docker and E737d3a5501b
Medium - Com - @21harsh12 - Devsecops Devops Project Deploying A Petshop Java Based Application With Ci CD Docker and E737d3a5501b
Search
19
Sign up to discover human stories that deepen your understanding of the world.
Membership
Free
Access the best member-only stories.
Sign up to discover human stories that deepen your understanding of the world.
Introduction
In this blog, I will walk you through the process of deploying a Petshop
Membership
Java- Based Application using Jenkins as a CI/CD tool. This deployment
utilizes
Free Docker for containerization, Kubernetes for container
Access the best member-only stories.
orchestration, and incorporates various security measures and
Distraction-free reading. No ads. Support independent authors.
automation tools like Terraform, SonarQube, Trivy, and Ansible. This
Organize your knowledge with lists and
project showcases a comprehensive approach Listen
highlights. to modern application
to audio narrations. Read
Warning⚠
Before proceeding, ensure you read and understand the code properly. Make
necessary changes to variables such as GitHub repository URLs,
credentials, DockerHub usernames etc. Failure to update these variables
can affect the deployment process. Always double-check configurations and
ensure they align with your environment.
Sign up to discover human stories that deepen your understanding of the world.
Membership
Project
Free
Overview
Access the best member-only stories.
The goal of this project is to deploy a Java-based Petshop application in a
Distraction-free reading. No ads. Support independent authors.
secure, scalable, and automated manner. Here are the key components and
Organize your knowledge with lists and
tools used: Listen to audio narrations. Read
highlights.
offline.
Tell your story. Find your audience.
Jenkins for Continuous Integration and Continuous
Join the Partner Deployment
Program and earn for your
(CI/CD) writing.
Sign up to discover human stories that deepen your understanding of the world.
Membership
Free
Access the best member-only stories.
repository.
Distraction-free reading. No ads. Support independent authors.
• Importance: Centralized
Organize your knowledge with lists and code management ensures version control and
Listen to audio narrations. Read
highlights.
collaboration.
offline.
Tell your story. Find your audience.
2. Jenkins Build Trigger: Join the Partner Program and earn for your
• Action: Jenkins monitors the GitHub repository
writing. for new commits.
4. Dependency-Check:
•Action: Maven integrates with Dependency-Check to scan
for vulnerabilities in the project’s dependencies.
•Importance: Identifies and mitigates potential security risks in third-
party libraries early in the development process.
7. Maven Compile
Distraction-free reading.and
No ads.Test: Support independent authors.
• Action: Maven compiles the code and runs tests to verify that the
Organize your knowledge with lists and
Listen to audio narrations. Read
application
highlights. works as expected.
offline.
• Importance:
Tell your story. FindAutomated
your audience. testing ensures that code changes do not
Join the Partner Program and earn for your
introduce new bugs.
writing.
8. SonarQube Analysis:
• Action: Jenkins integrates with SonarQube to perform static code
analysis, checking for code quality and security issues.
•Importance: Maintains high code quality and security standards,
ensuring that the application is reliable and maintainable.
Security: Uses
Distraction-free Trivy
reading. No ads.to scan for vulnerabilities,
Supportensuring that only secure
independent authors.
images
Organize are deployed.
your knowledge with lists and
Listen to audio narrations. Read
highlights.
Scalability: Deploys the application on Kubernetes,
Tell your story. Find your audience.
offline. enabling it to scale
seamlessly based on demand. Join the Partner Program and earn for your
writing.
Reliability: Automated testing and analysis ensure that new code
changes do not break the application, maintaining its reliability.
Docker: Containerization
1. Consistent Environment: Docker packages applications with all their
dependencies into containers. This ensures that the application runs the
same way regardless of where it is deployed, eliminating the “works on
my machine” problem.
4. Portability:
Distraction-free Containers
reading. No ads. can run on any system that
Support supports
independent authors. Docker,
Combined Benefits
1. Development to Production: Docker is ideal for packaging and running
individual applications during development. Kubernetes takes these
Sign up to discover human stories that deepen your understanding of the world.
Docker containers and provides the infrastructure to run them reliably
at scale in production.
Free
Access the best member-only stories.
# Distraction-free
Provider configuration
reading. No ads. Support independent authors.
provider "aws" {
Organize
region your knowledge with lists# and
= "ap-south-1" Specify the region Listen to audio narrations. Read
} highlights.
offline.
Tell your story. Find your audience.
# Create a new security group that allows all inbound and outbound traffic
resource "aws_security_group" "allow_all" { Join the Partner Program and earn for your
name = "allow_all_traffic" writing.
description = "Security group that allows all inbound and outbound traffic"
ingress {
from_port =
0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port =
0
to_port = 0
protocol = "-1"
cidr_blocks =
["0.0.0.0/0"]
}
}
tags = {
Name = "MyUbuntuInstance"
}
}
Sign up to discover human stories that deepen your understanding of the world.
Free
Access the best member-only stories.
terraform
Distraction-free
init terraform reading. No ads. Support independent authors.
apply
Organize your knowledge with lists and
Listen to audio narrations. Read
highlights.
offline.
Tell your story. Find your audience.
Join the Partner Program and earn for your
Step 2: Install Jenkins, Docker, and Trivy writing.
SSH into the EC2 instance with your key pair and run the
following commands:
# Install Jenkins
wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ >
/etc/apt/sources. sudo apt update -y
sudo apt install jenkins -
y sudo systemctl start
jenkins sudo systemctl
enable jenkins
# Install Docker
sudo apt install apt-transport-https ca-certificates curl software-properties-
common curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add
-
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $
sudo apt update -y
sudo apt install docker-ce -
y sudo usermod -aG docker $
{USER} newgrp docker
sudo chmod 777 /var/run/docker.sock
# Install Trivy
sudo apt install wget apt-transport-https gnupg lsb-release -y
wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | sudo apt-key a
echo deb https://aquasecurity.github.io/trivy-repo/deb $(lsb_release -sc) main |
sud sudo apt update -y
sudo apt install trivy -y
Sign up to discover human stories that deepen your understanding of the world.
since Apache Maven’s default proxy is 8080, we need to change the port of
Jenkins from 8080 to let’s say 8090, for that:
Membership
Free
Access the best member-only stories.
sudo systemctl stop jenkins
sudo systemctl
Distraction-free status
reading. jenkins
No ads. Support independent authors.
cd /etc/default
sudo vi your
Organize jenkins
knowledge#chnage port HTTP_PORT=8090 and save and
with lists and
Listen to audio narrations. Read
exit cd /lib/systemd/system
highlights.
sudo vi jenkins.service #change Environments="Jenkins_port=8090"
offline. save and exit
Tell your
sudo story. Finddaemon-reload
systemctl your audience.
sudo systemctl restart jenkins Join the Partner Program and earn for your
sudo systemctl status jenkins writing.
Sign up to discover human stories that deepen your understanding of the world.
Free
Access the best member-only stories.
docker run -d --name sonar -p 9000:9000 sonarqube:lts-community
Distraction-free reading. No ads. Support independent authors.
username
admin password
admin
Step 3: Install Plugins in Jenkins
In Jenkins, navigate to Manage Jenkins -> Available Plugins and install the
following plugins:
Maven
Membership
OWASP
Free Dependency Check
Access the best member-only stories.
Membership
Free
Access the best member-only stories.
Free
Access the best member-only stories.
Pipeline script:
pipeline{
agent
any
tools {
jdk 'jdk17'
maven
'maven3'
}
stages{
stage ('clean Workspace'){
steps{
cleanWs()
}
}
stage ('checkout scm') {
steps {
git 'https://github.com/<your-java-project-repo-or-fork-one>' #https
}
}
stage ('maven compile') {
steps {
sh 'mvn clean compile'
}
}
stage ('maven Test') {
steps {
sh 'mvn test'
}
Sign up to discover human stories that deepen your understanding of the world.
}
}
Membership
Free
Access the best member-only stories.
Membership
Free
Access the best member-only stories.
Sign up to discover human stories that deepen your understanding of the world.
Membership
Free
Access the best member-only stories.
Add details
#Name- jenkins
#in url section of quality gate
<http://jenkins-public-ip:8090>/sonarqube-webhook/
#leave the secret box blank
Sign up to discover human stories that deepen your understanding of the world.
Organizetools
#under your knowledge
section withadd
lists and
this environment Listen to audio narrations. Read
highlights.
environment {
SCANNER_HOME=tool 'sonar-scanner' offline.
Tell your story. Find your audience.
}
# in stages add this Join the Partner Program and earn for your
stage("Sonarqube Analysis "){ writing.
steps{
withSonarQubeEnv('sonar-server') {
sh ''' $SCANNER_HOME/bin/sonar-scanner -Dsonar.projectName=Petsh
-Dsonar.java.binaries=. \
-Dsonar.projectKey=Petshop '''
}
}
}
stage("quality gate"){ steps {
script {
waitForQualityGate abortPipeline: false, credentialsId: 'Sonar-tok
}
}
}
Apply, save and build. Now, go to your SonarQube Server and go to project:
Free
After installing the plugin, proceed to configure thethetool
Access by navigating
best member-only stories. to
Dashboard → Manage
Distraction-free Jenkins → Tools →.
reading. No ads. Support independent authors.
SignAdd
up tothe script of OWASP in pipeline now:
discover human stories that deepen your understanding of the world.
Sign up to discover human stories that deepen your understanding of the world.
Membership
Free
Access the best member-only stories.
tired???
Step 6: Docker Set-up
In Jenkins, navigate to Manage Jenkins -> Available Plugins and install
these:
- Docker
- Docker Commons
- Docker Pipeline
- Docker API
- docker-build-step
Sign up to discover human stories that deepen your understanding of the world.
Free
Add DockerHub Username and Password (Access Token) in stories.
Access the best member-only
Global Credentials:
Distraction-free reading. No ads. Support independent authors.
Sign up to discover human stories that deepen your understanding of the world.
sudo apt update -y
sudo apt install software-properties-common -y
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt install ansible -y
sudo apt install ansible-core -y Membership
ansible --version #to check if it installed properly or not
Free
Access the best member-only stories.
To add inventory
Organize you
your knowledge withcan create a new directory
lists and or add in the default
Listen to audio narrations. Read
highlights.
Ansible hosts file
offline.
Tell your story. Find your audience.
Join the Partner Program and earn for your
writing.
cd
/etc/ansible
sudo vi hosts
[local]
<Ip-of-Jenkins>
Sign up to discover human stories that deepen your understanding of the world.
Membership
In the
FreePrivate key section, paste your .pem key file content directly.
Access the best member-only stories.
CheckDistraction-free
your Ansible reading. No ads.
path on the server by, Support independent authors.
Membership
Free
Access the best member-only stories.
stage('Install Docker') {
steps {
dir('Ansible'){ script {
ansiblePlaybook credentialsId: 'ssh', disableHostKeyCheckin
}
}
}
}
Now after build process of the pipeline you would be able to see the result
of web application by visiting the below url:
<jenkins-ip:8081>/jpetstore
Sign up to discover human stories that deepen your understanding of the world.
Membership
Free
Access the best member-only stories.
# Create a new security group that allows all inbound and outbound traffic
resource "aws_security_group" "allow_all" {
name = "allow_all_traffic"
description = "Security group that allows all inbound and outbound traffic"
ingress {
from_port =
0
to_port = 0
protocol = "-1"
cidr_blocks =
["0.0.0.0/0"]
}
egress {
from_port =
0
to_port = 0
protocol = "-1"
cidr_blocks =
["0.0.0.0/0"]
}
}
Sign up #resource
toLaunch
discover human stories that deepen your understanding of the world.
the first EC2 instance
"aws_instance" "my_ec2_instance1" {
ami = "ami-0f58b397bc5c1f2e8" # Ensure this AMI ID is valid for
your r instance_type = "t2.medium"
key_name = "MyNewKeyPair"
security_groups = [aws_security_group.allow_all.name]
Membership
Free
# Root block device with default size (8 GB for most Linux AMIs)
root_block_device { Access the best member-only stories.
volume_size = 8
Distraction-free reading. No ads. Support independent authors.
}
Organize your knowledge with lists and
tags = { Listen to audio narrations. Read
highlights.
Name = "k8s-master"
offline.
Tell
} your story. Find your audience.
} Join the Partner Program and earn for your
writing.
# Launch the second EC2 instance
resource "aws_instance" "my_ec2_instance2" {
ami = "ami-0f58b397bc5c1f2e8" # Ensure this AMI ID is valid for
your r instance_type = "t2.medium"
key_name = "MyNewKeyPair"
security_groups = [aws_security_group.allow_all.name]
}
tags = {
Name = "k8s-slave"
}
# Install kubectl
sudo apt-get update
sudo apt-get install -y apt-transport-https gnupg2 curl
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add
- echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee
/etc/apt/sou sudo apt-get update
sudo apt-get install -y kubectl
kubectl version --client
# Install Minikube
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd6
sudo install minikube-linux-amd64 /usr/local/bin/minikube
minikube start
Sign up to discover human stories that deepen your understanding of the world.
for simplicity, connect both newly created instance via SSH in side-by-side
Membership
terminal and change their hostname to master and worker, we can do by
Free
using this command: Access the best member-only stories.
Sign up to discover human stories that deepen your understanding of the world.
Membership
Free
Access the best member-only stories.
In worker instance,
Copy the config file to Jenkins master or the local file manager and save it,
you can find it in master node by,
cd /.kube
cat
config
Sign up to discover human stories that deepen your understanding of the world.
copy it and save it in documents or another folder save it as secret-file.txt.
Free
Access the best member-only stories.
Free
Access the best member-only stories.
Sign up to discover human stories that deepen your understanding of the world.
cd .ssh
cat id_rsa.pub #copy this public key
Membership
Freecopying the public key from the Ansible Main, navigate to the
After .ssh
Access the best member-only stories.
directory on the Kubernetes master machine and
Distraction-free reading. No ads.
paste the copied public
Support independent authors.
key into the authorized_keys file.
Organize your knowledge with lists and
Listen to audio narrations. Read
highlights.
offline.
Tell your story. Find your audience.
Join the Partner Program and earn for your
cd .ssh #on k8s master
writing.
sudo vi authorized_keys
Note: Add the copied public key as a new line in the authorized_keys file
without deleting any existing keys, then save and exit.
By adding the public key from the main to the Kubernetes machine, keyless
access is now configured. To verify, try accessing the Kubernetes master
using the following command format.
ssh ubuntu@<public-ip-k8s-master>
Now, open the hosts file on the Ansible server and add the public IP of the
Kubernetes master.
SignPlease
up to discover
note that human stories that deepen
here Ansible-master your
referring to understanding of the we
Main instance which world.
created first in this project and the other ones are k8s-master and k8s-slave.
Membership
Free
[k8s]
Access the best member-only stories.
public ip of k8s-master
Distraction-free reading. No ads. Support independent authors.
<slave-ip:serviceport(30699)>/jpetstore
# port may vary, you can check it from the above cmd (kubectl get
Sign up to discover human stories that deepen your understanding of the world.
all)
Membership
Free
Access the best member-only stories.
Complete Pipeline:
pipeline{
agent any
tools {
jdk 'jdk17'
maven
'maven3'
}
environment {
SCANNER_HOME=tool 'sonar-scanner'
}
stages{
stage ('clean Workspace'){
steps{
cleanWs()
}
}
stage ('checkout scm') {
steps {
git 'https://github.com/your-github-repo'
}
}
stage ('maven compile') {
steps {
sh 'mvn clean compile'
}
stage ('maven Test')
{ steps {
sh 'mvn test'
}
}
stage("Sonarqube Analysis "){
Sign up to discover human stories that deepen your understanding of the world.
steps{
withSonarQubeEnv('sonar-server') {
sh ''' $SCANNER_HOME/bin/sonar-scanner -Dsonar.projectName=Petst
-Dsonar.java.binaries=. \
-Dsonar.projectKey=Petstore '''
} Membership
}
Free
} Access the best member-only stories.
stage("quality gate")
Distraction-free{reading.
stepsNo{ads. Support independent authors.
script {
Organize your knowledgewaitForQualityGate
with lists and abortPipeline: false,
Listen to audiocredentialsId:
narrations. Read 'Sonar-tok
highlights. }
} offline.
Tell your story. Find your audience.
}
stage ('Build war file'){ Join the Partner Program and earn for your
steps{ writing.
sh 'mvn clean install -DskipTests=true'
}
}
stage("OWASP Dependency Check")
{ steps{
dependencyCheck additionalArguments: '--scan ./ --format XML ',
odcI dependencyCheckPublisher pattern: '**/dependency-check-
report.xml'
}
stage('Ansible docker Docker') {
steps {
dir('Ansible')
{ script {
ansiblePlaybook credentialsId: 'ssh', disableHostKeyChecking
}
}
}
}
stage('k8s using ansible'){
steps{
dir('Ansible') {
script{
ansiblePlaybook credentialsId: 'ssh', disableHostKeyChecking
}
}
}
}
}
}
Conclusion
By following these steps, we successfully deployed a Java-based Petshop
Sign up to discover human stories that deepen your understanding of the world.
application using Jenkins, Docker, Kubernetes, Terraform, SonarQube,
Trivy, and Ansible. This project not only demonstrates a comprehensive
approach to modern application deployment but also highlights the
Membership
importance of automation and security in the DevOps pipeline.
Free
Access the best member-only stories.
Sign up to discover human stories that deepen your understanding of the world.
Let’s continue to strengthen our skills and build secure, scalable
applications together!
#DevOps #DevSecOps #CI #CD #AWS #Terraform #Jenkins #Docker
Membership
#Kubernetes
Free #SecurityAutomation #SonarQube #Trivy #Ansible
Access the best member-only stories.
Sign up to discover human stories that deepen your understanding of the world.
Harshit Gupta Harshit Gupta
Feb 19 Distraction-free
10 reading. No ads. May 22 Support
16 independent authors.
Jan 23 4 Mar 6 8 1
Sign up to discover human stories that deepen your understanding of the world.
Membership
Free
Access the best member-only stories.
Yasin Emir Akbaş Szilárd Mátis in The Quantified World
Distraction-free reading. No ads. Support independent authors.
Terraform Modules and import
Organize your knowledge with lists and
Setup Grafana and Prometheus
Listen to Compose
with Docker audio narrations. Read
The purpose of this hands-on training is to
highlights.
provide you with knowledge of Terraform… How to run Grafana and Prometheus in
offline.
Tell your story. Find your audience.
Docker
Join the Partner Program and earn for your
writing.
Jun 13 9 Mar 15 157 3
Lists
Coding & Development General Coding Knowledge
11 stories · 675 saves 20 stories · 1332 saves
Sign up Jun
to13discover
4 human stories that deepen your
May 5understanding
74 1 of the world.
Membership
Free
Access the best member-only stories.
May 6 16
May 11 414 1
Sign up to discover human stories that deepen your understanding of the world.
Membership
Free
Access the best member-only stories.