Launching the Jenkins server via Kubernetes deployment

Based on the previous recipes, now you have:

  • A custom Jenkins container image
  • A service account

Finally, you can launch your custom Jenkins server on your Kubernetes cluster. Remember that we need to run a docker command in the Docker environment, which needs to mount /var/run/docker.sock from the local Kubernetes node.

In addition, we need to use a jenkins-sa service account to launch a Jenkins pod. It needs to specify spec.template.spec.serviceAccountName: jenkins-sa in the deployment configuration.

It is also recommended to have a PersistentVolume to preserve Jenkins home (/var/jenkins_home), in case a pod is restarted. We just simply use the hostPath /data/jenkins-data directory (assuming you use minikube). You may change to another path or other types of PersistentVolume to fit with your environment.

Overall, the deployments YAML configuration for Jenkins is as follows:

$ cat jenkins.yaml 
apiVersion: apps/v1
kind: Deployment
...
spec:
serviceAccountName: jenkins-sa
containers:
- name: my-jenkins
image: hidetosaito/my-jenkins
readinessProbe:
initialDelaySeconds: 40
tcpSocket:
port: 8080
volumeMounts:
- mountPath: /var/run/docker.sock
name: docker-sock
- mountPath: /var/jenkins_home
name: jenkins-data
volumes:
- name: docker-sock
hostPath:
path: /var/run/docker.sock
- name: jenkins-data
hostPath:
path: /data/jenkins-data
...


$ kubectl create -f jenkins.yaml
deployment.apps "my-jenkins" created
service "my-jenkins-service" created

After a few minutes, Kubernetes pulls your custom Jenkins image and runs a Jenkins pod which is capable of running a docker command and a kubectl command without any configuration due to mounting the /var/run/docker.sock and jenkins-sa service account: 

//check Jenkins Pod status
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
my-jenkins-758b89849c-t2sm9 1/1 Running 0 17m


//access to Jenkins Pod
$ kubectl exec -it my-jenkins-758b89849c-t2sm9 -- /bin/bash


//within Jenkins Pod, you can run docker command
root@my-jenkins-758b89849c-t2sm9:/# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
e7bb522d92ff: Pull complete
6edc05228666: Pull complete
cd866a17e81f: Pull complete
Digest: sha256:926b086e1234b6ae9a11589c4cece66b267890d24d1da388c96dd8795b2ffcfb
Status: Downloaded newer image for nginx:latest


//within Jenkins Pod, you can run kubectl command
root@my-jenkins-758b89849c-t2sm9:/# kubectl get nodes
NAME STATUS ROLES AGE VERSION
gke-chapter5-default-pool-97f6cad9-19vm Ready <none> 1h v1.8.6-gke.0
gke-chapter5-default-pool-97f6cad9-1qxc Ready <none> 1h v1.8.6-gke.0
gke-chapter5-default-pool-97f6cad9-cglm Ready <none> 1h v1.8.6-gke.0


//go back to your terminal
root@my-jenkins-758b89849c-t2sm9:/# exit
exit

You are all set! Now you can configure a Jenkins job to build your application, build a container, and deploy to Kubernetes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.232.189