WebAssembly 组件是生成和部署应用程序的新方法。 它们旨在成为编程语言的可移植编译目标,可在各种环境中运行。 WebAssembly 组件通过设计进行沙盒处理,并使用定义的接口公开其功能。 组件与容器不同,因为它们的单个二进制文件要小得多且启动速度更快。 查找模型的完整说明以及规范中的组件。
wasmCloud 是一个云原生计算基础(CNCF)项目,旨在快速跟踪 WebAssembly 组件的开发、部署和业务流程。 本文档详细说明了如何在 Azure Kubernetes 服务 (AKS) 群集上部署 wasmCloud 的说明。
在您开始之前
- 本文假定你对 Kubernetes 概念有基本的了解。 有关详细信息,请参阅 Azure Kubernetes 服务 (AKS) 的 Kubernetes 核心概念。
- 你需要一个有效的 Azure 订阅。 如果没有订阅,请在开始之前创建一个试用帐户。
- 需要一个 AKS 群集。 如果没有现有群集,可以使用 Azure CLI、 Azure PowerShell 或 Azure 门户创建一个群集。
- 需要在版本 0.39.0 或更高版本上安装 wasmCloud CLI(wash)。 有关安装选项,请参阅 wasmcloud-cli。
先决条件
- 无法使用 Azure 门户将 wasmCloud 部署到 AKS 群集。
部署 wasmCloud
若要在 AKS 群集上部署 wasmCloud,需要将所有 Helm 图表安装到群集中。 这会将以下服务安装到单个命名空间中:
- NATS:NATS 为 wasmCloud 中组件之间的所有通信提供主干。
- wadm:wasmCloud 应用程序管理器,也称为 wadm,是用于管理在 wasmCloud 上运行的应用程序的编排器。
- wasmCloud 操作器:wasmCloud 操作器是一个 Kubernetes 操作器,用于管理 wasmCloud 主机的生命周期。
建议将 wasmCloud 部署到专用 Kubernetes 命名空间。
使用
kubectl: 创建 wasmCloud 的命名空间:kubectl create namespace wasmcloud使用
helm upgrade以下命令将 NATS 和 wadm 安装到 wasmCloud 命名空间中:# By default, the chart installs NATS, Wadm, and wasmCloud Operator subcharts helm upgrade --install \ wasmcloud-platform \ --values https://raw.githubusercontent.com/wasmCloud/wasmcloud/main/charts/wasmcloud-platform/values.yaml \ oci://ghcr.io/wasmcloud/charts/wasmcloud-platform:0.1.2 \ --dependency-update此命令的输出应如下所示:
NAME: wasmcloud-platform LAST DEPLOYED: Thu Nov 7 14:58:54 2024 NAMESPACE: default STATUS: deployed REVISION: 1 NOTES:请等待所有 Pod 开始使用
kubectl:kubectl rollout status deploy,sts -l app.kubernetes.io/name=nats kubectl wait --for=condition=available --timeout=600s deploy -l app.kubernetes.io/name=wadm kubectl wait --for=condition=available --timeout=600s deploy -l app.kubernetes.io/name=wasmcloud-operator使用 CRD 启动 wasmCloud 主机,并且
kubectl:cat << EOF | kubectl apply -f - apiVersion: k8s.wasmcloud.dev/v1alpha1 kind: WasmCloudHostConfig metadata: name: wasmcloud-host spec: lattice: default version: "1.4.1" EOF验证 wasmCloud 主机是否正在运行:
kubectl get pod -l app.kubernetes.io/instance=wasmcloud-host输出应如下所示:
NAME READY STATUS RESTARTS AGE wasmcloud-host-f9d67b8cf-lvsq6 2/2 Running 0 84s使用
wash验证是否可以连接到 wasmCloud 主机:在单独的 shell 中,运行以下命令,将端口转发到正在运行的 NATS Pod 之一:
kubectl port-forward nats-0 4222此命令会将端口 4222 上的所有流量本地转发到群集中运行的 NATS Pod。 必须转发此端口才能使用
wash连接到 wasmCloud。在原始 shell 中,运行以下命令
wash,验证 wasmCloud 主机是否正在运行:wash get hosts输出应如下所示:
Host ID Friendly name Uptime (seconds) ND2G4FRXLBCV3YL52OD4NRSS66Z5YOR3JOSL3Q7T5I6ZJM4EII3Y73CZ frosty-resonance-6227 312
部署 wasmCloud 应用程序
现在,wasmCloud 已在 AKS 群集上运行,可以部署 wasmCloud 应用程序。 wasmCloud 应用程序是由 wasmCloud 应用程序管理器(wadm)协调的 WebAssembly 组件的集合。
创建 wasmCloud 应用程序清单文件。 此文件描述构成应用程序的组件。 将以下 yaml 文件另存为
hello-world.yaml。apiVersion: core.oam.dev/v1beta1 kind: Application metadata: name: hello-world annotations: description: 'HTTP hello world demo in Rust, using the WebAssembly Component Model and WebAssembly Interfaces Types (WIT)' spec: components: - name: http-component type: component properties: image: ghcr.io/wasmcloud/components/http-hello-world-rust:0.1.0 traits: # Govern the spread/scheduling of the component - type: spreadscaler properties: instances: 1 # Add a capability provider that enables HTTP access - name: httpserver type: capability properties: image: ghcr.io/wasmcloud/http-server:0.23.2 traits: # Establish a unidirectional link from this http server provider (the "source") # to the `http-component` component (the "target") so the component can handle incoming HTTP requests, # # The source (this provider) is configured such that the HTTP server listens on 0.0.0.0:8080 - type: link properties: target: http-component namespace: wasi package: http interfaces: [incoming-handler] source_config: - name: default-http properties: address: 0.0.0.0:8080使用
wash部署 wasmCloud 应用程序:wash app deploy hello-world.yaml输出应如下所示:
Deployed application "hello-world", version "01JC44TVDBC2V6MJ1NJTKNNX1J"验证应用程序是否正在使用
wash:wash app status hello-world输出应如下所示:
Name Kind Status http_component SpreadScaler Deployed httpserver -(wasi:http)-> http_component LinkScaler Deployed httpserver SpreadScaler Deployed如有必要,请重复此命令,直到所有组件都处于
Deployed状态。使用
curl和kubectl port-forward调用 http 组件:在单独的 shell 中运行以下命令,将端口转发到
hello-world应用程序kubectl port-forward port-forward deployment/wasmcloud-host 8080此命令会将端口 8080 上的所有流量转发到群集中运行的 wasmCloud 主机。 必须转发此端口才能使用
curl连接到 wasmCloud。在原始 shell 中运行以下命令以调用 http 组件:
curl http://localhost:8080输出应如下所示:
Hello from Rust!清理已安装的资源
若要清理创建的资源,请运行以下命令:
kubectl delete namespace wasmcloud此命令从 AKS 群集中删除 wasmCloud 组件。
后续步骤
可以按照 wasmCloud 快速入门指南继续学习,该指南指导你从头开始创建 WebAssembly 组件、使用不同的 wasmCloud 功能和缩放应用程序。