avatarPavan Kumar

Summary

The website content provides a comprehensive guide on setting up Jenkins Operator in a Kubernetes cluster, including installation, configuration, and the use of Jenkins CRDs, seed jobs, and the Kubernetes Plugin for Jenkins.

Abstract

The article "Introduction to Jenkins Operator" is a technical tutorial aimed at Kubernetes users interested in deploying Jenkins within their clusters. It begins by explaining the concept of a Kubernetes operator, highlighting the Jenkins Operator as a specific example. The Jenkins Operator extends Kubernetes API capabilities to manage Jenkins instances efficiently. The guide details the prerequisites for installation, such as a Kubernetes cluster and Helm client, and provides step-by-step instructions for installing the Jenkins Operator using a Helm chart. It also covers the creation and management of Jenkins instances using Custom Resource Definitions (CRDs) and seed jobs, which are jobs that can be automatically created from a source control manager (SCM) like GitHub. The article further explains how to use the Kubernetes Plugin to dynamically provision Jenkins agents as Kubernetes Pods. The author demonstrates the process with examples, including screenshots and code snippets, and concludes by providing access to additional resources and related articles for further learning.

Opinions

  • The author emphasizes the importance of Kubernetes operators in managing complex applications, suggesting that the Jenkins Operator is a valuable tool for Kubernetes users.
  • The article implies that using the Jenkins Operator simplifies the deployment and management of Jenkins on Kubernetes, making it a preferred method over traditional deployment approaches.
  • The author seems to advocate for the use of seed jobs and SCMs to automate job creation in Jenkins, which aligns with modern CI/CD practices.
  • The use of the Kubernetes Plugin for Jenkins is presented as a beneficial feature that enhances the scalability and efficiency of Jenkins agents within a Kubernetes environment.
  • The author's inclusion of personal GitHub links and branches suggests a hands-on approach to learning and a willingness to share practical knowledge with the community.
  • The conclusion and recommendation sections indicate that the author believes in continuous learning and encourages readers to explore related topics and tools within the Kubernetes ecosystem.

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/chart
helm 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 -d
kubectl 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

Kubernetes
Jenkins
DevOps
Github
Cloud Computing
Recommended from ReadMedium