
MAX Serve 云上部署实战解读 mojo 仓库 cloud-configs 的 AWS / Azure / GCP 基础设施即代码模板【免费下载链接】mojoThe Modular Platform (includes MAX Mojo)项目地址: https://gitcode.com/GitHub_Trending/mo/mojoMAXModular MAX是基于 Modular 平台的高性能推理服务框架而本仓库The Modular Platform包含 MAX 与 Mojo在 max/examples/cloud-configs 目录下为“在云端部署 Llama 3 并运行 MAX Serve”提供了全套基础设施即代码IaC模板与配套监控脚本。本文以该目录为线索逐份剖析 AWS CloudFormation、Azure ARM、GCP Deployment Manager 三类模板的完整配置并结合仓库内配套教程 docs/max/serve/local-to-cloud.mdx 还原从创建资源、等待就绪、获取公网 IP、健康检查到清理资源的全流程帮助读者直接复用这些模板在自己的云账号中拉起一个可对外提供 OpenAI 兼容接口的 GPU 推理服务。cloud-configs 目录能做什么max/examples/cloud-configs/的职责很聚焦为 MAX Serve 的云上部署提供声明式配置文件。目录内为 AWS、AzureNVIDIA 与 AMD 两套、GCP 各准备了一套模板和一个notify.sh启动监控脚本结构如下max/examples/cloud-configs/ ├── aws │ ├── max-nvidia-aws.yaml # AWS CloudFormation 模板 │ └── notify.sh # CloudWatch 日志监控脚本 ├── azure │ ├── amd │ │ ├── max-amd-azure.json # Azure ARM 模板AMD MI300X / ROCm │ │ └── notify.sh │ └── nvidia │ ├── max-nvidia-azure.json# Azure ARM 模板NVIDIA A10 / CUDA │ └── notify.sh └── gcp ├── max-nvidia-gcp.jinja # GCP Deployment Manager 模板 └── notify.sh这些文件对应仓库中“Deploy Llama 3 on GPU with MAX Serve”的实战教程教程全文见 docs/max/serve/local-to-cloud.mdx。三套模板遵循完全一致的设计思路创建一个带 GPU 的虚拟机EC2 / Azure VM / GCE Instance在启动脚本中依次安装 Docker 与 GPU 容器运行时NVIDIA Container Toolkit 或 ROCm 设备直通拉取官方镜像modular/max-nvidia-fullAMD 场景为modular/max-amd以-p 80:8000暴露端口把容器内的 MAX Serve 端口 8000 映射到宿主机的 80通过环境变量HF_TOKEN传入 Hugging Face 访问令牌加载默认模型modularai/Llama-3.1-8B-Instruct-GGUF由notify.sh轮询日志与/v1/health健康接口直到服务就绪。由于四个云模板在“容器运行参数”上高度一致可以先理解这一公共内核再分别看各家 IaC 语法差异。部署前的统一准备按教程 docs/max/serve/local-to-cloud.mdx 的说明动手前需要在 Hugging Face 上申请一个具备模型读取权限的 Access Token即HF_TOKEN例如用于下载modularai/Llama-3.1-8B-Instruct-GGUF完成对应云厂商 CLI 的登录AWS配置awsCLI 凭证GCPaz init不需要GCP 侧用gcloud并先gcloud auth loginAzureaz init初始化后执行az login登录。说明教程原文以git clone的方式获取该目录在只读仓库环境中读者直接从本仓库的max/examples/cloud-configs/路径取用同名文件即可文件内容一致。另外教程特别提示堆栈/部署的创建需要一段时间且各云厂商完成时间不同创建多个环境时堆栈名与部署名必须保持唯一。AWSCloudFormation 模板深度解析AWS 模板 aws/max-nvidia-aws.yaml 使用 CloudFormationAWSTemplateFormatVersion: 2010-09-09声明全部资源。参数Parameters参数类型默认值说明InstanceTypeStringg5.4xlargeEC2 实例类型模板限定AllowedValues仅允许g5.4xlargeNVIDIA A10G GPUAmiIdStringami-02769e6d1f6a88067Deep Learning Base OSS Nvidia Driver AMIAmazon Linux 2us-east-1预装 NVIDIA 驱动HuggingFaceHubTokenString无Hugging Face Hub API TokenNoEcho: true保证不回显HuggingFaceRepoIdStringmodularai/Llama-3.1-8B-Instruct-GGUF要部署的模型仓库 ID资源Resources模板共声明 5 类资源IAM 角色与实例配置文件MaxServeInstanceRole/MaxServeInstanceProfile允许ec2.amazonaws.com服务担任该角色挂载三个 AWS 托管策略AmazonEC2ContainerRegistryReadOnly拉取 ECR 镜像、AmazonSSMManagedInstanceCoreSSM 会话管理与CloudWatchAgentServerPolicy另附内联策略CloudWatchLogsAccess仅授予对日志组arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/ec2/${AWS::StackName}-logs:*的logs:CreateLogStream/logs:PutLogEvents/logs:DescribeLogStreams权限。日志组MaxServeLogGroup日志组名/aws/ec2/${AWS::StackName}-logsRetentionInDays: 1日志只保留 1 天并设置了DeletionPolicy: Delete。安全组MaxServeSecurityGroup入站放行 TCP 80HTTP 服务与 TCP 22SSH源为0.0.0.0/0。EC2 实例MaxServeInstance挂载 100 GBgp3根卷DeleteOnTermination: trueUserData内嵌了完整的引导脚本。输出OutputsInstanceId与PublicDNS供后续获取公网地址。UserData 引导脚本的执行阶段UserData是整个 AWS 部署的核心按阶段执行阶段一日志与监控前置。安装amazon-cloudwatch-agent创建/var/log/max-serve目录与container.log文件写入 CloudWatch agent 配置JSONmetrics_collection_interval: 60、force_flush_interval: 15采集三个日志源/var/log/messages、/var/log/max-serve/container.log、/var/log/user-data.log统一汇入日志组/aws/ec2/${AWS::StackName}-logs的instance-logs日志流随后启动并systemctl enable amazon-cloudwatch-agent。阶段二Docker 与 NVIDIA 运行时。yum install -y docker aws-cfn-bootstrap启动 Docker再通过 NVIDIA 官方 yum 源安装nvidia-docker2重启 Docker并用nvidia-smi与docker info | grep -i nvidia验证 GPU 可见。阶段三拉取并运行 MAX 容器。关键命令如下模板原文CONTAINER_ID$(sudo docker run -d \ --env HF_TOKEN${HuggingFaceHubToken} \ --env HF_HUB_ENABLE_HF_TRANSFER1 \ -v /home/ec2-user/.cache/huggingface:/root/.cache/huggingface \ --gpus 1 \ -p 80:8000 \ --ipchost \ modular/max-nvidia-full:latest \ --model ${HuggingFaceRepoId})其中HF_HUB_ENABLE_HF_TRANSFER1启用 HF 的高速下载--ipchost与 MAX Serve 的共享内存需求相关docker pull或docker run失败时会调用cfn-signal -e 1向 CloudFormation 报告失败并退出。最后用docker logs -f $CONTAINER_ID /var/log/max-serve/container.log 21 把容器日志持续写入 CloudWatch 采集的文件。AWS 部署命令序列cd aws export REGIONus-east-1 # 示例区域 export STACK_NAMEmax-serve-stack aws cloudformation create-stack --stack-name ${STACK_NAME} \ --template-body file://max-nvidia-aws.yaml \ --parameters \ ParameterKeyInstanceType,ParameterValueg5.4xlarge \ ParameterKeyHuggingFaceHubToken,ParameterValue${HF_TOKEN} \ ParameterKeyHuggingFaceRepoId,ParameterValuemodularai/Llama-3.1-8B-Instruct-GGUF \ --capabilities CAPABILITY_IAM \ --region $REGION注意模板创建了 IAM 角色因此create-stack必须携带--capabilities CAPABILITY_IAM。等待创建完成并获取公网信息aws cloudformation wait stack-create-complete --stack-name ${STACK_NAME} --region ${REGION} INSTANCE_ID$(aws cloudformation describe-stacks --stack-name ${STACK_NAME} \ --query Stacks[0].Outputs[?OutputKeyInstanceId].OutputValue \ --output text --region ${REGION}) PUBLIC_IP$(aws ec2 describe-instances --instance-ids ${INSTANCE_ID} \ --query Reservations[0].Instances[0].PublicIpAddress \ --output text --region ${REGION}) aws ec2 wait instance-running --instance-ids ${INSTANCE_ID} --region ${REGION}监控启动进度见下文 notify.sh 章节bash notify.sh ${REGION} ${STACK_NAME} ${PUBLIC_IP}AzureNVIDIAARM 模板与 NVIDIA AI Enterprise 镜像Azure NVIDIA 模板 azure/nvidia/max-nvidia-azure.json 是标准 ARM 部署模板schema2019-04-01一次性声明虚拟网络、公网 IP、网络安全组、网卡、虚拟机与 Custom Script 扩展。参数与资源要点参数默认值说明adminUsername/adminPassword无管理员账号密码为securestringvmSizeStandard_NV36ads_A10_v5带 NVIDIA A10 GPU 的虚拟机规格osDiskSizeGB128系统盘大小GBvnetAddressPrefix/subnetAddressPrefix10.0.0.0/16/10.0.0.0/24虚拟网络与子网地址空间startupScript无Base64 编码的启动脚本部署时由命令行生成locationwestus3资源所在区域资源链为maxServeVNet含maxServeSubnet→maxServePublicIPDynamic 分配→maxServeNSG三条规则allowHTTP优先级 100 放行 80 端口、allowSSH优先级 200 放行 22 端口、allowOutbound优先级 300 放行全部出站→maxServeNIC→maxServeVM。VM 的关键差异点在于使用了 NVIDIA AI Enterprise 市场镜像plan: { name: nvaie_gpu_1_gen2, publisher: nvidia, product: nvidia-ai-enterprise }, storageProfile: { imageReference: { publisher: nvidia, offer: nvidia-ai-enterprise, sku: nvaie_gpu_1_gen2, version: 24.07.03 } }最后一个资源是maxServeVM/customScriptExtensionMicrosoft.Azure.Extensions/CustomScript/ 2.1通过settings.script接收 Base64 编码的startupScript在 VM 创建后执行。Azure 部署命令序列cd azure/nvidia export REGIONwestus3 export RESOURCE_GROUP_NAMEmaxServeResourceGroup export DEPLOYMENT_NAMEmaxServeDeployment az group create --name ${RESOURCE_GROUP_NAME} --location ${REGION} az group show -n ${RESOURCE_GROUP_NAME} --query properties.provisioningState -o tsv # 生成并 Base64 编码启动脚本 STARTUP_SCRIPT#!/bin/bash sudo usermod -aG docker $USER sudo systemctl restart docker sleep 10 HF_TOKEN$1 HUGGING_FACE_REPO_ID${2:-modularai/Llama-3.1-8B-Instruct-GGUF} sudo docker run -d \ --restart unless-stopped \ --env HF_TOKEN${HF_TOKEN} \ -v ~/.cache/huggingface:/root/.cache/huggingface \ -v ~/.cache/max_cache:/opt/venv/share/max/.max_cache \ --gpus 1 \ -p 80:8000 \ --ipchost \ modular/max-nvidia-full:latest \ --model ${HUGGING_FACE_REPO_ID} export STARTUP_SCRIPT$(echo $STARTUP_SCRIPT | base64)与 AWS 模板相比Azure 的启动脚本还额外挂载了~/.cache/max_cache:/opt/venv/share/max/.max_cache目录用于持久化 MAX 的编译缓存并加了--restart unless-stopped保证容器异常退出后自动拉起。由于使用 NVIDIA AI Enterprise 市场镜像首次部署可能需要先接受镜像条款az vm image terms accept --urn nvidia:nvidia-ai-enterprise:nvaie_gpu_1_gen2:latest随后创建部署export VM_PASSWORDYOUR-SECURE-PASSWORD-123 # 换成自己的强密码用于后续 ssh az deployment group create \ --name ${DEPLOYMENT_NAME} \ --resource-group ${RESOURCE_GROUP_NAME} \ --template-file max-nvidia-azure.json \ --parameters \ adminUsernameazureuser \ adminPassword${VM_PASSWORD} \ vmSizeStandard_NV36ads_A10_v5 \ osDiskSizeGB128 \ vnetAddressPrefix10.0.0.0/16 \ subnetAddressPrefix10.0.0.0/24 \ startupScript${STARTUP_SCRIPT} \ location${REGION}等待与取公网 IPaz deployment group wait --name ${DEPLOYMENT_NAME} --resource-group ${RESOURCE_GROUP_NAME} --created PUBLIC_IP$(az network public-ip show \ --resource-group ${RESOURCE_GROUP_NAME} --name maxServePublicIP \ --query ipAddress -o tsv)AzureAMDMI300X / ROCm 场景Azure 是唯一提供 AMD GPU 云部署配置的厂商。azure/amd/max-amd-azure.json 与 NVIDIA 版结构完全一致差异集中在三处默认 VM 规格为Standard_ND96isr_MI300X_v5AMD MI300X系统盘默认256GB默认区域westus镜像换为 Azure DSVM 的 ROCm 版 Ubuntu HPC 镜像publisher: microsoft-dsvm、offer: ubuntu-hpc、sku: 2204-rocm、version: 22.04.2025030701即预装 ROCm 驱动的 Ubuntu 22.04启动脚本中的容器替换为modular/max-amd:latest并通过 ROCm 设备直通暴露 GPUsudo docker run -d \ --restart unless-stopped \ --env HF_TOKEN${HF_TOKEN} \ -v ~/.cache/huggingface:/root/.cache/huggingface \ -v ~/.cache/max_cache:/opt/venv/share/max/.max_cache \ -p 80:8000 \ --ipchost \ --device /dev/kfd \ --device /dev/dri \ modular/max-amd:latest \ --model ${HUGGING_FACE_REPO_ID}注意 AMD 版启动脚本中没有--gpus 1而是把 ROCm 设备文件/dev/kfd与/dev/dri直接传入容器--device直通模式。部署命令与 NVIDIA 版相同仅把--template-file max-amd-azure.json、vmSizeStandard_ND96isr_MI300X_v5、osDiskSizeGB256换成 AMD 参数即可。教程在 docs/max/serve/local-to-cloud.mdx 中也明确提示需要部署 AMD GPU 时使用azure/amd/max-amd-azure.json。GCPDeployment Manager Jinja 模板GCP 模板 gcp/max-nvidia-gcp.jinja 使用 Cloud Deployment Manager 的 Jinja 语法所有可变项都通过properties注入。模板参数properties属性部署示例值作用instanceNamemax-serve-instance实例名zoneus-east1-d可用区machineTypeg2-standard-8带 NVIDIA L4 的机器类型acceleratorTypenvidia-l4GPU 加速器类型acceleratorCount1GPU 数量sourceImagecommon-cu123-v20240922-ubuntu-2204-py310Deep Learning 系列镜像CUDA 12.3 / Ubuntu 22.04 / Python 3.10huggingFaceHubToken/huggingFaceRepoId${HF_TOKEN}/modularai/Llama-3.1-8B-Instruct-GGUF模型访问与模型 ID实例与网络配置实例引用projects/deeplearning-platform-release/global/images/${sourceImage}启动镜像100 GB 启动盘autoDelete: true网络使用默认 VPC配ONE_TO_ONE_NAT公网访问服务账号使用defaultscope 为cloud-platform调度策略preemptible: false、onHostMaintenance: TERMINATEGPU 实例禁用热迁移、automaticRestart: true防火墙规则内联在模板中allow-http源0.0.0.0/0、目标标签http-server、放行 TCP 80模板 outputs 输出instanceName与instancePublicIPnatIP。启动脚本metadatastartup-script的流程是安装google-fluentd日志代理并启用 Stackdriver/Cloud Logging → 若/opt/google/cuda-installer不存在则调用 Deep Learning 镜像自带的/opt/deeplearning/install-driver.sh装驱动 → 添加 Docker 官方源与 NVIDIA Container Toolkit 源 → 安装docker-ce与nvidia-container-toolkit→ 运行 MAX 容器参数与 AWS 版一致HF_TOKEN、HF_HUB_ENABLE_HF_TRANSFER1、--gpus 1、-p 80:8000、--ipc host、--model。GCP 部署命令序列GCP 需要先启用三个 APIcd gcp PROJECT_IDYOUR PROJECT ID export ZONEus-east1-d gcloud services enable deploymentmanager.googleapis.com --project${PROJECT_ID} \ gcloud services enable logging.googleapis.com --project${PROJECT_ID} \ gcloud services enable compute.googleapis.com --project${PROJECT_ID}创建部署export DEPLOYMENT_NAMEmax-serve-deployment export INSTANCE_NAMEmax-serve-instance gcloud deployment-manager deployments create ${DEPLOYMENT_NAME} \ --template max-nvidia-gcp.jinja \ --properties \ instanceName:${INSTANCE_NAME},\ zone:${ZONE},\ machineType:g2-standard-8,\ acceleratorType:nvidia-l4,\ acceleratorCount:1,\ sourceImage:common-cu123-v20240922-ubuntu-2204-py310,\ huggingFaceHubToken:${HF_TOKEN},\ huggingFaceRepoId:modularai/Llama-3.1-8B-Instruct-GGUF \ --project ${PROJECT_ID}等待部署完成、给实例打上http-server标签并取公网 IPgcloud deployment-manager deployments describe ${DEPLOYMENT_NAME} --project${PROJECT_ID} EXISTING_RULE$(gcloud compute firewall-rules list --filternameallow-http \ --formatvalue(name) --project${PROJECT_ID}) if [ -z $EXISTING_RULE ]; then gcloud compute firewall-rules create allow-http \ --allow tcp:80 --source-ranges 0.0.0.0/0 --target-tags http-server \ --description Allow HTTP traffic on port 80 --project${PROJECT_ID} fi gcloud compute instances add-tags ${INSTANCE_NAME} \ --project${PROJECT_ID} --zone ${ZONE} --tags http-server PUBLIC_IP$(gcloud compute instances describe ${INSTANCE_NAME} \ --zone ${ZONE} --formatget(networkInterfaces[0].accessConfigs[0].natIP) \ --project${PROJECT_ID})模板本身已经内联了allow-http防火墙规则教程中的手动建规则与打标签命令是为了覆盖“模板防火墙与实例标签绑定”的时序问题建议按教程顺序执行。notify.sh三家云厂商的启动监控脚本对比四个notify.sh脚本逻辑同构都是“轮询日志 探测健康接口”但日志获取通道因云而异值得逐一说明公共判定逻辑check_server_status三个脚本以及 Azure AMD 版共用一个三段式状态判定日志中出现Pulling from modular/max-nvidia-fullGCP 为Pulling from docker.modular.com/modular/max-nvidia-fullAMD 为Pulling from modular/max-amd→ 镜像仍在拉取curl -s -f http://${PUBLIC_IP}/v1/health返回成功 →服务器就绪日志出现Building/Compiling/Downloading weight files for→ 处于首次编译模型阶段。默认MAX_WAIT_MINUTES30每 60 秒轮询一次超时即退出退出码 1。各云日志获取方式平台脚本参数日志来源底层命令AWSREGION STACK_NAME PUBLIC_IPCloudWatch 日志组/aws/ec2/${STACK_NAME}-logs、日志流instance-logsaws logs get-log-events --limit 50GCPPROJECT_ID INSTANCE_NAME ZONE PUBLIC_IPCloud Logging按实例 ID 过滤gcloud logging read resource.typegce_instance AND resource.labels.instance_id... AND jsonPayload.message:* --limit10AzureRESOURCE_GROUP_NAME VM_PASSWORD PUBLIC_IPSSH 进 VM 拉取 Docker 日志或 Custom Script 日志sshpass sshdocker logs或cat /var/log/azure/custom-script/handler.log、/var/lib/waagent/custom-script/download/0/stdout、stderrAzure 脚本的 fetch_logs 会先尝试docker ps -q -f ancestormodular/max-nvidia-full:latestAMD 版为modular/max-amd:latest定位容器拿到容器 ID 后直接docker logs容器尚未运行时则回退读取 Azure 的 Custom Script 部署日志。Azure 版还需要本地安装sshpass以支持非交互式 SSH 密码认证。端到端验证健康检查与推理请求MAX Serve 容器就绪的标志是日志出现Server ready on http://0.0.0.0:8000notify.sh轮询的就是同一事实/v1/health通过即代表可对外服务。随后即可用公网 IP 发起 OpenAI 兼容的流式对话请求来自 docs/max/serve/local-to-cloud.mdxcurl -N http://$PUBLIC_IP/v1/chat/completions \ -H Content-Type: application/json \ -d { model: modularai/Llama-3.1-8B-Instruct-GGUF, stream: true, messages: [ {role: system, content: You are a helpful assistant.}, {role: user, content: Who won the World Series in 2020?} ] } | grep -o content:[^]* | sed s/content://g | sed s///g | tr -d \n | sed s/\\n/\n/g实践提醒教程原文服务启动后云厂商公网 IP 生效可能存在短暂延迟若首次请求报错等待约一分钟再重试。拿到公网 IP 后还可以用 MAX 自带的基准测试能力评估吞吐、时延与 GPU 利用率参见仓库 docs/max/serve/benchmark.mdx 相关说明。资源清理与成本控制三套模板创建的都是一小时级成本不低的 GPU 资源教程明确强调“避免不必要成本而清理资源至关重要”清理命令如下平台命令AWSaws cloudformation delete-stack --stack-name ${STACK_NAME}随后aws cloudformation wait stack-delete-complete --stack-name ${STACK_NAME} --region ${REGION}GCPgcloud deployment-manager deployments delete ${DEPLOYMENT_NAME} --project${PROJECT_ID}Azureaz group delete --name ${RESOURCE_GROUP_NAME}删除资源组即级联删除全部资源成本构成方面教程归纳为四类GPU 计算实例如g5.4xlarge、g2-standard-8、Standard_NV36ads_A10_v5占大头、网络出入流量、系统盘等存储、以及日志监控等配套服务费用。优化建议包括非关键负载使用 Spot/抢占式实例、按需配置自动扩缩容、监控网络用量、设置成本告警与预算。模板本身也有成本友好的默认值——例如 AWS 日志组RetentionInDays: 1即 CloudWatch 日志只保留 1 天避免长期产生日志存储费用。总结三套模板的共性设计回顾整个 cloud-configs 目录其价值在于把“GPU 云主机 Docker NVIDIA/ROCm 运行时 MAX Serve 容器 日志监控 健康检查”这套可重复的部署流程固化为声明式模板让用户可以在三家主流云厂商之间低成本切换运行面统一全部采用官方镜像modular/max-nvidia-full/modular/max-amd端口映射统一为80:8000模型参数统一走--model鉴权统一走HF_TOKENIaC 语法各异AWS 用 CloudFormation YAMLAzure 用 ARM JSONCustom Script 接收 Base64 启动脚本GCP 用 Deployment Manager Jinjastartup-script 内联AMD 特例仅 Azure 提供 AMD 配置通过--device /dev/kfd --device /dev/dri直通 ROCm 设备监控脚本同构四个notify.sh共享同一套“日志关键词 /v1/health”判定逻辑仅日志通道不同。如需进一步理解 MAX Serve 的本地运行方式与 CLI 基准测试可继续阅读仓库内 docs/max/serve/recipes.mdx 与 docs/max/serve/benchmark.mdx并结合这些 IaC 模板搭建自己的云上推理服务。【免费下载链接】mojoThe Modular Platform (includes MAX Mojo)项目地址: https://gitcode.com/GitHub_Trending/mo/mojo创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考