Taikun Logo

An incredibly easy beginner’s guide to WebAssembly (WASM) and Kubernetes

Table of Contents

WebAssembly or WASM has been making waves in the developer community for a few years now. In fact, the quote above by the creator of Docker, Solomon Hykes summarizes it best.

So, we will have a look at what WASM is and how it is relevant for your cloud-native setups. We will explore it in enough detail for you to start using WASM and take advantage of this amazing technology.

Let’s start from the basics.

Introduction to WebAssembly

Remember Java and how it is platform-agnostic? 

The way Java code across platforms is that once you compile the Java code into bytecode – this bytecode can run on any platform as long as a Java runtime is installed on that platform.

WebAssembly is similar but built for the web (well, at least initially). Let’s understand this further.

What is WebAssembly (WASM)?

WASM is a contraction of the word WebAssembly. It is basically an instruction format that can run on any “stack-based virtual machine.” 

What is a stack machine? Most of our virtual machines are what we call register-based Virtual Machines. These machines have registers that store values and can be accessed to perform any operation.

A stack-based VM has a LIFO (Last-In-First-Out) data structure with PUSH and POP operations to move data around and process them. So, the same additional task above requires four instructions instead of one. 

Now, you might think this is inefficient. However, note that, in a stack-based VM, there is no need to remember the register address. All arithmetic and logic operations happen by pushing and popping the values in the stack.

WebAssembly is a language that works on a stack machine via a runtime environment (just like a JVM). The text-like instruction set is converted into a binary (called a WASM binary file) using WASM-code compilers and runs on any environment using a WASM runtime.

This makes WASM binaries much more portable. In some cases, they are almost one-tenth of the size of a JavaScript file that performs the same function in a browser.

How WASM binary is made and executed

As mentioned, WebAssembly is a low-level assembly-like language. The WebAssembly code is written in text format and then converted into a binary file called a WASM file. WebAssembly documentation gives you more detail about it. 

Developers do not have to learn the new WebAssembly language. They can write code in their favorite language like C, C++, Rust etc and then convert that into a WASM binary file using compilers like Emscripten.

Now, initially WebAssembly binaries were made for web browsers. So, the WASM binary would get downloaded to the web browser like Firefox or Chrome and then get executed. Since 2019, WASM is also a W3C standard and hence all major browsers support it.

Once the binary is downloaded, the browser’s JavaScript engine would get it translated into machine-level code and executed at near-native speed.

Introduction to WASI

We learned how WASM works very well and super fast with browsers. In 2019, Mozilla Foundation introduced WASI – WebAssembly Systems Interface. This is an API interface that allows WASM files to interact with system resources like network, filesystem, memory and so on. 

WASM for CloudNative ecosystem

WASI expanded the scope of WASM to also be used on the server side. This means now, all the benefits of a WASM module is available in Cloud native ecosystems. 

Here are some of the key benefits of WASM-WASI modules in Cloud ecosystems: 

  1. Being binary code, WASM modules have significantly faster loading times. This improves startup times for serverless functions. 
  1. Since the WASM files are much smaller in size, they utilize much less compute resources and memory, thus saving huge costs in cloud setups. 
  1. WASM files are also platform and architecture-agnostic. This reduces the dependency and complexities of making it compatible with multiple environments.
  1. WASM files run in strict sandbox environments which improves the security of the host environment.

How is WASM + WASI different from Docker?

Both WASM and Docker intend to enhance the portability of applications across platforms and architectures.

Docker portability

Docker allows the separation of layers from the Host OS by creating an isolated software package called a Container that has everything it needs to run the application. 

The separation from the host hardware and OS is done by the Container Engine or Runtime which runs the container. The container is the portable module.

This containerization is created by a Docker Image, which is specific to the hardware architecture on which it runs.

WASM portability

WASM binaries also need a runtime environment, but they do not rely on host OS or architecture. WASI (WebAssembly System Interface) helps interact with the hardware resources without relying on any of the host OS libraries.

Thus, WASM binaries can achieve the same results in a much shorter time and with fewer system resources.

Kubernetes and WASM

With Docker, the role of Kubernetes came in the form of container orchestration and management. 

When we have thousands of containers running in a system, Kubernetes brings much-needed capabilities of auto-scaling, persistent storage, and lifecycle management to containers.

Kubernetes with WASM also works in the same way. Except in this case, we need WASM runtime in every node. 

WASM runtimes in Kubernetes

You could set up WASM runtimes in Kubernetes in two ways. 

  1. Popular high-level runtimes like containerd have WASM-runtime subprojects like runwasi, which is a low-level WASM runtime that enables the execution of WASM binaries.
  2. The second method is to set up low-level WASM runtimes like crun, youki, wasmedge, or wasmtime.

We will talk more about how to setup WASM runtimes in Kubernetes in the next blog (link to blog 2).

Growing WebAssembly landscape

Ever since WebAssembly became a W3C standard in December 2019, the adoption of the WASM technology has picked up pace. 

Here is a short overview of the technology landscape that works with WasmEdge (a popular WASM runtime).

If you’re looking to simplify Kubernetes management and easily enable WebAssembly (Wasm), Taikun CloudWorks provides an integrated solution. With CloudWorks, you can effortlessly manage your Kubernetes clusters and take advantage of Wasm capabilities, saving both time and effort in your operations.