Nikhil Verma

Sep 4, 20232 min

Build Jenkins Pipeline Using Git, Docker Hub, Podman and Kubernetes - Part 4

Now our Jenkins and Kubernetes configuration completed.

Let's start with pipeline deployment:

We need to understand Architecture first :

On your local machine , let's build python application:

  1. Python app : I have create python web app using flask :

from flask import Flask
 
app = Flask(__name__)
 

 
@app.route('/')
 
def hello_world():
 
return '<h1>Hello World from Flask!</h1>'
 

 
if __name__ == "__main__":
 
app.run(host="0.0.0.0", port=int("5000"), debug=True)

2. Dockerfile: A Dockerfile is a text document that contains all the commands a user needs to call on the command line to assemble an image. It is a set of instructions that Docker uses to build an image.

FROM python:3.9-slim-buster
 
ADD hello.py /
 
COPY . /app
 
WORKDIR /app
 
RUN pip3 install Flask
 
EXPOSE 5000
 
CMD [ "python3", "./hello.py" ]

3. For Kubernetes , we require Deployment yaml and Service Yaml .

deployment.yaml

apiVersion: apps/v1
 
kind: Deployment
 
metadata:
 
creationTimestamp: null
 
labels:
 
app: hello
 
name: hello
 
spec:
 
replicas: 5
 
selector:
 
matchLabels:
 
app: hello
 
strategy: {}
 
template:
 
metadata:
 
creationTimestamp: null
 
labels:
 
app: hello
 
spec:
 
serviceAccountName: jenkins
 
containers:
 
- image: docker.io/vniks/jenkins-pipeline:v1
 
name: python-hello-app
 
ports:
 
- name: hello
 
containerPort: 5000
 
protocol: TCP

Service.yaml

apiVersion: v1
 
kind: Service
 
metadata:
 
creationTimestamp: null
 
labels:
 
app: hello
 
name: hello-svc
 
spec:
 
ports:
 
- port: 5000
 
nodePort: 30080
 
protocol: TCP
 
targetPort: 5000
 
selector:
 
app: hello
 
type: NodePort

Apart from this application files we need Podman container to Build and push image. So we need to create image with Podman installed.

FROM jenkins/inbound-agent:alpine
 
USER root
 

 
RUN apk add --no-cache --update podman fuse-overlayfs
 

 
RUN sed -i 's/#mount_program/mount_program/' /etc/containers/storage.conf
 
RUN echo jenkins:100000:65536 >/etc/subuid
 
RUN echo jenkins:100000:65536 >/etc/subgid
 
USER jenkins

Build this image and push in your dockerhub repository.

podman login docker.io
 
podman build -t docker.io/vniks/python-hello-app:v1 .
 
podman push vniks/python-hello-app:v1

For creating Pipeline we need Jenkinsfile with all required stages :

podTemplate(containers: [
 
containerTemplate(name: 'podman', image: 'vniks/podman-agent:v1', command: 'cat', privileged: true, user: 'root', ttyEnabled: true),
 
containerTemplate(name: 'kubectl', image: 'lachlanevenson/k8s-kubectl:latest', command: 'cat', ttyEnabled: true)
 
],
 
volumes: [
 
hostPathVolume(mountPath: '/run/podman/podman.sock', hostPath: '/run/podman/podman.sock')
 
]) {
 

 
node(POD_LABEL) {
 
stage('Get a git project') {
 
git 'https://github.com/vnikhil89/jenkins-pipeline.git'
 
container('podman') {
 
stage('Build a docker image and push') {
 
withCredentials([string(credentialsId: 'dockerhub-pwd', variable: 'dockerhubpwd')]){
 
sh 'podman login docker.io -u vniks -p ${dockerhubpwd}'
 
sh 'podman build -t docker.io/vniks/jenkins-pipeline:v1 .'
 
sh 'podman push docker.io/vniks/jenkins-pipeline:v1'
 
}
 
}
 
}
 
stage('Deployment') {
 
container('kubectl') {
 
sh 'kubectl apply -f hello.yaml'
 
sh 'kubectl apply -f hello-svc.yaml'
 
}
 
}
 
}
 
}
 
}

In Pod template we need to specify which containers we want to start along with Jenkins-Inbound agent container. In my case i have used Podman container to build and push image to docker hub. Second container Kubectl which is required to create deployment and service on Kubernetes cluster.

We also require socket for communication between podman container and podman.

Let's Create Pipeline :

  1. Choose Pipeline option and click Ok.

I have configured 15 min polling interval

Choose Option Pipeline script with SCM

You have to provide Git hub URL and credentials

Click Save

Let's click on Build Now

I am able to successfully deploy Hello pipeline.

Let's check deployment status :

Check service status

Tested my app , accessible on NodePort 30080.

    2580
    0