Deploy Python App to on-premise Kubernetes Cluster by Jenkins Pipeline

We will deploy python app to on-premise Kubernetes cluster using Jenkins CI/CD pipeline. To be able to do that, we need to configure ssh keygen for ssh connections in Kubernetes cluster master node server. Connect to master node and apply following commands to create a secure ssh key and save private key file to your computer for both of your and Jenkins ssh conection. If you want you can download my sshd_config file to update your current one for more secure ssh configuration.

I have changed port from 22 to 52025 and disabled password authentication. That’s why after you have changed sshd_config create ssh key and download it before you restart ssh service. If you don’t have a java installation, you can install it with “apt install openjdk-17-jre -y” command. We will add this Kubernetes master node to Jenkins as a worker node, that’s why we need java in this master node. Create a Jenkins folder under your kubeadm user which can be root or your desired user. We will use this Jenkins folder for Jenkins pipelines workspace.

wget https://raw.githubusercontent.com/semiharsan/devops/main/sshd_config

mv sshd_config /etc/ssh

ssh-keygen -t ed25519

chmod 700 ~/.ssh

touch ~/.ssh/authorized_keys

chmod 600 ~/.ssh/authorized_keys

cat ~/.ssh/id_ed25519.pub >> ~/.ssh/authorized_keys

cat /root/.ssh/id_ed25519

mkdir jenkins

ssh restart

Now we need to create a credentials so that Jenkins can use it to connect Kubernetes master node via ssh. Go to Manage Jenkins – Credentials – System – Global. Click on New Credentials. Write a description and your user name which has right to access Kubernetes cluster and run kubectl commands. We had copied the private key in master node console, just paste it here in key area and click to create.

 

Go to Manage Jenkins – Nodes and click on New Node to add a node. Fill the form according to your Kubernetes cluster information and save it. Just remember what you will write in Label text box which will be used in pipelines so that pipeline can run commands in this node instead of built-in node.

 

Let us go to Manage Jenkins – Plugins again to install DockerHub plugin to be able to login docker hub with our username and password in pipeline step.  Restart Jenkins after installation is finished.

 

We have added Kubernetes Master node as a Jenkins worker node. Now Jenkins will be able to access to this master node and run shell commands what we will write in stage pipelines. Go to dashboard and click on New Item to create a new pipeline. Download my Jenkins pipeline script and change values according to your design.

https://raw.githubusercontent.com/semiharsan/devops/main/jenkinspipelineforon-premise-kubernetes

 

Click on PythonApp pipeline in Dashboard and Pipeline Syntax on the left menu to get dockerhub credentials script. First we will add docker hub user account to Jenkins credential store and then we will create docker login script along with this credential.

 

Now go to pipeline configuration and choose GithubHook and Poll SCM options to be able to allow pipeline can check new codes in github repository. When someone checked in new codes into the repository then pipeline will catch that and run to deploy new code to Kubernetes cluster.

jenkins pipeline

 

You can see that steps run properly and seamlessly. Pipeline deployed python app to Kubernetes Cluster. We will access it via service ip and port number.

 

Well done. Your application is published. Here are the commands to run in Kubernetes cluster master node to be able to access application.

kubectl get svc

curl http://10.96.6.60:8000

lshw -class network -short

ip a | grep ens160

curl http://10.1.130.75:31700

 

Now go to your code and change somethings and check in. Pipeline will get it after 1 minute and deploy new code.

deploy python app

 

Congratulations guys !!! You just deployed your python application to on—premise Kubernetes Cluster using Jenkins CI/CD pipeline. I hope this article can be useful for your needs. You can check on-premise Kubernetes cluster installation steps in below article.

 

<< GO TO SETUP KUBERNETES CLUSTER WITH 2 NODES ON VCENTER >>

<< GO TO SETUP JENKINS ON UBUNTU LINUX VIRTUAL MACHINE AND CONFIGURE FOR AWS >>

<< GO TO DEPLOY PYTHON APP ON AWS ECS SERVICE BY JENKINS CI/CD PIPELINE >>

<< GO TO HOW TO DEPLOY PYTHON APP MAIN POST >>