Open In App

How To Deploy Python Application In Kubernetes ?

In today’s IT world we are moving from the monolithic to microservice architecture to make our applications highly available and scalable to bring fault tolerance. In this transformation, containerization i.e., containerizing the application are a fundamental aspect of this micro services. In this article we will guide you how to deploy a python application in Kubernetes.

Understanding Of Primary Terminologies

Deploy Python Application In Kubernetes: A Step-By-Step Guide

Step 1: Login In AWS Console



Step 2: Login In With The Password

Step 3: Create Instance



Step 4: Choosing AMI

Step 5: Create Key Pair

Create a Key pair to this creating instance.

Step 6: Choosing Pem Key

Step 7: Choosing Key Pair

Step 8: Configure Security Groups

Step 9: Launching The Instance

Step 10: Connect Instance

Step 11: Connect EC2 Console

Step 12: Switch To Root User

sudo su -

Step 13: Install Docker

yum update

yum install docker -y

Step 14: Start Docker Service

systemctl enable docker --now

Step 15: Verify Docker

docker --version

systemctl status docker

Step 16: Create A Dockerfile

FROM python:3.8-slim

# Set the working directory in the container
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed dependencies specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Run app.py when the container launches
CMD ["python", "app.py"]

Step 17: Build Your Docker Image

docker build -t mypython_img:v1 .

docker images

docker login

Step 18: Push Docker Image To Registry

docker tag mypython_img:v1 [username]/mypython_img:v1

minikube start

kubectl get pods

kubectl get deployments 

Step 19: Create Kubernetes Deployment Manifest:

apiVersion: apps/v1
kind: Deployment
metadata:
name: your-app
spec:
replicas: 3
selector:
matchLabels:
app: your-app
template:
metadata:
labels:
app: your-app
spec:
containers:
- name: your-app
image: bnvschaitanya/mypython_img:v1
ports:
- containerPort: 5000

Step 20: Apply Deployment to Kubernetes

kubectl apply -f deployment.yaml

Deploy Python Application And Kubernetes – FAQ’s

What Are The Benefits Of Deploying A Python Application On Kubernetes?

Deploying the Python application on top of Kubernetes facilitates the features such as scalability, flexibility, and efficient resource utilization.

How Do I Containerize My Python Application For Kubernetes Deployment?

Create a Dockerfile to your python application and build a Docker image to it.

What Is The Purpose Of A Kubernetes Deployment Object?

A Kubernetes Deployment object manages the lifecycle of your application with providing featues such as scaling, updates, and rollbacks.

How Can I Expose My Python Application Running On Kubernetes To External Traffic?

Expose your Python application by creating a Kubernetes Service with a LoadBalancer type to route external traffic to your application’s pods.


Article Tags :