Hello. Still in the sequence of Jenkins x Kubernetes posts. In this one, I want to show the next step when it comes to Pods, Containers and Pipelines. I also highly recommend reading our next post: Jenkins – Shared Libraries, Containers and Manifests.
Using Pod Templates configured directly in the Kubernetes Cloud makes it easier, but you are stuck with only those types of containers.
Taking into account that the idea is to have smaller and more specialised images, and it is not possible to have a Pod Template that fits all the pipeline options you may have. Creating several Containers in the Pod Template and using only a few is even worse.
The solution is to declare the Pod Template at Pipeline runtime.
To do this, I made two changes to our Cloud configuration.
1 – I renamed it to kubernetes
2 – I deleted the Pod Template.
The example pipeline that I will show below creates 2 containers in the POD with different images, even to simulate a situation that requires Containers with specialised images:
1 – container-1 with the image registry.devops-db.internal:5000/img-jenkins-devopsdb:2.0
2 – container-2 with the image ubuntu:22.04
Another item in this POD is a volume shared between the PODs. This is very important for sharing the results of a process, for example files, between containers and thus achieve a more fluid process and communication of information. I will demonstrate this by creating a file in one Container and catting it in another.
I also added a 10-minute Sleep, so I can go to Bash and validate the creation of the POD and Container.
Remember, as in Post Jenkins – Pods, Containers and Pipelines, you must indicate the Container in which the Step will be executed.
pipeline {
agent {
kubernetes {
yaml """
apiVersion: v1
kind: Pod
metadata:
labels:
some-label: "pod-template-${env.BUILD_ID}"
spec:
containers:
- name: container-1
image: registry.devops-db.internal:5000/img-jenkins-devopsdb:2.0
env:
- name: CONTAINER_NAME
value: "container-1"
volumeMounts:
- name: shared-volume
mountPath: /mnt
command:
- cat
tty: true
- name: container-2
image: ubuntu:22.04
env:
- name: CONTAINER_NAME
value: "container-2"
volumeMounts:
- name: shared-volume
mountPath: /mnt
command:
- cat
tty: true
volumes:
- name: shared-volume
emptyDir: {}
"""
retries 2
}
}
stages {
stage('test container-1') {
steps {
container('container-1') {
script {
sh 'echo $CONTAINER_NAME'
sh 'echo $CONTAINER_NAME > /mnt/shared_volume.txt'
}
}
}
}
stage('test container-2') {
steps {
container('container-2') {
script {
sh 'echo $CONTAINER_NAME'
sh 'cat /mnt/shared_volume.txt'
sh 'sleep 10m'
}
}
}
}
}
}
Let’s validate kubernetes:
kubectl get pods --namespace jenkins-devopsdb
NAME READY STATUS RESTARTS AGE
infrastructure-pipelines-tests-k8-test-70-9h4jl-2hv83-6zvk3 3/3 Running 0 3m49s
In fact, there is a POD with 3 Containers. Let’s see which containers.
kubectl get pods infrastructure-pipelines-tests-k8-test-70-9h4jl-2hv83-6zvk3 --namespace jenkins-devopsdb -o="custom-columns=NAME:.metadata.name,INIT-CONTAINERS:.spec.initContainers[*].name,CONTAINERS:.spec.containers[*].name"
NAME INIT-CONTAINERS CONTAINERS
infrastructure-pipelines-tests-k8-test-70-9h4jl-2hv83-6zvk3 <none> container-1,container-2,jnlp
There are Containers: container-1, container-2 and jnlp (created automatically)
And this was the result of the pipeline in Jenkins:
Leave a Reply