Menu
Grafana Cloud

Configure remote_write with a Prometheus ConfigMap

In this guide, you’ll learn how to configure Prometheus to ship scraped samples to Grafana Cloud using Prometheus’s remote_write feature.

Follow this guide if you have Prometheus installed and running in your cluster, and configured using a Kubernetes ConfigMap.

Before you begin

Before you begin, you should have the following items available to you:

  • A Kubernetes >=1.16.0 cluster.
  • A Grafana Cloud Standard account. To learn how to create an account, see Grafana Cloud Quickstarts.
  • A Grafana Cloud API key with the MetricsPublisher role. To learn how to create a Grafana Cloud API key, see Create a Grafana Cloud API key.
  • The kubectl command-line tool installed on your local machine and configured to connect to your cluster. See how to install kubectl.
  • The Prometheus monitoring system installed and running in your cluster as a deployment, configured using a ConfigMap. Installing Prometheus goes beyond the scope of this guide. To learn how to install Prometheus Operator in your cluster, see Install Prometheus Operator with Grafana Cloud for Kubernetes. Prometheus Operator abstracts away much of Prometheus’s configuration and management overhead.

Modify Prometheus ConfigMap

  1. Locate your Grafana Cloud Metrics username and password by navigating to your stack in the Cloud Portal and clicking Details next to the Prometheus panel.

    Your password corresponds to an API key that you can generate by clicking on Generate now in this same panel. To learn how to create a Grafana Cloud API key, see Create a Grafana Cloud API key.

  2. Once you’ve noted your username and password, inject it into your Prometheus configuration file by modifying the Kubernetes ConfigMap resource containing Prometheus’s configuration.

    Unfortunately Prometheus does not support pulling environment variables from the execution environment so you can’t readily use a Kubernetes Secret object in this case. Using Secrets or envsubst goes beyond the scope of this guide.

  3. Locate the ConfigMap manifest and open it in an editor. It will look similar to the following example:

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: prometheus
      namespace: monitoring
    data:
      prometheus.yml: |-
        global:
          scrape_interval: 5s
          evaluation_interval: 5s
        rule_files:
          - /etc/prometheus/prometheus.rules
        alerting:
          alertmanagers:
          - scheme: http
            static_configs:
            - targets:
              - "alertmanager.monitoring.svc:9093"
    
    . . .
    

    This ConfigMap is installed in the monitoring Namespace, where the Prometheus deployment should also be running.

  4. Modify this ConfigMap by adding the following remote_write configuration block:

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: prometheus_v2
      namespace: monitoring
    data:
      prometheus.yml: |-
        global:
          scrape_interval: 5s
          evaluation_interval: 5s
    
    . . .
    
    remote_write:
    - url: <Your Metrics instance remote_write endpoint>
      basic_auth:
        username: <your_grafana_cloud_metrics_username>
        password: <your_grafana_cloud_metrics_password>
    

    You can find the /api/prom/push URL, username, and password for your metrics endpoint by clicking on Details in the Prometheus card of the Cloud Portal.

    This block creates a default remote_write configuration that ships samples to the Cloud Metrics Prometheus endpoint. It also sets the authorization header on remote_write requests with your Grafana Cloud credentials. To tune the default remote_write parameters, see Remote Write Tuning from the Prometheus documentation.

    Be sure to give the ConfigMap a new versioned name as well, by appending a suffix like _v2.

  5. When you’re done, save and close the file.

In the next step you’ll roll out this updated configuration into your cluster.

Update the running Prometheus deployment

  1. To roll out your configuration changes, update the Prometheus deployment with the new versioned ConfigMap.

    Note: You can roll out configuration changes in Kubernetes clusters in many different ways. The steps in this guide focus on configuring remote_write and are not meant to cover blue-green or production Prometheus rollout scenarios.

    Given that in the previous step you assigned the ConfigMap a new name, when you update the ConfigMap reference in the deployment, Kubernetes redeploys any Pods using the old ConfigMap.

  2. Open the Prometheus deployment in an editor. It will look similar to the following example:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: prometheus-deployment
      labels:
        app: prometheus
    spec:
      replicas: 2
      selector:
    
    . . .
    
      template:
        metadata:
          labels:
            app: prometheus
    
    . . .
    
        spec:
          containers:
          - name: prometheus
            image: prometheus
            ports:
            - containerPort: 9090
            volumeMounts:
              - name: config-volume
                mountPath: /etc/prometheus/
          volumes:
            - name: config-volume
              configMap:
                name: prometheus
    

    This file may vary depending on how you configured and deployed Prometheus. The above manifest defines a 2-pod Prometheus deployment that references a ConfigMap called prometheus. The prometheus.yml key containing Prometheus’s configuration is mounted to /etc/prometheus/prometheus.yml.

  3. To update this deployment, change the ConfigMap:

    . . .
          volumes:
            - name: config-volume
              configMap:
                name: prometheus_v2
    

    You update the configMap’s name field from prometheus to prometheus_v2 to reference the new ConfigMap defined earlier.

  4. When you’re done, save and close the file.

  5. Roll out the changes using kubectl apply -f:

    kubectl apply -f <your_prometheus_deployment_manifest>.yaml
    

Check your work

At this point, you’ve configured Prometheus to remote_write scraped metrics to Grafana Cloud. You can verify that your running Prometheus instance is remote_writing correctly using port-forward.

To verify your changes:

  1. Get the Prometheus server’s service name:

    kubectl get svc
    
  2. Use port-forward to forward a local port to the Prometheus service:

    kubectl --namespace monitoring port-forward svc/<prometheus-service-name> 9090:80
    

    Replace monitoring with the appropriate namespace, and <prometheus-service-name> with the name of the Prometheus service.

  3. Navigate to http://localhost:9090 in your browser, and then Status and Configuration.

  4. Verify that the remote_write block you appended above has propagated to your running Prometheus instance configuration.

  5. Finally, log in to your Grafana instance to begin querying your cluster data. You can use the Billing/Usage dashboard to inspect incoming data rates in the last 5 minutes to confirm the flow of data to Grafana Cloud.