You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Pulumi在Azure配置Traefik容器卷挂载失败求助

问题原因与修复方案

我一眼就发现了问题所在——你在Traefik容器的配置里用错了字段名!在Pulumi的Azure NextGen SDK中,容器的卷挂载配置字段应该是**volume_mounts**(蛇形命名,全小写加下划线),而不是你写的VolumeMount(大驼峰命名)。这个字段名不匹配会导致Azure直接忽略你的卷挂载配置,所以控制台里才会显示容器没连接任何卷。

另外还有个小细节:你最后导出变量时用了错误的资源名,data_group应该改成datahike_group,不然运行时会报变量未定义的错误。

下面是修正后的完整代码:

import pulumi
import pulumi_azure_nextgen as azure

data_rg = azure.resources.latest.ResourceGroup(
    "data-rg",
    resource_group_name="data-rg",
    location="West Europe"
)

datahike_group = azure.containerinstance.latest.ContainerGroup(
    "data-group",
    location="West Europe",
    container_group_name="data-cg",
    resource_group_name=data_rg.name,
    containers=[{
        "name":"data",
        "image": "wordpress:latest",
        "resources": {
            "requests": {
                "cpu": 0.5,
                "memory_in_gb": 1.5
            }
        },
    }, {
        "name": "proxy",
        "image": "traefik:latest",
        "resources": {
            "requests": {
                "cpu": 0.5,
                "memory_in_gb": 1.5
            }
        },
        "ports": [{
            "port": 80,
            "protocol": "TCP",
        }],
        # 核心修正:将错误的VolumeMount改为正确的volume_mounts
        "volume_mounts": [{
            "mount_path": "/etc/traefik/config_base.yml",
            "name": "traefik-volume",
        }],
        "environment_variables": [{
            "name": "TRAEFIK_CONFIG_FILE",
            "value": "file"
        },{
            "name": "TRAEFIK_CONFIG_PATH",
            "value": "/etc/traefik/config_base.yml"
        } ],
    }],
    ip_address={
        "dnsNameLabel": "dnsnamelabel1",
        "ports": [{
            "port": 80,
            "protocol": "TCP",
        }],
        "type": "Public",
    },
    volumes=[
        {
            "emptyDir": {},
            "name": "datahike-volume",
        },
        {
            "name": "traefik-volume",
            "secret": {
                "secretKey1": "SecretValue1InBase64",
            },
        },
    ],
    os_type="Linux",
    tags={
        "environment": "testing",
    }
)

# 修正:变量名从data_group改为datahike_group
pulumi.export("data_ip", datahike_group.ip_address)

额外说明

  1. Pulumi的Azure NextGen SDK严格遵循Python的蛇形命名规范,所有配置字段都是小写加下划线,和Azure原生REST API的驼峰命名不同,这是新手很容易踩的坑。
  2. 你用secret卷挂载到单个文件的写法是对的,只要确保secret的内容是Base64编码的即可,这点可以继续保持。

内容的提问来源于stack exchange,提问作者elMor3no

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 09:22:37