Most organizations use Jenkins as there CI/CD automation tool and Kubernetes as the platform to run the application. We can achieve almost everything using Jenkins pipeline from building an application to deploying it and upgrading the code in production. Let's see it with example.
In legacy systems Jenkins pipeline used to run on worker node scheduled through master node. In containerization world we can not have extra virtual machine just for running Jenkins pipeline instead we can use existing Kubernetes cluster and schedule our pipelines in form of container.
For configuring Kubernetes with Jenkins we use "Kubernetes Plugin".

Configure Kubernetes Cloud in Jenkins
Once Jenkins and Kubernetes plugin are installed it's time we connect it with our Kubernetes cluster. For configuration we need access to Kubernetes cluster with credentials like username and password or secret token to access the cluster.
Go to configure page of Jenkins you will see "Configure clouds" section and add details:
- Name of your cloud: When you add multiple clouds Name will be used to run pipeline on particular cloud.
- Kubernetes URL: Kubernetes API URL, can be found in Kubernetes configuration details
- Server certificate key: Server key for the Kubernetes certificate. For testing you can disable https certificate check instead of certificate key.
- Credentials: You can add credentials in "Manage Credentials" section. For secret use secret text as the credential type.
- After filling all the details you can test connection if it's successful, then we can proceed further.
- Add Jenkins URL, Jenkins Tunnel and keep timeout parameters as it is, you can tweak these parameters going further according to your needs.
- Note: Jenkins tunnel should not have protocol mentioned
We can add multiple clouds Kubernetes plugin supports multiple flavors of Kubernetes including Openshift.

Configure Pod Template
Pod template specifies how your pod will look like, what labels it will have and in which namespace it will be created the cluster.
- Name: Name of the pod
- Namespace: In which namespace pod should be created. Keeping it blank as we can override this in pipeline.
- Labels: Custom Labels for spawn pod
Configure Container Template
Each pod can have multiple containers. By default Jenkins will spawn jnlp container with default image which will handle communication between Jenkins and the Pod.
If you want to use custom agent image you can add container with name "jnlp" with image details.
Add custom environment variable if needed for the container.
Once this configuration is complete now we can head over to creating a pipeline which will run on this configured cluster.
Create a test Pipeline
Now create a pipeline with pipeline script using new item wizard. Use "Pipeline" as type and go further to configure it.
In "Advanced Project Options" select "Pipeline Script" and paste code given below in the snippet.
Below declarative code can be used to create a test pipeline.
Note: cloud directive specifies name of the cluster we configured.
pipeline {
agent {
kubernetes {
label "build-pod"
cloud "kubernetes"
yaml '''
apiVersion: v1
kind: Pod
metadata:
namespace: build-ns
labels:
job: bootvar-build-pod
spec:
containers:
- name: bootvar-container
image: alpine:latest
tty: true
command: ['cat']
'''
}
}
stages {
stage("First") {
steps {
container("bootvar-container") {
sh "ls -lart"
}
}
}
}
}
Now run the pipeline it should start pod on the Kubernetes cluster.
PS C:\Users\Suhas\Documnts> kubectl get pods --namespace=build-ns
NAME READY STATUS RESTARTS AGE
build-pod-6c58b4d8bb 1/1 Running 0 3s