Github仓库地址

1. 部署

1.1 下载源码

  1. git clone https://github.com/prometheus-operator/kube-prometheus.git
  2. cd kube-prometheus

注意:请确认好**Kubernetes**集群版本,下载对应版本的源码包。
image.png

1.2 修改yaml

主要是修改一下Service的类型和镜像源。默认ServiceClusterIP,为了提供一个集群访问入口,这里设置为NodeProt类型,如果是在云服务上部署的,可以改成LoadBalancer;另外就是有几个服务的镜像源为谷歌的镜像仓库,国内无法访问,因此需要更换一下镜像。

1.2.1 修改grafana-service.yaml

  1. sed -i '/spec:/a\ type: NodePort' manifests/grafana-service.yaml

1.2.2 修改prometheus-service.yaml

  1. sed -i '/spec:/a\ type: NodePort' manifests/prometheus-service.yaml

1.2.3 修改alertmanager-service.yaml

  1. sed -i '/spec:/a\ type: NodePort' manifests/alertmanager-service.yaml

1.2.4 修改镜像源

查看包含imageyaml文件:grep -l image: manifests/*.yaml

  1. sed -ri 's#(image:) k8s.*#\1 bitnami/kube-state-metrics:2.4.2#' manifests/kubeStateMetrics-deployment.yaml
  2. sed -ri 's#(image:) k8s.*#\1 willdockerhub/prometheus-adapter:v0.9.1#' manifests/prometheusAdapter-deployment.yaml

1.3 部署 CRD

  1. kubectl create -f manifests/setup

注意:使用**apply**时会提示以下错误,可以使用**create**代替。

  1. The CustomResourceDefinition "prometheuses.monitoring.coreos.com" is invalid: metadata.annotations: Too long: must have at most 262144 bytes

1.4 部署 Prometheus 生态组件

注意:默认为高可用部署,即关键组件都会启动多个副本,副本数请按需修改。

  1. kubectl apply -f manifests
  1. 查看部署情况:
  1. [root@k8s-master01 ~]# kubectl get pod -n monitoring
  2. NAME READY STATUS RESTARTS AGE
  3. alertmanager-main-0 2/2 Running 0 35m
  4. blackbox-exporter-5cb5d7479d-n59tf 3/3 Running 0 42m
  5. grafana-d595885ff-6gg6t 1/1 Running 0 42m
  6. kube-state-metrics-d64589d79-hwx7j 3/3 Running 0 42m
  7. node-exporter-9l7k5 2/2 Running 0 42m
  8. node-exporter-fm78w 2/2 Running 0 42m
  9. node-exporter-tw54d 2/2 Running 0 42m
  10. prometheus-adapter-86df476d87-27dp4 1/1 Running 0 42m
  11. prometheus-adapter-86df476d87-65dfz 1/1 Running 0 42m
  12. prometheus-k8s-0 2/2 Running 0 35m
  13. prometheus-operator-7cc5c6fd68-xfkfh 2/2 Running 0 42m
  14. [root@k8s-master01 ~]# kubectl get svc -n monitoring
  15. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  16. alertmanager-main ClusterIP 10.109.19.210 <none> 9093/TCP,8080/TCP 64m
  17. alertmanager-operated ClusterIP None <none> 9093/TCP,9094/TCP,9094/UDP 57m
  18. blackbox-exporter ClusterIP 10.99.75.89 <none> 9115/TCP,19115/TCP 64m
  19. grafana NodePort 10.99.120.219 <none> 3000:32287/TCP 64m
  20. kube-state-metrics ClusterIP None <none> 8443/TCP,9443/TCP 64m
  21. node-exporter ClusterIP None <none> 9100/TCP 64m
  22. prometheus-adapter ClusterIP 10.103.205.208 <none> 443/TCP 64m
  23. prometheus-k8s NodePort 10.99.26.234 <none> 9090:30447/TCP,8080:30361/TCP 64m
  24. prometheus-operated ClusterIP None <none> 9090/TCP 57m
  25. prometheus-operator ClusterIP None <none> 8443/TCP 64m