Open In App

How To Run Python Script In Kubernetes Pod ?

Last Updated : 06 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Execution of Python scripts helps in bringing the automation of work. Bringing these Python scripts into containerization as containerized applications will enhance the seamless performance and facilitate with high availability and scalability of the application. In this article, we guide you on how to make Python scripts as containerized applications and run those Python scripts in Kubernetes pods step by step effectively.

Understanding Of Primary Terminologies

  • Kubernetes: It is an open-source container orchestration platform that helps with automating the deployments, scaling, and management of containerized applications.
  • Pod: It is the smallest deployable unit in Kubernetes, it contains one or more containers with sharing command resources like storage and network.
  • Python Script: It is a set of instructions written in the Python programming language that is used to perform specific tasks or functions.

A Step-By-Step Implementation Of Running Python Script In Kubernetes Pod

Step 1: Login In AWS Account

  • Navigate to your AWS Account and provide your Username.
  • AWS Account Login

Login_email

  • Provide the Password with respective to your Username.

Login_password

Step 2: Create An Instance

Create-Instance

Step 3: Choosing AMI

  • Choose the AMI with name Amazon Linux 2 which is the latest version of Amazon Linux distribution and provide number of instances as 1 and instance name as "my_aws_instance".

Choosing-AMI

Step 4: Choosing Key Pair

  • Choose the existing the key pair, It helps in connecting the instance remotely from the local console with ssh protocol. Or else you can use choose without key pair option.

Choosing-Created-Key-Pair

Step 5: Configuring Security Groups

  • Configure the network security groups by clicking on edit option and provide the option values as shown in the below screenshot.

Configuring-Security-groups

Step 6: Launching Instance

  • After reviewing the definitions and configuration of the instance click on Launch Instance button.

Launching-Instance

Step 7: Connect Instance

  • After creating the instance with name "my_aws_instance" , it will take some time to come for running state. After that Connect to the instance by clicking on connect button as shown in below screenshot.

Connect-Instance

Step 8: Navigate EC2 Console

  • Click on the instance, navigate inside to it and go through EC2 Instance Connect section and click on connect to use EC2 Console.

EC2-Console

Step 9: Switch To Root User

  • After landing on the EC2 Console, Run the following command to switch to root user.
sudo su -

Switching-Root

Step 10: Install Docker

  • Now, install the docker software with running the following command:
yum install docker 
  • Enable the docker service with the following command:
systemctl enable docker --now

Installing-Docker

Step 11: Write Your Python Script

  • Create or obtain the Python script you want to run in your Kubernetes pod. Ensure it's appropriately structured and contains the necessary functionality.

Create-python-script

  • Printing the python script file with cat command:

Printing-python-script

Step 12: Dockerize Your Python Script

FROM python:3.9-slim
# Set the working directory in the container
WORKDIR /app

# Copy the Python script into the container
COPY your_script.py /app/

# Install any dependencies required by your script
RUN pip install dependency1 dependency2

# Specify the command to run your script
CMD [ "python", "./your_script.py" ]

Defining-Dockerfile

  • Printing the dockerfile with cat command:

Printing-Dockerfile

Step 13: Build Your Docker Image

  • Build the Docker image using the Dockerfile:
docker build -t your-image-name .

Docker-build

  • Changing the docker image with docker tag command:

Docker-tag

Step 14: Push Your Docker Image to a Container Registry

  • Push the built Docker image to a container registry like Docker Hub, Google Container Registry, or any other registry of your choice:
docker push your-image-name

Docker-push

Step 15: Deploy Your Python Script in a Kubernetes Pod

  • Write a Kubernetes Deployment YAML file (`deployment.yaml`) to deploy your Docker image as a pod:
apiVersion: apps/v1
kind: Deployment
metadata:
name: python-script
spec:
replicas: 1
selector:
matchLabels:
app: python-script
template:
metadata:
labels:
app: python-script
spec:
containers:
- name: python-script
image: your-image-name
imagePullPolicy: Always

Kubernetes-Pod

Step 16: Apply the Deployment Configuration

kubectl apply -f deployment.yaml

Kubernetes-Deployment

Step 17: Verify Deployment

  • Verify that your deployment is running and check for any errors:
kubectl get pods
kubectl logs <pod-name>

Step 18: Monitor and Manage

  • Monitor the logs of your pod to ensure your Python script is running as expected:
kubectl logs -f <pod-name>
  • You can also manage your deployment by scaling it, updating the image, or deleting it as needed:
kubectl scale deployment python-script --replicas=3
kubectl set image deployment python-script python-script=your-new-image-name
kubectl delete deployment python-script
  • That's it! Your Python script is now running in a Kubernetes pod.

Next Article

Similar Reads