From 495ab8c0b119c86c651b4d91024d1421a249802d Mon Sep 17 00:00:00 2001 From: Anshika Vashistha <93611566+anshikavashistha@users.noreply.github.com> Date: Sun, 4 Aug 2024 17:50:32 +0530 Subject: [PATCH 1/3] Create k3s.md This PR addresses the issue of missing documentation for integrating WasmEdge with k3s, a lightweight Kubernetes distribution ideal for edge computing. The goal is to provide a comprehensive guide that outlines the necessary steps for configuring and deploying WasmEdge applications on a k3s cluster. Signed-off-by: Anshika Vashistha <93611566+anshikavashistha@users.noreply.github.com> --- docs/develop/deploy/kubernetes/k3s.md | 170 ++++++++++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 docs/develop/deploy/kubernetes/k3s.md diff --git a/docs/develop/deploy/kubernetes/k3s.md b/docs/develop/deploy/kubernetes/k3s.md new file mode 100644 index 00000000..9019ec2c --- /dev/null +++ b/docs/develop/deploy/kubernetes/k3s.md @@ -0,0 +1,170 @@ +### Integrating WasmEdge with k3s + +This guide will help you integrate WasmEdge with k3s, a lightweight Kubernetes distribution for edge computing. Follow the steps below to set up and deploy WasmEdge applications on k3s. + +#### Prerequisites +- A machine with Linux (preferably Ubuntu) +- Docker installed +- `k3s` installed +- `kubectl` command-line tool installed +- `crun` container runtime installed + +#### Step 1: Install k3s + +First, install k3s on your machine. You can do this by running the following command: + +```sh +curl -sfL https://get.k3s.io | sh - +``` + +This command installs k3s and starts it automatically. To check the status of k3s, run: + +```sh +sudo systemctl status k3s +``` + +#### Step 2: Install containerd and crun-wasmedge + +WasmEdge requires the `crun` runtime to run WebAssembly applications. Follow these steps to install and configure `containerd` and `crun-wasmedge`. + +1. **Install containerd**: + +```sh +sudo apt-get update +sudo apt-get install -y containerd +``` + +2. **Install crun**: + +```sh +sudo apt-get install -y crun +``` + +3. **Install WasmEdge**: + +Follow the installation instructions from the [WasmEdge documentation](https://wasmedge.org/docs/start/install). For example: + +```sh +curl -sSf https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/utils/install.sh | sudo bash +``` + +4. **Configure containerd to use crun with WasmEdge**: + +Edit the `config.toml` file for containerd to enable crun as the runtime handler for WasmEdge. This file is typically located at `/etc/containerd/config.toml`. + +Add or modify the following lines: + +```toml +[plugins] + [plugins.cri.containerd] + [plugins.cri.containerd.runtimes] + [plugins.cri.containerd.runtimes.crun-wasmedge] + runtime_type = "io.containerd.runc.v2" + [plugins.cri.containerd.runtimes.crun-wasmedge.options] + BinaryName = "crun" + SystemdCgroup = true +``` + +Restart `containerd` to apply the changes: + +```sh +sudo systemctl restart containerd +``` + +#### Step 3: Configure k3s to use containerd with crun-wasmedge + +Edit the k3s service configuration to use the configured containerd runtime. Typically, the configuration file is located at `/etc/systemd/system/k3s.service` or `/etc/systemd/system/k3s.service.d/k3s.conf`. + +Add the following environment variable to the service configuration: + +```ini +[Service] +Environment="CONTAINERD_RUNTIME_HANDLER=crun-wasmedge" +``` + +Reload the systemd configuration and restart k3s: + +```sh +sudo systemctl daemon-reload +sudo systemctl restart k3s +``` + +#### Step 4: Deploy a WasmEdge application on k3s + +Create a Kubernetes deployment YAML file for your WasmEdge application. For example, `wasmedge-deployment.yaml`: + +```yaml +apiVersion: apps/v1 +kind: Deployment +metadata: + name: wasmedge-demo +spec: + replicas: 1 + selector: + matchLabels: + app: wasmedge-demo + template: + metadata: + labels: + app: wasmedge-demo + spec: + containers: + - name: wasmedge-demo + image: your-wasmedge-image:latest + command: ["your-wasmedge-command"] + resources: + limits: + memory: "128Mi" + cpu: "500m" + ports: + - containerPort: 8080 +``` + +Apply the deployment file using `kubectl`: + +```sh +kubectl apply -f wasmedge-deployment.yaml +``` + +Check the status of your deployment: + +```sh +kubectl get pods +``` + +#### Step 5: Access your WasmEdge application + +Expose your WasmEdge application using a Kubernetes service. Create a service YAML file, e.g., `wasmedge-service.yaml`: + +```yaml +apiVersion: v1 +kind: Service +metadata: + name: wasmedge-service +spec: + selector: + app: wasmedge-demo + ports: + - protocol: TCP + port: 80 + targetPort: 8080 + type: LoadBalancer +``` + +Apply the service file: + +```sh +kubectl apply -f wasmedge-service.yaml +``` + +Retrieve the external IP address of the service: + +```sh +kubectl get svc wasmedge-service +``` + +You can now access your WasmEdge application via the external IP address. + +#### Conclusion + +You have successfully integrated WasmEdge with k3s and deployed a WebAssembly application. You can now build and deploy more WasmEdge applications on your k3s cluster. From c7e96b2ca0a326474ad7319d744997983141cb81 Mon Sep 17 00:00:00 2001 From: Anshika Vashistha <93611566+anshikavashistha@users.noreply.github.com> Date: Sun, 4 Aug 2024 21:54:40 +0530 Subject: [PATCH 2/3] Update quick_start_docker.md Signed-off-by: Anshika Vashistha <93611566+anshikavashistha@users.noreply.github.com> --- docs/start/getting-started/quick_start_docker.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/start/getting-started/quick_start_docker.md b/docs/start/getting-started/quick_start_docker.md index 615bbb09..580d6205 100644 --- a/docs/start/getting-started/quick_start_docker.md +++ b/docs/start/getting-started/quick_start_docker.md @@ -21,9 +21,10 @@ In this quick start guide, we cover how to run WASM container apps using Docker ## Prerequisite -You must have Docker Desktop 4.15+ installed. Make sure you have turned on the containerd image store feature in your Docker Desktop. +You must have Docker Desktop 4.21 installed. Make sure you have turned on the containerd image store feature in your Docker Desktop. + +![image](https://github.com/user-attachments/assets/3f0a5896-3e3e-4df1-a335-9e3b6a5240a0) -![Docker config](docker_config.png) ## Run a standalone WASM app From e12b80999faea6095556fd1cd0455ea8ff2897f3 Mon Sep 17 00:00:00 2001 From: Anshika Vashistha <93611566+anshikavashistha@users.noreply.github.com> Date: Sun, 4 Aug 2024 22:47:06 +0530 Subject: [PATCH 3/3] Delete docs/start/getting-started/quick_start_docker.md Signed-off-by: Anshika Vashistha <93611566+anshikavashistha@users.noreply.github.com> --- .../getting-started/quick_start_docker.md | 110 ------------------ 1 file changed, 110 deletions(-) delete mode 100644 docs/start/getting-started/quick_start_docker.md diff --git a/docs/start/getting-started/quick_start_docker.md b/docs/start/getting-started/quick_start_docker.md deleted file mode 100644 index 580d6205..00000000 --- a/docs/start/getting-started/quick_start_docker.md +++ /dev/null @@ -1,110 +0,0 @@ ---- -sidebar_position: 2 ---- - -# Quick start with Docker - -In this guide, we will walk you through how to quickly run WasmEdge apps in Docker Desktop. There is no additional dependencies as the entire development and runtime environments are managed by Docker Desktop. - - -:::note -If you are not using Docker Desktop, [get started here](quick_start.md). -::: - -We will cover the following examples. - -- [Run a standalone WASM app](#run-a-standalone-wasm-app) -- [Run an HTTP server](#run-an-http-server) -- [Run a JavaScript server (node.js)](#run-a-javascript-based-server) - -In this quick start guide, we cover how to run WASM container apps using Docker commands. If you are interested in how to build, publish, and compose WASM container apps from source code, check out the [Docker + wasm chapter](../build-and-run/docker_wasm.md). - -## Prerequisite - -You must have Docker Desktop 4.21 installed. Make sure you have turned on the containerd image store feature in your Docker Desktop. - -![image](https://github.com/user-attachments/assets/3f0a5896-3e3e-4df1-a335-9e3b6a5240a0) - - -## Run a standalone WASM app - -The Hello world example is a standalone Rust application. Its source code and build instructions are available [here](https://github.com/second-state/rust-examples/tree/main/hello). - -Use Docker to run the containerized WASM app. The WASM container image is stored in Docker Hub, and its image size is only 500KB. This image can run on any OS and CPU platform Docker supports. - -```bash -$ docker run --rm --runtime=io.containerd.wasmedge.v1 --platform=wasi/wasm secondstate/rust-example-hello:latest -Hello WasmEdge! -``` - -To learn more about how to create WASM apps in Rust - -- [Basic Rust examples for WasmEdge](https://github.com/second-state/rust-examples) -- [Rust developer guides](/category/develop-wasm-apps-in-rust) - - WASI-NN with [PyTorch](../../develop/rust/wasinn/pytorch.md), [OpenVINO](../../develop/rust/wasinn/openvino.md), or [Tensorflow Lite](../../develop/rust/wasinn/tensorflow_lite.md) backends - - [HTTP and HTTPS client](../../develop/rust/http_service/client.md) - - [MySQL database client](../../develop/rust/database/my_sql_driver.md) - - Redis client - - Kafka client - -## Run an HTTP server - -This example is a standalone HTTP server written in Rust. It demonstrates that Rust + WasmEdge as a lightweight stack for microservices. Its source code and build instructions are available [here](https://github.com/second-state/rust-examples/tree/main/server). - -Use Docker to pull the container image (around 800KB) from Docker Hub and then run it in a WasmEdge container. The container starts as a server. Note how we map the container's port 8080 to the local host's port 8080 so that the server becomes accessible from outside of the WASM container. - -```bash -$ docker run -dp 8080:8080 --rm --runtime=io.containerd.wasmedge.v1 --platform=wasi/wasm secondstate/rust-example-server:latest -Listening on http://0.0.0.0:8080 -``` - -From another terminal window, do the following. - -```bash -$ curl http://localhost:8080/ -Try POSTing data to /echo such as: `curl localhost:8080/echo -XPOST -d 'hello world'` - -$ curl http://localhost:8080/echo -X POST -d "Hello WasmEdge" -Hello WasmEdge -``` - -To learn more about how to create WASM services in Rust - -- [Rust developer guides](/category/develop-wasm-apps-in-rust) -- [HTTP application examples](https://github.com/WasmEdge/wasmedge_hyper_demo) -- [Database application examples](https://github.com/WasmEdge/wasmedge-db-examples) -- Lightweight microservices in Rust and WasmEdge - - [WasmEdge + Nginx + MySQL](https://github.com/second-state/microservice-rust-mysql) - - [WasmEdge + Kafka + MySQL](https://github.com/docker/awesome-compose/tree/master/wasmedge-kafka-mysql) - - [Dapr + WasmEdge](https://github.com/second-state/dapr-wasm) - -## Run a JavaScript-based server - -This example is a standalone HTTP server written in JavaScript using the node.js API. It demonstrates WasmEdge as a lightweight runtime for zero-dependency and portable node.js applications. Its source code is available [here](https://github.com/second-state/wasmedge-quickjs/tree/main/example_js/docker_wasm/server). - -```bash -$ docker run -dp 8080:8080 --rm --runtime=io.containerd.wasmedge.v1 --platform=wasi/wasm secondstate/node-example-server:latest -... ... -``` - -From another terminal window, do the following. - -```bash -$ curl http://localhost:8080/echo -X POST -d "Hello WasmEdge" -Hello WasmEdge -``` - -To learn more about how to run JavaScript apps in WasmEdge. - -- [The WasmEdge QuickJS runtime](https://github.com/second-state/wasmedge-quickjs) -- [AI inference application examples](https://github.com/second-state/wasmedge-quickjs/tree/main/example_js/tensorflow_lite_demo) -- [Web service client examples with fetch()](https://github.com/second-state/wasmedge-quickjs/blob/main/example_js/wasi_http_fetch.js) - -## Next steps - -- [Learn more about building and managing WASM containers in Docker](../build-and-run/docker_wasm.md) -- [Basic Rust examples for WasmEdge](https://github.com/second-state/rust-examples) -- Use Docker Compose to build and Rust-based microservices - - [WasmEdge / MySQL / Nginx](https://github.com/docker/awesome-compose/tree/master/wasmedge-mysql-nginx) - Sample Wasm-based web application with a static HTML frontend, using a MySQL (MariaDB) database. The frontend connects to a WASM microservice written in Rust, that runs using the WasmEdge runtime. - - [WasmEdge / Kafka / MySQL](https://github.com/docker/awesome-compose/tree/master/wasmedge-kafka-mysql) - Sample Wasm-based microservice that subscribes to a Kafka (Redpanda) queue topic, and transforms and saves any incoming message into a MySQL (MariaDB) database. -- Write WASM apps in your favorite languages, like [Rust](/category/develop-wasm-apps-in-rust), [C/C++](/category/develop-wasm-apps-in-cc), [JavaScript](/category/develop-wasm-apps-in-javascript), [Go](/category/develop-wasm-apps-in-go), and many other languages.