GitOps with Grafana: Automate Observability as Code

Explore how GitOps enables DevOps engineers and SREs to manage Grafana dashboards, datasources, and alerts as code. Learn practical examples, repository patterns, and automation with Argo CD and the Grafana Operator.

GitOps with Grafana: Automate Observability as Code

Introduction

GitOps with Grafana is revolutionizing observability management for DevOps engineers and SREs. By treating monitoring as code, teams can automate deployments, gain auditability, and collaborate efficiently. This post details how to integrate GitOps with Grafana using Kubernetes, Argo CD, and the Grafana Operator, with actionable examples and code.

Why Adopt GitOps for Grafana?

  • Declarative configuration management: All dashboards, datasources, and alerts are versioned in Git, providing a single source of truth.
  • Continuous delivery: Changes are applied automatically to your infrastructure via tools like Argo CD or Flux.
  • Auditability and change history: Every update is tracked as a Git commit, enabling full audit trails.
  • Collaboration: Pull requests and code reviews improve reliability and reduce errors in monitoring configurations.

Core Components of Grafana GitOps

  • Kubernetes cluster: Hosts Grafana and related services.
  • Grafana Operator: Manages Grafana as Kubernetes Custom Resources (CRs).
  • Argo CD: Synchronizes desired state from Git to your cluster.
  • Git repository: Stores all configurations as code.

Example Git Repository Structure

multi-tenancy-gitops/
├── gitops-0-bootstrap/
│   ├── 0-bootstrap/
│   │   └── single-cluster/
│   │       ├── 1-infra/
│   │       └── 2-services/

This modular approach supports scalable management and multi-environment deployments.

Step-by-Step: Deploying Grafana with GitOps

1. Prepare Your Git Repository

Organize your repo for clarity. For example, under 2-services, store your Grafana Operator and instance manifests:

- argocd/operators/grafana-operator.yaml
- argocd/instances/grafana-instana.yaml

Commit and push changes. Argo CD will detect updates and synchronize your cluster automatically.

2. Deploy Grafana Operator and Instance

Declare the Grafana Operator and an instance using Kubernetes manifests. Example grafana-cloud.yaml:

apiVersion: grafana.integreatly.org/v1beta1
kind: Grafana
metadata:
  name: my-grafana-cloud
  namespace: grafana-operator
spec:
  external:
    url: https://my-stack.grafana.net/
  apiKey:
    name: grafana-cloud-credentials
    key: GRAFANA_CLOUD_INSTANCE_TOKEN

Store your API token securely in a Kubernetes Secret, referenced by the apiKey field.

3. Declarative Management of Dashboards and Datasources

With the operator running, manage dashboards and datasources via custom resources. Place dashboard JSON definitions in a dashboards folder in your repo and reference them in your CRD manifests. Argo CD ensures your cluster state matches Git.

apiVersion: integreatly.org/v1alpha1
kind: GrafanaDashboard
metadata:
  name: node-exporter-dashboard
spec:
  json:
    path: dashboards/node-exporter.json

Any update to the dashboard JSON is automatically deployed to Grafana.

Enabling GitOps Automation with Argo CD

  1. Install Argo CD: Deploy Argo CD in your cluster and configure it to monitor your Git repository.
  2. Configure applications: Define Argo CD Applications to sync each layer (infrastructure, services, monitoring).
  3. Automatic synchronization: Argo CD applies changes to your cluster when you push updates to Git, ensuring your monitoring stack is always up-to-date.

Practical Example: Sync Dashboards with Argo CD

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: grafana-dashboards
spec:
  source:
    repoURL: 'https://github.com/example-org/grafana-dashboards'
    path: dashboards
    targetRevision: main
  destination:
    server: 'https://kubernetes.default.svc'
    namespace: grafana-operator
  project: default
  syncPolicy:
    automated:
      prune: true
      selfHeal: true

This configuration keeps your dashboards in sync with Git, automatically healing any drift in cluster state.

Using Grafana Git Sync for Direct Dashboard Management

Grafana 12 introduces Git Sync, enabling direct bidirectional syncing between Grafana and Git. When you edit dashboards in the UI, changes are committed to Git. When you update dashboard JSON in Git, changes propagate to Grafana.

Quick Setup Example (Docker Compose)

services:
  grafana:
    image: grafana/grafana:12.2.0-16979757807
    ports:
      - 3000:3000
    environment:
      - GF_FEATURE_TOGGLES_ENABLE=provisioning,kubernetesDashboards
  prometheus:
    image: prom/prometheus
    ports:
      - 9090:9090

Configure Git Sync

  1. Set up a GitHub repo and personal access token.
  2. Enable Git Sync in Grafana UI or via configuration.
  3. Changes made in Grafana are pushed to Git, and vice versa.

Note: Git Sync is experimental and best suited for development/test environments.
For production, prefer GitOps workflows with Argo CD and the Grafana Operator.

Best Practices and Considerations

  • Store secrets securely: Reference Kubernetes Secrets for API tokens and credentials.
  • Review changes: Use pull requests for all dashboard and configuration updates.
  • Automate testing: Validate JSON and manifests before merging to main.
  • Monitor sync status: Use Argo CD and Grafana alerts to track health and sync failures.

Conclusion

GitOps empowers teams to manage Grafana as code, ensuring consistency, scalability, and reliability for observability. Combining Argo CD, the Grafana Operator, and Git Sync creates a modern, automated monitoring stack perfectly suited to cloud-native environments. Start implementing GitOps with Grafana today to streamline your monitoring workflows and enhance collaboration.