首页 使用krustlet调度wasm到k8s
文章
取消

使用krustlet调度wasm到k8s

使用krustlet调度wasm到k8s

1. 使用rust编写一个wasm程序

  1. 使用cargo创建一个hello-world项目
    1
    
    $ cargo new hello-world 
    
  2. 之后,在src/main.rs中编写我们的代码逻辑,这里程序将会循环输入hello world
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
     use std::thread::sleep;
     use std::time::Duration;
    
     fn main() {
         loop {
             println!("Hello, world!");
             sleep(Duration::from_secs(5))
         }
     }
    
  3. 使用cargo尝试运行我们的程序
    1
    2
    3
    
    $ cargo run
    Hello, world!
    Hello, world! 
    
  4. 使用cargo将代码编译为wasm
    1
    
    $ cargo build --release --target wasm32-wasi
    

    这一步将会在target/wasm32-wasi/release下生成hello-world.wasm文件,可以尝试使用wasmtime来运行该文件

    1
    2
    3
    
    $ wasmtime target/wasm32-wasi/release/hello-world.wasm
    Hello, world!
    Hello, world! 
    

将wasm文件打包为oci容器镜像并推送到镜像仓库

使用github的容器注册表作为镜像仓库,文档:https://docs.github.com/cn/packages/working-with-a-github-packages-registry/working-with-the-container-registry

使用wasm-to-oci工具将wasm推送到镜像仓库

1
$ wasm-to-oci push target/wasm32-wasi/release/hello-loop.wasm [image-registry]

将工作负载部署到k8s

运行krustlet

从krustlet官网下载可执行文件,如果是 m1 的mac,需要自己动手从代码编译。

下载之后直接执行

1
$ ./krustlet-wasi

之后可以使用kubectl查看当前连接的节点

1
2
3
4
$ kubectl get nodes
...
weibodemacbook-pro.local   Ready    <none>                 12s   1.0.0-alpha.1
...

可以看到krustlet已经注册为一个k8s的节点

部署项目

创建yaml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
apiVersion: v1
kind: Pod
metadata:
  name: krustlet-tutorial
spec:
  containers:
    - name: krustlet-tutorial
      image: <image>
  imagePullSecrets:
    - name: pull-secret
  tolerations:
    - key: "kubernetes.io/arch"
      operator: "Equal"
      value: "wasm32-wasi"
      effect: "NoExecute"
    - key: "kubernetes.io/arch"
      operator: "Equal"
      value: "wasm32-wasi"
      effect: "NoSchedule"

使用kubectl部署项目

1
$ kubectl apply -f krustlet.yaml

查看日志

1
2
3
4
5
6
7
8
9
$ kubectl logs krustlet-tutorial 
Hello, world!
Hello, world!
Hello, world!
Hello, world!
Hello, world!
Hello, world!
Hello, world!
Hello, world!
本文由作者按照 CC BY 4.0 进行授权
文章内容