Deploying a Flask App with Helm on Kubernetes

Introduction

Helm is a powerful package manager for Kubernetes that simplifies application deployment through reusable templates called charts. In this post, we’ll walk through creating a Helm chart for a simple Flask application and deploying it to a Kubernetes cluster.

Prerequisites

Before we begin, ensure you have the following installed:

  • Docker
  • Kubernetes (Minikube or a cloud provider setup)
  • Helm
  • kubectl

Step 1: Create a Simple Flask Application

First, let’s create a small Flask app. Create a directory named flask-app and add the following files:

app.py:

from flask import Flask

app = Flask(__name__)

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

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

Dockerfile:

FROM python:3.9
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "app.py"]

requirements.txt:

flask

Build and push the image to a registry:

docker build -t my-flask-app .
docker tag my-flask-app <your-dockerhub-username>/my-flask-app:v1
docker push <your-dockerhub-username>/my-flask-app:v1

Step 2: Create a Helm Chart

Generate a Helm chart:

helm create flask-chart
cd flask-chart

Modify the values.yaml file to define the image:

values.yaml:

image:
  repository: <your-dockerhub-username>/my-flask-app
  tag: v1
  pullPolicy: IfNotPresent

service:
  type: LoadBalancer
  port: 80

ingress:
  enabled: false

Modify the templates/deployment.yaml to use the Flask image:

templates/deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Release.Name }}
spec:
  replicas: 1
  selector:
    matchLabels:
      app: flask
  template:
    metadata:
      labels:
        app: flask
    spec:
      containers:
      - name: flask
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        ports:
        - containerPort: 5000

Understanding .Release.Name

.Release.Name is a built-in Helm variable that represents the name of the Helm release. When you install a Helm chart, you provide a release name, which helps identify and manage deployments. For example, if you run:

helm install my-flask-release ./flask-chart

.Release.Name will be set to my-flask-release, ensuring that resources created by this chart have unique and identifiable names. This is useful when deploying multiple instances of the same application within a cluster.

Step 3: Install and Run the Helm Chart

Deploy the application:

helm install my-flask-release ./flask-chart

Check the deployed pods:

kubectl get pods

Find the service’s external IP:

kubectl get services

Access the application via the service IP.

Conclusion

Using Helm, we packaged and deployed a simple Flask app to Kubernetes efficiently. Helm simplifies deployment and management, making it easy to scale and update applications. Try modifying the Helm chart for additional configurations like environment variables, ingress, or autoscaling!

Scroll to Top