Deploy Python App on Kubernetes Using Kustomize

In this article we are going to cover Deploy Python App on Kubernetes Using Kustomize.

Kubernetes is a powerful container orchestration platform, but managing configurations for different environments can be challenging. This is where Kustomize comes in handy. Kustomize is a built-in feature of kubectl that allows you to customize Kubernetes configurations without modifying the original YAML files.

In this guide, we will walk through deploying a simple Python application on Kubernetes using Kustomize. We will cover everything from setting up the Python app to containerizing it with Docker, creating Kubernetes manifests, and using Kustomize for configuration management. Additionally, we will explore how to test the application locally and access it via port forwarding in a Kubernetes cluster. This tutorial is beginner-friendly and provides a step-by-step approach to make deployment seamless.

Prerequisites

Before we begin, ensure you have the following installed:

  • Ubuntu 24.04 LTS server
  • Docker (for containerization)
  • Kubectl (for Kubernetes management)
  • Kustomize (built into kubectl since version 1.14)
  • A Kubernetes Cluster (Minikube, Kind, or a cloud-managed cluster like AWS EKS, GKE, or AKS)

Step #1:Create a Simple Python App

First, create a directory for your project:

mkdir python-kustomize && cd python-kustomize

Python Project Directory Structure:

python-kustomize/
│-- app.py
│-- requirements.txt
│-- Dockerfile
│-- base/
    │-- deployment.yaml
    │-- service.yaml
    │-- kustomization.yaml

Simple python code:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
    return "Hello, Kubernetes with Kustomize!"

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)

Create a requirements.txt file:

flask

Step #2:Create a Docker Image for Python App

Now, create a Dockerfile to containerize the Python app:

FROM python:3.9

WORKDIR /app

COPY requirements.txt .
RUN pip install -r requirements.txt

COPY app.py .

CMD ["python", "app.py"]

Build the Docker image:

docker build -t your-dockerhub-username/python-kustomize .
Deploy Python App on Kubernetes Using Kustomize 1

Test the app locally using Docker:

docker run -p 5000:5000 your-dockerhub-username/python-kustomize
Deploy Python App on Kubernetes Using Kustomize 2

Open a browser and go to:

http://<instance-ip>:5000

You should see the response “Hello, Kubernetes with Kustomize!”

Deploy Python App on Kubernetes Using Kustomize 3

Once tested, push the image to Docker Hub:

docker push your-dockerhub-username/python-kustomize
Deploy Python App on Kubernetes Using Kustomize 4
Deploy Python App on Kubernetes Using Kustomize 5

Step #3:Set Up Kubernetes Manifests for Python App

Create a base/ directory to store Kubernetes configurations:

mkdir -p base

Create a deployment.yaml inside base/:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: python-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: python-app
  template:
    metadata:
      labels:
        app: python-app
    spec:
      containers:
        - name: python-app
          image: your-dockerhub-username/python-kustomize:latest
          ports:
            - containerPort: 5000

Create a service.yaml inside base/:

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

Create a kustomization.yaml inside base/:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
  - deployment.yaml
  - service.yaml

Step #4:Deploy Python Application Using Kustomize

Apply the configuration using Kustomize:

kubectl apply -k base/
Deploy Python App on Kubernetes Using Kustomize 6

Verify that the pods are running:

kubectl get pods
Deploy Python App on Kubernetes Using Kustomize 7

Step #5:Access Python Application on Kubernetes

If you’re using a local Kubernetes cluster (like Minikube or Kind) and don’t have an external IP, you can access the app using port forwarding:

kubectl port-forward --address 0.0.0.0 svc/python-service 8080:80
Deploy Python App on Kubernetes Using Kustomize 8

Now, open a browser and go to:

http://localhost:8080

You should see “Hello, Kubernetes with Kustomize!”

Deploy Python App on Kubernetes Using Kustomize 9

Step #6:Delete the Deployment

If you want to remove the deployment, use the following command:

kubectl delete -k base/
Deploy Python App on Kubernetes Using Kustomize 10

Verify that the resources have been deleted:

kubectl get pods
Deploy Python App on Kubernetes Using Kustomize 11

Conclusion:

Deploying a Python application using Kubernetes and Kustomize simplifies the process of managing configurations and deploying updates efficiently. In this guide, we created a simple Flask application, containerized it using Docker, and deployed it using Kubernetes and Kustomize. We also covered how to access the app, manage different configurations, and delete the deployment when needed. By following these steps, you now have a solid foundation for using Kustomize in real-world Kubernetes deployments.

Related Articles:

Deploy NodeJS App on Kubernetes Using Kustomize

Reference:

kustomize official page

Harish Reddy

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap