deploying pod from k8 plugin jenkins
start minikube
$ minikube start --bootstrapper=localkube --cpus 4 --memory 8192
$ kubectl get pods --show-labels
$ minikube ip
$ kubectl get events --watch
$ kubectl get -a pods --watch
jenkinsfile pipeline script
podTemplate(
inheritFrom: 'default',
nodeSelector: 'kubernetes.io/hostname=minikube',
containers: [
containerTemplate(name: 'maven', image: 'maven:3.3.9-jdk-8-alpine', ttyEnabled: true, command: 'cat' ),
containerTemplate(name: 'jnlp', image: 'jenkins/jnlp-slave', args: '${computer.jnlpmac} ${computer.name})')
]) {node(POD_LABEL) {
stage('Get a Maven project') {
git 'https://github.com/<project>.git/'
container('maven') {
stage('Build a Maven project') {
sh 'mvn -B -gs ./settings.xml clean install'
}
}
}
}
}
scripted way
def label = "pod1"
podTemplate(label: label, nodeSelector:"kubernetes.io/hostname=minikube", yaml: """
apiVersion: v1
kind: Pod
metadata:
labels:
jenkins: slave
spec:
containers:
- name: jnlp
image: "jenkins/jnlp-slave:3.35-2-alpine"
imagePullPolicy: Always
env:
-
name: JENKINS_TUNNEL
value: "127.0.0.1:50001"
-
name: JENKINS_URL
value: "127.0.0.1:8080"
-
name: DOCKER_HOST
value: "tcp://192.168.99.121:2376"
-
name: JENKINS_AGENT_WORKDIR
value: /home/jenkins
-
name: JENKINS_ARGS
value: "--prefix=/jenkins"
restartPolicy: OnFailure
securityContext: {}
""",
containers: [containerTemplate(name: 'maven', image: 'maven:3.3.9-jdk-8-alpine', ttyEnabled: true, command: 'cat')])
{
node("pod1") {
stage('Get a Maven project') {
git '<git>/'
container('maven') {
stage('Build a Maven project') {
sh 'mvn -B -gs ./settings.xml clean install'
}
}
}
}
}
declarative way
pipeline {
agent {
kubernetes {
yaml """
apiVersion: v1
kind: Pod
metadata:
labels:
kubernetes.io/hostname=minikube
spec:
securityContext:
runAsUser: 1000
containers:
- name: maven
image: maven:3.3.9-jdk-8-alpine
command:
- cat
args: [ "while true; do sleep 600; done;" ]
tty: true
nodeSelector:
key1: value1
"""
}
}
stages {
stage('Run maven') {
steps {
container('maven') {
git 'https://github.com/<project>.git/'
sh 'mvn -B -gs ./settings.xml clean install'
}
}
}
}
}
some basic understanding
- jenkins k8s plugin lets you control the creation of jenkins slave pod from the pipeline and add one or more build containers to the slave pod to accommodate build requirements and dependencies.
- new build from jenkins master is scheduled to create a new slave pod
- each stage of the build is run in a container in the slave pod
- by default each stage runs in jenkins slave (jnlp) container, unless specified.
- slave pod is atleast one container which must be Jenkins jnlp container.
ex: jenkins/jnlp-slave:3.35–2-alpine - pipeline defines a podTemplate which specifies all the containers that make up the jenkins slave pod.
from the above figure:
- mybuilder is the podTemplate.
- jenkins-slave is default container.
golang is another container.
gcc is another container.
Actual working using helm
$ helm install -n jenkins stable/jenkins$ printf $(kubectl get secret --namespace default jenkins -o jsonpath="{.data.jenkins-admin-password}" | base64 --decode);echo$ export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/component=jenkins-master" -l "app.kubernetes.io/instance=jenkins" -o jsonpath="{.items[0].metadata.name}")$ kubectl --namespace default port-forward $POD_NAME 8080:8080
// changed port to 8081 since 8080 was already alloted
$ kubectl --namespace default port-forward $POD_NAME 8081:8080
settings in configuring cloud :
Kubernetes URL : https://kubernetes.default or https://kubernetes.default.svc.cluster.localJenkins URL : http://jenkins:8080
Jenkins Tunnel : jenkins-agent:50000
Pod Labels:
key = jenkins/jenkins-jenkins-slave
value =
Pod Retention : Never
Pod Templates:
Name = default
Labels = jenkins-jenkins-slave
Usage = Use this node as much as possible
Containers:
Name = jnlp
docker_image = jenkins/jnlp-slave:3.27-1
working_directory = /home/jenkins
command_to_run = /bin/sh -c
arguments = ${computer.jnlpmac} ${computer.name}
EnvVars:
key = JENKINS_URL
value = http://jenkins.default.svc.cluster.local:8080
Jenkinsfile — scripted way
podTemplate(
nodeSelector: 'kubernetes.io/hostname=minikube',
label: 'mypod',
containers: [
containerTemplate(name: 'git', image: 'alpine/git', ttyEnabled: true, command: 'cat'),
containerTemplate(name: 'maven', image: 'maven:3.3.9-jdk-8-alpine', command: 'cat', ttyEnabled: true),
containerTemplate(name: 'docker', image: 'docker', command: 'cat', ttyEnabled: true)
],
volumes: [
hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'),
hostPathVolume(mountPath: '/home/jenkins/agent', hostPath: '//c/Users/<user>/mounted'),
hostPathVolume(mountPath: '/root/.m2', hostPath: '//c/Users/<user>/.m2'
]
) {
node('mypod') {
stage('Check running containers') {
container('docker') {
// example to show you can run docker commands when you mount the socket
sh 'hostname'
sh 'hostname -i'
sh 'docker ps'
}
}
stage('Clone repository') {
container('git') {
dir('springboot_deploy_helm'){
deleteDir()
}
sh 'whoami'
sh 'hostname -i'
sh 'git clone https://github.com/<user>/springboot_deploy_helm.git'
}
}stage('Maven Build') {
container('maven') {
dir('springboot_deploy_helm/') {
sh 'hostname'
sh 'hostname -i'
sh 'mvn clean install'
}
}
}
}
}
Issues when jenkins is run outside minikube cluster
- created a new Node called “slave1"
executors: 1
labes: minikube_pod
Tools: maven,jdk,git - config global security → Agents
Tcp Port for inbound Agents: Random
Agent Protocols: check the box - now lauch the node using jnlp or jar
- used this label in podTemplate label properties
ERROR: Node is not a Kubernetes node: slave1 - the above error is justifiable since Node are not cloud ( k8s, Docker)
- create a k8s cloud with slave pod as an jenkins slave agent.
References: