# Jenkins The IT industry is rapidly moving towards using containers in software development. This shift is driven by the cost-effectiveness of containers and the reduced development time they offer. To manage these containers, orchestration tools like Kubernetes come into play. However, before diving deeper, let's get a quick overview of Jenkins and Kubernetes. Jenkins is an open-source tool created by Google that manages containers and ensures their reliability. It supports a wide array of CI/CD tools and Kubernetes security tools, enabling developers to run tests, deploy builds, and update applications without downtime. For more information, you can explore [Kubernetes Architecture and its Components](#). Jenkins is one of the most popular CI/CD tools due to its ability to continuously monitor tasks and highlight errors early in the development process. ## Introduction Continuous Integration/Continuous Deployment (CI/CD) pipelines are core components of the DevOps environment. They streamline workflows across multiple teams, enhancing productivity. Jenkins serves as a widely-used open-source automation server to set up CI/CD pipelines. ## Prerequisites - A working Kubernetes cluster - `kubectl` installed on your local machine/host ([installation guide](https://kubernetes.io/docs/tasks/tools/)) ## Setup Jenkins On Kubernetes Cluster To set up a Jenkins cluster on Kubernetes, we will perform the following steps: 1. Create a Namespace 2. Create a service account with Kubernetes admin permissions. 3. Create a local persistent volume for persistent Jenkins data on Pod restarts. 4. Create a deployment YAML and deploy it. 5. Create a service YAML and deploy it. 6. Access the Jenkins application on a Node Port. ## Kubernetes Jenkins Deployment Here is a high-level view of what we are going to do. ![Jenkins Deployment Overview](../images/J1.jpg) Let’s get started with deploying Jenkins on Kubernetes. ### Step 1: Create a Namespace for Jenkins It is a good practice to categorize all DevOps tools in a separate namespace from other applications. ```bash kubectl create namespace jenkins ``` Next, create the YAML file that will deploy Jenkins. Create and open a new file called jenkins.yaml using vim or your preferred editor: ```bash vim jenkins.yaml ``` Now add the following code to define the Jenkins image, its port, and several more configurations: ```bash apiVersion: apps/v1 kind: Deployment metadata: name: jenkins spec: replicas: 1 selector: matchLabels: app: jenkins template: metadata: labels: app: jenkins spec: containers: - name: jenkins image: jenkins/jenkins:lts ports: - name: http-port containerPort: 8080 - name: jnlp-port containerPort: 50000 volumeMounts: - name: jenkins-vol mountPath: /var/jenkins_vol volumes: - name: jenkins-vol emptyDir: {} ``` This YAML file creates a deployment using the Jenkins LTS image and also opens port 8080 and 50000. You use these ports to access Jenkins and accept connections from Jenkins workers respectively. Now create this deployment in the jenkins namespace: ```bash kubectl create -f jenkins.yaml --namespace jenkins ``` Give the cluster a few minutes to pull the Jenkins image and get the Jenkins pod running. Use kubectl to verify the pod’s state: ```bash kubectl get pods -n jenkins ``` Note that the pod name will be different in your environment. Once the pod is running, you need to `expose it using a Service `_ . You will use the `NodePort Service type `_ for this tutorial. Also, you will create a ClusterIP type service for workers to connect to Jenkins. Create and open a new file called jenkins-service.yaml: ```bash vim jenkins-service.yaml ``` Add the following code to define the NodePort Service: ```bash apiVersion: v1 kind: Service metadata: name: jenkins spec: type: NodePort ports: - port: 8080 targetPort: 8080 nodePort: 30000 selector: app: jenkins --- apiVersion: v1 kind: Service metadata: name: jenkins-jnlp spec: type: ClusterIP ports: - port: 50000 targetPort: 50000 selector: app: jenkins ``` In the above YAML file, you define your NodePort Service and then expose port 8080 of the Jenkins pod to port 30000. Now create the Service in the same namespace: ```bash kubectl create -f jenkins-service.yaml --namespace jenkins ``` Check that the Service is running: ```bash kubectl get services --namespace jenkins ``` You will receive an output like this: ```bash Output NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE jenkins NodePort your_cluster_ip 8080:30000/TCP 15d ``` With NodePort and Jenkins operational, you are ready to access the Jenkins UI and begin exploring it. ## Step 2: Accessing the Jenkins UI. In this step, you will access and explore the Jenkins UI. Your NodePort service is accessible on port 30000 across the cluster nodes. You need to retrieve a node IP to access the Jenkins UI. Use kubectl to retrieve your node IPs: ```bash kubectl get nodes -o wide ``` Copy one of the your_external_ip values. Now open a web browser and navigate to http://your_external_ip:30000. A page will appear asking for an administrator password and instructions on retrieving this password from the Jenkins Pod logs. Let’s use kubectl to pull the password from those logs. First, return to your terminal and retrieve your Pod name: ```bash kubectl get pods -n jenkins ``` You will receive an output like this: ```bash Output NAME READY STATUS RESTARTS AGE jenkins-9va733qco1-twnvn 1/1 Running 0 5m12s ``` Next, check the Pod’s logs for the admin password. Replace the highlighted section with your pod name: ```bash kubectl logs jenkins-9va733qco1-twnvn -n jenkins ``` You might need to scroll up or down to find the password: ```bash Output Running from: /usr/share/jenkins/jenkins.war webroot: EnvVars.masterEnvVars.get("JENKINS_HOME") .......................... Jenkins initial setup is required. An admin user has been created and a password generated. Please use the following password to proceed to installation: your_jenkins_password This may also be found at: /var/jenkins_home/secrets/initialAdminPassword ``` Copy your_jenkins_password. Now return to your browser and paste it into the Jenkins UI. Once you enter the password, Jenkins will prompt you to install plugins. Because you are not doing anything unusual, select Install suggested plugins. After installation, Jenkins will load a new page and ask you to create an admin user. Fill out the fields, or skip this step by pressing the skip and continue as admin link. This will leave your username as admin and your password as your_jenkins_password. Another screen will appear asking about instance configuration. Click the Not now link and continue. After this, Jenkins will create a summary of your choices and print "Jenkins is ready!" Click on **Start using Jenkins**, and the Jenkins home page will appear. ![Jenkins Ready](../images/j3.png) Now that you have installed and configured Jenkins on your cluster, let’s demonstrate its capabilities by running a sample pipeline. ## Step 3: Running a Sample Pipeline Jenkins excels at creating pipelines and managing CI/CD workflows. In this step, we will build one of Jenkins’ sample pipelines. 1. From the Jenkins home page, click on the **New Item** link in the left-hand menu. 2. A new page will appear. Choose **Pipeline** and press **OK**. ![New Item](../images/j4.png) 3. Jenkins will redirect you to the pipeline’s configuration page. Find the **Pipeline** section and select **Hello World** from the **Try sample pipeline** dropdown menu. This menu appears on the right-hand side. After selecting **Hello World**, click the **Save** button. ![Pipeline Configuration](../images/j5.png) 4. Jenkins will redirect you to the pipeline home page. Click on **Build Now** from the left-hand menu and watch the pipeline begin to run. The `#1` signifies that this is the first build. Once the task completes, you will see some stats about the build. ![Build Now](../images/j6.png) 5. You can also check the console output to see what happened while the pipeline was running. Hover over `#1`, and a dropdown menu will appear. Choose **Console Output** to view the build’s details. Your Hello World pipeline may not be very sophisticated, but it effectively demonstrates how Jenkins can create and manage CI/CD workflows. ## Conclusion In this tutorial, you installed and configured Jenkins on a Kubernetes cluster and then you ran a sample pipeline. Jenkins has a large repository of plugins that can help you perform very complex operations. You can also add your GitHub repositories, multiple types of worker instances, and more. To learn more about using Jenkins, explore the official `Jenkins documentation `_ . ---