Zudoku
Guides

MongoDB

This guide shows you how to use MongoDB, a source-available, cross-platform, document-oriented database program.

To run it, 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/mongodb/ directory:

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

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 27017:27017/tls -M 1024 .

The output shows the instance URL and other details:

TerminalCode
[โ—] Deployed successfully! โ”‚ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ name: mongodb-6tiuu โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ uuid: 99779597-0bdb-4160-b902-a160c3ab4b2a โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ state: running โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ fqdn: bold-brook-khkwv7of.fra0.kraft.host โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ image: mongodb@sha256:e6ff5153f106e2d5e2a10881818cd1b90fe3ff1294ad80879b2239ffc52aff0e โ”œโ”€โ”€โ”€โ”€โ”€ boot time: 82.41 ms โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ memory: 1024 MiB โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€ service: bold-brook-khkwv7of โ”œโ”€โ”€ private fqdn: mongodb-6tiuu.internal โ”œโ”€โ”€โ”€โ”€ private ip: 172.16.6.4 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ args: /usr/bin/mongod --bind_ip_all --nounixsocket

In this case, the instance name is mongodb-6tiuu; it's different for each run.

To test the deployment, first forward the port with the kraft cloud tunnel command:

TerminalCode
kraft cloud tunnel 27017:mongodb-6tiuu:27017

Then, on a separate console, you can use the mongosh client to connect to the server:

Code
mongosh mongodb://localhost

You should see output similar to:

TerminalCode
Current Mongosh Log ID: 65d75b96310f70e63565e0f1 Connecting to: mongodb://localhost/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.1.5 (node:79750) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead. (Use `node --trace-deprecation ...` to show where the warning was created) Using MongoDB: 6.0.13 Using Mongosh: 2.1.5 For mongosh info see: https://docs.mongodb.com/mongodb-shell/ To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy). You can opt-out by running the disableTelemetry() command. test>

To disconnect, kill the tunnel command with Ctrl+c.

We use kraft cloud tunnel only when a service doesn't support TLS and is not HTTP-based (we use TLS/SNI to determine the correct instance to send traffic to). Also note that tunnel command is not needed when connecting via an instance's private IP/FQDN, i.e., when a MongoDB instance is used as a backend to another instance that acts as a frontend and which does support TLS.

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 mongodb-6tiuu bold-brook-khkwv7of.fra0.kraft... running 20 minutes ago mongodb@sha256:e6ff5153f106e2... 1.0 GiB /usr/bin/mongod --bind_ip_all... 82410us

When done, you can remove the instance:

TerminalCode
kraft cloud instance remove mongodb-6tiuu

Customize your Application

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

  • Kraftfile: the Unikraft Cloud specification, including command-line arguments
  • Dockerfile: In case you need to add files to your instance's rootfs

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