Home Running Jenkins on Kubernetes
Post
Cancel

Running Jenkins on Kubernetes

Jenkins is a self-contained, open source automation server which can be used to automate all sorts of tasks related to building, testing, and delivering or deploying software.

Prerequisites:

  • Kubernetes cluster
  • Helm 3.x

Installing Jenkins with Helm:

Helm v3.0+ must be installed on your workstation.

Add the jenkins Helm repository:

1
helm repo add jenkins https://charts.jenkins.io

Fetch the latest charts from the repository:

1
helm repo update

Retrieve the package from jenkins repository, and download it locally:

1
helm fetch jenkins/jenkins --untar

Lets update values.yaml file as follow,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Change admin password
controller:
  admin:
    username: "admin"
    password: 'SecureP@ssw0rd!'

# Install Additional Plugins
controller:
  additionalPlugins: ['pipeline-graph-view', 'job-dsl', 'junit', 'coverage', 'dark-theme']

# Configure Dark Theme
controller:
  JCasC:
    configScripts:
      dark-theme: |
        appearance:
          themeManager:
            disableUserThemes: true
            theme: "dark"

# Configure welcome message
controller:
  JCasC:
    configScripts:
      welcome-message: |
        jenkins:
          systemMessage: 🚀 Welcome To Jenkins Prod instance 🚀

Install jenkins in the jenkins namespace:

1
helm install jenkins jenkins/jenkins --values /tmp/jenkins/values.yaml -n jenkins --create-namespace

To confirm that the deployment succeeded, run:

1
kubectl -n jenkins get pods

Get the Jenkins URL to visit by running these commands in the same shell:

1
2
3
4
5
6
7
8
kubectl patch svc jenkins --namespace jenkins -p '{"spec": {"type": "NodePort"}}'

export NODE_PORT=$(kubectl get --namespace jenkins -o jsonpath="{.spec.ports[0].nodePort}" services jenkins)

export NODE_IP=$(kubectl get nodes --namespace jenkins -o jsonpath="{.items[0].status.addresses[0].address}")

echo http://$NODE_IP:$NODE_PORT

jenkinsUrl must be set correctly else stylesheet won’t load and the dark theme.

1
2
3
4
controller:
  jenkinsUrl: http://$NODE_IP:$NODE_PORT
  jenkinsAdminEmail: mkbn@mkbn.com
  # don't set these via JCasC, set as helm chart values

Run helm upgrade

1
helm upgrade jenkins jenkins/jenkins --values /tmp/jenkins/values.yaml -n jenkins

Login with the username: admin and password: SecureP@ssw0rd!

Expose jenkins ui using ingress

Install nginx ingress controller

1
2
3
4
5
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update

helm install ingress-nginx ingress-nginx/ingress-nginx \
--namespace ingress-nginx --create-namespace

Check the status of the Ingress Controller:

1
2
kubectl get pods -n ingress-nginx
kubectl get svc -n ingress-nginx

Note: Add the EXTERNAL-IP address of your ingress controller to your domain’s DNS records as an A record.

Installing Cert-Manager

1
2
3
4
5
6
helm repo add jetstack https://charts.jetstack.io --force-update

helm install cert-manager jetstack/cert-manager \
--namespace cert-manager --create-namespace \
--version v1.16.1 \
--set installCRDs=true

Verify Cert-Manager is running

1
kubectl get pods -n cert-manager

Create the ClusterIssuer resource

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cat <<EOF | kubectl apply -f -
apiVersion: cert-manager.io/v1
kind: ClusterIssuer
metadata:
  name: letsencrypt-prod
spec:
  acme:
    server: https://acme-v02.api.letsencrypt.org/directory
    email: mkbn@mkbn.in
    privateKeySecretRef:
      name: letsencrypt-prod
    solvers:
    - http01:
        ingress:
          class: nginx
EOF

Create the ingress resource

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
cat <<EOF | kubectl apply -f -
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: jenkins-ingress
  namespace: jenkins
  annotations:
    cert-manager.io/cluster-issuer: "letsencrypt-prod"
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx
  tls:
  - hosts:
    - jenkins.mkbn.in
    secretName: jenkins-tls
  rules:
  - host: jenkins.mkbn.in
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: jenkins
            port:
              number: 8080
EOF

Now access the jenkins ui with https://jenkins.mkbn.in

Reference Links:

This post is licensed under CC BY 4.0 by the author.