Coder Social home page Coder Social logo

k3s-kubernetes-flask-hello-world's Introduction

Hello World from Flask in a k3s deployed container

This is yet another simple tutorial on how to deploy a Flask application on Kubernetes, specifically on k3s.

Flask Hello World

The python code that composes the Python application is stored in the app.py file and is as follows:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "<p>This is a Hello World application</p>"

if __name__ == "__main__":
    app.run(host='0.0.0.0', debug=True)

This can be launched with the command python app.py and will start a service reachable at http://localhost:5000.

The only requirement for this to run is Flask.

Docker

Inside the Dockerfile, there is the specification to create the image that will be later deployed in the k3s cluster.

The Dockerfile is as follows:

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

Build the image with: docker build -t k3s-kubernetes-flask-hello-world .

To run the image use docker run k3s-kubernetes-flask-hello-world -p 5000:5000. The service will again be reachable at http://localhost:5000.

k3s setup

If you have already set up your k3s cluster, you can skip this part, otherwise follow the steps described in the front page of the k3s website.

Importing the image in k3s

Save the image as into a .tar archive and import it in k3s via containerd:

docker save --output k3s-kubernetes-flask-hello-world.tar k3s-kubernetes-flask-hello-world
sudo k3s ctr images import kubernetes-flask-hello-world.tar

Another option that does it in one line and does not require saving the image:

docker save k3s-kubernetes-flask-hello-world | sudo k3s ctr images import -

Deployment configuration

Now that the image has been built, it is time to configure the k3s deployment as follows:

apiVersion: apps/v1
kind: Deployment
metadata:
name: flask-app-deployment
spec:
replicas: 3
selector:
    matchLabels:
    app: flask-app-pod
template:
    metadata:
    labels:
        app: flask-app-pod
    spec:
    containers:
    - name: flask-app-pod-image
        image: k3s-kubernetes-flask-hello-world
        imagePullPolicy: Never
        resources: 
        limits:
            memory: "128Mi"
            cpu: "500m"
        ports:
        - containerPort: 5000

---
apiVersion: v1
kind: Service
metadata:
name: flask-app-service
spec:
selector:
    app: flask-app-pod
ports:
- protocol: "TCP"
    port: 6000
    targetPort: 5000
type: LoadBalancer

You can start the service by applying the aforementioned configuration with k3s kubectl apply -f deployment.yml. This will spin up three pods running the previously written Python application and a load balancer. The latter can be reached at http://localhost:6000.

k3s-kubernetes-flask-hello-world's People

Contributors

cipz avatar

Watchers

 avatar  avatar

Forkers

kristofer

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.