skip to Main Content

I would like to configure AKS Monitor in Azure Kubernetes Service by setting the Cost Presets to Logs And Events using bicep.

I wrote the code below but it does not change the value of the cost preset. Data Collection Rules are correctly set but don’t reflect in AKS Monitoring. Am I missing something here?
The following is created:

  • Data Collection Rules (MSCI-westeurope-abc-aks-we-dev). Properly reflects changed values (Cost Presets) when looking up in the Azure Portal, however it does not reflect in Azure Kubernetes Service.
  • Data Collection Rules Association, deployment seems OK but does not seem to link between Data Collection Rule and AKS Monitoring.

To simulate do the following in the Azure Portal:

  • Create a Resource Group named: aks-dev

  • Create an Azure Kubernetes Service called abc-aks-we-dev (within the aks-dev resource group)

    In Azure DevOps:

  • Store the bicep code below in a repository called Deploy-Bicep

  • Add a new pipeline refering to azure-pipelines.yml

main.bicep

@description('Prefix')
param prefix string = 'abc'
@description('Environment')
param env string = 'dev'
@description('Location')
param loc string = resourceGroup().location

param skuName string = 'pergb2018'
param retentionInDays int = 30
param dailyQuotaGb int = 1

module logAnalytics 'loganalytics.bicep' = {
  name: 'log-analytics'
  params: {
    prefix: prefix
    env: env
    loc: loc
    retentionInDays: retentionInDays
    skuName: skuName
    dailyQuotaGb: dailyQuotaGb
  }
}

module appInsights 'appInsights.bicep' = {
  name: 'application-insights'
  params: {
    prefix: prefix
    env: env
    loc: loc
    retentionInDays: retentionInDays
    logAnalyticsId: logAnalytics.outputs.loganalyticsId
  }
}

loganalytics.bicep

@description('Prefix')
param prefix string = 'abc'
@description('Environment')
param env string = 'dev'
@description('Location')
param loc string = resourceGroup().location

param skuName string = 'pergb2018'
param retentionInDays int = 30
param dailyQuotaGb int = 2

var name = '${prefix}-log-we-${env}'

resource loganalytics 'Microsoft.OperationalInsights/workspaces@2020-10-01' = {
  name: name
  location: loc
  properties: {
    sku: {
      name: skuName
    }
    workspaceCapping: {
      dailyQuotaGb: dailyQuotaGb
    }
    retentionInDays: retentionInDays
  }

}

output loganalyticsId string = loganalytics.id

appInsights.bicep

@description('Prefix')
param prefix string = 'abc'
@description('Environment')
param env string = 'dev'
@description('Location')
param loc string = resourceGroup().location
param retentionInDays int = 1
@description('LogAnalytics Id')
param logAnalyticsId string

resource appInsights 'Microsoft.Insights/components@2020-02-02' = {
  name: '${prefix}-appi-we-${env}'
  kind: 'other'
  location: loc
  properties:{
    Application_Type:'other'
    RetentionInDays: retentionInDays
    IngestionMode: 'LogAnalytics'
    WorkspaceResourceId: logAnalyticsId
  }
}

resource appInsights_dataCollection 'Microsoft.Insights/dataCollectionRules@2022-06-01' = {
  properties: {
    dataSources: {
      extensions: [
        {
          streams: [
            'Microsoft-ContainerLog'
            'Microsoft-ContainerLogV2'
            'Microsoft-KubeEvents'
            'Microsoft-KubePodInventory'
          ]
          extensionName: 'ContainerInsights'
          extensionSettings: {
            dataCollectionSettings: {
              interval: '5m'
              namespaceFilteringMode: 'Off'
              enableContainerLogV2: true
            }
          }
          name: 'ContainerInsightsExtension'
        }
      ]
    }
    destinations: {
      logAnalytics: [
        {
          workspaceResourceId: resourceId('Microsoft.OperationalInsights/workspaces', '${prefix}-log-we-${env}')
          name: 'ciworkspace'
        }
      ]
    }
    dataFlows: [
      {
        streams: [
          'Microsoft-ContainerLog'
          'Microsoft-ContainerLogV2'
          'Microsoft-KubeEvents'
          'Microsoft-KubePodInventory'
        ]
        destinations: [
          'ciworkspace'
        ]
      }
    ]
  }
  location: loc
  kind: 'Linux'
  name: 'MSCI-${loc}-abc-aks-we-${env}'
}

resource managedCluster 'Microsoft.ContainerService/managedClusters@2022-02-01' existing = {
  name: '${prefix}-aks-we-${env}'
}

resource appInsights_dataCollectionRuleAssociations 'Microsoft.Insights/dataCollectionRuleAssociations@2022-06-01' = {
  name: 'ContainerInsightsExtension'
  scope: managedCluster
  properties: {
    dataCollectionRuleId: appInsights_dataCollection.id
    description: 'Association of data collection rule. Deleting this association will break the data collection for this AKS Cluster.'
  }
}
``

azure-pipelines.yml

trigger:
- main

pool:
  vmImage: 'ubuntu-latest'

stages:
- stage: Deploy
  jobs:
  - job: DeployInfrastructure
    steps:
    - checkout: self

    - task: AzureCLI@2
      inputs:
        azureSubscription: 'bicep-aks-test'
        scriptType: 'bash'
        scriptLocation: 'inlineScript'
        inlineScript: |
          az group create --name aks-dev --location westeurope

          az deployment group create 
            --resource-group aks-dev 
            --template-file main.bicep 

Monitor Settings cost presets are set to Standard

2

Answers


  1. Chosen as BEST ANSWER

    @Vinay B: Thank you for sharing the code. I've added the AKS Cluster to the code and it still doesn't work. If I manually change the Cost Presets it just jumps back to Standard. Any idea what is going on?

    Here is the full code:

    main.bicep

    @description('Prefix')
    param prefix string = 'abc'
    @description('Environment')
    param env string = 'dev'
    @description('Location')
    param loc string = resourceGroup().location
    
    param skuName string = 'pergb2018'
    param retentionInDays int = 30
    param dailyQuotaGb int = 1
    
    module kubernetes 'aks.bicep' = {
      name: 'kubernetes'
      params: {
        prefix: prefix
        env: env
        loc: 'northeurope'
        logAnalyticsId: logAnalytics.outputs.loganalyticsId
      }
    }
    
    module logAnalytics 'loganalytics.bicep' = {
      name: 'log-analytics'
      params: {
        prefix: prefix
        env: env
        loc: loc
        retentionInDays: retentionInDays
        skuName: skuName
        dailyQuotaGb: dailyQuotaGb
      }
    }
    
    module appInsights 'appInsights.bicep' = {
      name: 'application-insights'
      params: {
        prefix: prefix
        env: env
        loc: loc
        retentionInDays: retentionInDays
        logAnalyticsId: logAnalytics.outputs.loganalyticsId
        }
        dependsOn: [ 
          kubernetes
         ]
    }
    

    aks.bicep

    @description('Prefix')
    param prefix string = 'abc'
    @description('Environment')
    param env string = 'dev'
    @description('Location')
    param loc string = resourceGroup().location
    
    @description('Identifier of LogAnalytics')
    param logAnalyticsId string
    
    // @description('Ssh public key')
    // param aks_ssh_rsa_public_key string
    // param platform_admin_groupId string
    
    @description('Identifier of the managed identity')
    param identityId string = '/subscriptions/d90bd1e8-9407-4978-973e-da1d7af68ec6/resourceGroups/aks-dev/providers/Microsoft.ManagedIdentity/userAssignedIdentities/aks'
    
    @description('Tier of managed cluster SKU')
    var name = '${prefix}-aks-we-${env}'
    var aksVersion = '1.27.9'
    
    
    resource aks 'Microsoft.ContainerService/managedClusters@2021-10-01' = {
      name: name
      location: loc
      identity: {
        type: 'UserAssigned'
        userAssignedIdentities: {
          '${identityId}': {}
        }
      }
      properties: {
        kubernetesVersion: aksVersion
        agentPoolProfiles: [
          {
            name: 'nodepool1'
            count: 3
            vmSize: 'Standard_DS2_v2'
            osType: 'Linux'
            type: 'VirtualMachineScaleSets'
            mode: 'System'
          }
        ]
        enableRBAC: true
        dnsPrefix: 'aks'
        addonProfiles: {
          'omsagent': {
            enabled: true
            config: {
              logAnalyticsWorkspaceResourceID: logAnalyticsId
            }
          }
        }
      }
      sku: {
        name: 'Basic'
        tier: 'Free'
      }
    }
    

    appinsights.biecp (unchanged)

    @description('Prefix')
    param prefix string = 'abc'
    @description('Environment')
    param env string = 'dev'
    @description('Location')
    param loc string = resourceGroup().location
    param retentionInDays int = 1
    @description('LogAnalytics Id')
    param logAnalyticsId string
    
    resource appInsights 'Microsoft.Insights/components@2020-02-02' = {
      name: '${prefix}-appi-we-${env}'
      kind: 'other'
      location: loc
      properties:{
        Application_Type:'other'
        RetentionInDays: retentionInDays
        IngestionMode: 'LogAnalytics'
        WorkspaceResourceId: logAnalyticsId
      }
    }
    
    resource appInsights_dataCollection 'Microsoft.Insights/dataCollectionRules@2022-06-01' = {
      properties: {
        dataSources: {
          extensions: [
            {
              streams: [
                'Microsoft-ContainerLog'
                'Microsoft-ContainerLogV2'
                'Microsoft-KubeEvents'
                'Microsoft-KubePodInventory'
              ]
              extensionName: 'ContainerInsights'
              extensionSettings: {
                dataCollectionSettings: {
                  interval: '5m'
                  namespaceFilteringMode: 'Off'
                  enableContainerLogV2: true
                }
              }
              name: 'ContainerInsightsExtension'
            }
          ]
        }
        destinations: {
          logAnalytics: [
            {
              workspaceResourceId: logAnalyticsId
              name: 'ciworkspace'
            }
          ]
        }
        dataFlows: [
          {
            streams: [
              'Microsoft-ContainerLog'
              'Microsoft-ContainerLogV2'
              'Microsoft-KubeEvents'
              'Microsoft-KubePodInventory'
            ]
            destinations: [
              'ciworkspace'
            ]
          }
        ]
      }
      location: loc
      kind: 'Linux'
      name: 'MSCI-${loc}-abc-aks-we-${env}'
    }
    
    resource managedCluster 'Microsoft.ContainerService/managedClusters@2022-02-01' existing = {
      name: '${prefix}-aks-we-${env}'
    }
    
    resource appInsights_dataCollectionRuleAssociations 'Microsoft.Insights/dataCollectionRuleAssociations@2022-06-01' = {
      name: 'ContainerInsightsExtension'
      scope: managedCluster
      properties: {
        dataCollectionRuleId: appInsights_dataCollection.id
        description: 'Association of data collection rule. Deleting this association will break the data collection for this AKS Cluster.'
      }
    }
    

    loganalytics.bicep

    @description('Prefix')
    param prefix string = 'abc'
    @description('Environment')
    param env string = 'dev'
    @description('Location')
    param loc string = resourceGroup().location
    
    param skuName string = 'pergb2018'
    param retentionInDays int = 30
    param dailyQuotaGb int = 2
    
    var name = '${prefix}-log-we-${env}'
    
    resource loganalytics 'Microsoft.OperationalInsights/workspaces@2020-10-01' = {
      name: name
      location: loc
      properties: {
        sku: {
          name: skuName
        }
        workspaceCapping: {
          dailyQuotaGb: dailyQuotaGb
        }
        retentionInDays: retentionInDays
      }
    
    }
    
    output loganalyticsId string = loganalytics.id
    

    azure-pipelines.yml

    trigger:
    - main
    
    pool:
      vmImage: 'ubuntu-latest'
    
    stages:
    - stage: Deploy
      jobs:
      - job: DeployInfrastructure
        steps:
        - checkout: self
    
        - task: AzureCLI@2
          inputs:
            azureSubscription: 'aks-dev'
            scriptType: 'bash'
            scriptLocation: 'inlineScript'
            inlineScript: |
              az group create --name aks-dev --location westeurope
    
              az deployment group create 
                --resource-group aks-dev 
                --template-file main.bicep 
    

  2. Configure AKS Monitoring cost presets with data collection rule associations using bicep

    I tried an updated configuration for the where it successfully establishes the connection between DCR and cluster by making few changes in the configuration.

    Deployment:

    param location string = 'eastus'
    param clusterName string = 'vksbCluster'
    param kubernetesVersion string = '1.30.3'
    param logAnalyticsWorkspaceName string = 'mvksbWorkspace'
    param dcrName string = 'vskbDCR'
    
    var servicePrincipalClientId = 'xxxxxx-e29a-xxxx-xxxx-8016e579e93c'
    var servicePrincipalClientSecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    
    resource logAnalyticsWorkspace 'Microsoft.OperationalInsights/workspaces@2020-08-01' = {
      name: logAnalyticsWorkspaceName
      location: location
      properties: {
        retentionInDays: 30  
        workspaceCapping: {
          dailyQuotaGb: 5  
        }
      }
    }
    
    resource aksCluster 'Microsoft.ContainerService/managedClusters@2021-03-01' = {
      name: clusterName
      location: location
      properties: {
        kubernetesVersion: kubernetesVersion
        agentPoolProfiles: [
          {
            name: 'nodepool1'
            count: 3
            vmSize: 'Standard_DS2_v2'
            osType: 'Linux'
            type: 'VirtualMachineScaleSets'
            mode: 'System'
          }
        ]
        enableRBAC: true
        dnsPrefix: 'aks'
        addonProfiles: {
          omsagent: {
            enabled: true
            config: {
              logAnalyticsWorkspaceResourceID: logAnalyticsWorkspace.id
            }
          }
        }
        servicePrincipalProfile: {
          clientId: servicePrincipalClientId
          secret: servicePrincipalClientSecret
        }
      }
    }
    
    resource dataCollectionRule 'Microsoft.Insights/dataCollectionRules@2022-06-01' = {
      name: dcrName
      location: location
      properties: {
        dataSources: {
          extensions: [
            {
              streams: [
                'Microsoft-ContainerLog'
                'Microsoft-ContainerLogV2'
                'Microsoft-KubeEvents'
                'Microsoft-KubePodInventory'
              ]
              extensionName: 'ContainerInsights'
              extensionSettings: {
                dataCollectionSettings: {
                  interval: '5m'
                  namespaceFilteringMode: 'Off'
                  enableContainerLogV2: true
                }
              }
              name: 'ContainerInsightsExtension'
            }
          ]
        }
        destinations: {
          logAnalytics: [
            {
              workspaceResourceId: logAnalyticsWorkspace.id
              name: 'ciworkspace'
            }
          ]
        }
        dataFlows: [
          {
            streams: [
              'Microsoft-ContainerLog'
              'Microsoft-ContainerLogV2'
              'Microsoft-KubeEvents'
              'Microsoft-KubePodInventory'
            ]
            destinations: [
              'ciworkspace'
            ]
          }
        ]
      }
    }
    
    resource dcrAssociation 'Microsoft.Insights/dataCollectionRuleAssociations@2022-06-01' = {
      name: 'ContainerInsightsExtension'
      scope: aksCluster
      properties: {
        dataCollectionRuleId: dataCollectionRule.id
        description: 'Association of data collection rule with AKS.'
      }
    }
    

    Deployment:

    enter image description here

    enter image description here

    enter image description here

    Refer:

    Configure AKS Monitoring Cost Presets with Bicep (trycatchdebug.net)

    Microsoft.Insights/dataCollectionRuleAssociations 2022-06-01 – Bicep, ARM template & Terraform AzAPI reference | Microsoft Learn

    Microsoft.Insights/dataCollectionRules 2022-06-01 – Bicep, ARM template & Terraform AzAPI reference | Microsoft Learn

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search