Zudoku
Guides

Imaginary

This example uses Imaginary, an HTTP microservice for high-level image processing. Imaginary can be deployed as Unikraft Cloud service and can be queried via HTTP to process images.

To run this example, follow these steps:

  1. Install the kraft CLI tool and a container runtime engine, e.g. Docker.

  2. Clone the examples repository and cd into the examples/imaginary/ directory:

TerminalCode
git clone https://github.com/kraftcloud/examples cd examples/imaginary/

Make sure to log into Unikraft Cloud by setting your token and a metro close to you. We use fra0 (Frankfurt, ๐Ÿ‡ฉ๐Ÿ‡ช) in this guide:

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

When done, invoke the following command to deploy this application on Unikraft Cloud:

TerminalCode
kraft cloud deploy -p 443:8080 -M 256 .

The output shows the instance URL and other details:

TerminalCode
[โ—] Deployed successfully! โ”‚ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ name: imaginary-mwb4y โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ uuid: 8cf18bf7-2bf6-4f23-be07-f9c234c7962d โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ state: running โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ url: https://divine-wind-1ycjvhqs.fra0.kraft.host โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ image: sha256:673834bc531038bb621266f7fd635a04e559050cbe82876df811fd4b975ea4fe โ”œโ”€โ”€โ”€โ”€โ”€ boot time: 32.26 ms โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ memory: 256 MiB โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€ service: divine-wind-1ycjvhqs โ”œโ”€โ”€ private fqdn: imaginary-mwb4y.internal โ”œโ”€โ”€โ”€โ”€ private ip: 172.16.3.3 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ args: /usr/bin/imaginary -p 8080

In this case, the instance name is imaginary-mwb4y and the URL is https://divine-wind-1ycjvhqs.fra0.kraft.host. They are different for each run.

Use curl to query the Unikraft Cloud instance of Imaginary. You will get a health status of the service:

TerminalCode
curl -s https://divine-wind-1ycjvhqs.fra0.kraft.host/health | jq
JSONCode
{ "uptime": 414, "allocatedMemory": 0.19, "totalAllocatedMemory": 0.72, "goroutines": 6, "completedGCCycles": 13, "cpus": 1, "maxHeapUsage": 3.63, "heapInUse": 0.19, "objectsInUse": 846, "OSMemoryObtained": 7.73 }

To test the Imaginary instance on Unikraft Cloud use the /form endpoint. That is, open the https://divine-wind-1ycjvhqs.fra0.kraft.host/form URL in the browser and use the existing forms to process an image.

To make actual use of the Imaginary instance, use the endpoints of the HTTP API. Endpoints are provided, together with parameters, for different image processing options: /crop, /resize, /flip, /convert, /watermark, /rotate, /blur etc.

At any point in time, you can list information about the instance:

TerminalCode
kraft cloud instance list
Code
NAME FQDN STATE CREATED AT IMAGE MEMORY ARGS BOOT TIME imaginary-mwb4y divine-wind-1ycjvhqs.fra0.kraft.host running 54 seconds ago sha256:673834bc531038bb621266f7fd635a04e5... 256 MiB /usr/bin/imaginary -p 8080 32262us

When done, you can remove the instance:

TerminalCode
kraft cloud instance remove divine-wind-1ycjvhqs

The Imaginary Unikraft Cloud service mostly works as is: you deploy it and then you query the endpoints of the HTTP API. You can customize the command line options used to start the service, by updating the cmd line in the Kraftfile:

YAMLCode
spec: v0.6 runtime: imaginary:1.2 cmd: ["/usr/bin/imaginary", "-p", "8080"]

You can update the cmd line with command line option for Imaginary.

Learn More

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

TerminalCode
kraft cloud --help

Or visit the CLI Reference.

Last modified on