Deploy Python application in Kubernetes Cluster

Introduction

Python is a general-purpose, high-level programming language. With a lot of indentation, its design philosophy emphasizes code readability. Garbage collection and dynamic typing are features of Python. It works with structured, object-oriented, and functional programming paradigms, among others.

Python 3 is the latest version of the Python programming language and it was released in 2008. Some of the key features of Python 3 include:

  • Improved support for Unicode: Python 3 has better support for handling and manipulating Unicode strings, which makes it more suitable for internationalization and localization.
  • Improved support for integers: Python 3 has improved support for integers, including the ability to handle arbitrarily large integers and support for the “//” operator for integer division.
  • Improved support for text and binary data: Python 3 has improved support for working with text and binary data, including the addition of a new “bytes” data type and a new “io” module for working with input and output.
  • New syntax features: Python 3 introduces new syntax features such as the “nonlocal” statement, the “async def” statement, and the “await” expression.
  • Improved library support: Python 3 has improved support for libraries such as concurrent programming, networking, and encryption.
  • Backwards Incompatible: Python 3 is not completely backwards-compatible with Python 2, meaning that some Python 2 code may not run correctly under Python 3 without modification.
  • Improved Support for OOP.
  • New Modules and libraries.
  • Asyncio library.

and many more, Python 3 is actively developed and supported and it’s recommended to use latest version of python3.

Kubernetes is a powerful Platform as a Service that enables you to manage a cluster of nodes that run the Linux operating system. Kubernetes can be used to manage a variety of applications, including web applications, database and more.

Details about the Kubernetes components

  • API Server – Intract with kubectl command utility, Primary component, Authorization work.
  • Controller Manager – Replica controller, Node Controller maintainer the define pods and nodes.
  • Scheduler – Matain proper balacing of hardware utilization and assign new pods to sutable workder node as per ranking.
  • ECTD – Database its store the data using key and value form format.
  • Kubelet – Pod moniotr, Pod creation and deletation.
  • Kube-proxy – Pods communication between 2 nodes, Network related rules manages by kube-proxy.

In this post, We will deploy Python application on Kubernetes cluster.

Step 1: Write a Deployment YAML

To deploy the Python docker container in K8s, you can use the provided code by creating a deployment YAML file with the name python-deployment.yaml.

vim python-deployment.yaml

and paste the following code.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: python
  labels:
    app: python
spec:
  replicas: 1
  selector:
    matchLabels:
      app: python
  template:
    metadata:
      labels:
        app: python
    spec:
      containers:
      - name: python
        image: <your-image-url>
        ports:
        - containerPort: 5000

Take a look at the above YAML configuration:

apiVersion: Define the Kubernetes API in order to create objects.
kind: specify the type of object you intend to create.

metadata: define the object name ( including a name string, uniquely identifying the object, the UID, and an optional namespace ).
spec: Define the state of objects.
selector: specify the mapping between deployment and managed pods. 

We need to save and exit from text editor, Our deployment YAML is ready Lets deploy the python pod on K8s cluster.

Step 2: Create a Deployment

We need to execute the following command to deploy the python pods which is container node image.

kubectl create -f python-deployment.yaml

You will get success message for the Go deployment on your screen.

We will get a single pod of the go image and, according to the Dockerfile configuration, Python application will be up and running on port 5000. 

Step 3: Let’s Verify the Deployment

We need to execute the given command to see pods and running or not.

To Validate the pod.

kubectl get pods

To check deployment.

kubectl get deployment

Step 4: Create a Service

We need to create a python-service.yaml file in order access tomcat server, Create a python-service.yaml file with following code.

To create a service.yaml file

vim python-service.yaml

Paste the following code.

apiVersion: v1
kind: Service
metadata:
  name: python-service
spec:
  selector:
    app: python
  type: NodePort
  ports:
  - protocol: TCP
    port: 5000
    targetPort: 30453

The Kubernetes service section: K8s services allow network access to the set of pods.

port: Define the application’s port inside the Docker container.
nodePort: The port number to use to connect to the base machine.
protocol: define a network layer type like TCP/UDP, To communicate with the network.
selector: To know and identify the deployment-created objects.

Save and close the text editor. As per our service configuration, we define port 5000 to access the Python app, which is mapped with a label like app: python, We are using NodePort to access the Python app server on each node using port 30453.

Step 5: Deploy Service

We need to execute the given command to deploy the python-service on K8s cluster.

kubectl create -f python-service.yaml

We should get a success message on the screen, To check the service status for node, execute the given command.

To get service status.

kubectl get svc 

Here we can see that NodePort assigned the ramdon port to access the node app on each node

Note: If you are using minikube So you need to use the given command to expose the total url to access your kubernets application.

minikube service python-service --url

Click here to get apache deployment process step by step on kubernetes cluster.

Conclusion

We have successfully deploy Python app on kubernetes cluster in a simple way, If you still have questions, please post them in the comments section below.

Author

Deploy Python application in Kubernetes Cluster

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top