如何从 Bicep AKS 配置中引用模块中创建的子网

huangapple go评论84阅读模式
英文:

How can I refer to a subnet created in a module from a bicep AKS configuration

问题

我正在尝试创建一个 Azure Kubernetes 服务 (Azure Kubernetes Service) 集群并将其连接到现有的虚拟网络。我需要使用 CNI 网络,因此需要一个预定义的子网。这个子网位于不同的资源组中。

我的解决方案是使用一个模块来创建子网,然后将其加载为现有资源:

  1. @description('拥有虚拟网络的资源组')
  2. param vnetRG string
  3. @description('将拥有子网的虚拟网络')
  4. param vNet string
  5. param location string = resourceGroup().location
  6. param otherParameter string
  7. param sshPubKey string
  8. module subnet 'subnet.bicep' = {
  9. name: '${deployment().name}-subnet'
  10. scope: resourceGroup(vnetRG)
  11. params: {
  12. virtualNetworkName: vNet
  13. }
  14. }
  15. resource subnetFromModule 'Microsoft.Network/virtualNetworks/subnets@2022-07-01' existing = {
  16. name: subnet.outputs.subnetName
  17. scope: resourceGroup(vnetRG)
  18. }
  19. // Azure Kubernetes Service 集群
  20. resource aks 'Microsoft.ContainerService/managedClusters@2022-05-02-preview' = {
  21. name: otherParameter
  22. location: location
  23. identity: {
  24. type: 'SystemAssigned'
  25. }
  26. properties: {
  27. dnsPrefix: 'dummy'
  28. publicNetworkAccess: 'Enabled'
  29. networkProfile: {
  30. networkPlugin: 'azure'
  31. }
  32. agentPoolProfiles: [
  33. {
  34. name: 'lnxnod'
  35. osDiskSizeGB: 60
  36. count: 3
  37. vmSize: 'Standard_D2s_v3'
  38. osType: 'Linux'
  39. mode: 'System'
  40. vnetSubnetID: subnetFromModule.id
  41. }
  42. ]
  43. linuxProfile: {
  44. adminUsername: otherParameter
  45. ssh: {
  46. publicKeys: [
  47. {
  48. keyData: sshPubKey
  49. }
  50. ]
  51. }
  52. }
  53. }
  54. }
  55. output subnetId string = subnetFromModule.id

但尝试应用此模板导致错误:InvalidTemplateDeploymentError - 在资源组 testclusterthing 中为容器服务 lsdkf 的部署提供资源失败。消息:在资源组 GLRclusterRG 中为容器服务 lsdkf 的部署提供资源失败。消息:部署模板验证失败:'vnetRG' 模板参数未找到。请参阅 https://aka.ms/arm-syntax-parameters 以获取使用详细信息。. 详细信息:。我做错了什么?

英文:

I am trying to create an Azure Kubernetes Service cluster to an existing virtual network. I need to use CNI networking, so I need a predefined subnet. The subnet is in a different resource group.

My solution was to use a module to create the subnet, then load it as an existing resource:

  1. @description('Resource group with the vNet')
  2. param vnetRG string
  3. @description('The vNet that will have the subnet')
  4. param vNet string
  5. param location string = resourceGroup().location
  6. param otherParameter string
  7. param sshPubKey string
  8. module subnet 'subnet.bicep' = {
  9. name: '${deployment().name}-subnet'
  10. scope: resourceGroup( vnetRG )
  11. params: {
  12. virtualNetworkName: vNet
  13. }
  14. }
  15. resource subnetFromModule 'Microsoft.Network/virtualNetworks/subnets@2022-07-01' existing = {
  16. name: subnet.outputs.subnetName
  17. scope: resourceGroup( vnetRG )
  18. }
  19. // The Azure Kubernetes Service cluster.
  20. resource aks 'Microsoft.ContainerService/managedClusters@2022-05-02-preview' = {
  21. name: otherParameter
  22. location: location
  23. identity: {
  24. type: 'SystemAssigned'
  25. }
  26. properties: {
  27. dnsPrefix: 'dummy'
  28. publicNetworkAccess: 'Enabled'
  29. networkProfile: {
  30. networkPlugin: 'azure'
  31. }
  32. agentPoolProfiles: [
  33. {
  34. name: 'lnxnod'
  35. osDiskSizeGB: 60
  36. count: 3
  37. vmSize: 'Standard_D2s_v3'
  38. osType: 'Linux'
  39. mode: 'System'
  40. vnetSubnetID: subnetFromModule.id
  41. }
  42. ]
  43. linuxProfile: {
  44. adminUsername: otherParameter
  45. ssh: {
  46. publicKeys: [
  47. {
  48. keyData: sshPubKey
  49. }
  50. ]
  51. }
  52. }
  53. }
  54. }
  55. output subnetId string = subnetFromModule.id

But trying to apply this template resulted in the error: InvalidTemplateDeploymentError - Provisioning of resource(s) for container service lsdkf in resource group testclusterthing failed. Message: Provisioning of resource(s) for container service lsdkf in resource group GLRclusterRG failed. Message: Deployment template validation failed: 'The template parameter 'vnetRG' is not found. Please see https://aka.ms/arm-syntax-parameters for usage details.'.. Details: . Details:. What am I doing wrong?

答案1

得分: 0

我找到了问题。一开始,我以为错误是来自于模块声明,因为在那里用字面值替换参数可以解决问题。但后来我尝试删除模块引用后面的所有内容,这也解决了问题。最后,我成功地将问题定位到了agentPoolProfiles中的vnetSubnetID: subnetFromModule.id。由于某种原因,在这里引用模块创建的现有资源失败了。

我通过不使用existing加载子网资源来解决了这个问题。相反,我将所有相关的元数据作为模块的output返回:

  1. @description('带有 vNet 的资源组')
  2. param vnetRG string
  3. @description('将拥有子网的 vNet')
  4. param vNet string
  5. param location string = resourceGroup().location
  6. param otherParameter string
  7. param sshPubKey string
  8. module subnet 'subnet.bicep' = {
  9. name: '${deployment().name}-subnet'
  10. scope: resourceGroup(vnetRG)
  11. params: {
  12. virtualNetworkName: vNet
  13. }
  14. }
  15. // Azure Kubernetes Service 集群。
  16. resource aks 'Microsoft.ContainerService/managedClusters@2022-05-02-preview' = {
  17. name: otherParameter
  18. location: location
  19. identity: {
  20. type: 'SystemAssigned'
  21. }
  22. properties: {
  23. dnsPrefix: 'dummy'
  24. publicNetworkAccess: 'Enabled'
  25. networkProfile: {
  26. networkPlugin: 'azure'
  27. }
  28. agentPoolProfiles: [
  29. {
  30. name: 'lnxnod'
  31. osDiskSizeGB: 60
  32. count: 3
  33. vmSize: 'Standard_D2s_v3'
  34. osType: 'Linux'
  35. mode: 'System'
  36. vnetSubnetID: subnet.outputs.subnetId
  37. }
  38. ]
  39. linuxProfile: {
  40. adminUsername: otherParameter
  41. ssh: {
  42. publicKeys: [
  43. {
  44. keyData: sshPubKey
  45. }
  46. ]
  47. }
  48. }
  49. }
  50. }
  51. output subnetId string = subnet.outputs.subnetId
英文:

I figured out the problem. At first I thought the error was coming from the module declaration, because replacing the parameter with a literal value there fixed the problem. But then I tried deleting everything after the module reference, and that fixed the issue as well. I was finally able to pin down the issue to vnetSubnetID: subnetFromModule.id in agentPoolProfiles. For some reason a reference to the existing resource created by the module failed here.

I worked around the issue by not using existing to load the subnet resource. Instead I just returned all the relevant metadata as outputs from the module:

  1. @description('Resource group with the vNet')
  2. param vnetRG string
  3. @description('The vNet that will have the subnet')
  4. param vNet string
  5. param location string = resourceGroup().location
  6. param otherParameter string
  7. param sshPubKey string
  8. module subnet 'subnet.bicep' = {
  9. name: '${deployment().name}-subnet'
  10. scope: resourceGroup( vnetRG )
  11. params: {
  12. virtualNetworkName: vNet
  13. }
  14. }
  15. // The Azure Kubernetes Service cluster.
  16. resource aks 'Microsoft.ContainerService/managedClusters@2022-05-02-preview' = {
  17. name: otherParameter
  18. location: location
  19. identity: {
  20. type: 'SystemAssigned'
  21. }
  22. properties: {
  23. dnsPrefix: 'dummy'
  24. publicNetworkAccess: 'Enabled'
  25. networkProfile: {
  26. networkPlugin: 'azure'
  27. }
  28. agentPoolProfiles: [
  29. {
  30. name: 'lnxnod'
  31. osDiskSizeGB: 60
  32. count: 3
  33. vmSize: 'Standard_D2s_v3'
  34. osType: 'Linux'
  35. mode: 'System'
  36. vnetSubnetID: subnet.outputs.subnetId
  37. }
  38. ]
  39. linuxProfile: {
  40. adminUsername: otherParameter
  41. ssh: {
  42. publicKeys: [
  43. {
  44. keyData: sshPubKey
  45. }
  46. ]
  47. }
  48. }
  49. }
  50. }
  51. output subnetId string = subnet.outputs.subnetId

huangapple
  • 本文由 发表于 2023年2月23日 22:33:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75546237.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定