Thursday, October 17, 2024

How to Deploy Jenkins on Kubernetes with Helm Chart

Deploying applications on Kubernetes can be complex, involving numerous configurations and resource management. Helm, a package manager for Kubernetes, simplifies this process by packaging applications into charts, making deployment and management much more accessible. This article walks through creating and deploying a custom Helm chart for Jenkins on Kubernetes, demonstrating how Helm empowers users with streamlined deployments and enhanced control.

Why Choose Helm for Jenkins Deployment?

Helm's advantages extend beyond simply automating deployments. It offers several key features that make managing Kubernetes applications more efficient and reliable:

  • Packaging Kubernetes Resources: Helm charts encapsulate all necessary manifests for deploying an application, including deployments, services, and other configurations. This consolidates the entire application's setup within a single package, ensuring consistent and organized resource management.

  • Version Control: Each release of a Helm chart is tracked by a version number. If needed, this facilitates rolling back to previous versions, promoting stability and reducing deployment risks.

  • Customization: Helm charts are designed for flexibility. Parameters can be defined, allowing users to adjust configurations for different environments or specific deployments, tailoring the application to suit various needs.

  • Repeatability: Helm ensures consistency across multiple deployments. Using the same chart with customized values, you can replicate deployments accurately across development, testing, and production environments.

Crafting a Custom Helm Chart for Jenkins

Let's create a Helm chart named "myhelmchart" for deploying Jenkins on Kubernetes. This chart will package all necessary configurations and resources for a seamless Jenkins deployment.

Step 1: Initialize the Helm Chart

Start by creating the chart directory using the helm create command:

      helm create myhelmchart
    

This command generates a basic directory structure with essential files and templates, setting the foundation for our chart.

Step 2: Examining the Chart Structure

The generated chart directory includes several vital files and folders:

  • Chart.yaml: This file contains the metadata for the Helm chart, including the chart name, version, and the application version (appVersion) being deployed.

  • values.yaml: This file holds default values for the chart's configurations, such as image names, ports, and service types. These values can be overridden during installation.

  • templates/: This directory houses the Kubernetes manifests (YAML files) responsible for defining deployments, services, and other necessary resources.

  • NOTES.txt: This file provides instructions displayed after successful chart installation, guiding users on further actions or configurations.

Step 3: Defining the Jenkins Version

Open the Chart.yaml file and specify the desired Jenkins version in the appVersion field:

      appVersion: "2.249.2"
    

This ensures that the correct version of Jenkins is deployed using our chart.

Step 4: Streamlining the Chart

Remove unnecessary files and directories to simplify the chart structure and focus on essential components:

      rm -rf templates/*.yaml templates/tests
    

Step 5: Creating Deployment and Service Manifests

Create two new files in the templates/ directory: jenkins-deployment.yaml and jenkins-service.yaml. These files will contain the YAML configurations for the Jenkins deployment and service.

1. Jenkins Deployment Manifest (Jenkins-deployment.yaml)

Paste the following content into the jenkins-deployment.yaml file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "myhelmchart.fullname" . }}
  labels:
    app: jenkins
spec:
  replicas: 1
  selector:
    matchLabels:
      app: jenkins
  template:
    metadata:
      labels:
        app: jenkins
    spec:
      containers:
        - name: jenkins
          image: {{ printf "%s:%s" .Values.image.repository .Values.image.tag | quote }}
          env:
            - name: JENKINS_USERNAME
              value: {{ default "test" .Values.jenkinsUsername | quote }}
            - name: JENKINS_PASSWORD
              {{- if .Values.jenkinsPassword }}
              value: {{.Values.jenkinsPassword }}
              {{- else }}
              value: testPassword
              {{- end }}
          ports:
            {{- range .Values.containerPorts }}
            - name: {{ .name }}
              containerPort: {{ .port }}
            {{- end }}
    

This manifest defines a Deployment resource for Jenkins:

  • metadata: Provides the name and labels for the deployment, making it easily identifiable within the Kubernetes cluster.

  • spec.replicas: Sets the number of replicas (Jenkins instances) to be deployed, in this case, 1.

  • spec.template: Describes the pod template for the deployment, including metadata and containers. It defines environment variables for the Jenkins container, including the JENKINS_USERNAME and JENKINS_PASSWORD, which are set using values from the values.yaml file.

  • containers: Specifies the Jenkins container using the image defined in the values.yaml file, along with its container port.

2. Jenkins Service Manifest (jenkins-service.yaml)

Add the following content to the jenkins-service.yaml file:

apiVersion: v1
kind: Service
metadata:
  name: {{ template "myhelmchart.fullname" . }}
spec:
  type: {{ .Values.service.type }}
  ports:
    - name: http
      port: 8080
      targetPort: http
      nodePort: {{ .Values.service.nodePort }}
  selector:
    app: jenkins
    

This manifest defines a Service resource that exposes the Jenkins pod:

  • spec.type: Specifies the service type, which is set to NodePort in this case, exposing the service on a specific port of the node (EC2 instance).

  • ports: Maps the http port of the Jenkins pod to the specified NodePort, allowing external access.

  • selector: Targets pods labeled with app: jenkins, ensuring the service interacts with the correct Jenkins pod.

Step 6: Updating NOTES.txt

The NOTES.txt file provides instructions to users after a successful Helm installation. Update it with a brief message:

      echo “This is my first chart” > NOTES.txt
    

Step 7: Defining Values in values.yaml

Open the values.yaml file and configure the default values for our chart:

image:
  repository: bitnami/jenkins
  tag: latest

jenkinsUsername: ""
jenkinsPassword: ""

containerPorts:
  - name: http
    port: 8080

service:
  type: NodePort
  nodePort: 30080
    

This values.yaml file specifies the following:

  • image.repository: Uses the official Bitnami Jenkins image for deployment.

  • jenkinsUsername and jenkinsPassword: These can be left blank or set to desired values during installation.

  • containerPorts: Defines the port used by the Jenkins container (8080 in this case).

  • service: Configures the service type as NodePort and assigns a specific nodePort (30080) for external access.

Step 8: Verifying Template Renderings

Before deploying the chart, it's essential to verify the rendered Kubernetes manifests using the helm template command:

      helm template myhelmchart . -s templates/jenkins-deployment.yaml
    

This command renders the templates based on the defined values, displaying the resulting Kubernetes manifests without applying them to the cluster.

Step 9: Installing the Helm Chart

With the chart configured, install it using the helm install command:

      helm install myhelmchart ./myhelmchart
    

This command installs the chart named "myhelmchart" from the specified directory.

Step 10: Accessing Jenkins

To access the deployed Jenkins instance, you need the public IP of the node hosting the Jenkins pod. Get this information from the Kubernetes cluster using the kubectl command:

      kubectl get nodes -o wide
    

Locate the EXTERNAL_IP or PUBLIC_IP of the node running the Jenkins pod.

Finally, access the Jenkins console in your web browser using the following URL:

      http://<Public_IP>:30080
    

Step 11: Logging into the Jenkins Dashboard

The Jenkins console will prompt you for a username and password. Use the credentials defined in the Helm chart:

  • Username: The value of jenkinsUsername (or "test" if not set).

  • Password: The value of jenkinsPassword (or "testPassword" if not set).

After successful login, you'll have access to the Jenkins dashboard, enabling you to manage and configure Jenkins instances deployed on Kubernetes.

Conclusion

This comprehensive guide demonstrates the power of Helm for deploying Jenkins on Kubernetes. By encapsulating deployment configurations within a chart, Helm streamlines the process, offering version control, customization, and repeatability. This approach simplifies managing Kubernetes deployments, ensuring consistent and reliable Jenkins deployments across various environments. With Helm's capabilities, deploying and managing Jenkins on Kubernetes becomes more accessible and efficient, freeing developers to focus on building and delivering high-quality applications.

0 comments:

Post a Comment