Jenkins Kubernetes Plugin
Defining the environments needed for the build is not really easy if you are using the standard way of Jenkins pipelines. Here, I propose to use Jenkins Kubernetes Plugin to be able to use containers as nodes.
Jenkins has to be installed in the Kubernetes cluster to be able accessing to the cluster. How to install Jenkins to Kubernetes I described earlier here.
Kubernetes Plugin
Kubernetes Plugin allows you to create a Pod with dynamic containers, which you will define a bit later and this Pod will be used by Jenkins as a Node where will be running your pipelines.
Install the Kubernetes Plugin
Go to Manage Jenkins → Manage Plugins → Available and find the Kubernetes:
Let’s install and restart your Jenkins.
Jenkins has to be able access to the cluster, for that we have to go Manage Jenkins → Manage Credentials and select Store named Jenkins → Global credentials, after click Add Credentials:
As a kind choose a Secret file, the Scope has to be Global to be able using this credentials for any pipelines and to File field attach the kubeconfig.yaml
file.
To get kubeconfig to execute the following command in your cluster:
kubectl config view --minify
Configure Kubernetes Plugin
Go to Manage Jenkins → Manage Nodes and Clouds in the left menu select Configure Clouds, where you will see Kubernetes:
If you cannot see Kubernetes section, click “Add a new cloud” and select Kubernetes.
Next, we have to configure that and click Kubernetes Cloud details.
-
Kubernetes URL - an API or domain name of your cluster.
-
Disable https certificate check - set as true if access by the defined URL above does not use HTTPS protocol.
-
Kubernetes Namespace - the name of Kubernetes Namespace where Jenkins will create new nodes for your builds.
-
Credential - as a credential choose early created creds, which contains
kubeconfig.yaml
. -
Jenkins URL - this URL is an URL to Jenkins through Kubernetes Service, in my case, it named as jenkins [http://jenkins:8080].
-
Jenkins tunnel - the same as for Jenkins URL contains the name of Kubernetes Service for Jenkins Agent by port.
Simple Jenkinsfile based on Kubernetes
Now, we need to create a Jenkinsfile with an agent define the kubernetes {}
where we could define the YAML file or as demonstrated below put the YAML content containing information about needed Node for our builds. Which containers and which version should we use.
After, we are able to use container ([name]) {}
construction if we want to execute specific instructions under a particular container.
container ([name]) {
..
}
Bellow, I provided a simple Jenkins Pipeline where I use this way:
// Jenkinsfile
pipeline {
agent {
kubernetes {
yaml """\
apiVersion: v1
kind: Pod
metadata:
labels:
some-label: some-label-value
spec:
containers:
- name: maven
image: maven:alpine
command:
- cat
tty: true
- name: busybox
image: busybox
command:
- cat
tty: true
""".stripIndent()
}
}
stages {
stage('Run maven') {
steps {
container('maven') {
sh 'mvn -version'
}
container('busybox') {
sh '/bin/busybox'
}
}
}
}
}
If you still have questions, feel free to ask in the comments below.