The Startup
Published in

The Startup

Introduction to Jenkins Operator

Getting started with Jenkins Operator in Kubernetes

Before we dive into Jenkins Operator, let us spare some time to understand what a Kubernetes operator actually is. As mentioned in RedHat’s Documentation page, a Kubernetes operator is an application-specific controller that extends the functionality of the Kubernetes API to create, configure, and manage instances of complex applications on behalf of a Kubernetes user. It is a method of packaging, deploying, and managing a Kubernetes application. The Kubernetes Operator concept was developed by engineers at CoreOS in 2016. There are numerous operators available in the market as of now. Out of them, the most widely used operators are

  1. Istio Operator
  2. ArgoCD Operator
  3. Cert Manager Operator
  4. Horizontal Pod Autoscaler Operator
  5. Kong Operator
  6. Falco Operator
  7. Prometheus Operator etc

In this article, we would learn about how to set up Jenkins Operator in a Kubernetes Cluster.

Jenkins Operator

What is the entire story all about? (TLDR)

  1. Install Jenkins Operator on our Kubernetes cluster( EKS, AKS, GKE, Local, Kind cluster )
  2. Use the Jenkins CRD to setup Jenkins
  3. Seed Jobs from an SCM ( GitHub )
  4. Use the Kubernetes Plugin for Jenkins ( This Plugin creates a Kubernetes Pod for each agent started )

Prerequisites

  1. Kubernetes cluster ( Either an EKS, AKS, GKE, Local, Kind cluster )
  2. Helm client Installed ( To Install the Helm chart of Jenkins Operator )
  3. An SCM to store your Jobs and Pipelines

Story Resources

  1. GitHub Link: https://github.com/pavan-kumar-99/medium-manifests
  2. GitHub Branch: jenkins_operator

Install Jenkins Operator

You can install the Jenkins operator directly using the helm chart

kubectl create namespace jenkins-operator helm repo add jenkins https://raw.githubusercontent.com/jenkinsci/kubernetes-operator/master/charthelm install jenkins-operator jenkins/jenkins-operator -n jenkins-operator --set jenkins.enabled=false 
Jenkins Operator Logs

And now you should see the Jenkins Operator Pod starting in the jenkins-operator namespace. This operator pod will keep watching for any Jenkins CRD’s created. Let us now create one to deploy our Jenkins to the cluster.

Use the Jenkins CRD to setup Jenkins

Let us now understand the whole jenkins.yaml file.

  1. apiVersion = jenkins.io/v1alpha2
  2. kind = Jenkins
  3. metadata.name = The name of the Jenkins Instance to be created
  4. spec.service.type = LoadBalancer ( The type of the service in which Kubernetes is exposed. It can be either ClusterIP, NodePort, LoadBalancer )
  5. spec.service.port = The port on which the service is to be exposed
  6. spec.master = These properties represents the Jenkins master node properties
  7. spec.master.basePlugins: BasePlugins contains plugins required by the Jenkins operator.
  8. spec.master.plugins: The list of plugins required by the user to be installed
  9. seedJobs: It defines the list of Jenkins seed Job Configurations from SCM’s. Here I have given my GitRepo URL and the name of the branch from which the seedJobs should be configured.

This would now Install the Jenkins in your Kubernetes cluster

curl -s https://gist.githubusercontent.com/pavan-kumar-99/9a129bc789c85249713d0f3398f125ed/raw/1c3b5cd0a15d3dafca0e5537271d311865c4bb5e/jenkins.yaml | kubectl apply -n jenkins-operator -f -
Components Installed in the jenkins-operator namespace

Let us now get the username and password of the Jenkins

kubectl get secret jenkins-operator-credentials-prod-jenkins -n jenkins-operator -o 'jsonpath={.data.user}' | base64 -dkubectl get secret jenkins-operator-credentials-prod-jenkins -n jenkins-operator -o 'jsonpath={.data.password}' | base64 -d

Let us now get the IP of our LoadBalancer. In case if you are using a local cluster, you can use metallb as a LoadBalancer Implementation. ( MetalLb is a load-balancer implementation for bare metal Kubernetes Clusters ).

kubectl get services -n jenkins-operator jenkins-operator-http-prod-jenkins -o jsonpath='{.status.loadBalancer.ingress[0].ip}'

You can now access your Jenkins at http://${LoadBalancerIp}:8080

Jenkins Console

Seed Jobs from an SCM ( GitHub )

A seed job is a Jenkins job that runs a DSL script to generate a new job. You can thus use a groovy script to create a larger number of Jobs. As mentioned here let us prepare our repo in the same format.

cicd folder in our GitHub

You can also clone my repo containing the cicd files

git clone https://github.com/pavan-kumar-99/medium-manifests.git -b jenkins_operator 

Where the jobs folder contains the files required to create the Jenkins jobs and the pipelines folder contain the actual Jenkins pipeline files.

Jenkins jobs file
Jenkins pipeline files use the pod template to create a pod

So our Jenkins pipeline will just execute the hostname command. Now if you have noticed in the Jenkins node our seed jobs were already created !!!

Noice ….
SeedJob from our GitHub

Use the Kubernetes Plugin for Jenkins

Kubernetes Plugin is a Jenkins plugin to run dynamic agents in a Kubernetes cluster. This Plugin creates a Kubernetes Pod for each agent started. However, It is not required to run the Jenkins master inside Kubernetes. Now let us run the pipeline configured by our SeedJob. You can take a look at the pipeline configuration in my Github. As soon as I build my pipeline I can see a pod getting created in my jenkins-operator namespace. So all the stages defined in our pipeline file will be running in the same pod created by Jenkins. Once the steps in the pipeline are completed, the pod also gets terminated ( You can retain them if needed by changing the configuration ).

Pipeline creating a pod
Jenkins pipeline Console Output

Conclusion

Thanks for reading my article. Hope you have liked it. Here are some of my other articles that may interest you.

Recommended

Reference

--

--

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