Installation Of Python In Kubernetes : A Step-By-Step Guide

Here, We are going to deploy Python software in a running kubernetes pod and kubernetes is installed in AWS EC2 instance.
Step 1: Create an AWS account and search for ec2 service and select it.

Launch an instance with configuration:

  • AMI- amazon Linux 2
  • instance type- t2.micro
  • Security group- allow SSH(22),HTTP (80),HTTPS(443) traffic from anywhere
  • Configure storage – 8gb with root volume type gp2
  • connect this instance with any CLI terminal by using SSH
 ssh -i  "pemfile" ec2-user@<instance-public-ip address>compute-1.amazonaws.com

Step 2: Install KUBECTL and KOPS from google “https://kops.sigs.k8s.io/getting_started/install/”

  • After downloading the kubectl and kops related keys and repos then install kubectl.
sudo yum install -y  kubectl

Step 3: Now,give your aws access key and secret key by the command.

  • Here, we use my aws access key and secret key instead of this you can attach EC2 role with admin full access to the ec2 instance.
aws configure

Step 4: Create s3 bucket from this k8 workstation instance to store the objects of the k8s master data.

aws s3 mb  s3://k8wp

Step 5: Export this Amazon S3 bucket

  • we have to export this s3 bucket for backup and restore the copy files between Kubernetes and S3 bucket.
export KOPS_STATE_STORE=s3://k8wp

Step 6: Generate A ssh key

  • ssh-keygen command is a component of most SSH implementations used to generate a public key pair for use when authenticating with a remote server
 ssh-keygen

Step 7: Create a cluster with the DNS “php.k8s.local”

 kops create cluster --name php.k8s.local --state s3://k8wp --zones us-east-1a,us-east-1b --node-count 2 --node-size t2.micro --yes

  • The following illustrates the creating process of cluster:

  • We have to check the cluster whether it is healthy or not and also cluster is validate or not
kops validate cluster 

Now our cluster is ready ….!!

Step 8: Create a Pod YAML

  • Create a YAML file describing your Pod. Here’s the YAML file (python-pod.yaml):
  • create a python-pod file with yml extension
sudo vi python-pod.yml

Now the copy the below provided code to the file named python-pod.yml:

 apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: python-container
image: amazonlinux:2
command: ["sleep", "infinity"] # Keep the container running
  • apiVersion specifies the Kubernetes API version being used.
  • kind defines the type of Kubernetes resource, which in this case is a Pod.
  • metadata contains information like the name of the Pod.
  • spec specifies the desired state for the Pod, including the container(s) it should run.
  • containers is a list of containers to run within the Pod. In this case, there’s one container.
  • name is the name given to the container.
  • image specifies the Docker image to use for the container. Here, it’s amazonlinux:2.
  • command defines the command to run in the container. In this case, it’s sleep infinity, which keeps the container running indefinitely

Step 9: Apply The YAML

  • Apply the Pod YAML to create the Pod in your cluster:
  • execute or apply these python-pod.yml with help of kubectl.
kubectl apply -f python-pod.yml

  • After this our python pod has been created and will ready to run
  • to check the pods execute this command
kubectl get pods

  • You can see the pod status is running and pod name is mypod.

Step 10: Access the Pod

  • You can access the running Pod using kubectl exec.
  • To install Python and pip within your container, you’ll need to update your Pod definition to include commands to install Python. Here’s how you can modify the Pod definition:
kubectl exec -it mypod -- /bin/bash
yum install -y python3
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py
kubectl exec -it mypod -- pip install python2

  • finally,python software is successfully installed in kubernetes pod.
  • And you can verify installed python.
kubectl exec -it mypod -- python -c "print('Python installed successfully')"

How To Install Python In Kubernetes Pod ?

Deploying Python applications inside Kubernetes units offers influential solutions for managing and scaling containerized tasks at hand. Python, renowned for its straightforwardness, simplicity, and extensive system, tracks down the wide applicable in web improvement, data science, data visualization, and computerization assignments. Kubernetes, a main holder organization stage, gives strong elements to mechanizing sending, scaling, and overseeing containerized applications across bunches of machines.

In this guide, we will explore the deploying Python application in a Kubernetes unit including making a Docker image that encapsulates the Python runtime environment along with any required libraries. This Docker image is then deployed as a container inside Kubernetes pods using YAML configuration files. By utilizing Kubernetes’ declarative model, developers can ensure consistency and producibility in their Python deployments while benefiting from Kubernetes’ capacities, for example, auto-scaling, load balancing, and administration disclosure.

Python is a flexible and generally used programming language known for its effortlessness, intelligibility, and broad environment of libraries and structures. In the present powerful registering scene, sending Python applications inside Kubernetes, a main compartment organization stage, has become progressively famous. Kubernetes offers strong highlights for overseeing containerized applications at scale, giving computerization, adaptability, and flexibility. it is frequently utilized in Kubernetes conditions. In this aide, we’ll stroll through the moves toward introducing Python in a Kubernetes case.

Similar Reads

What Is Kubernetes?

Kubernetes is a container orchestration platform developed by Google that helps manage and deploy applications run in containers, automating many of manual process involves in deploying, managing, health checking and scaling application. kubernetes also called as k8s because The 8 in K8s represents the number of letters between the “K” and the “s” in the name....

Understanding Of Primary Terminologies

kubectl: Kubernetes provides a command line tool for communicating with a Kubernetes clusters’ control plane, using the Kubernetes API. kOps: kOps helps us to create,destroy,upgrade and maintain the clusters. With kOps, teams can automate the management of Kubernetes clusters. Primary Function: Kubernetes is a platform for running and managing containers. Scaling: While a two-node cluster is suitable for testing and development purposes, consider scaling up the cluster as the workload to meet performance. Automatic scaling based on application demand. Networking: Networking configuration is essential to enable communication between nodes and pods within the cluster. And provides complex network setup and supports network policies. Storage: Supports wide range of storage solutions such as persistence, projected and ephemeral volumes. Fault Tolerance: Replaces failed containers automatically Portability: Ensures efficient running of deployed applications in any environment....

What Is Python?

Python is a general-purpose, high-level programming language. It is used for web development, software development, machine learning, and data science. python programming is used in data visualization, big data analytics and making graphs and mathematical problem solving. Python is an interpreted language, which means that it does not need to be compiled before it can be run. Often, programmers fall in love with Python because of the increased productivity it provides. Since there is no compilation step, the edit-test-debug cycle is incredibly fast. Debugging Python programs is easy....

Installation Of Python In Kubernetes : A Step-By-Step Guide

Here, We are going to deploy Python software in a running kubernetes pod and kubernetes is installed in AWS EC2 instance.Step 1: Create an AWS account and search for ec2 service and select it....

Kubernetes Pod And Python – FAQ’s

How Would I Scale Python Applications Sent In Kubernetes?...