In this tutorial we'll be:
- installing UniK
- writing a simple HTTP Daemon in Python
- compiling to a unikernel and launching an instance on Virtualbox
Ensure that each of the following are installed
- Docker installed and running with at least 6GB available space for building images
jq
make
- Virtualbox
- Install UniK
$ git clone https://github.com/solo-io/unik.git
$ cd unik
$ make
note: make
will take quite a few minutes the first time it runs. the UniK Makefile
is pulling all of the Docker images that bundle UniK's dependencies.
Then, place the unik
executable in your $PATH
to make running UniK commands easier:
$ mv _build/unik /usr/local/bin/
- Configure a Host-Only Network on Virtualbox
- Open Virtualbox
- Open Preferences > Network > Host-only Networks
- Click the green add button on the right side of the UI
- Record the name of the new Host-Only adapter. You will need this in your UniK configuration
- Ensure that the Virtualbox DHCP Server is Enabled for this Host-Only Network:
- With the Host-Only Network selected, Click the edit button (screwdriver image)
- In the Adapter tab, note the IPv4 address and netmask of the adapter.
- In the DHCP Server tab, check the Enable Server box
- Set Server Address an IP on the same subnet as the Adapter IP. For example, if the adapter IP is
192.168.100.1
, make set the DHCP server IP as192.168.100.X
, where X is a number between 2-254. - Set Server Mask to the netmask you just noted
- Set Upper / Lower Address Bound to a range of IPs on the same subnet. We recommend using the range
X-254
where X is one higher than the IP you used for the DHCP server itself. E.g., if your DHCP server is192.168.100.2
, you can set the lower and upper bounds to192.168.100.3
and192.168.100.254
, respectively.
- Configure UniK daemon
- Using a text editor, create and save the following to
$HOME/.unik/daemon-config.yaml
:
providers:
virtualbox:
- name: my-vbox
adapter_type: host_only
adapter_name: NEW_HOST_ONLY_ADAPTER
replacing NEW_HOST_ONLY_ADAPTER
with the name of the network adapter you created.
- Launch UniK and automatically deploy the Virtualbox Instance Listener
- Open a new terminal window/tab. This terminal will be where we leave the UniK daemon running.
cd
to the_build
directory created bymake
- run
./unik daemon --debug
(the--debug
flag is optional, if you want to see more verbose output) - UniK will compile and deploy its own 30 MB unikernel. This unikernel is the Unik Instance Listener. The instance listener uses udp broadcast to detect instance ips and bootstrap instances running on Virtualbox.
- After this is finished, UniK is running and ready to accept commands.
- Open a new terminal window and type
unik target --host localhost
to set the CLI target to the your local machine.
-
Open a new terminal window, but leave the window with the daemon running. This window will be used for running UniK CLI commands.
-
Make sure
python3.5
andpip
are installed:
- Python 3.5: https://www.python.org/downloads/release/python-351/
- Pip (for OS X): http://softwaretester.info/install-and-upgrade-pip-on-mac-os-x/
- Pip (Ubuntu): http://www.saltycrane.com/blog/2010/02/how-install-pip-ubuntu/ (on Ubuntu/Debian)
- Create a file
server.py
using a text editor. Copy and paste the following code in that file:
import http.server
import socketserver
PORT = 8080
Handler = http.server.SimpleHTTPRequestHandler
httpd = socketserver.TCPServer(("", PORT), Handler)
print("serving at port", PORT)
httpd.serve_forever()
This will be our simple Python server.
Note: dependencies in Python applications must be installed with pip
to the project directory (rather than the global site-packages
for the current user). See rump-python3 for more information.
-
Try running this code with
python3 server.py
. Visit http://localhost:8080/ to see that the server is running. -
We need to create a manifest file to tell UniK the name of the file which contains the entrypoint to our application. In this case, it's just
server.py
.
- Create a file named
manifest.yaml
and paste the following inside:
main_file: server.py
- Great! Now we're ready to compile this code to a unikernel.
- run the following command from the directory where your
server.py
is located:
unik build --name myImage --path ./ --base rump --language python --provider virtualbox
this command will instruct UniK to compile the sources found in the working directory (./
) using the rump-python-virtualbox
compiler.
-
You can watch the output of the
build
command in the terminal window running the daemon. -
When
build
finishes, the resulting disk image will reside at$HOME/.unik/virtualbox/images/myImage/boot.vmdk
-
Run an instance of this image with
unik run --instanceName myInstance --imageName myImage
-
When the instance finishes launching, let's check its IP and see that it is running our application.
-
Run
unik instances
. The instance IP Address should be listed. -
Direct your browser to
http://instance-ip:8080
and see that your instance is running! -
To clean up your image and the instance you created
unik rmi --force --image myImage