Problem
Jenkins is a “the [self-proclaimed] leading open source automation server” for continuous integration and software deployment. Given it’s popularity, it’s a good potential standard staple to have included in a major coding project. But how can we quickly plop (yes that’s the technical word) it into our Kubernetes cluster and spool it up hassle free?
Assumptions
- You’re running a services echo system in Kubernetes cluster
- You have external access to all services routed through an ingress controller
- You want Jenkins to live as another service alongside your other applications and services and accessible through some domain name / path via ingress
Solution
First let’s set up the basic deployment. Since we will use the strategy of having an ingress barrier secure all external traffic into the kubernetes cluster with SSL (TLS/HTTPS), let’s keep the deployment for Jenkins simple. Note that we use the alpine image for Jenkins for its size efficiency.
Let’s name the file jenkins-pod.yaml
apiVersion: apps/v1 kind: Deployment metadata: name: jenkins spec: replicas: 1 selector: matchLabels: pod: jenkins template: metadata: labels: pod: jenkins spec: containers: - name: jenkins image: jenkins/jenkins:alpine imagePullPolicy: IfNotPresent ports: - containerPort: 8080
Now, for a very simple service that will route port 80 to the Jenkins pod. Using port 80 for all services works nicely with the ingress ‘barrier’ strategy.
Lets name this file jenkins-svc.yaml
apiVersion: v1 kind: Service metadata: name: jenkins spec: selector: pod: jenkins ports: - protocol: TCP port: 80 targetPort: 8080
Assuming you have the ingress controller already configured and running (I strongly recommend using kubernetes’ ingress-nginx), You can modify your main ingress router as follows.
Let’s call this ing-router.yaml
apiVersion: networking.k8s.io/v1beta1 kind: Ingress metadata: name: jm annotations: kubernetes.io/ingress.allow-http: "false" kubernetes.io/ingress.class: "nginx" nginx.ingress.kubernetes.io/force-ssl-redirect: "true" nginx.ingress.kubernetes.io/ssl-redirect: "true" nginx.ingress.kubernetes.io/proxy-body-size: 20m spec: rules: - host: www.example.org http: paths: - path: / backend: serviceName: example servicePort: 80 - host: jenkins.example.org http: paths: - path: / backend: serviceName: jenkins servicePort: 80
Now lets apply these manifests.
kubectl apply -f jenksin-pod.yaml kubectl apply -f jenkins-svc.yaml kubectl apply -f ing-router.yaml
To finalize the setup of Jenkins, get the generated password from the running pod using kubectl logs.
kubectl logs -l pod=jenkins
You’ll get output like below.
Simply go to https://jenkins.example.org
and paste the password in.
You’ll then be asked to create the admin account for Jenkins and done!
Usage
Simply select your plugins and use Jenkins!
Leave a Reply
Your email is safe with us.