k8s 安装 prometheus 过程记录

开始以为只要安装 prometheus-operator 就行了。

git clone https://github.com/coreos/prometheus-operator.git
cd prometheus-operator
sed 's/namespace: default/namespace: monitoring/g' bundle.yaml | kubectl apply -f -

安装后发现只有一个 prometheus-operator pod 。

$ kubectl get pods -n monitoring
NAME                                  READY   STATUS    RESTARTS   AGE
prometheus-operator-99dccdc56-qq5lm   1/1     Running   0          20m

这时才发现真正要安装的是 kube-prometheus

https://github.com/coreos/kube-prometheus.git
cd kube-prometheus
kubectl create -f manifests/setup
until kubectl get servicemonitors --all-namespaces ; do date; sleep 1; echo ""; done
kubectl create -f manifests/

kube-prometheus 中包含了 prometheus 监控所用到的所有组件,当然也包含 prometheus-operator 。

$ kubectl get pods -n monitoring
NAME                                  READY   STATUS    RESTARTS   AGE
alertmanager-main-0                   2/2     Running   0          112m
grafana-58dc7468d7-pv256              1/1     Running   0          112m
kube-state-metrics-769f4fd4d5-4hfpk   3/3     Running   0          112m
node-exporter-4grdx                   2/2     Running   0          112m
prometheus-adapter-5cd5798d96-6zq6f   1/1     Running   0          112m
prometheus-k8s-0                      3/3     Running   1          112m
prometheus-operator-99dccdc56-vqq7m   1/1     Running   0          112m

安装 prometheus 之前,运行 kubectl top 命令会报错 。

$ kubectl top node
Error from server (NotFound): the server could not find the requested resource (get services http:heapster:)

这是由于没有安装 metrics-server ,在 kube-prometheus 的 readme 中有这样一段说明文字:

The kube-prometheus stack includes a resource metrics API server, so the metrics-server addon is not necessary. Ensure the metrics-server addon is disabled on minikube.

从而知道 kube-prometheus 中包含了 resource metrics API server ,所以安装好 kube-prometheus 之后 kubectl top node 也能正常执行了。

ubectl top nodes
NAME          CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%     
k8s-master0   1715m        85%    1001Mi          26%         
k8s-master1   347m         17%    972Mi           25%         
k8s-master2   286m         14%    1016Mi          26%         
k8s-node4     373m         9%     1550Mi          19% 

安装好 prometheus 之后通过 kubectl port-forward 命令暴露端口,可以通过浏览器分别访问 Prometheus, Grafana, AlertManager 控制台了,控制台效果见 Kubernetes Monitoring with Prometheus

kubectl --namespace monitoring port-forward svc/prometheus-k8s 9090 --address 10.0.1.81
kubectl port-forward $(kubectl get  pods --selector=app=grafana -n  monitoring --output=jsonpath="{.items..metadata.name}") -n monitoring 3000 --address 10.0.1.81
kubectl port-forward -n monitoring alertmanager-main-0 9093 --address 10.0.1.81

注:上面的 10.0.1.81 换成你的服务器 IP 地址,如果不指定 --address ,只能通过 localhost 访问。

猜你喜欢

转载自www.cnblogs.com/dudu/p/12146344.html