Kubernetes is a very dynamic environment. In order to monitor this, we need a tool like Prometheus which can monitor such dynamic environment. In this tutorial, we will setup Prometheus which will collect the data from Kubernetes cluster and visualize it in Grafana. We will use Helm chart to setup Prometheus and Grafana easily đ
Prerequisite
- A dynamic persistent provisioner. You can check my other guide on Setting up dynamic NFS provisioning in Kubernetes with Helm 3.
Setting default storage class
- First, letâs check the status of the storage class in our cluster. We deployed this storage class while we setup dynamic NFS provisioning.
|
|
Our storage class name is ânfs-clientâ which is currently not default. We need to make this storage class default so that Prometheus and Grafana use that storage class without explicitly defining it in their configuration files.
- Disabling default storage class for âstandardâ and making ânfs-clientâ as default
|
|
- Check the default storage class
|
|
Default storage class is now set to nfs-client. Cool.
Â
Setting up Prometheus
- Next, we will setup Prometheus using Helm chart. Make sure that Helm version is 3
|
|
- Adding Prometheus to Helm repo and update
|
|
- We will save the Helm values of Prometheus in current directory. We need to change some settings before deploying the Helm chart in the cluster.
|
|
- The above command will download the values required for deploying Prometheus and save in
prometheus.values
file in current directory. Open theprometheus.values
and modify the service block like below
|
|
In above service block, we changed the service type to NodePort so that we can access Prometheus externally. We also define nodePort to 32322. Save and exit the file.
- Provisioning Prometheus with custom values
|
|
- Run the following command to get the IP address and port as described in the picture to access Prometheus server. In my case it is http://172.20.0.2:32322
|
|
- Visit the URL with port. You should see a page like this
Note: If you canât access the server, you might need to add a custom route. Run the below command (ONLY if you canât access the server). Replace the IP address according to your Prometheus URL. Also change the network (-net) address. After that, you should see above page.
|
|
Setting up Grafana
Now we will setup Grafana to visualize the data from Prometheus which are now currently collecting the data from Kubernetes cluster.
- Adding the Grafana to the Helm repo and update
|
|
- Like Prometheus, we will save the Helm values of Grafana in current directory.
|
|
- Open the
grafana.values
. We need to modify in fields
i) Find the service block and modify like below
|
|
ii) Setting the admin user and password
|
|
ii) Enabling PVC
|
|
Save and exit.
- Install Grafana with custom values
|
|
- You should see the output like below. Similarly, in order to get the IP and port for accessing Grafana dashbord, run the following commands
|
|
In my case, the IP and port is http://172.20.0.2:32323. Visit the URL and you should see the Grafana dashboard. Login with your admin username and password.
Configure Grafana for collecting data from Prometheus
- Our setup part is done. Now we will configure Grafana for collecting the data from Prometheus. Click âAdd your first data sourceâ and select âPrometheusâ
- Set âURLâ to the Prometheus server URL. Also set HTTP method to âGETâ. When done, âSave & Testâ. You should see âData source is workingâ message.
- Now we will import a dashboard to visualize the collected data. From left menu, click import
- You can visit grafanaâs community built dashboard. Select any of the dashboard and copy itâs id. Paste it to the import page and click âLoadâ
- Select âPrometheusâ as data source and click âImportâ
A nice beautiful dashboard will be presented to you which will show the current clusterâs info
We just successfully setup Prometheus and Grafana to monitor and visualize our Kubernetes cluster.