Deploying Grafana and Prometheus on AKS using Terraform
Deploying Grafana and Prometheus on AKS using Terraform
Monitoring is a critical component of any Kubernetes deployment. In this guide, we will deploy Prometheus for monitoring and Grafana for visualization on an existing Azure Kubernetes Service (AKS) cluster using Terraform.
Prerequisites
Before we begin, ensure you have the following:
- An existing AKS cluster
- Terraform installed (version 1.0.0+)
- Azure CLI configured with appropriate permissions
- Helm installed (for manual verification if needed)
Project Structure
Organize your Terraform project as follows:
aks-monitoring/
├── main.tf
├── variables.tf
├── outputs.tf
└── terraform.tfvars
Provider Configuration
In main.tf
, configure the Terraform provider for Azure:
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.0"
}
}
}
provider "azurerm" {
features {}
}
Deploying Prometheus and Grafana via Helm
Add the Helm provider and configure the deployments.
provider "helm" {
kubernetes {
host = azurerm_kubernetes_cluster.aks.kube_config.0.host
client_certificate = base64decode(azurerm_kubernetes_cluster.aks.kube_config.0.client_certificate)
client_key = base64decode(azurerm_kubernetes_cluster.aks.kube_config.0.client_key)
cluster_ca_certificate = base64decode(azurerm_kubernetes_cluster.aks.kube_config.0.cluster_ca_certificate)
}
}
resource "helm_release" "prometheus" {
name = "prometheus"
repository = "https://prometheus-community.github.io/helm-charts"
chart = "prometheus"
namespace = "monitoring"
create_namespace = true
values = [<<EOF
alertmanager:
enabled: true
server:
persistentVolume:
enabled: true
EOF
]
}
resource "helm_release" "grafana" {
name = "grafana"
repository = "https://grafana.github.io/helm-charts"
chart = "grafana"
namespace = "monitoring"
depends_on = [helm_release.prometheus]
values = [<<EOF
persistence:
enabled: true
adminPassword: "SuperSecurePassword"
service:
type: LoadBalancer
EOF
]
}
Variables Configuration
Define variables for customization in variables.tf
:
variable "aks_cluster_name" {
type = string
description = "Name of the existing AKS cluster"
}
variable "resource_group_name" {
type = string
description = "Resource group where the AKS cluster is deployed"
}
Output Configuration
Expose the Grafana service URL and credentials in outputs.tf
:
output "grafana_dashboard_url" {
value = "http://${helm_release.grafana.name}.monitoring.svc.cluster.local"
}
output "grafana_admin_password" {
value = "SuperSecurePassword"
sensitive = true
}
Deploying the Monitoring Stack
- Initialize Terraform:
terraform init
- Configure
terraform.tfvars
:
aks_cluster_name = "my-aks-cluster"
resource_group_name = "my-resource-group"
- Plan and apply the configuration:
terraform plan
terraform apply
- Verify the installation:
kubectl get pods -n monitoring
Conclusion
This guide demonstrates how to deploy Grafana and Prometheus on an AKS cluster using Terraform and Helm. By automating the deployment, you ensure consistency and scalability in your monitoring setup.
For further customization, consider integrating Azure Monitor and setting up persistent storage for metrics retention.