prometheus-operator安装部署
如果只需要获取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 .
响应正常,部署成功
每个人都有潜在的能量,只是很容易被习惯所掩盖,被时间所迷离,被惰性所消磨~