Exporting Individual Pod Telemetry for Custom Metrics
Monitoring individual pods
While CloudFlow will provide general metrics on things like cpu and memory usage, you may have a use case where your application generates custom metrics that you want to export.
One solution for this would be to run have a process run in each container which remotely writes those metrics to some endpoint. In this article we will describe how to run a Prometheus Agent which can scrape metrics from all your pods and remote write them to destinations of your choosing. In this configuration you will run a prometheus agent in the same project as the pods you want to monitor, and an agent will end up running in each of the locations that your application runs. This means that if you inspect individual Prometheus Agents you will only see them succeed in scraping the pods that are local to them, but they will all ultimately write results to your remote_write
configured endpoints.
Configuration
The following YAML file defines a ConfigMap with configuration for the Prometheus agent. Replace the following accordingly: CLOUDFLOW_KUBERNETES_URL
, CLOUDFLOW_API_TOKEN
. You will also need to fill in the remote_write
portion of the config for the destinations you want, or remove it. Fine more information configuring remote_write here. Learn how to obtain the CLOUDFLOW items here.
apiVersion: v1
kind: ConfigMap
metadata:
name: prometheusagent-config
data:
prometheus.yml: |
global:
external_labels:
monitor: 'cloudflow-monitor'
scrape_configs:
- job_name: 'mykubernetespods'
metrics_path: '/metrics'
params:
'match[]':
- '{__name__=~".+"}'
scheme: 'http'
relabel_configs:
- source_labels: [__meta_kubernetes_pod_name]
separator: ;
regex: (.*)
target_label: pod
replacement: $1
action: replace
- source_labels: [__meta_kubernetes_pod_container_image]
separator: ;
regex: (.*)
target_label: container_image
replacement: $1
action: replace
- source_labels: [__meta_kubernetes_pod_container_name]
separator: ;
regex: (.*)
target_label: container_name
replacement: $1
action: replace
kubernetes_sd_configs:
- role: 'pod'
api_server: CLOUDFLOW_KUBERNETES_URL
authorization:
credentials: CLOUDFLOW_API_TOKEN
remote_write:
- url: <FILL IN REMOTE WRITE URL>
authorization:
credentials: <FILL IN CREDENTIAL>
Deploy it with kubectl apply -f configmap.yaml
.
Deployment
The following deployment will run the Prometheus agent on CloudFlow.
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: prometheusagent
name: prometheusagent
spec:
replicas: 1
selector:
matchLabels:
app: prometheusagent
template:
metadata:
labels:
app: prometheusagent
spec:
containers:
- image: prom/prometheus
imagePullPolicy: Always
name: prometheusagent
ports:
- containerPort: 9090
volumeMounts:
- name: prometheusagent-config
mountPath: /etc/prometheus
resources:
requests:
memory: ".5Gi"
cpu: "500m"
limits:
memory: ".5Gi"
cpu: "500m"
volumes:
- name: prometheusagent-config
configMap:
name: prometheusagent-config
Deploy it with kubectl apply -f grafana-deployment.yaml
.
Mark pods for scraping
With the above setup, you can now add the label prometheus.io/scrape: "true"
to the pods in your deployments and their metrics will be scraped from /metrics. You can learn more about customing the agent configuration here
View Metrics
You can now view metrics in the remote systems you have configured, or you can try and test things are working by running kubectl port-forward <prometheus agent pod name> 9090
and view data at localhost:9090. Not that as described earlier, you will only see the local data with this test method, not the data from ever location your app is deployed.