本文介绍一种通过containerd shim支持在k8s中运行wasm的方法
先决条件
Intall k3d
wget -q -O - https://raw.githubusercontent.com/k3d-io/k3d/main/install.sh | bash
kubectl
curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl
启动k3d
在这个镜像中我内置添加了containerd-shim-dapr-v1
二进制,及为containerd添加了containerd-shim的配置。
k3d cluster create wasm-cluster --image docker.io/docker4zc/k3swithshims:latest -p "8081:8080@loadbalancer" --agents 1
让我们为节点增加标签,因为我们测试yaml中指定了runtimeClass的selector。
kubectl label nodes k3d-wasm-cluster-server-0 spin-enabled=true
执行以下指令,这会添加dapr wasm runtime class以及部署一个wasm container
kubectl apply -f https://github.com/Taction/containerd-shim-wasm/tree/main/k3d/workloads/runtime.yaml
kubectl apply -f https://github.com/Taction/containerd-shim-wasm/tree/main/k3d/workloads/workload.yaml
runtime.yaml
apiVersion: node.k8s.io/v1
kind: RuntimeClass
metadata:
name: dapr
handler: dapr
scheduling:
nodeSelector:
dapr-enabled: "true"
workload.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: wasm-dapr
spec:
replicas: 1
selector:
matchLabels:
app: wasm-dapr
template:
metadata:
labels:
app: wasm-dapr
spec:
runtimeClassName: dapr
containers:
- name: dapr-hello
imagePullPolicy: IfNotPresent
image: docker.io/docker4zc/dwhttp:v0.0.3
command: ["/"]
resources: # limit the resources to 128Mi of memory and 100m of CPU
limits:
cpu: 100m
memory: 128Mi
requests:
cpu: 100m
memory: 128Mi
# ...
然后执行以下命令向wasm container发送请求。
curl -v http://127.0.0.1:8081/dapr/hello
你应该能够看到网络请求返回Hello from WASM!
。
另外如果你想转发更多的端口出来的话,可以通过类似以下命令对k3d的node进行修改。
k3d node edit k3d-wasm-cluster-serverlb --port-add 8082:80
了解更多可以查看:
https://github.com/Taction/containerd-shim-wasm/tree/main/k3d/REEADME.md
https://github.com/Taction/containerd-shim-wasm