Loading...
Loading...
Configures alerting policies in Terraform for Google Kubernetes Engine (GKE) clusters, workloads, and services using PromQL and Google Cloud Managed Service for Prometheus. Use when writing, analyzing, validating, or deploying Terraform alerting policies to monitor GKE service latency, traffic, error rates using Multi-Window Multi-Burn-Rate SLO alerts, memory saturation, and cluster health such as CrashLoopBackOff and Node NotReady conditions. Don't use for non-GKE compute runtimes such as standalone Compute Engine VMs or standalone Cloud Run services without GKE.
npx skill4agent add google/skills gke-alert-configurationcompute.googleapis.com/instance/cpu/utilizationrun.googleapis.com/request_latenciesgoogle_monitoring_alert_policycondition_thresholdkube-state-metricskube-state-metricskube_cronjob_*kube_pod_status_phasekube_persistentvolume_*kube_deployment_*kube_statefulset_*kube_job_*kube_daemonset_*kube-state-metricskube-state-metricsPodMonitoringmetricRelabelingaction: keep--metric-allowlistcontainer_memory_working_set_bytescontainer_spec_memory_limit_byteskube_pod_container_resource_limitscontainer_*kube_changes.jsonpython3 scripts/validate_config.py --plan changes.jsonalerts.tfabsent()default 0container_memory_working_set_bytescontainer_spec_memory_limit_bytescontainer_cpu_usage_seconds_totalcondition_prometheus_query_languagecondition_threshold.tfalerts.tfvariables.tfby (cluster, namespace, service, pod, container)project_idcluster_namenamespacevar.project_idvar.cluster_namevar.namespacevariables.tfincrease(...[15m]) > 3duration = "0s""60s"duration = "300s"[15m]duration = "300s"kube_node_status_condition == 0rate(5xx)/rate(total) > 0.05absent()default 0rate(...) == 0default 0sum(rate(...[5m])) default 0 == 0absent(...) == 1kubernetes.io/awesome-prometheus-alertsgoogle_monitoring_alert_policycondition_prometheus_query_languagevariables.tfproject_idcluster_namenamespace# variables.tf
variable "project_id" {
type = string
description = "Google Cloud Project ID"
}
variable "cluster_name" {
type = string
description = "GKE Cluster Name"
}
variable "namespace" {
type = string
description = "Target Kubernetes Namespace"
default = "default"
}
variable "slo_target" {
type = number
description = "SLO Target fraction (for example 0.99 for 99%)"
default = 0.99
}# alerts.tf
# Example: Multi-Window Multi-Burn-Rate (MWMBR) SLO Alert (Fast Burn: 14.4x, 1h & 5m windows)
resource "google_monitoring_alert_policy" "k8s_service_error_rate_slo" {
project = var.project_id
display_name = "[K8s] ${var.cluster_name} - Service Error Rate SLO Fast Burn"
combiner = "OR"
conditions {
display_name = "Error Budget Fast Burn (14.4x over 1h and 5m)"
condition_prometheus_query_language {
query = <<-EOT
(
(
sum(
rate(
http_requests_total{
cluster="${var.cluster_name}",
namespace="${var.namespace}",
status=~"5.."
}[5m]
)
) by (service, namespace, cluster)
/
sum(
rate(
http_requests_total{
cluster="${var.cluster_name}",
namespace="${var.namespace}"
}[5m]
)
) by (service, namespace, cluster)
) > (1 - ${var.slo_target}) * 14.4
)
and
(
(
sum(
rate(
http_requests_total{
cluster="${var.cluster_name}",
namespace="${var.namespace}",
status=~"5.."
}[1h]
)
) by (service, namespace, cluster)
/
sum(
rate(
http_requests_total{
cluster="${var.cluster_name}",
namespace="${var.namespace}"
}[1h]
)
) by (service, namespace, cluster)
) > (1 - ${var.slo_target}) * 14.4
)
EOT
duration = "0s"
}
}
}kubernetes.io/awesome-prometheus-alertsvalidate_config.pychanges.jsonpython3 scripts/validate_config.py --plan changes.jsonpython3 scripts/validate_config.py --directory [TARGET_TF_DIR] --cluster-var "${var.cluster_name}"python3 scripts/validate_config.py --file [PATH_TO_TF_FILE]duration = "300s"increase(...[15m])[15m]increase(...[15m]) > 3duration = "300s"duration = "0s""60s"duration = "300s"kube_node_status_condition == 0container_memory_working_set_bytescontainer_spec_memory_limit_bytescontainer_spec_memory_limit_bytesNaNcontainer_spec_memory_limit_bytesresources.limits.memoryNaNpredict_linearpredict_linear(kubelet_volume_stats_available_bytes[6h:5m], 4 * 24 * 3600) < 0predict_linearkubelet_volume_stats_available_bytesapiserver_request_totalrest_client_requests_totalapiserver_request_totalrest_client_requests_totalabsent()default 0http_requests_totalsum(rate(...[5m])) == 0sum(rate(...[5m])) default 0 == 0absent(...) == 1duration = "0s"kube_pod_container_status_restarts_total