Join GitHub today
Packages: python3: A Flask Unikernel
A Flask Unikernel
This tutorial will cover the following topics:
- Building a Python environment containing the Flask module
- Setting up networking for communication
- Including the Python environment in your unikernel
For this tutorial you will need to know the locations of a few files. To make things easier, begin in the rumprun-packages/python3/examples/ directory. If you begin in this directory, the code snippets will be easy to copy and paste. Make sure you have a properly built the Python package, and have verified the example from the README works.
Building the environment
Building the Python environment is fairly straight forward. You will need to create a Python Virtual Environment running the same version of Python as your unikernel (probably 3.5.1). Activate this environment and use pip to install any packages you require. For this tutorial, the only package we need is Flask.
# Make sure you are in the python3/examples directory pyvenv-3.5 tutorial-env source tutorial-env/bin/activate pip install flask
Now that Flask is installed, you can create the main module. This process is just like the README instructs us to do. The example module will be very simple for this tutorial. Create a file called flask_main.py which contains the following:
# flask_main.py from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello World!" if __name__ == "__main__": app.run(host='0.0.0.0')
To include all your requirements in the unikernel, you will need to generate an ISO containing the installed packages and the main module. This ISO will be mounted inside the unikernel (just the same as package readme).
genisoimage -r -o tutorialenv.iso flask_main.py tutorial-env/lib/python3.5/site-packages
Setting up networking requires a TAP device. This will let the host machine pass traffic to your unikernel. Setup the IP address 10.0.120.100. Your unikernel will listen on 10.0.120.101.
sudo ip tuntap add tap0 mode tap sudo ip addr add 10.0.120.100/24 dev tap0 sudo ip link set dev tap0 up
Building the unikernel
Now you are able to start the unikernel. Refer to the README for further details. This is where file locations are important!
You should already have a baked python.bin available in the current directory. There should be a python.iso in the ../images/ directory.
Using rumprun's arguments, launch the rumprun unikernel with the generated ISO and the proper network settings.
rumprun kvm -i \ -I if,vioif,'-net tap,ifname=tap0,script=no' \ -W if,inet,static,10.0.120.101/24 \ -b ../images/python.iso,/python/lib/python3.5 \ -b tutorialenv.iso,/python/lib/python3.5/site-packages \ -e PYTHONHOME=/python \ -- python.bin -m flask_main
Give it a shot. Run the script, and visit http://10.0.120.101:5000. You should get the "Hello World" message to pop up!