Setup Jenkins Pipeline and Blue Ocean in Kubernetes

Jenkins Pipeline (or simply “Pipeline” with a capital “P”) is a suite of plugins which supports implementing and integrating continuous delivery pipelines into Jenkins.

Blue Ocean rethinks the user experience of Jenkins. Designed from the ground up for Jenkins Pipeline, but still compatible with freestyle jobs, Blue Ocean reduces clutter and increases clarity for every member of the team.

Image for post
Image for post

Install Jenkins in K8s

Create namespace

Create Jenkins containers

Create pv/pvc/deployment/service/ingress by yaml files

Storage yaml

The content of ops-storage.yml is as bellow.

RBAC yaml

The content of rbac.yml is as bellow.

Jenkins yaml

The content of jenkins.yml is as bellow. Remember to replace the Traefik ingress configuration [domain] to your domain.

Wait until running

Check if pods runs

Login Jenkins

Check the Jenkins password and login, then install plugins.

Install plugins

Installed plugins include:

  • Blue Ocean
  • Kubernetes plugin (Job and Stage Monitoring Plugin)
  • NodeJS Plugin
  • Embeddable Build Status Plugin

Jenkins Slave Image

Jenkins Slave jobs will run in your Kubernetes cluster, so it is important that the kubectl version for you Kubernetes cluster is the same with the Jenkins Slave docker image, otherwise you may meet some version conflict issues for Jenkins Pipelines jobs.

I build my own Jenkins Slave docker image hustakin/jenkins-slave:latest with the kubectl version v1.14.2. For different version, you should build your own jenkins slave docker image with another kubectl version based on image cnych/jenkins:jnlp.

Kuberctl replace

The easiest way to customize different version kubectl is to replace the executable file “kubectl” from image cnych/jenkins:jnlp. You can find the file in your kubernetes cluster or download it.

Dockerfile

Copy the kubectl file to current folder and build the Dockerfile.

Jenkins Configurations

Configure Kubernetes

Click Manage Jenkins->Configure System->Add a new cloud->Kubernetes. Test the connection and input namespace and Jenkins inner url by format (..svc.cluster.local:8080)

Image for post
Image for post

Configure Jenkins Slave k8s template

Add pod template, and remember to clean the “Command to run” and “Arguments to pass to the command” (This’s really important!).

The image hustakin/jenkins-slave:latest has the kubectl version of v1.14.2

Image for post
Image for post

Configure Jenkins Slave k8s volumes

Add volumes in the pod template including docker, .kube config files as well as the maven cache. (Each node of the cluster will cache the maven into /home/ec2-user/.m2)

Image for post
Image for post

Configure service account

The service account is created in previous RBAC yaml step.

Image for post
Image for post

Configure global tool

Click Manage Jenkins->Global Tool Configuration. Configure maven installations.

Image for post
Image for post

Configure NodeJS installations

Click Manage Jenkins->Global Tool Configuration. Configure NodeJS installations.

Image for post
Image for post

Configure DockerHub credentials

Click Jenkins->Credentials->(global)->Add credentials. Specify your DockerHub username and password. Remember the ID here will be used in your Jenkinsfile to specify “credentialsId”.

Image for post
Image for post

Configure Pipelines

Write Dockerfile and Jenkinsfile and k8s.yaml in your project root folder and push it to GitHub. Create the repository in Dockerhub for your project in order to save docker built images.

Jenkinsfile content for SpringBoot

The Jenkinsfile content is as bellow

Dockerfile content for SpringBoot

The Dockerfile content is as bellow

K8s content for SpringBoot

The k8s.yaml content is as bellow

Jenkinsfile content for Angular

The Jenkinsfile content is as bellow

Dockerfile content for Angular

The Dockerfile content is as bellow

Nginx conf content for Angular

The nginx/default.conf content is as bellow.

K8s content for Angular

The k8s.yaml content is as bellow

Create Github Pipeline

Click Jenkins->Open Blue Ocean->New Pipeline, and create pipeline for your project.

Image for post
Image for post

Create Github Pipeline

The jenkins pipeline should work now. Enter your jenkins job and master branch, you could see the stage view here.

Image for post
Image for post

Trigger pipeline

Push your code changes to Github. Click Jenkins->Open Blue Ocean. You can trigger the build for all pipelines you created by clicking the Run button.

Image for post
Image for post

Blue Ocean build

Click Jenkins->Open Blue Ocean, and click the pipeline you just created, you will see the Jenkinsfile stages and related logs here.

Image for post
Image for post

Written by

Researcher | Architect | Full-Stack | @hustakin

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store