jenkins master outside of Open Shift , use Kubernetes plugin - unix1998/technical_notes GitHub Wiki
we can use the Jenkins Kubernetes plugin to create and manage agent pods dynamically even if the Jenkins master itself is not running as a container within a Kubernetes or OpenShift cluster. Here's how it works:
Jenkins Master on a Linux Host
If your Jenkins master is running directly on a Linux host (not containerized), you can still configure it to interact with a Kubernetes or OpenShift cluster to dynamically create agent pods. Here’s how:
-
Kubernetes Cluster Access:
- Ensure your Jenkins master has network access to the Kubernetes cluster API.
- Configure the necessary credentials and permissions for Jenkins to interact with the Kubernetes cluster.
-
Kubernetes Plugin Configuration:
- Install and configure the Kubernetes plugin on your Jenkins master.
- Set up the Kubernetes cloud in Jenkins with the details of your Kubernetes or OpenShift cluster.
Steps to Configure Jenkins Master on a Linux Host to Use Kubernetes Plugin
-
Install the Kubernetes Plugin:
- Go to Jenkins Dashboard -> Manage Jenkins -> Manage Plugins.
- Install the "Kubernetes" plugin.
-
Configure Kubernetes Cloud:
- Go to Jenkins Dashboard -> Manage Jenkins -> Manage Nodes and Clouds -> Configure Clouds -> Add a new cloud -> Kubernetes.
- Provide the Kubernetes cluster details:
- Kubernetes URL: The API endpoint of your Kubernetes cluster.
- Kubernetes Namespace: The namespace where Jenkins agent pods should be created.
- Credentials: Set up credentials for Jenkins to authenticate with the Kubernetes cluster. This could be a service account token with appropriate permissions.
-
Configure Jenkins Kubernetes Plugin:
- Add a Pod Template:
- Define the containers and configurations required for your Jenkins agents.
- Specify the retention annotations as needed (
Never
,OnFailure
,Always
).
- Add a Pod Template:
Example Jenkinsfile Using Kubernetes Plugin
Even with the Jenkins master on a Linux host, you can use the Kubernetes plugin in your Jenkinsfile as follows:
podTemplate(yaml: '''
apiVersion: v1
kind: Pod
metadata:
annotations:
jenkins.io/podRetention: Never # Other options are OnFailure, Always
spec:
containers:
- name: maven
image: maven:3.3.9-jdk-8-alpine
tty: true
command:
- cat
- name: golang
image: golang:1.8.0
tty: true
command:
- cat
''') {
node(POD_LABEL) {
stage('Get a Maven project') {
git 'https://github.com/jenkinsci/kubernetes-plugin.git'
container('maven') {
stage('Build a Maven project') {
sh 'mvn -B clean install'
}
}
}
stage('Get a Golang project') {
git url: 'https://github.com/hashicorp/terraform.git'
container('golang') {
stage('Build a Go project') {
sh """
mkdir -p /go/src/github.com/hashicorp
ln -s `pwd` /go/src/github.com/hashicorp/terraform
cd /go/src/github.com/hashicorp/terraform && make core-dev
"""
}
}
}
}
}
Key Points:
- Jenkins Master on Linux Host: The Jenkins master does not need to be containerized or running within the Kubernetes cluster. It can run on a standalone Linux host.
- Kubernetes Cluster Access: Ensure Jenkins has network access to the Kubernetes cluster and the necessary credentials.
- Dynamic Agent Pods: The Kubernetes plugin will still manage the creation of ephemeral agent pods within the Kubernetes cluster based on your job requirements.
Summary
- Jenkins Master Flexibility: The Jenkins master can be either containerized within Kubernetes/OpenShift or running directly on a Linux host.
- Kubernetes Plugin Configuration: Properly configure the Kubernetes plugin in Jenkins to interact with your Kubernetes cluster.
- Dynamic Pod Creation: The plugin will dynamically create and manage agent pods on the Kubernetes cluster, providing flexibility and scalability for your CI/CD pipelines.
This setup allows you to leverage the power of Kubernetes for dynamic and scalable build environments while maintaining a non-containerized Jenkins master if needed.