Install Prometheus and Grafana monitoring on Kubernetes (k8s)

Environmental preparation

Kubernetes preparation

Of course, the prerequisite environment is that you must first have a Kubernetes cluster with a version between v1.21.*~v1.27.*. Of course, I have already prepared Kubernetes:
Insert image description here

You can see that the Kubernetes version I prepared is 1.21.14, which meets the requirements. This article also uses this version for installation. The installation steps for the version mentioned above are generally the same as this version. Just follow the steps.

Installation project

Because it is troublesome to install Prometheus on Kubernetes, some big guys have contributed some projects to install them on Github. The project I recommend this time iskube-prometheus, the address is :https://github.com/prometheus-operator/kube-prometheus, but you should pay attention to the version of kube-prometheus installed and the corresponding list of versions :

Kubernetes version Kube-prometheus version
1.21.* 0.9.0
1.22.* 0.9.0、0.10.0
1.23.* 0.10.0、0.11.0
1.24.* 0.11.0、0.12.0
1.25.* 0.12.0
1.26.* main
1.27.* main

As shown in the picture:
Insert image description here

It should be noted that it is best not to use projects in the main branch, because it has not been officially released yet, so it is recommended to use Kubernetes below 1.25.*

start installation

Download and install the project

Go to this URL:https://github.com/prometheus-operator/kube-prometheus/releases and select the corresponding version Download the release version, because my Kubernetes installation is 1.21.*, so I downloaded version 0.9.0:
Insert image description here

After downloading, it looks like this:
Insert image description here

Installation project

Let’s unzip this project first:

tar -zxvf kube-prometheus-0.9.0.tar.gz

After decompression, a kube-prometheus-0.9.0 folder will appear as shown in the figure:
Insert image description here

Let’s go to the kube-prometheus-0.9.0/manifests directory first:

cd kube-prometheus-0.9.0/manifests/

Replace image

Because some of the mirrors are pulled from k8s.gcr.io, this mirror is on Google and cannot be pulled from China, so we need to modify the pull address. Because someone has already provided a replacement for this image on dockerhub, we can directly replace it here

Replace kube-state-metrics

Modify ./kube-state-metrics-deployment.yaml file (in kube-prometheus-0.9.0/manifests/kube-state-metrics-deployment.yaml):

vim kube-state-metrics-deployment.yaml

There is k8s.gcr.io/kube-state-metrics/kube-state-metrics:v2.1.1 image at about line 34 (the version number may be different, which does not affect the installation):
Insert image description here

Check the most popular ones on dockerhub:
Insert image description here

Select the same version number (⚠️Note that this version number does not have the v prefix):
Insert image description here

So we change k8s.gcr.io/kube-state-metrics/kube-state-metrics:v2.1.1 to docker.io/bitnami/kube-state-metrics:2.1.1 (plus docker.io prefix):
Insert image description here

Replace prometheus-adapter

Modify ./prometheus-adapter-deployment.yaml file (in kube-prometheus-0.9.0/manifests/prometheus-adapter-deployment.yaml):

vim ./prometheus-adapter-deployment.yaml

There is k8s.gcr.io/prometheus-adapter/prometheus-adapter:v0.9.0 image at about line 34 (the version number may be different, which does not affect the installation):
Insert image description here

Here we come to dockerhub to search, and we also use this most downloaded and collected one:
Insert image description here

Also pay attention to the version number correspondence:
Insert image description here

So we change k8s.gcr.io/prometheus-adapter/prometheus-adapter:v0.9.0 to docker.io/willdockerhub/prometheus-adapter:v0.9.0 (plus docker.io prefix):
Insert image description here

Modify Service

Because the type of some Service settings that we need to access externally is ClusterIP, we need to change it to NodePort or LoadBalancer.

Modify alertmanager-service.yaml

This file is in kube-prometheus-0.9.0/manifests/alertmanager-service.yaml , we modify it directly:

vim alertmanager-service.yaml

Follow the picture prompts to modify, add a new line under and modify the type to , and then < /span> Port:. This port can be specified by yourself. and modify the nodePort port to Add a new line belowspectype: NodePortNodePortspec.portsnodePort: 300933009330000~32767
Insert image description here

Save and exit after modification is completed.

Modify grafana-service.yaml

This file is in kube-prometheus-0.9.0/manifests/grafana-service.yaml , we modify it directly:

vim grafana-service.yaml

The steps are the same, so I won’t go into details here, but be careful not to conflict with the port numbers:
Insert image description here

Modify prometheus-service.yaml

This file is in kube-prometheus-0.9.0/manifests/prometheus-service.yaml , we modify it directly:

vim prometheus-service.yaml

The steps are the same, so I won’t go into details here, but be careful not to conflict with the port numbers:
Insert image description here

Execute these yaml resource manifest files

Come to kube-prometheus-0.9.0 directory first:
Insert image description here

Then execute the following lines of commands:

kubectl apply --server-side -f manifests/setup
kubectl wait \
	--for condition=Established \
	--all CustomResourceDefinition \
	--namespace=monitoring
kubectl apply -f manifests/

The execution result is as shown in the figure, a bunch of resources are created:
Insert image description here

We use the following command to check whether the creation is successful:

watch kubectl get pod -o wide -n monitoring

After the installation is completed, the values ​​of STATUS are all Running, and the values ​​of READY are all correct:
Insert image description here

Visit Grafana

Use this line of command to view grafana’s port:

kubectl get svc -o wide -n monitoring | grep grafana

You can see that the nodePort port for external access by grafana is 30300, which is also the value we set before:
Insert image description here

We can access grafana directly by accessing the IP address of any node through the browser and bringing this port:
Insert image description here

The default username and password areadmin/admin. We can log in directly. After logging in, we need to reset the password and set a new password for your admin user:
Insert image description here

Next we visit Grafana’s homepage:
Insert image description here

Follow my prompts and click Dashboards -> Manage:
Insert image description here

Then expand this Default:
Insert image description here

You can see that there are many kube-prometheus Dashboards that have been prepared for us:
Insert image description here

We select Node Exporter/Nodes’s Dashboard to enter:
Insert image description here

Here you can see the CPU and memory information of each node:
Insert image description here

point Kubernetes/Networking/Cluster progress:
Insert image description here

You can see the network access status under each namespace;
Insert image description here

Okay, you can check out more Dashboards yourself.

Access to other systems

Prometheus

Use the following command to view Prometheus’s NodePort:

kubectl get svc -o wide -n monitoring | grep prometheus-k8s

You can see that the NodePort port of Prometheus is 30090 which we set ourselves:
Insert image description here

Access can directly go to the home page of Prometheus:
Insert image description here

Then you can do whatever you want with Prometheus

AlertManager

We can directly query the NodePort port of alertmanager through the following command:

kubectl get svc -o wide -n monitoring | grep alertmanager-main

You can see that the port is still 30093 we set before:
Insert image description here

You can also access the AlertManager homepage by accessing port 30093 of any of our nodes:
Insert image description here

Okay, the steps to build Prometheus and Grafana on Kubernetes have been completed. I wish you happy coding.

Guess you like

Origin blog.csdn.net/m0_51510236/article/details/132601350