Deploy Python App into Kubernetes

Ashish MJ
Dev Genius
Published in
4 min readDec 13, 2022

--

This article aims to outline the basics of Kubernetes/K8s and learn how to deploy a simple python service into Kubernetes .

What are Kubernetes / k8s ?

Kubernetes or just k8s is an open source container orchestration system that lets one deploy, scale, and manage containerized apps.

Key Terminologies

  • Image- It is an immutable file that contains the source code, libraries, dependencies and tools to run an app.
  • Container- It is a running instance of an image.
  • Cluster- It is a collection of nodes that runs containerized apps.
  • Node- It is the smallest unit of computing hardware in Kubernetes.
  • Pod- They are the smallest deployable units in Kubernetes. Each pod can consist of one or more containers.
  • Service- It enables network access to a set of pods in Kubernetes.
  • Deployment- It is used to create or modify instances of pods and provides declarative updates to apps.
  • Docker- It is a set of tools for building ,sharing container images and running containers.
Overview

Kubernetes Objects

They are the persistent entities in the Kubernetes system. One can create these objects, and then Kubernetes makes sure that your desired state is maintained.Pods, Service, Volumes, Namespace, jobs, cronjobs, deployments are few examples for kubernetes objects. Each object has two specific properties

  1. state- object’s current state(creating , running , terminating , pending etc)
  2. spec- contains the details about the object (labels, ports to expose ,container image to run etc)

Kubernetes objects are described using a YAML file. This YAML file consists of a set of fields called required fields.

Required fields
Sample description of pod

kubectl

It is a command line tool used to run commands against the Kubernetes cluster. The general syntax of kubectl command is

kubectl <command><type><name><flag>

command- action to be performed (delete, get, apply, create)

type- kubernetes object (pod, service, deployment)

name- name given to the kubernetes object

flags- to specify options

The most commonly used kubectl commands are:

Most commonly used commands

Getting Started

We will create a simple notification service whose primary function is to send emails and later deploy this python service into kubernetes.

Project Overview

1. Installation

2. Code

  • app.py
app.py (notification-service)
  • requirements.txt
requirement.txt
  • Dockerfile
Dockerfile
  • deployment.yml
deployment.yml

3. Deployment

Open terminal and navigate to the project .

  • Build an image out of Dockerfile by running the below command .
docker build -t notification-service .
docker build
  • To verify the image built use the following command.
docker images
docker images
  • Create a deployment object using the command below.
kubectl apply -f deployment.yml
kubectl apply
  • To cross check the deployment created and pods are up and running. Execute these two commands.
kubectl get deployments
kubectl get pods
kubectl get

4. Testing

  • To test the functionality of the notification-service, firstly enter into the interactive terminal and then hit the curl command.
kubectl exec -it notification-service-5545c8f696-2vdjf -- bash
curl -H 'Content-Type: application/json' -d  '{"name": "Ashish M J" ,"email": "your_mail@gmail.com"}' -X POST http://localhost:5000/sendEmail

Note: Replace the pod name in first command and use proper emailId in second command

response status 202(Accepted)
Email received
  • In case of any issues , one can check the logs by executing the following command.
kubectl logs notification-service-5545c8f696-2vdjf

Note: Replace the pod name

kubectl logs

Conclusion

In this story, we have seen the basics of kubernetes and learnt how to deploy a python application into kubernetes in simple steps. Hope you have understood the basics of kubernetes.

Thanks for reading!

Github Website Linkedin

--

--

A Software Engineer with a demonstrated history of working in the IT .A keen learner, who always strives to feed curiosity and learn about new technologies.