You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Current »

Deploying a web application on Kubernetes(K8s) involves a series of steps that ensure your application is containerized, orchestrated, and managed efficiently. Below the key points for create and deploy a web application on k8s.

Create container image

K8s is a and orchestrator that works managing container image.  So as first step a your application need to be containerized, Docker is the most common tool for this, but other tools can be used, like Podman or other open source alternatives. Below is a simple python application and an example Dockrifle that permit to create the container image:


Make sure you have app.py and requirements.txt in the same directory as your Dockerfile. Here's a simple app.py example for a Flask application:

Python Example
import os
from flask import Flask
app = Flask(__name__)

# Use os.environ.get() to read the environment variable 'NAME'. 
# Provide a default value in case 'NAME' is not set.
name = os.environ.get('NAME', 'World')

@app.route('/')
def hello():
    # Use the 'name' variable in your application's response
    return f"Hello {name}!"

if __name__ == '__main__':
    # Run the app on all available interfaces on port 80
    app.run(host='0.0.0.0', port=80)

And your requirements.txt should at least contain Flask, like so:

Flask

this below is the docker file used for create a container version of the test applcaition

Dockerfile Example
# Use an official Python runtime as a parent image
FROM python:3.9-slim

# Set the working directory in the container
WORKDIR /usr/src/app

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

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

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

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

Here's a brief explanation of each command in the Dockerfile:

  • FROM python:3.9-slim: This line sets the base image for the Docker image, using a slim version of the official Python 3.9 image.
  • WORKDIR /usr/src/app: This line sets the working directory in the Docker image. Any relative file path will be set from this location.
  • COPY . .: This command copies all files in the current directory on the host machine into the working directory in the Docker image.
  • RUN pip install --no-cache-dir -r requirements.txt: This command installs the Python dependencies listed in the requirements.txt file.
  • EXPOSE 80: This line informs Docker that the application listens on port 80. You might need to adjust this depending on the port your Flask app is set to listen on.
  • ENV NAME World: This line sets an example environment variable that could be used by the application.
  • CMD ["python", "app.py"]: This is the command that runs when the container starts up. It starts your Flask application.


You can then build and run your Docker image with:



docker build -t my-python-app .
docker run -p 4000:80 my-python-app

app-test.zip

  • No labels