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
2
Answers
@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
aks.bicep
appinsights.biecp (unchanged)
loganalytics.bicep
azure-pipelines.yml
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:
Deployment:
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