k8s Helm安裝Prometheus Operator

Ubuntu 18 Kubernetes集羣的安裝和部署 以及Helm的安裝 完成了k8s的集羣和helm的安裝,今天咱們來看看Prometheus的監控怎麼搞。Prometheus Operator 是 CoreOS 開發的基於 Prometheus 的 Kubernete s監控方案,也多是目前功能最全面的開源方案。更多信息能夠查看https://github.com/coreos/prometheus-operatorhtml

建立命名空間

爲方便管理,建立一個單獨的 Namespace monitoring,Prometheus Operator 相關的組件都會部署到這個 Namespace。node

kubectl create namespace monitoring

使用Helm安裝Prometheus Operator

Prometheus Operator 全部的組件都打包成 Helm Chart,安裝部署很是方便。git

helm install --name prometheus-operator --set rbacEnable=true --namespace=monitoring stable/prometheus-operator
#helm install --name prometheus-operator --namespace=monitoring stable/prometheus-operator
#helm del --purge prometheus-operator
#removed CRDS
#kubectl delete crd prometheuses.monitoring.coreos.com
#kubectl delete crd prometheusrules.monitoring.coreos.com
#kubectl delete crd servicemonitors.monitoring.coreos.com
#kubectl delete crd podmonitors.monitoring.coreos.com
#kubectl delete crd alertmanagers.monitoring.coreos.com
kubectl get all -n monitoring #查看建立的資源
helm list #查看安裝後的release
kubectl get svc -n monitoring  #查看訪問類型
# kubectl get svc,pod -n monitoring #查看狀態
#kubectl describe pod prometheus-operator-prometheus-node-exporter-xxx  -n monitoring
#kubectl get po --all-namespaces -o=jsonpath="{range .items[*]}{.spec.nodeName}{'\t'}{.spec.hostNetwork}{'\t'}{.spec.hostNetwork}{'\t'}{.spec.containers..containerPort}{'\n'}{end}" #查看端口信息

方法1.修改 alertmanager、prometheus、grafana的訪問類型爲NodePort

kubectl edit svc prometheus-operator-grafana -n monitoring #grafana的訪問類型 修改type爲NodePort        
kubectl edit svc prometheus-operator-alertmanager -n monitoring   
kubectl edit svc prometheus-operator-prometheus -n monitoring
kubectl get svc -n monitoring #查看修改後的訪問類型

修改 kubelet 打開只讀端口

prometheus 須要訪問 kubelet 的 10255 端口獲取 metrics。可是默認狀況下 10255 端口是不開放的,會致使 prometheus 上有 unhealthy。打開只讀端口須要編輯全部節點的vi /var/lib/kubelet/config.yaml (我這裏有圖形界面能夠用gedit命令)文件,加入如下內容github

port: 10250
readOnlyPort: 10255          #增長此行

重啓 kubelet 服務  systemctl restart kubelet.service
訪問地址爲http://nodeip:30292/, 默認的用戶名/密碼爲:admin/prom-operator,登錄後以下圖:docker

若是想和我同樣遇到 prometheus-operator-prometheus-node-exporter 1 node(s) didn't match node selector, 2 node(s) didn't have free ports for the requested pod ports., 我這裏是由於9100端口被佔用,直接修改吧:json

方法2.經過 Ingress 來暴漏服務 

新建文件prometheus-ingress.yaml:api

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  namespace: monitoring
  name: prometheus-ingress
spec:
  rules:
  - host: grafana.domain.com
    http:
      paths:
      - backend:
          serviceName: prometheus-operator-grafana 
          servicePort: 3000
  - host: prometheus.domain.com
    http:
      paths:
      - backend:
          serviceName: prometheus-operator-prometheus
          servicePort: 9090
  - host: alertmanager.domain.com
    http:
      paths:
      - backend:
          serviceName: prometheus-operator-alertmanager
          servicePort: 9093

執行命令服務器

kubectl apply -f prometheus-ingress.yaml

修改hosts文件:app

192.168.100.11 grafana.domain.com
192.168.100.11 prometheus.domain.com
192.168.100.11 alertmanager.domain.com
#192.168.100.11  是上一篇文章的邊緣服務器

效果如圖:dom

如下修改不肯定有效,算是備註吧

prometheus 經過 4001 端口訪問 etcd metrics,可是 etcd 默認監聽 2379。解決方法是在 /etc/kubernetes/manifests/etcd.yaml

增長k8s-app: etcd-server 而後重啓 systemctl restart kubelet.service

因爲 kube-controller-manager 和 kube-scheduler 默認監聽 127.0.0.1 ,prometheus 沒法經過本機地址獲取數據,須要修改kube-controller-manager 和 kube-scheduler 監聽地址。
解決辦法以下:/etc/kubernetes/manifests/kube-controller-manager.yaml 增長 k8s-app: kube-controller-manager

/etc/kubernetes/manifests/kube-scheduler.yaml增長 k8s-app: kube-scheduler

參考:

prometheus-operator

使用 Prometheus Operator 監控 Kubernetes

全手動部署prometheus-operator監控Kubernetes集羣遇到的坑

相關文章
相關標籤/搜索