How to Install Jenkins on Kubernetes Cluster

Introduction

Jenkins is a continuous integration tool that automates a large portion of the software development process. Several development teams working on multiple projects in a complex microservices environment can be a strain on limited resources. Jenkins helps you deliver a flawless final product on schedule.

This tutorial will show you how to install Jenkins on a Kubernetes cluster.

article on installing jenkins on kubernetes cluster

Prerequisites

  • Access to a command line/terminal
  • Kubernetes cluster
  • A fully configured kubectl command-line tool on your local machine

Installing Jenkins on Kubernetes Cluster

A Kubernetes cluster adds a new automation layer to Jenkins. Kubernetes makes sure that resources are used effectively and that your servers and underlying infrastructure are not overloaded.

Kubernetes’ ability to orchestrate container deployment ensures that Jenkins always has the right amount of resources available.

The example below shows you how to use a set of YAML (Yet Another Markup Language) files to install Jenkins on a Kubernetes cluster. The .yaml files are easily tracked, edited, and can be reused indefinitely.

Note: The YAML files in this article illustrate the Jenkins deployment process. They should not be used in a production environment without modifying them to accommodate for your clusters’ specific settings and network resources.

Create a Namespace for the Jenkins Deployment

A distinct namespace provides an additional layer of isolation and more control over the continuous integration environment. Create a namespace for the Jenkins deployment by typing the following command on your terminal:

kubectl create namespace jenkins

The name of the namespace should be a DNS compatible label. In this example, we named it jenkins.

successfully created the jenkins namespace

Use the following command list existing namespaces:

kubectl get namespaces

The output confirms that the jenkins namespace was created successfully.

list of existing namespace on the Kubernetes master node

Create Jenkins Deployment File

Once we have a designated namespace, use your preferred Linux text editor to create a jenkins-deployment.yaml file.

The deployment file in this example utilizes the jenkins/jenkins:lts Docker image and creates 1 replica that is going to be exposed on port 8080.

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: jenkins-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: jenkins
  template:
    metadata:
      labels:
        app: jenkins
    spec:
      containers:
      - name: jenkins
        image: jenkins/jenkins:lts
        ports:
        - containerPort: 8080
volumeMounts:
          - name: jenkins-home
            mountPath: /var/jenkins_home
      volumes:
        - name: jenkins-home
          emptyDir: {}

The volumeMounts section of the file creates a Persistent Volume. The role of a persistent volume is to store basic Jenkins data and preserve it beyond the lifetime of a pod. Exit and save the changes once you add the content to the Jenkins deployment file.

Note: In a production cluster, you would not use hostPath. Instead, a cluster administrator would provision a network resource such as the Amazon Elastic Block Store volume, or the Google Compute Engine persistent disk.

Deploy Jenkins

Use the newly created file to deploy Jenkins:

kubectl create -f jenkins-deployment.yaml --namespace jenkins

The command also instructs the system to install Jenkins within the jenkins namespace.

Create and Deploy Jenkins Service

Kubernetes manages the lifecycle of a pod within the cluster. It regularly removes and deploys pods to attain the desired state and to balance workloads. A Service is an abstraction that exposes Jenkins to the wider network. It allows us to maintain a persistent connection to the pod regardless of the changes taking place within the cluster.

Create a service with a jenkins-service.yaml file by using a text editor to add the following content:

apiVersion: v1
kind: Service
metadata:
  name: jenkins
spec:
  type: NodePort
  ports:
    - port: 8080
      targetPort: 8080
      nodePort: 44000
  selector:
    app: jenkins

Tag the jenkins namespace and create the Service by entering the following command:

kubectl create -f jenkins-service.yaml --namespace jenkins

You are now able to access the Jenkins dashboard.

Access Jenkins Dashboard

Go to your browser and access a node by using its IP and the port you defined in the Service file.

For example:

http://node_ip_address:44000

To access Jenkins, you initially need to enter your credentials. The default username for new installations is admin. The password can be obtained in several ways. This example uses the Jenkins deployment pod name. To find the name of the pod, enter the following command:

kubectl get pods --namespace jenkins

Once you locate the name of the pod, use it to access the pod’s logs.

kubectl logs jenkins-deployment-45345423412-k9003 --namespace jenkins

The password is at the end of the log formatted as a long alphanumerical string.

displaying the location of the Jenkins password in the log file

You have successfully installed Jenkins on your Kubernetes cluster and can use it to create new and efficient development pipelines.

example screenshot of Jenkins dashboard

Note: Sometimes you will need to restart Jenkins while troubleshooting or while installing plugins. Refer to our article How to Restart Jenkins Manually to learn more.

How Does Jenkins Work? (Bonus Explanation)

Jenkins has become an industry standard for automating software development pipelines. Before the concept of continuous integration, the development process was often hampered by complex integrations and lengthy testing procedures. Discovering flaws in the code at a later stage would often break the build and delay software releases.

A continuous integration DevOps tool provides rapid assessments of the quality of the code in the early stages of development. Developers can use this information to make necessary changes, repeating the process until they create an error-free product.

Jenkins is a self-contained solution, compatible with hundreds of integration plugins, forming a comprehensive and easy-to-use environment.

An overview of a basic Jenkins server setup.

Developers commit code to a repository. A CI server (Jenkins) regularly listens to changes made on the source code repository. Jenkins then builds the source code instance and generates a deployable artifact. The build process includes code quality assurance tests and security checks.

Jenkins uses plugins, like SonarQube, to produce detailed metrics of the quality assurance and build process. If the build fails, the information and the reasons for the failed test are presented to the developer to make additional changes to the submitted code.

This continuous loop results in a well-polished product. Jenkins creates a ready-to-deploy JAR package that can progress to the next phase of development/production.

Conclusion

Now you know how to install Jenkins on a Kubernetes cluster. Jenkins can automate many tasks and help developers submit code efficiently and consistently. Your developers are far more productive and no longer need to waste time by manually running local builds and tests.

原创文章,作者:kirin,如若转载,请注明出处:https://blog.ytso.com/223188.html

(0)
上一篇 2022年1月6日
下一篇 2022年1月6日

相关推荐

发表回复

登录后才能评论