--- tags: Devops, jenkins , Jenkins on Kubentes --- # Deployment of HA Jenkins on Kubernetes Software: KVM Virt-Manager 2.2.1 Linux Ubuntu 18.04 ### Pre-Installation Steps On Both Master & Slave (To Install Kubernetes) ```javascript= # sudo su # apt-get update ``` ### Turn Off Swap Space (Add # infront of the last line) ```javascript= # swapoff -a # nano /etc/fstab ``` ![](https://i.imgur.com/bCFHyh1.png) ### Update The Hostnames ```javascript= # nano /etc/hostname ``` ![](https://i.imgur.com/Nob1j8l.png) ### Update The Hosts File With IPs Of Master & Node and set the IP address to be static ```javascript= # ifconfig ``` ![](https://i.imgur.com/6thBwBI.png) ```javascript= # nano /etc/network/interfaces ``` ![](https://i.imgur.com/MDUixzF.png) ```javascript= # nano /etc/hosts ``` ![](https://i.imgur.com/f2x3d9k.png) ### Install OpenSSH-Server ```javascript= # sudo apt-get install openssh-server ``` ### Install Docker ```javascript= # sudo su # apt-get update # apt-get install -y docker.io ``` **Next we have to install these 3 essential components for setting up Kubernetes environment: kubeadm, kubectl, and kubelet.** Run the following commands before installing the Kubernetes environment. ```javascript= # apt-get update && apt-get install -y apt-transport-https curl # curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - # cat <<EOF >/etc/apt/sources.list.d/kubernetes.list deb http://apt.kubernetes.io/ kubernetes-xenial main EOF # apt-get update ``` ### Install kubeadm, Kubelet And Kubectl ```javascript= # apt-get install -y kubelet kubeadm kubectl ``` ### Updating Kubernetes Configuration Environment=”cgroup-driver=systemd/cgroup-driver=cgroupfs” ```javascript= # nano /etc/systemd/system/kubelet.service.d/10-kubeadm.conf ``` ![](https://i.imgur.com/Kb43oTB.png) ## Steps Only For Kubernetes Master VM (kmaster) ### Step 1: Kuberentes Init ```javascript= # kubeadm init --apiserver-advertise-address=<ip-address-of-vm> --pod-network-cidr=192.168.0.0/16 ``` You will get the below output. The commands marked as (1), execute them as a non-root user. This will enable you to use kubectl from the CLI The command marked as (2) should also be saved for future. This will be used to join nodes to your cluster ![](https://i.imgur.com/qwkT3Ue.png) ### Step 2: As mentioned before, run the commands from the above output as a non-root user ```javascript= $ mkdir -p $HOME/.kube $ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config $ sudo chown $(id -u):$(id -g) $HOME/.kube/config ``` To verify, if kubectl is working or not, run the following command: ```javascript= $ kubectl get pods -o wide --all-namespaces ``` ### Step 3: You will notice from the previous command, that all the pods are running except one: ‘kube-dns’. For resolving this we will install a pod network. To install the CALICO pod network, run the following command: ```javascript= # kubectl apply -f https://docs.projectcalico.org/v3.0/getting-started/kubernetes/installation/hosted/kubeadm/1.7/calico.yaml ``` ![](https://i.imgur.com/2v4Doo1.png) ### Step 4: (Optional Step If you want to use Dashboard) Next, we will install the dashboard. To install the Dashboard, run the following command: ```javascript= # kubectl create -f https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml ``` ![](https://i.imgur.com/oYoXrZM.png) ### Step 5: Your dashboard is now ready with it’s the pod in the running state. ![](https://i.imgur.com/Lxj429s.png) ### Step 6: By default dashboard will not be visible on the Master VM. Run the following command in the command line: ```javascript= # kubectl proxy ``` ![](https://i.imgur.com/zLgDjez.png) To view the dashboard in the browser, navigate to the following address in the browser of your Master VM: http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/ You will then be prompted with this page, to enter the credentials: ![](https://i.imgur.com/YqS130p.png) ### Step 7: In this step, we will create the service account for the dashboard and get it’s credentials. Note: Run all these commands in a new terminal, or your kubectl proxy command will stop. Run the following commands: 1. This command will create a service account for dashboard in the default namespace ```javascript= kubectl create serviceaccount dashboard -n default ``` 2. This command will add the cluster binding rules to your dashboard account ```javascript= $ kubectl create clusterrolebinding dashboard-admin -n default --clusterrole=cluster-admin --serviceaccount=default:dashboard ``` 3. This command will give you the token required for your dashboard login ```javascript= $ kubectl get secret $(kubectl get serviceaccount dashboard -o jsonpath="{.secrets[0].name}") -o jsonpath="{.data.token}" | base64 --decode ``` ![](https://i.imgur.com/6ijbRf7.png) 4. Copy this token and paste it in Dashboard Login Page, by selecting token option 5. ![](https://i.imgur.com/TP8qxIe.png) 5. You have successfully logged into your dashboard! 6. ![](https://i.imgur.com/ZfrpoXD.png) ## Steps For Only Kubernetes Node VM (knode, Worker Node, Slave Node) ```javascript= sudo kubeadm join --apiserver-advertise-address=<ip-address-of-the master> --pod-network-cidr=192.168.0.0/16 ``` ![](https://i.imgur.com/uWe8QID.png) ## Deployment of Jenkins HA on K8s Kubernetes Cluster up and running now we can Deploy the Jenkins on K8s ### Step 1 Create the namespace: Create the namespace for Jenkins on k8s using the given code and kubectl command. ```javascript= # sudo nano jenkins-ns.yaml ``` ```javascript= apiVersion: v1 kind: Namespace metadata: name: jenkins ``` ![](https://i.imgur.com/xTw4VNg.png) ```javascript= # kubectl apply -f jenkins-ns.yaml -n jenkins ``` ![](https://i.imgur.com/smL2pjH.png) ### Step 2: Create the Persistent Volume: persistent volume plays more important role in order to high availability of cluster. Persistent volume helps you to keep your data in case of pod destroyed or container is deleted. Make a file with the code in pic and run the command to create persistent volume ```javascript= # sudo nano jenkinspv.yaml ``` ```javascript= apiVersion: v1 kind: PersistentVolume metadata: name: jenkins-home-pv namespace: jenkins labels: usage: jenkins-shared-deployement spec: storageClassName: default capacity: storage: 5Gi accessModes: - ReadWriteOnce persistentVolumeReclaimPolicy: Retain hostPath: path: "/var/jenkins_home" ``` ![](https://i.imgur.com/FGq1vna.png) ```javascript= # kubectl apply -f jenkinspv.yaml -n jenkins ``` ![](https://i.imgur.com/KNaHUfG.png) ### Step 3: Claim the persistent volume: ```javascript= # sudo nano jenkinspvc.yaml ``` ```javascript= apiVersion: v1 kind: PersistentVolumeClaim metadata: name: jenkins-home-pvc namespace: jenkins labels: app: jenkins spec: accessModes: - ReadWriteOnce storageClassName: default resources: requests: storage: 5Gi ``` ![](https://i.imgur.com/FUWbbVz.png) ```javascript= # kubectl apply -f jenkinspvc.yaml -n jenkins ``` ![](https://i.imgur.com/q3zulsx.png) ### Step 4: Jenkins Deployment: Create Kubernetes deployment of Jenkins using yaml file, In the yaml file you can give all the necessary details and its depends on the scenario or requirements. ```javascript= # sudo nano jenkins-deployment.yaml ``` ```javascript= apiVersion: apps/v1 kind: Deployment metadata: name: jenkins-master namespace: jenkins labels: app: jenkins spec: replicas: 2 strategy: type: RollingUpdate rollingUpdate: maxSurge: 1 maxUnavailable: 1 selector: matchLabels: app: jenkins template: metadata: labels: app: jenkins spec: securityContext: # Set runAsUser to 1000 to let Jenkins run as non-root user 'jenkins' which exists in 'jenkins/jenkins' docker image. runAsUser: 0 fsGroup: 1000 containers: - name: jenkins-master # https://github.com/jenkinsci/docker image: jenkins/jenkins:lts #jenkinsci/jenkins:2.150.1 imagePullPolicy: IfNotPresent env: - name: JENKINS_HOME value: /var/jenkins_home - name: JAVA_OPTS value: -Djenkins.install.runSetupWizard=false - name: JAVA_OPTS value: "-Xmx8192m" # - name: COPY_REFERENCE_FILE_LOG # value: $JENKINS_HOME/copy_reference_file.log ports: - name: jenkins-port # Access Port for UI containerPort: 8080 - name: jnlp-port # Inter agent communication via docker daemon containerPort: 50000 resources: # Resource limitations requests: memory: "256Mi" # Change Me cpu: "50m" limits: memory: "4Gi" # Change Me cpu: "2000m" volumeMounts: - name: jenkins-home-volume mountPath: "/var/jenkins_home" volumes: - name: jenkins-home-volume persistentVolumeClaim: claimName: jenkins-home-pvc ``` ![](https://i.imgur.com/eEYDtE4.png) ```javascript= # kubectl apply -f jenkins-deployment.yaml -n jenkins ``` ### Step 5 Jenkins Service: Create the service of Jenkins on k8s to expose on the browser using cluster IP, even we can use some other options as well ```javascript= # sudo nano jenkins-service.yaml ``` ```javascript= apiVersion: v1 kind: Service metadata: name: jenkins-ui-service namespace: jenkins spec: type: ClusterIP # NodePort, LoadBalancer ports: - protocol: TCP port: 8080 targetPort: 8080 # nodePort: 30100 name: ui selector: app: jenkins --- apiVersion: v1 kind: Service metadata: name: jenkins-jnlp-service namespace: jenkins spec: type: ClusterIP # NodePort, LoadBalancer ports: - port: 50000 targetPort: 50000 selector: app: jenkins ``` ![](https://i.imgur.com/6yFtVHJ.png) ```javascript= # kubectl apply -f jenkins-service.yaml -n jenkins ``` ### Step 6 Check the details: ```javascript= # Kubectl get pods,pv,pvc,svc,deployment,node -o wide --all-namespaces ``` ![](https://i.imgur.com/OLzU6hf.png) ### Step 7 Access the Jenkins Server. (Access the Jenkins server using cluster IP and port number) ![](https://i.imgur.com/GbWwZpM.png) ### Step 8 Copy the Password: you can get the password using log file of Jenkins deployment. ```javascript= # kubectl logs deployment/jenkins-master -n jenkins ``` ![](https://i.imgur.com/DfgJq4q.png) ### Step 9 Copy and Paste the Password and Click on Continue. ![](https://i.imgur.com/UsGmS0N.png) ### Step 10 Start Jenkins Server: Install suggested Plugins ![](https://i.imgur.com/dmt0TgF.png) ### Step 11 Plugin Installation going on: ![](https://i.imgur.com/X43DuIr.png) ### Step 12 After Installation is finished Create the User to use the Jenkins Server: ![](https://i.imgur.com/4o1ncGa.png) ### Step 13 Jenkins Server is Ready: ![](https://i.imgur.com/RGM1MqG.png) ### Step 14 Jenkins Server Interface ![](https://i.imgur.com/bb9HseZ.png)