Jenkins X on Kubernetes

Jenkins is one of the earliest open source antomation server and remains the most common option in use today. Over the years, Jenkins has evolved into a powerful and flexible framework with hundreds of plugins to support automation for any project.

Jenkins X, on the other hand, is a CI/CD platform (Jenkins Platform) for modern cloud applications on Kubernetes.

Here we talk about some basic concepts about Jenkins X and provide a hand-to-hand guide to deploy jenkins-x on Kubernetes.

For more information about jx itself, check Jenkins-X Github Repo

Architecture

Check this beautiful diagram.

https://jenkins-x.io/architecture/diagram/


Install

Create GKE cluster & Get Credentials

1gcloud init
2gcloud components update
 1CLUSTER_NAME=jenkins-server
 2#CLUSTER_NAME=jenkins-serverless
 3
 4gcloud container clusters create ${CLUSTER_NAME} \
 5  --num-nodes 1 \
 6  --machine-type n1-standard-4 \
 7  --enable-autoscaling \
 8  --min-nodes 1 \
 9  --max-nodes 2 \
10  --zone asia-east1-b \
11  --preemptible
12
13# After cluster initialization, get credentials to access cluster with kubectl
14gcloud container clusters get-credentials ${CLUSTER_NAME}
15
16# Check cluster stats.
17kubectl get nodes

Install jx on Local Machine

[Jenkins X Release](https://github.com/jenkins-x/jx/releases](https://github.com/jenkins-x/jx/releases)

 1JX_VERSION=v2.0.2
 2OS_ARCH=darwin-amd64
 3#OS_ARCH=linux-amd64
 4curl -L https://github.com/jenkins-x/jx/releases/download/"${JX_VERSION}"/jx-"${OS_ARCH}".tar.gz | tar xzv
 5sudo mv jx /usr/local/bin
 6jx version
 7
 8NAME               VERSION
 9jx                 2.0.2
10Kubernetes cluster v1.11.7-gke.12
11kubectl            v1.11.9-dispatcher
12helm client        v2.11.0+g2e55dbe
13helm server        v2.11.0+g2e55dbe
14git                git version 2.20.1
15Operating System   Mac OS X 10.14.4 build 18E226

(Option 1) Install Serverless Jenkins Pipeline

1DEFAULT_PASSWORD=mySecretPassWord123
2jx install \
3  --default-admin-password=${DEFAULT_PASSWORD} \
4  --provider='gke'

Options:

  • Enter Github user name
  • Enter Github personal api token for CI/CD
  • Enable Github as Git pipeline server
  • Select Jenkins installation type:
    • Serverless Jenkins X Pipelines with Tekon
    • Static Master Jenkins
  • Pick default workload build pack
    • Kubernetes Workloads: Automated CI+CD with GitOps Promotion
    • Library Workloads: CI+Release but no CD
  • Select the organization where you want to create the environment repository:
    • chechiachang
1Your Kubernetes context is now set to the namespace: jx
2INFO[0231] To switch back to your original namespace use: jx namespace jx
3INFO[0231] Or to use this context/namespace in just one terminal use: jx shell
4INFO[0231] For help on switching contexts see: https://jenkins-x.io/developing/kube-context/
5INFO[0231] To import existing projects into Jenkins:       jx import
6INFO[0231] To create a new Spring Boot microservice:       jx create spring -d web -d actuator
7INFO[0231] To create a new microservice from a quickstart: jx create quickstart

(Option 2) Install Static Jenkins Server

1DEFAULT_PASSWORD=mySecretPassWord123
2
3jx install \
4  --default-admin-password=${DEFAULT_PASSWORD} \
5  --provider='gke'

Options:

  • Enter Github user name
  • Enter Github personal api token for CI/CD
  • Enable Github as Git pipeline server
  • Select Jenkins installation type:
    • Serverless Jenkins X Pipelines with Tekon
    • Static Master Jenkins
  • Pick default workload build pack
    • Kubernetes Workloads: Automated CI+CD with GitOps Promotion
    • Library Workloads: CI+Release but no CD
  • Select the organization where you want to create the environment repository:
    • chechiachang
1INFO[0465]Your Kubernetes context is now set to the namespace: jx
2INFO[0465] To switch back to your original namespace use: jx namespace default
3INFO[0465] Or to use this context/namespace in just one terminal use: jx shell
4INFO[0465] For help on switching contexts see: https://jenkins-x.io/developing/kube-context/
5INFO[0465] To import existing projects into Jenkins:       jx import
6INFO[0465] To create a new Spring Boot microservice:       jx create spring -d web -d actuator
7INFO[0465] To create a new microservice from a quickstart: jx create quickstart

Access Static Jenkins Server through Domain with username and password Domain http://jenkins.jx.11.22.33.44.nip.io/

Uninstall

1jx uninstall
2# rm -rf ~/.jx

Setup CI/CD Pipeline

Create Quickstart Repository

1kubectl get pods --namespace jx --watch
1# cd workspace
2jx create quickstart

Options:

  • Which organisation do you want to use? chechiachang
  • Enter the new repository name: serverless-jenkins-quickstart
  • select the quickstart you wish to create [Use arrows to move, type to filter] angular-io-quickstart aspnet-app dlang-http

golang-http jenkins-cwp-quickstart jenkins-quickstart node-http

1INFO[0121] Watch pipeline activity via:    jx get activity -f serverless-jenkins-quickstart -w
2INFO[0121] Browse the pipeline log via:    jx get build logs chechiachang/serverless-jenkins-quickstart/master
3INFO[0121] Open the Jenkins console via    jx console
4INFO[0121] You can list the pipelines via: jx get pipelines
5INFO[0121] Open the Jenkins console via    jx console
6INFO[0121] You can list the pipelines via: jx get pipelines
7INFO[0121] When the pipeline is complete:  jx get applications

Check log of the first run

1jx logs pipeline

Add Step to Pipeline

Add a setup step for pullrequest

1cd serverless-jenkins-quickstart
2jx create step --pipeline pullrequest \
3  --lifecycle setup \
4  --mode replace \
5  --sh "echo hello world"

Validate pipeline step for each modification

1jx step validate

A build-pack pod started after git push. Watch pod status with kubectl.

1kubectl get pods --namespace jx --watch

Check Build Status on Prow (Serverless)

http://deck.jx.130.211.245.13.nip.io/ Login with username and password

Import Existing Repository

In source code repository:

Import jx to remote jenkins-server. This will apply a Jenkinsfile to repository by default

1jx import --url git@github.com:chechiachang/serverless-jenkins-quickstart.git

Update jenkins-x.yml

1jx create step

git commit & push

Trouble Shooting

Failed to get jx resources

1jx get pipelines

Make sure your jx (or kubectl) context is with the correct GKE and namespace

1kc config set-context gke_my-project_asia-east1-b_jenkins \
2  --namespace=jx

Why not use helm chart?

It’s readlly depend on what we need in CI/CD automation.

Jenkins Helm Chart create Jenkins master and slave cluster on Kubernetes utilizing the Jenkins Kubernetes plugin. Jenkin Platform with jx is Jenkins Platform native to Kubernetes. It comes with powerful cloud native components like Prow automation, Nexus, Docker Registry, Tekton Pipeline, …

Check jenkins-x examples

https://github.com/jenkins-x-buildpacks/jenkins-x-kubernetes/tree/master/packs


Client

1jx get urls
2Name                      URL
3jenkins                   http://jenkins.jx.11.22.33.44.nip.io
4jenkins-x-chartmuseum     http://chartmuseum.jx.11.22.33.44.nip.io
5jenkins-x-docker-registry http://docker-registry.jx.11.22.33.44.nip.io
6jenkins-x-monocular-api   http://monocular.jx.11.22.33.44.nip.io
7jenkins-x-monocular-ui    http://monocular.jx.11.22.33.44.nip.io
8nexus                     http://nexus.jx.11.22.33.44.nip.io

Get Cluster Status

1jx diagnose

Get Applications & Pipelines

1jx get applications
2jx get pipelines

Get CI Activities & build log

1jx get activities
2jx get activities --filter='jenkins-x-on-kubernetes'
3
4jx get build log
5
6INFO[0003] view the log at: http://jenkins.jx.11.22.33.44.nip.io/job/chechiachang/job/jenkins-x-on-kubernetes/job/feature-add-test/3/console
7...

Trigger Build & Check Activity

1jx start pipeline
2jx start pipeline --filter='jenkins-x-on-kubernetes/feature-add-test'
3
4jx get activities --filter='jenkins-x-on-kubernetes'

Create Pull Request

1jx create pullrequest