如果只需要獲取node和pod的metrics,安裝metrics-server就夠了,如果要安裝prometheus-operator,則不需要安裝metrics-server,里邊自帶
curl -SLO https://addons.kuboard.cn/metrics-server/0.3.6/metrics-server.yaml
kubectl apply -f metrics-server.yaml
kubectl top node
kubectl top pod
安裝
提前准備好k8s環境,下載prometheus-operator安裝包,我這里使用的0.7.0版本,k8s版本為v1.19.8,其版本與k8s版本有對應關系,請對應下載
tar zxvf kube-prometheus-0.7.0.tar.gz
cd kube-prometheus-0.7.0/manifests/
對應關系如下:
kube-prometheus stack | Kubernetes 1.18 | Kubernetes 1.19 | Kubernetes 1.20 | Kubernetes 1.21 | Kubernetes 1.22 |
---|---|---|---|---|---|
release-0.6 |
✗ | ✔ | ✗ | ✗ | ✗ |
release-0.7 |
✗ | ✔ | ✔ | ✗ | ✗ |
release-0.8 |
✗ | ✗ | ✔ | ✔ | ✗ |
release-0.9 |
✗ | ✗ | ✗ | ✔ | ✔ |
HEAD |
✗ | ✗ | ✗ | ✔ | ✔ |
替換國內鏡像源
quay.mirrors.ustc.edu.cn/coreos/k8s-prometheus-adapter-amd64
sed -i 's/quay.io/quay.mirrors.ustc.edu.cn/g' setup/prometheus-operator-deployment.yaml
sed -i 's/quay.io/quay.mirrors.ustc.edu.cn/g' prometheus-prometheus.yaml
sed -i 's/quay.io/quay.mirrors.ustc.edu.cn/g' alertmanager-alertmanager.yaml
sed -i 's/quay.io/quay.mirrors.ustc.edu.cn/g' kube-state-metrics-deployment.yaml
sed -i 's/quay.io/quay.mirrors.ustc.edu.cn/g' node-exporter-daemonset.yaml
sed -i 's/quay.io/quay.mirrors.ustc.edu.cn/g' prometheus-adapter-deployment.yaml
sed -i 's/quay.io/quay.mirrors.ustc.edu.cn/g' blackbox-exporter-deployment.yaml
sed -i 's#k8s.gcr.io/kube-state-metrics/kube-state-metrics#bitnami/kube-state-metrics#g' kube-state-metrics-deployment.yaml
修改promethes,alertmanager,grafana的service類型為NodePort類型
prometheus-service.yaml
spec:
type: NodePort #新增
ports:
- name: web
port: 9090
targetPort: web
selector:
app: prometheus
app.kubernetes.io/component: prometheus
app.kubernetes.io/name: prometheus
app.kubernetes.io/part-of: kube-prometheus
prometheus: k8s
sessionAffinity: ClientIP
同理,alertmanager-service.yaml、grafana-service.yaml
安裝prometheus-operator
kubectl apply -f setup/
查看狀態,到podRunning
再進行下一步
kubectl get pods -n monitoring
kubectl apply -f .
查看狀態,到所有podRunning
再進行下一步
kubectl get pods -n monitoring
獲取prometheus端口
[root manifests]# kubectl get svc -n monitoring | grep NodePort
alertmanager-main NodePort 10.101.19.108 <none> 9093:31274/TCP 10m
grafana NodePort 10.101.15.228 <none> 3000:30547/TCP 10m
prometheus-k8s NodePort 10.97.1.58 <none> 9090:32576/TCP 10m
卸載命令
kubectl delete --ignore-not-found=true -f manifests/ -f manifests/setup
到這里只是部署了 metrics api server ,只能獲取到 CPU 與內存這2個核心指標,通過下面的命令可以看到這個 api service v1beta1.metrics.k8s.io :
kubectl get apiservices -n monitoring | grep metrics
NAME SERVICE AVAILABLE AGE
v1beta1.metrics.k8s.io monitoring/prometheus-adapter True 27h
向已經部署的 metrics api 發請求
kubectl get --raw "/apis/metrics.k8s.io/v1beta1" | jq .
接下來,使用helm安裝custom metrics api server
helm repo add stable http://mirror.azure.cn/kubernetes/charts
helm repo list
helm install prometheus-adapter stable/prometheus-adapter --namespace kube-system --set prometheus.url=http://prometheus-k8s.monitoring,prometheus.port=9090
查看custom metrics api
[root ~]# kubectl get apiservices -n monitoring | grep metrics
v1beta1.custom.metrics.k8s.io kube-system/prometheus-adapter True 11h
v1beta1.external.metrics.k8s.io keda/keda-metrics-apiserver True 12h
v1beta1.metrics.k8s.io monitoring/prometheus-adapter True 15h
給custom metrics api 發請求
kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1" | jq .
響應正常,部署成功