Hello friends, we will be deploying a .Net-based application. This is an everyday use case scenario used by several organizations. We will be using Jenkins as a CICD tool and deploying our application on a Docker Container and Kubernetes cluster. Hope this detailed blog is useful.
This project shows the detailed metric i.e CPU Performance of our instance where this project is launched.
Step 1 — Create an Ubuntu T2 Large Instance with 30GB storage
Step 2 — Install Jenkins, Docker and Trivy. Create a Sonarqube Container using Docker.
Step 3 — Install Plugins like JDK, Sonarqube Scanner
Step 4 — Install OWASP Dependency Check Plugins
Step 5 — Configure Sonar Server in Manage Jenkins
Step 6 — Create a Pipeline Project in Jenkins using Declarative Pipeline
Step 7 — Install make package
Step 8 — Docker Image Build and Push
Step 9 — Deploy the image using Docker
Step 10 — Access the Real World Application
Step 11 — Kubernetes Set Up
Step 12 — Terminate the AWS EC2 Instance
Step 1 — Launch an AWS T2 Large Instance. Use the image as Ubuntu. You can create a new key pair or use an existing one. Enable HTTP and HTTPS settings in the Security Group.
Step 2 — Install Jenkins, Docker and Trivy
- 2A — To Install Jenkins
Connect to your console, and enter these commands to Install Jenkins
sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \
https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update
sudo apt-get install jenkins
sudo apt update
sudo apt install openjdk-17-jdk
sudo apt install openjdk-17-jre
sudo systemctl enable jenkins
sudo systemctl start jenkins
sudo systemctl status jenkins
Once Jenkins is installed, you will need to go to your AWS EC2 Security Group and open Inbound Port 8080, since Jenkins works on Port 8080.
Now, grab your Public IP Address
<EC2 Public IP Address:8080>
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Unlock Jenkins using an administrative password and install the required plugins.
Jenkins will now get installed and install all the libraries and follow below steps:
Jenkins Getting Started Screen and we can see Jenkins Dashboard.
- 2B — Install Docker
sudo apt-get update
sudo apt-get install docker.io -y
sudo usermod -aG docker $USER
sudo chmod 777 /var/run/docker.sock
sudo docker ps
After the docker installation, we create a sonarqube container (Remember added 9000 port in the security group)
Now, Install Sonarqube by executing default sonarqube community image.
docker run -d --name sonar -p 9000:9000 sonarqube:lts-community
Now, Login to SonarQube by using <Ec2_Public_IP>:9000
Note: By default username and password is admin
we can change password.
Username : admin
Password : admin
We can see SonarQube Dashboard:
- 2C — Install Trivy
sudo apt-get install wget apt-transport-https gnupg lsb-release
wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | gpg --dearmor | sudo tee /usr/share/keyrings/trivy.gpg > /dev/null
echo "deb [signed-by=/usr/share/keyrings/trivy.gpg] https://aquasecurity.github.io/trivy-repo/deb $(lsb_release -sc) main" | sudo tee -a /etc/apt/sources.list.d/trivy.list
sudo apt-get update
sudo apt-get install trivy -y
Step 3 — Install Plugins like JDK, Sonarqube Scanner, OWASP Dependency Check,
Go to Manage Jenkins → Plugins → Available Plugins →
Install below plugins
1 → Eclipse Temurin Installer (Install without restart)
2 → SonarQube Scanner (Install without restart)
- Go to Manage Jenkins → Plugins → Available Plugins →
After selecting Plugins, Click on install without restart.
- Configure Java in Global Tool Configuration
Goto Manage Jenkins → Tools → Install JDK17 → Click on Apply and Save
Step 4 — GotoDashboard → Manage Jenkins → Plugins → OWASP Dependency-Check. Click on it and install without restart.
First, we configured Plugin and next we have to configure Tool
Goto Dashboard → Manage Jenkins → Tools →
Click on apply and Save here.
Grab the Public IP Address of your EC2 Instance, Sonarqube works on Port 9000, <EC2_Public_IP>:9000
Goto your Sonarqube Server. Click on Administration → Security → Users → Click on Tokens and Update Token → Give it a name → and click on Generate Token
Click on Update Token
Create a token with a name and generate
Copy this Token
Go to Jenkins Dashboard → Manage Jenkins → Credentials → Add Secret Text. It should look like this
You will see this page once you click on create
Now, go to Jenkins Dashboard → Manage Jenkins → Configure System
Click on Apply and Save
The Configure System option is used in Jenkins to configure different server
Global Tool Configuration is used to configure different tools that we install using Plugins
We will install a sonar scanner in the tools.
In the Sonarqube Dashboard add a quality gate also
Administration → Configuration →Webhooks
Click on Create
Add details
# URL Section of Quality Gate
<http://jenkins-public-ip:8080>/sonarqube-webhook
After creating Sonar Webhook. we can see below page.
Step 6 — Create a Pipeline Project in Jenkins using Declarative Pipeline.
Go to Jenkins Dashboard and Create Pipeline Job
Add Below Script and Build it.
pipeline {
agent any
tools {
jdk 'jdk17'
}
environment {
SCANNER_HOME = tool 'sonar-scanner'
}
stages {
stage('clean workspace') {
steps {
cleanWs()
}
}
stage('Checkout From Git') {
steps {
git branch: 'main', url: 'https://github.com/naveensilver/DotNet-monitoring.git'
}
}
stage("Sonarqube Analysis ") {
steps {
withSonarQubeEnv('sonar-server') {
sh """$SCANNER_HOME/bin/sonar-scanner -Dsonar.projectName=Dotnet-Webapp \
-Dsonar.projectKey=Dotnet-Webapp"""
}
}
}
stage("quality gate") {
steps {
script {
waitForQualityGate abortPipeline: false, credentialsId: 'sonar-token'
}
}
}
stage("TRIVY File scan") {
steps {
sh "trivy fs . > trivy-fs_report.txt"
}
}
stage("OWASP Dependency Check") {
steps {
dependencyCheck additionalArguments: '--scan ./ --format XML ', odcInstallation: 'DP-Check'
dependencyCheckPublisher pattern: '**/dependency-check-report.xml'
}
}
}
}
Click on Build now, you will see the stage view like this.
You will see that in status, a graph will also be generated and Vulnerabilities.
To see the report, you can go to Sonarqube Server and go to Projects.
Step 7 — Install make package
sudo apt install make
# to check version install or not
make -v
Step 8 — Docker Image Build and Push
We need to install the Docker tool in our system, Goto Dashboard → Manage Plugins → Available plugins → Search for Docker and install these plugins
- Docker
- Docker Commons
- Docker Pipeline
- Docker API
- docker-build-step
and click on install without restart
Now, goto Dashboard → Manage Jenkins → Tools →
Add DockerHub Username and Password under Global Credentials.
In the makefile, we already defined some conditions to build, tag and push images to dockerhub.
that’s why we are using make image and make a push in the place of docker build -t and docker push
Add this stage to Pipeline Script
stage("Docker Build & tag"){
steps{
script{
withDockerRegistry(credentialsId: 'docker', toolName: 'docker'){
sh "make image"
}
}
}
}
stage("TRIVY"){
steps{
sh "trivy image naveensilver/dotnet-monitoring:latest > trivy.txt"
}
}
stage("Docker Push"){
steps{
script{
withDockerRegistry(credentialsId: 'docker', toolName: 'docker'){
sh "make push"
}
}
}
}
When all stages in docker are successfully created then you will see the result You log in to Dockerhub, and you will see a new image is created
You will see Stage view like this,
Step 9 — Deploy the image using Docker
Add this stage to your pipeline syntax
stage("Deploy to container"){
steps{
sh "docker run -d --name dotnet -p 5000:5000 naveensilver/dotnet-monitoring:latest"
}
}
You will see the Stage View like this,
(Add port 5000 to Security Group)
And you can access your application on Port 5000. This is a Real World Application that has all Functional Tabs.
Step 10 — Access the Real World Application
Step 11 — Kubernetes Set Up
Take-Two Ubuntu 20.04 instances one for k8s master and the other one for worker.
Install Kubectl on Jenkins machine as well.
sudo apt update
sudo apt install curl
curl -LO https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
kubectl version --client
To Install Kubernetes Cluster on Ubuntu 22.04 (Master & Slave)
Visit >> https://www.linuxtechi.com/install-kubernetes-on-ubuntu-22-04/
After installing Kuberneter Cluster,
Go to Master Node
cd .kube/
cat config
Copy the config file to Jenkins master or the local file manager and save it.
[Here, i am saving it in Local file manager i.e., k8s.txt]
Now, Install Kubernetes Plugins
Once it’s installed successfully.
Go to manage Jenkins → manage credentials → Click on Jenkins global → add credentials
The final step to deploy on the Kubernetes cluster, add this stage to the pipeline.
stage('Deploy to k8s'){
steps{
dir('K8S') {
withKubeConfig(caCertificate: '', clusterName: '', contextName: '', credentialsId: 'k8s', namespace: '', restrictKubeConfigAccess: false, serverUrl: '') {
sh 'kubectl apply -f deployment.yaml'
}
}
}
}
Before starting a new build remove Old containers. docker rm <contName>
Output
kubectl get svc
#copy service port
<worker-ip:svc port>
Check the Output in All the servers.
Step 12 — Terminate the AWS EC2 Instance
Lastly, do not forget to terminate the AWS EC2 Instance.
pipeline {
agent any
tools {
jdk 'jdk17'
}
environment {
SCANNER_HOME = tool 'sonar-scanner'
}
stages {
stage('clean workspace') {
steps {
cleanWs()
}
}
stage('Checkout From Git') {
steps {
git branch: 'main', url: 'https://github.com/naveensilver/DotNet-monitoring.git'
}
}
stage("Sonarqube Analysis ") {
steps {
withSonarQubeEnv('sonar-server') {
sh """$SCANNER_HOME/bin/sonar-scanner -Dsonar.projectName=Dotnet-Webapp \
-Dsonar.projectKey=Dotnet-Webapp"""
}
}
}
stage("quality gate") {
steps {
script {
waitForQualityGate abortPipeline: false, credentialsId: 'sonar-token'
}
}
}
stage("TRIVY File scan") {
steps {
sh "trivy fs . > trivy-fs_report.txt"
}
}
stage("OWASP Dependency Check") {
steps {
dependencyCheck additionalArguments: '--scan ./ --format XML ', odcInstallation: 'DP-Check'
dependencyCheckPublisher pattern: '**/dependency-check-report.xml'
}
}
stage("Docker Build & tag"){
steps{
script{
withDockerRegistry(credentialsId: 'docker', toolName: 'docker'){
sh "make image"
}
}
}
}
stage("TRIVY"){
steps{
sh "trivy image naveensilver/dotnet-monitoring:latest > trivy.txt"
}
}
stage("Docker Push"){
steps{
script{
withDockerRegistry(credentialsId: 'docker', toolName: 'docker'){
sh "make push"
}
}
}
}
stage("Deploy to container"){
steps{
sh "docker run -d --name dotnet -p 5000:5000 naveensilver/dotnet-monitoring:latest"
}
}
stage('Deploy to k8s'){
steps{
dir('K8S') {
withKubeConfig(caCertificate: '', clusterName: '', contextName: '', credentialsId: 'k8s', namespace: '', restrictKubeConfigAccess: false, serverUrl: '') {
sh 'kubectl apply -f deployment.yaml'
}
}
}
}
}
}
The Given declarative Jenkins Pipeline script written in Groovy, which defines a continuous delivery pipeline for a .NET web application. Let's break down the script step by step:
-
Agent and Tools:
- The pipeline is configured to run on any available agent and uses JDK 17 as the specified tool.
-
Environment:
- The
SCANNER_HOME
environment variable is set to the location of the Sonar Scanner tool using thetool
directive.
- The
-
Stages:
- Clean Workspace: This stage cleans the workspace before the build, ensuring a fresh start for the pipeline.
- Checkout From Git: The pipeline checks out the source code from the specified Git repository (
https://github.com/naveensilver/DotNet-monitoring.git
). - Sonarqube Analysis: This stage performs a SonarQube analysis on the codebase using the configured SonarQube server and scanner. It sets project name and key for the analysis.
- Quality Gate: The code waits for the SonarQube quality gate status, allowing the pipeline to continue or abort based on the quality gate result.
- TRIVY File Scan: Trivy is used to scan the filesystem for vulnerabilities, and the results are saved to a report file.
- OWASP Dependency Check: This stage performs a security check for project dependencies using OWASP Dependency Check, generating a report in XML format.
- Docker Build & Tag: The pipeline builds and tags a Docker image using the specified credentials and
make image
command. - TRIVY: Trivy is used to scan the Docker image for vulnerabilities, and the results are saved to a text file named
trivy.txt
. - Docker Push: The Docker image is pushed to the specified Docker registry using the credentials and
make push
command. - Deploy to Container: The Docker image is run as a container (named
dotnet
) on port 5000. - Deploy to k8s: The pipeline deploys the Kubernetes manifest described in the
deployment.yaml
file to a Kubernetes cluster.
Overall, the pipeline automates the entire process of building, testing, and deploying the .NET web application, integrating security checks and containerization for efficient continuous delivery.
Here is the GitHub for this project : https://github.com/naveensilver/DotNet-monitoring.git
Hope this blog was useful. If yes, please follow me for more content on DevOps. Thank you.
[Project Source : https://medium.com/aws-in-plain-english/real-time-devsecops-pipeline-for-a-dotnet-web-app-ece99ba14219]