Si vous souhaitez mettre en place Grafana et Prometheus via Terraform sur votre cluster AKS, voici les templates que je vous invite à utiliser :
# Deploy AKS Prometheus
resource « azapi_resource » « aks_prometheus » {
type = « microsoft.monitor/accounts@2021-06-03-preview »
name = « prom${var.application_name}${var.application_env} »
parent_id = data.azurerm_resource_group.resourcegroup.id
location = data.azurerm_resource_group.resourcegroup.location
response_export_values = [« * »]
}
# Deploy data collection endpoint
resource « azapi_resource » « dataCollectionEndpoint » {
type = « Microsoft.Insights/dataCollectionEndpoints@2021-09-01-preview »
name = « MSProm${azurerm_kubernetes_cluster.aks_cluster.name} »
parent_id = data.azurerm_resource_group.resourcegroup.id
location = data.azurerm_resource_group.resourcegroup.location
body = jsonencode({
kind = « Linux »
properties = {}
})
}
#Deploy Data Collection Rule
resource « azapi_resource » « dataCollectionRule » {
schema_validation_enabled = false
type = « Microsoft.Insights/dataCollectionRules@2021-09-01-preview »
name = « MSProm${azurerm_kubernetes_cluster.aks_cluster.name} »
parent_id = data.azurerm_resource_group.resourcegroup.id
location = data.azurerm_resource_group.resourcegroup.location
body = jsonencode({
kind = « Linux »
properties = {
dataCollectionEndpointId = azapi_resource.dataCollectionEndpoint.id
dataFlows = [
{
destinations = [« MonitoringAccount1 »]
streams = [« Microsoft-PrometheusMetrics »]
}
]
dataSources = {
prometheusForwarder = [
{
name = « PrometheusDataSource »
streams = [« Microsoft-PrometheusMetrics »]
labelIncludeFilter = {}
}
]
}
destinations = {
monitoringAccounts = [
{
accountResourceId = azapi_resource.aks_prometheus.id
name = « MonitoringAccount1 »
}
]
}
}
})
}
#Deploy Rule Association
resource « azapi_resource » « dataCollectionRuleAssociation » {
schema_validation_enabled = false
type = « Microsoft.Insights/dataCollectionRuleAssociations@2021-09-01-preview »
name = « MSProm${azurerm_kubernetes_cluster.aks_cluster.name} »
parent_id = azurerm_kubernetes_cluster.aks_cluster.id
body = jsonencode({
scope = azurerm_kubernetes_cluster.aks_cluster.id
properties = {
dataCollectionRuleId = azapi_resource.dataCollectionRule.id
}
})
}
#Deploy Prometheus
resource « azapi_resource » « prometheusRuleGroup » {
type = « Microsoft.AlertsManagement/prometheusRuleGroups@2021-07-22-preview »
name = « ruleGroup${var.application_name}${var.application_env} »
parent_id = data.azurerm_resource_group.resourcegroup.id
location = data.azurerm_resource_group.resourcegroup.location
body = jsonencode({
properties = {
description = « Prometheus Rule Group »
scopes = [azapi_resource.aks_prometheus.id]
enabled = true
clusterName = azurerm_kubernetes_cluster.aks_cluster.name
interval = « PT1M »
rules = [
{
record = « instance:node_cpu_utilisation:rate5m »
expression = « 1 – avg without (cpu) (sum without (mode)(rate(node_cpu_seconds_total{job=\ »node\ », mode=~\ »idle|iowait|steal\ »}[5m]))) »
labels = {
workload_type = « job »
}
enabled = true
},
{
record = « node_namespace_pod_container:container_cpu_usage_seconds_total:sum_irate »
expression = « sum by (cluster, namespace, pod, container) ( irate(container_cpu_usage_seconds_total{job=\ »cadvisor\ », image!=\ »\ »}[5m])) * on (cluster, namespace, pod) group_left(node) topk by (cluster, namespace, pod) ( 1, max by(cluster, namespace, pod, node) (kube_pod_info{node!=\ »\ »})) »
labels = {
workload_type = « job »
}
enabled = true
}
]
}
})
}
#Deploy Grafana
resource « azapi_resource » « azgrafana » {
type = « Microsoft.Dashboard/grafana@2022-08-01 »
name = « grafaks${var.application_name}${var.application_env} »
parent_id = data.azurerm_resource_group.resourcegroup.id
location = data.azurerm_resource_group.resourcegroup.location
identity {
type = « SystemAssigned »
}
body = jsonencode({
sku = {
name = « Standard »
}
properties = {
publicNetworkAccess = « Enabled »,
zoneRedundancy = « Enabled »,
apiKey = « Enabled »,
deterministicOutboundIP = « Enabled »
}
})
}