Creating Jenkins Pipeline

  • On Dashboard click on New Item. Enter the item name, example myapp, and select Pipeline Option.

  • Scroll down to Pipeline and add below Pipeline script under script section. Make sure you replace your-credential-id (obtained in step 8-2), http://link:port (obtained in step 7) and the paths with your actual values. Then click on Save.

This Jenkins pipeline script automates the deployment of a Java application to a Kubernetes cluster. It comprises three stages: Build compiles the Java code, Dockerize builds a Docker image, and Deploy to Minikube applies the Kubernetes deployment using a specified service account token and server URL with TLS verification skipped. The script integrates with Jenkins credentials to securely access the Kubernetes service account token and executes commands to deploy the application to the specified Minikube cluster.

pipeline {
agent any

stages {
stage('Build') {
steps {
// Compile Java code
bat "javac \"C:\\Users\\deployJavaAppMain.java\""
}
}

stage('Dockerize') {
steps {
script {
// Disable BuildKit
bat 'SET DOCKER_BUILDKIT=0'

// Build Docker image
bat 'docker build -t java-app "C:\\Users\\deployJavaApp"'
}
}
}

stage('Deploy to Minikube') {
steps {
// Apply Kubernetes deployment using the Kubernetes service account
withCredentials([string(credentialsId: 'your-credential-id', variable: 'KUBE_SA_TOKEN')]) {
bat """
kubectl apply -f "C:\\Users\\deployJavaApp\\kubernetes-deployment.yaml" \
--token="$KUBE_SA_TOKEN" \
--server=http://link:port \
--insecure-skip-tls-verify
"""
}
}
}
}
}
  • Then click on Build Now which starts the pipeline to Build and automates the deployment process.

  • Now deployment is successful and you can access the deployed application through exposed service or by accessing the pods directly or you can visit the Kubernetes dashboard to manage your deployment.

Java Application Deployment In Kubernetes With Jenkins CI/CD Pipeline

In modern software development, deploying applications to Kubernetes clusters has become a common practice due to their scalability and reliability. Automating this deployment process using continuous integration and continuous deployment (CI/CD) pipelines with Jenkins streamlines the development workflow and ensures consistent, reliable deployments. This article demonstrates deploying a sample Java application on a Kubernetes cluster using Minikube.

Similar Reads

1. Creating a sample Java application

We’ll start by creating a simple Java application. Compile the Java code using the javac command....

2. Creating a Docker image

We’ll create a Dockerfile to package the Java application into a Docker image. The Dockerfile will copy the compiled Java class file into the image and specify the command to run the application....

3. Defining Kubernetes Deployment YAML File

The deployment YAML file specifies the desired state of the application, including the number of replicas, container image, and other configurations....

4. Creating Jenkins Role YAML File (Optional)

If Jenkins needs permission to interact with Kubernetes resources, you can create a Kubernetes role YAML file to define the required permissions....

5. Creating a Service Account in Kubernetes

Start the minikube....

6. Generating Token using OpenSSl and setting it for the service account

This command generates a random 32-byte token encoded in base64. Copy the generated token as we need it in the next step....

7. Finding the URL and port of the Minikube cluster

The minikube dashboard –url command is used to retrieve the URL of the Kubernetes dashboard running on your Minikube cluster....

8. Configuring Jenkins

Install Jenkins LTS version for your Operating System and complete the on screen setup. On new browser tab visit http://localhost:8080/...

9. Creating Jenkins Pipeline

On Dashboard click on New Item. Enter the item name, example myapp, and select Pipeline Option....

Java application deployment in Kubernetes with Jenkins CI/CD pipeline – FAQ’s

How do I deploy an application in Kubernetes using Jenkins?...

Contact Us