Skip to content

Latest commit

Β 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

README.md

Flask + Redis HTTP Server

This guide explains how to create and deploy a Flask app with a Redis database on Unikraft Cloud. The example consists of two services: a Flask web server that increments a page view counter stored in Redis.

To run this example, follow these steps:

  1. Install the CLI. Use the unikraft CLI or the legacy kraft CLI. You need a BuildKit builder. The easiest way to get one is via Docker. Alternatively, you can also directly set up and use BuildKit, see the quick start.

    Note: The unikraft CLI is the current standard, while kraft is the legacy version. Choose one of the CLIs below and only run the commands associated with it for the rest of this guide.

  2. Clone the examples repository and cd into the examples/httpserver-flask-redis directory:

    git clone https://github.com/unikraft-cloud/examples
    cd examples/httpserver-flask-redis/

Make sure to log into Unikraft Cloud and pick a metro close to you. This guide uses fra (Frankfurt, πŸ‡©πŸ‡ͺ):

Using the unikraft CLI (Recommended)

unikraft login

or

Using the legacy kraft CLI

# Set Unikraft Cloud access token
export UKC_TOKEN=token
# Set metro to Frankfurt, DE
export UKC_METRO=fra

Deploy Redis

First, deploy the Redis instance. Redis is an internal service (not publicly accessible), reached via the redis.internal domain:

Using the unikraft CLI (Recommended)

unikraft build redis/ --output <my-org>/redis:latest
unikraft run --metro fra \
  -m 256M \
  --scale-to-zero policy=idle,cooldown-time=1000,stateful=true \
  --domain redis.internal \
  --image <my-org>/redis:latest

or

Using the legacy kraft CLI

kraft cloud deploy \
  -M 256Mi \
  --scale-to-zero idle \
  --scale-to-zero-stateful \
  --scale-to-zero-cooldown 1s \
  --domain redis.internal \
  redis/

The output shows the Redis instance details:

Using the unikraft CLI (Recommended)

metro:         fra
name:          redis-09p2q
uuid:          0355f93a-7b60-4ade-9359-b01671e812b3
state:         starting
image:         <my-org>/redis
resources:
  memory:      256MiB
  vcpus:       1
service:
  uuid:        19c050b2-b5c9-4e8e-9d25-a9053ff09270
  name:        damp-brook-xmmmwzwo
  domains:
  - fqdn:      redis.internal
networks:
- uuid:        97df34f7-63bc-4f02-a1b4-5ba07278de00
  private-ip:  10.0.0.41
  mac:         12:b0:0a:00:00:29
timestamps:
  created:     just now
scale-to-zero: policy=idle,stateful=true,cooldown-time=1s

or

Using the legacy kraft CLI

[●] Deployed successfully!
 β”‚
 β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€ name: redis-o9abw
 β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€ uuid: 9dd4c7b5-7cc4-4b99-ac70-b10af54ef075
 β”œβ”€β”€β”€β”€β”€β”€β”€β”€ metro: https://api.fra.unikraft.cloud/v1
 β”œβ”€β”€β”€β”€β”€β”€β”€β”€ state: starting
 β”œβ”€β”€β”€β”€β”€β”€β”€ domain: redis.internal
 β”œβ”€β”€β”€β”€β”€β”€β”€β”€ image: oci://unikraft.io/<my-org>/redis@sha256:3e35bc6c8048269741a0c0b4b165a10112736c48dbf869c76bb263a237502462
 β”œβ”€β”€β”€β”€β”€β”€β”€ memory: 256 MiB
 β”œβ”€β”€β”€β”€β”€β”€ service: sparkling-fire-rnd4o9kb
 β”œβ”€ private fqdn: redis-o9abw.internal
 └─── private ip: 10.0.0.41

Deploy Flask

Next, deploy the Flask web server. It connects to Redis using the REDIS_HOST and REDIS_PORT environment variables:

Using the unikraft CLI (Recommended)

unikraft build flask/ --output <my-org>/flask:latest
unikraft run --metro fra \
  -m 512M \
  -p 443:8000/tls+http \
  --scale-to-zero policy=on,cooldown-time=1000 \
  --env REDIS_HOST=redis.internal \
  --env REDIS_PORT=6379 \
  --image <my-org>/flask:latest

or

Using the legacy kraft CLI

kraft cloud deploy \
  -M 512Mi \
  -p 443:8000/tls+http \
  --scale-to-zero on \
  --scale-to-zero-cooldown 1s \
  --env REDIS_HOST=redis.internal \
  --env REDIS_PORT=6379 \
  flask/

The output shows the Flask instance details:

Using the unikraft CLI (Recommended)

metro:          fra
name:           flask-q62qa
uuid:           d081ec92-629b-45b3-8caf-059e0a87dffd
state:          starting
image:          <my-org>/flask
runtime:
  env:
    REDIS_HOST: redis.internal
    REDIS_PORT: 6379
resources:
  memory:       512MiB
  vcpus:        1
service:
  uuid:         ac1cd90e-a1fe-478c-8f58-8598fa80c494
  name:         hidden-silence-htyml3s2
  domains:
  - fqdn:       hidden-silence-htyml3s2.fra.unikraft.app
networks:
- uuid:         d2f3d024-3082-4e98-be22-f36df84263a1
  private-ip:   10.0.0.53
  mac:          12:b0:0a:00:00:35
timestamps:
  created:      just now
scale-to-zero:  policy=on,cooldown-time=1s

or

Using the legacy kraft CLI

[●] Deployed successfully!
 β”‚
 β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€ name: flask-a06h2
 β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€ uuid: 431f67ed-1f18-4dc1-86db-68dac4edff51
 β”œβ”€β”€β”€β”€β”€β”€β”€β”€ metro: https://api.fra.unikraft.cloud/v1
 β”œβ”€β”€β”€β”€β”€β”€β”€β”€ state: starting
 β”œβ”€β”€β”€β”€β”€β”€β”€ domain: https://withered-cherry-xfcrfp93.fra.unikraft.app
 β”œβ”€β”€β”€β”€β”€β”€β”€β”€ image: oci://unikraft.io/<my-org>/flask@sha256:3d29ca3a8a3d434f41acf7c8093435836dcbacca9190c51ec39dbb4ddf1ee45a
 β”œβ”€β”€β”€β”€β”€β”€β”€ memory: 512 MiB
 β”œβ”€β”€β”€β”€β”€β”€ service: withered-cherry-xfcrfp93
 β”œβ”€ private fqdn: flask-a06h2.internal
 └─── private ip: 10.0.0.53

In this case, the Flask instance address is https://withered-cherry-xfcrfp93.fra.unikraft.app. It's different for each run.

Test the deployment

Use curl to query the Flask instance. Each request increments the Redis counter:

curl https://withered-cherry-xfcrfp93.fra.unikraft.app
This webpage has been viewed 1 time(s)
curl https://withered-cherry-xfcrfp93.fra.unikraft.app
This webpage has been viewed 2 time(s)

You can list information about the instances by running:

Using the unikraft CLI (Recommended)

unikraft instances list

or

Using the legacy kraft CLI

kraft cloud instance list

Clean up

When done, remove the instances:

Using the unikraft CLI (Recommended)

unikraft instances delete flask-q62qa
unikraft instances delete redis-09p2q

or

Using the legacy kraft CLI

kraft cloud instance remove flask-a06h2
kraft cloud instance remove redis-o9abw

Learn more

Use the --help option for detailed information on using Unikraft Cloud:

kraft cloud --help

Or visit the CLI Reference.