|
| 1 | +--- |
| 2 | +meta: |
| 3 | + title: How to send metrics from your Kubernetes cluster to your Cockpit |
| 4 | + description: Learn how to send your pod metrics to your Cockpit using Scaleway's comprehensive guide. This tutorial covers sending Kubernetes pods metrics to Scaleway's Cockpit for centralized monitoring and analysis using Grafana, ensuring efficient monitoring and metrics analysis in your infrastructure. |
| 5 | +content: |
| 6 | + h1: How to send metrics from your Kubernetes cluster to your Cockpit |
| 7 | + paragraph: Learn how to send your pod metrics to your Cockpit using Scaleway's comprehensive guide. This tutorial covers sending Kubernetes pods metrics to Scaleway's Cockpit for centralized monitoring and analysis using Grafana, ensuring efficient monitoring and metrics analysis in your infrastructure. |
| 8 | +tags: kubernetes cockpit metrics observability monitoring cluster |
| 9 | +categories: |
| 10 | + - observability |
| 11 | +dates: |
| 12 | + validation: 2025/01/20 |
| 13 | + posted: 2025/01/20 |
| 14 | +--- |
| 15 | + |
| 16 | + |
| 17 | +In this page we will show you how to send application metrics from your Kubernetes cluster to your Cockpit, either by using a Helm chart or deploying a Helm chart with [Terraform](https://www.terraform.io/). |
| 18 | + |
| 19 | +We will use the [k8s-monitoring](https://artifacthub.io/packages/helm/grafana/k8s-monitoring/1.6.16) Helm Chart, which installs an Alloy Daemon set to export your Kubernetes cluster's metrics to your Cockpit. |
| 20 | + |
| 21 | +<Macro id="requirements" /> |
| 22 | + |
| 23 | + - A Scaleway account metricsged into the [console](https://console.scaleway.com) |
| 24 | + - [Owner](/identity-and-access-management/iam/concepts/#owner) status or [IAM permissions](/identity-and-access-management/iam/concepts/#permission) allowing you to perform actions in the intended Organization |
| 25 | + - [Created](/observability/cockpit/how-to/create-external-data-sources/) a custom external data source of the [metrics type](/observability/cockpit/concepts/#data-types) |
| 26 | + - [Created](/observability/cockpit/how-to/create-token/) a Cockpit token in the same region as the metrics data source |
| 27 | + - A running Kubernetes cluster containing your deployed application |
| 28 | + - [Created](/identity-and-access-management/iam/how-to/create-api-keys/) an API key and retrieved your API secret key |
| 29 | + |
| 30 | +<Message type="important"> |
| 31 | + - Sending metrics for Scaleway resources or personal data using an external path is a billable feature. In addition, any data that you push yourself is billed, even if you send data from Scaleway products. Refer to the [product pricing](https://www.scaleway.com/en/pricing/?tags=available,managedservices-observability-cockpit) page for more information. |
| 32 | +</Message> |
| 33 | + |
| 34 | + |
| 35 | +## Configure the Helm chart |
| 36 | + |
| 37 | +Create a `values.yml` file to configure your Helm chart, using the example below. Make sure that you replace `$SCW_CLUSTER_NAME` with the name of your Scaleway Kubernetes cluster, `$COCKPIT_CUSTOM_METRICS_DATASOURCE_URL` with the URL of your custom metrics data source (you can find it under the "API URL" section in the [Data sources tab](https://console.scaleway.com/cockpit/dataSource) of the Scaleway console), and `$COCKPIT_TOKEN` with your Cockpit token. |
| 38 | + |
| 39 | +```yaml |
| 40 | +cluster: |
| 41 | + name: "$SCW_CLUSTER_NAME" |
| 42 | +global: |
| 43 | + scrape_interval: 60s |
| 44 | +destinations: |
| 45 | + - name: "my-cockpit-metrics" |
| 46 | + type: "prometheus" |
| 47 | + protocol: "http" |
| 48 | + metrics: |
| 49 | + enabled: true |
| 50 | + url: "$COCKPIT_CUSTOM_METRICS_DATASOURCE_URL/api/v1/push" |
| 51 | + tenantId: "$COCKPIT_TOKEN" |
| 52 | + |
| 53 | + logs: |
| 54 | + enabled: false |
| 55 | + traces: |
| 56 | + enabled: false |
| 57 | + |
| 58 | +annotationAutodiscovery: |
| 59 | + enabled: true |
| 60 | + destinations: ["my-cockpit-metrics"] |
| 61 | + |
| 62 | +alloy-metrics: |
| 63 | + enabled: true |
| 64 | +alloy-singleton: |
| 65 | + enabled: true |
| 66 | +``` |
| 67 | +
|
| 68 | +<Message type="note"> |
| 69 | + The template above is for sending metrics to your Cockpit. You can also configure it to send logs to Cockpit using this Helm chart. |
| 70 | + Refer to our dedicated documentation to [send logs from your cluster to Cockpit](/observability/cockpit/how-to/send-logs-from-k8s-to-cockpit) |
| 71 | +</Message> |
| 72 | +
|
| 73 | +## Add annotations for auto-discovery |
| 74 | +
|
| 75 | +Annotations in Kubernetes provide a way to attach metadata to your resources. For `k8s-monitoring`, these annotations signal which pods should be scraped for metrics, and what port to use. For the sake of this documentation, we are adding annotations to specify we want `k8s-monitoring` to scrape the pods from our deployment. Make sure that you replace `$METRICS_PORT` with the port where your application exposes Prometheus metrics. |
| 76 | + |
| 77 | +### Kubernetes deployment template |
| 78 | + |
| 79 | +```yaml |
| 80 | +apiVersion: apps/v1 |
| 81 | +kind: Deployment |
| 82 | +metadata: |
| 83 | + ... |
| 84 | + annotations: |
| 85 | + "k8s.grafana.com/metrics.portNumber" = "$METRICS_PORT" |
| 86 | + "k8s.grafana.com/scrape" = "true" |
| 87 | +spec: |
| 88 | + ... |
| 89 | +``` |
| 90 | + |
| 91 | +### Terraform deployment template |
| 92 | + |
| 93 | +```terraform |
| 94 | +resource "kubernetes_deployment_v1" "your_application_deployment" { |
| 95 | + ... |
| 96 | + spec { |
| 97 | + ... |
| 98 | + template { |
| 99 | + metadata { |
| 100 | + ... |
| 101 | + annotations = { |
| 102 | + "k8s.grafana.com/metrics.portNumber" = "$METRICS_PORT" |
| 103 | + "k8s.grafana.com/scrape" = "true" |
| 104 | + } |
| 105 | + } |
| 106 | + ... |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +## Send Kubernetes metrics using Helm chart with Terraform |
| 113 | + |
| 114 | +1. Create a `provider.tf` file and paste the following template to set up the Helm Terraform provider: |
| 115 | + ```terraform |
| 116 | + provider "helm" { |
| 117 | + kubernetes { |
| 118 | + host = your_k8s_cluster_host # The URL of your Kubernetes API server. |
| 119 | + token = your_k8s_cluster_token # Authentication token to access the cluster. |
| 120 | + cluster_ca_certificate = base64decode( |
| 121 | + your_k8s_cluster_ca_certificate # The cluster's CA certificate. |
| 122 | + ) |
| 123 | + } |
| 124 | + } |
| 125 | + ``` |
| 126 | +2. Create a `maint.tf` file and paste the following template to create a Helm release resource. Make sure that you replace `/your-path/to/values.yml` with the actual path to your values file. |
| 127 | + ``` |
| 128 | + resource "helm_release" "alloy" { |
| 129 | + name = "name-of-your-metrics-ingester" |
| 130 | + repository = "https://grafana.github.io/helm-charts" |
| 131 | + chart = "k8s-monitoring" |
| 132 | + version = "2.0.2" |
| 133 | + |
| 134 | + namespace = "metrics-ingester" |
| 135 | + create_namespace = true |
| 136 | + values = [file("/your-path/to/values.yml")] |
| 137 | + } |
| 138 | + ``` |
| 139 | +3. Save your changes. |
| 140 | +4. Run `terraform init` to initialize your Terraform configuration and download any necessary providers. |
| 141 | +5. Run `terraform apply` to apply your configuration. |
| 142 | +6. Type `yes` when prompted to confirm the actions. |
| 143 | +
|
| 144 | +## Send Kubernetes metrics using Helm chart |
| 145 | +
|
| 146 | +Once you have configured your `values.yml` file, you can use Helm to deploy the metric-forwarding configuration to your Kubernetes cluster. Before installing the Helm chart, ensure that your `kubectl` tool is properly connected to your Kubernetes cluster. `kubectl` is the command-line tool for interacting with Kubernetes clusters. |
| 147 | +
|
| 148 | +1. [Connect](/containers/kubernetes/how-to/connect-cluster-kubectl/) `kubectl` to your Kubernetes cluster |
| 149 | +2. Run the command below to install the `k8s-monitoring` Helm chart: |
| 150 | + ``` |
| 151 | + helm install -f /your-path/to/values.yml name-of-your-choice-for-your-metric-ingester k8s-monitoring --version 1.6.16 |
| 152 | + ``` |
| 153 | + The `-f` flag specifies the path to your `values.yml` file, which contains the configuration for the Helm chart. Make sure that you replace `/your-path/to/values.yml` with the correct path where your `values.yml` file is stored. Make sure that you also replace `name-of-your-choice-for-your-metric-ingester` with a clear name (ex. `alloy-metrics-ingester`). In our configuration, we are using `alloy-lm-ingester`. |
| 154 | + |
| 155 | + Helm installs the `k8s-monitoring` chart, which includes the Alloy DaemonSet configured to collect metrics from your Kubernetes cluster. |
| 156 | + The DaemonSet ensures that a pod is running on each node in your cluster, which collects metrics and forwards them to the specified Prometheus endpoint in your Cockpit. |
| 157 | +
|
| 158 | +3. Optionally, check the status of the release to ensure it was installed: |
| 159 | +
|
| 160 | + ``` |
| 161 | + helm list |
| 162 | + ``` |
| 163 | + |
| 164 | + |
| 165 | +## Explore your metrics in Cockpit |
| 166 | + |
| 167 | +Now that your metrics are exported to your Cockpit, you can access and query them. |
| 168 | + |
| 169 | +1. Click **Cockpit** in the Observability section of the Scaleway [console](https://console.scaleway.com/) side menu. The **Cockpit Overview** page displays. |
| 170 | +2. Click **Open dashboards** to open your managed dashboards in Grafana. You are redirected to the Grafana website. |
| 171 | +3. Log in to Grafana using your [Grafana credentials](/observability/cockpit/how-to/retrieve-grafana-credentials/). |
| 172 | +4. Click the **Home** icon, then click **Explore**. |
| 173 | +5. Select your custom data source in the search drop-down on the upper left corner of your screen. |
| 174 | +6. In the **Labels filter** drop-down, select the `cluster` label and in the **Value** drop-down, select your cluster. |
| 175 | +7. Optionally, click the **Clock** icon on the top right corner of your screen and filter by time range. |
| 176 | +8. Click **Run query** to see your metrics. An output similar to the following should display. |
| 177 | +<Lightbox src="scaleway-cpt-k8s-terraform-metrics.webp" alt="" /> |
0 commit comments