K8s搭建prometheus
Github地址:https://github.com/prometheus-operator/kube-prometheus
根据需要K8s版本号下载
unzip kube-prometheus-release-0.8.zip
cd kube-prometheus-release-0.8
#根据下方命令安装
kubectl create -f manifests/setup
until kubectl get servicemonitors --all-namespaces ; do date; sleep 1; echo ""; done
kubectl create -f manifests/
#如果镜像拉取缓慢报错
#例:
kubectl describe pods -n monitoring kube-state-metrics-76f6cb7996-zxxrj
Failed to pull image "k8s.gcr.io/kube-state-metrics/kube-state-metrics:v2.0.0": rpc error: code = Unknown desc = Error response from daemon: Get "https://k8s.gcr.io/v2/": net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
#修改镜像
cd manifests/
vim kube-state-metrics-deployment.yaml
image: k8s.gcr.io/kube-state-metrics/kube-state-metrics:v2.0.0
改为 image:bitnami/kube-state-metrics:2.0.0
#如果有其他拉取镜像失败使用相同的方法
进入docker.hub地址:https://hub.docker.com
搜索镜像:
等待所有pod的启动
kubectl get service -n monitoring #查看端口信息
#这个时候需要将pod的端口映射出来
cd manifests
vim alertmanager-service.yaml
spec:
ports:
- name: web
port: 9093
targetPort: web
type: NodePort #添加这行nodeport
selector:
alertmanager: main
#重启
kubectl apply -f alertmanager-service.yaml
#修改grafana
vim grafana-service.yaml
#和上面的相同添加nodeport
kubectl apply -f grafana-service.yaml
#修改prometheus
vim prometheus-service.yaml
#和上面的相同添加nodeport
kubectl apply -f prometheus-service.yaml
kubectl get service -n monitoring
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
alertmanager-main NodePort 10.105.176.221 <none> 9093:30864/TCP 43m
alertmanager-operated ClusterIP None <none> 9093/TCP,9094/TCP,9094/UDP 43m
blackbox-exporter ClusterIP 10.103.217.46 <none> 9115/TCP,19115/TCP 43m
grafana NodePort 10.106.166.254 <none> 3000:30058/TCP 43m
kube-state-metrics ClusterIP None <none> 8443/TCP,9443/TCP 43m
node-exporter ClusterIP None <none> 9100/TCP 43m
prometheus-adapter ClusterIP 10.108.200.177 <none> 443/TCP 43m
prometheus-k8s NodePort 10.109.3.154 <none> 9090:30795/TCP 43m
prometheus-operated ClusterIP None <none> 9090/TCP 43m
prometheus-operator ClusterIP None <none> 8443/TCP 44m
查看端口信息并访问grafana
192.168.11.11:30058 #默认admin admin
#默认已经添加了很多图片可以点击查看