Zudoku
Guides

Rust (Tokio)

This example uses Tokio, a popular Rust asynchronous runtime. To run this example, follow these steps:

  1. Install the kraft CLI tool and a container runtime engine, for example Docker.

  2. Clone the examples repository and cd into the examples/http-rust1.75-tokio/ directory:

Code(bash)
git clone https://github.com/kraftcloud/examples cd examples/http-rust1.75-tokio/

Make sure to log into Unikraft Cloud by setting your token and a metro close to you. This guide uses fra (Frankfurt, ๐Ÿ‡ฉ๐Ÿ‡ช):

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

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

Code(bash)
kraft cloud deploy -p 443:8080 .

The output shows the instance address and other details:

Code(ansi)
[โ—] Deployed successfully! โ”‚ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ name: http-rust175-tokio-6gxsp โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ uuid: d5719f64-0653-42d7-b2de-aa6dee0ce467 โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ state: running โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ url: https://empty-dawn-3coedrce.fra.unikraft.app โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ image: http-rust175-tokio@sha256:0ce75912711aa2329232a2ca6c3ccb7a244b6d546fafc081f815c2fde8224856 โ”œโ”€โ”€โ”€โ”€โ”€ boot time: 21.41 ms โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ memory: 128 Mi โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€ service: empty-dawn-3coedrce โ”œโ”€โ”€ private fqdn: http-rust175-tokio-6gxsp.internal โ”œโ”€โ”€โ”€โ”€ private ip: 172.16.6.3 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ args: /server

In this case, the instance name is http-rust175-tokio-6gxsp and the address is `https://empty-dawn-3coedrce.fra.unikraft.app. They're different for each run.

Use curl to query the Unikraft Cloud instance of the Tokio-based HTTP web server:

Code(bash)
curl https://empty-dawn-3coedrce.fra.unikraft.app
Code(text)
Hello, World!

You can list information about the instance by running:

Code(bash)
kraft cloud instance list
Code(text)
NAME FQDN STATE CREATED AT IMAGE MEMORY ARGS BOOT TIME http-rust175-tokio-6gxsp empty-dawn-3coedrce.fra.unikraft.app running 1 minute ago http-rust175-tokio@sha256:0ce75912... 128 MiB /server 21412us

When done, you can remove the instance:

Code(bash)
kraft cloud instance remove http-rust175-tokio-6gxsp

Customize your app

To customize the app, update the files in the repository, listed below:

  • src/main.rs: the actual server
  • Cargo.toml: the Cargo package manager configuration file
  • Kraftfile: the Unikraft Cloud specification
  • Dockerfile: the Docker-specified app filesystem

The following options are available for customizing the app:

  • If you only update the implementation in the src/main.rs source file, you don't need to make any other changes.

  • If you create any new source files, copy them into the app filesystem by using the COPY command in the Dockerfile. If you add new Rust source code files, be sure to configure required dependencies in the Cargo.toml file.

  • If you build a new executable, update the cmd line in the Kraftfile and replace /server with the path to the new executable.

  • More extensive changes may require extending the Dockerfile (see Dockerfile syntax reference).

Learn more

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

Code(bash)
kraft cloud --help

Or visit the CLI Reference.

Last modified on