Grafana is essential for real-time monitoring of pod metrics in Kubernetes, allowing users to create dynamic dashboards for tracking and analyzing cluster performance with ease. Elevate monitoring with Grafana's intuitive interface and customization options.
First, Prometheus and Grafana must be installed in the Kubernetes cluster. You can see my previous article here for detailed steps on setting that up.
The next step is connecting to Grafana. There are 2 steps needed to achieve that:
- Port forward the Grafana service from the Kubernetes cluster to the desktop with the following command:
kubectl port-forward svc/kube-prometheus-stack-grafana 3000:80 -n metrics
- Open a browser and connect on http://localhost:3000/
The username to log in is admin and the password can be found by running the following command and decoding the results in base64 (or a cursory Google search typically yields the default value):
kubectl get secret kube-prometheus-stack-grafana -o jsonpath="{.data.admin-password}" -n metrics
Once logged in to Grafana, select the Dashboards link in the sidebar. A list of pre-configured dashboards can be reviewed for samples of what can be done in Grafana. Click the New button then select New Dashboard to start with a fresh dashboard. From here, panels can be added, click Add visualization then select Prometheus as the data source to get started. As a sample of monitoring pod memory usage, configure as follows:
- Under Metric, select container_memory_usage_bytes
- Under Label filters, select pod = <select the name of the pod from the dropdown>
- Click Run queries and validate data is coming through as expected
- Optionally update the Panel options on the right side
- Click Apply to save the changes
Pod memory should now be coming through in the panel. Additional panels can be added and configured by selecting Add -> Visualization. Panels can also be resized and moved by dragging the bottom right of the panel or dragging the top of the panel.
Additionally, it's possible to configure inputs for dynamically choosing which pod metrics to display. To do that:
- Select the Dashboard Settings gear above the panels
- Click the Variables tab
- Click Add variable
- Numerous types can be added here but for this example, select:
- Variable type: Text box
- Name: podname
- Label: Pod Name
- Click Apply then close
- Click edit on the visualization created earlier
- Above the metric, click Code next to Builder to swap the Metric to code mode
- Update the metric so it is (podname is the variable name configured above, for additional parameters, unique names should be used):
- container_memory_usage_bytes{pod="$podname"}
- Click Apply
- Enter the name of the pod to display metrics for in the Pod Name text field above the panels then click enter
Pod metrics for the entered pod should now come through in the graph! Numerous other metrics can also be displayed and it's even possible to set up multiple panels to display pod data side by side. Experiment around with the panels to see what's possible!
Finally, when not using persistent storage with the Prometheus Stack app, dashboards will not be saved when the pod is restarted. We recommend saving the dashboard configuration by:
- Select the Dashboard Settings gear above the panel
- Select the JSON Model tab and copy out the entire JSON structure
Then if the pod is restarted or if importing into another environment:
- Navigate to Dashboards in the Grafana app
- Select New -> Import
- Copy the JSON model saved previously into the Import via the dashboard JSON model
It's also possible to use the metrics here for Horizontal Pod Auto-scaling, stay tuned to this space for more information on that!
No Comments Yet
Let us know what you think