Kubernetes and Jenkins Integration
Some of us might facing issues in integrating K8s and Jenkins so , follow these steps to setup an environment for either deployment or others applications.
My setup: My base OS is Windows and I am using Oracle VM Virtual box on which Redhat 8 is running. Jenkins is installed and running in my Redhat OS. I have installed minikube and kubectl on windows (To start minikube use minikube start ).Minikube is a tool that makes it easy to run Kubernetes locally and Kubectl is a command line tool for controlling Kubernetes clusters.
STEP 1. Setting up network connection between Minikube and Redhat .
Go to settings of Redhat 8 in Virtual Box in Network Section set Adapter 1: NAT (port) Adapter 2: Host-only adapter and with port number suppose #2 Similarly go to the settings of minikube: Adapter 1: NAT (port) Adapter 2: Host-only adapter and with same port number #2 as in Redhat 8. # Here NAT port is for giving internet access. # And Host-only for connecting minikube to the Redhat VM.
# You can check the connection between them by pinging to their Ip's .
STEP 2. Setting up kubectl in linux.
Go to your Redhat terminal: Run following codes:-
chmod +x kubectl # Make the kubectl binary executable. mv kubectl /usr/bin/ # Move the binary in to your PATH.
Now you have setup kubectl in your Redhat but we don't have key or certificate or authority in short permission to access its features. For that we need to import few files from Windows ( where K8s is running). For that I have used WinSCP to transfer files from Windows to Linux at ease.
Go to .minikube > profiles > minikube Drag and Drop these files from windows side (left) to the Linux side (right): # ca.crt # client.key # client.crt
Now you can use this command to use kubectl like: # kubectl get pods --server 192.168.99.102:8443 --client-key /root/client.key --client-crt /root/client.crt --certificate-authority /root/ca.crt
here 192.168.99.102 is the IP where K8s in installed i.e, Windows and :8443 is the apiServer port .
Instead of writing these long codes i.e, --client-key , --client-crt etc , we can use config file to do this automatically.
# vim myinfo ( opens editor )
This is the same code which we were writing for giving permission to run kubectl. But this is in YAML format so that K8s can understand it.
Now, if I have to write any code (--kubeconfig myinfo) is the extra code we have to write. kubectl get pods --kubeconfig myinfo //some code.
Now to reduce further : mkdir .kube // In root dir cp myinfo .kube/config // copy myinfo file to .kube/config
Here Config file is created andwe can run codes same as in windows without writing --client-key .. n all.
STEP 3. Creating YAML file .
In short this file contacts kubernetes and based on this code and specifications PODs are created.
Here I am using ReplicaSet , with name myweb-rs .
Now create a repository on github and upload this yml file to github.
Here my repo name jenkins-k8s is created and rs1.yml file is uploaded.
STEP 4. Configuring Jenkins to launch PODs using Kubernetes.
Now access Jenkins with 192.168.99.101:8080 here IP is of my port enp0s8 (host-port IP).
Create New item/job and install Git plugin (from manage plugins).
Copy the repo URL from github and paste in the SCM > git > URL section.
Here I am creating ReplicaSet with name myweb-rs and also exposing it.
DONE .
CONCLUSION
As soon as you Build , the Jenkins will automatically contact to Kubernetes and launch pods based on the specifications specified in the YAML file uploaded on Github.
Here curl 192.168.99.102:32157 shows the website.
Console Output: