Kubernetes

version: 1.0.0

Built with Teadocs

8-3.部署 prometheus

# 部署 prometheus

  • 参考资料

    helm/charts

    # k8s Helm安装Prometheus Operator

    Prometheus监控k8s集群使用邮箱和微信告警 【这篇文章有grafana搭建】

    九析带你轻松完爆 helm3 Prometheus 【推荐这篇文章解决问题】

  • # 创建命名空间

    为方便管理,创建一个单独的 Namespace monitoring,Prometheus 相关的组件都会部署到这个 Namespace。

    kubectl create namespace monitoring
    kubectl create namespace prometheus
  • helm获取安装包

    helm fetch stable/prometheus
    tar -zxvf prometheus-5.4.0.tgz
  • # 使用Helm安装Prometheus
    # helm install stable/prometheus -n prometheus --namespace monitoring 
    helm install stable/prometheus -n prometheus --namespace  prometheus
  • # 【问题】pvc一直处理Pending状态

    kubectl get pvc    -n prometheus
    [root@k8s-master01 helm-prometheus]# kubectl get pvc
    NAME                                    STATUS    VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
    halting-indri-prometheus-alertmanager   Pending                                                     84m
    halting-indri-prometheus-server         Pending                                                     84m
    • 没有找到绑定的 pv,需要创建pv:

      • pv-prometheus-2g.yaml
      apiVersion: v1
      kind: PersistentVolume
      metadata:
        name: prometheus-2g
      spec:
        capacity:
          storage: 2Gi
        volumeMode: Filesystem
        accessModes:
        -  ReadWriteOnce
        persistentVolumeReclaimPolicy: Retain
        nfs:
          server: 192.168.33.100
          path: /home/prometheus/2g
      • pv-prometheus-8g.yaml
      apiVersion: v1
      kind: PersistentVolume
      metadata:
        name: prometheus-8g
      spec:
        capacity:
          storage: 8Gi
        volumeMode: Filesystem
        accessModes:
        -  ReadWriteOnce
        persistentVolumeReclaimPolicy: Retain
        nfs:
          server: 192.168.33.100
          path: /home/prometheus/8g
    • 执行如下语句创建 pv:

      kubectl apply -f pv-prometheus-2g.yaml
      kubectl apply -f pv-prometheus-8g.yaml
    • 添加 deployment selector:

       编辑 templates/alertmanager-deployment.yaml 
       编辑 templates/kube-state-metrics-deployment.yaml 
       编辑 templates/pushgateway-deployment.yaml 
       编辑 templates/server-deployment.yaml 
      添加如下:
         selector:
          matchLabels:
            app: {{ template "prometheus.name" . }}
    • 查看pvc的yaml文件信息

      kubectl get pvc -n prometheus halting-indri-prometheus-alertmanager -o yaml
      kubectl get pvc -n prometheus prometheus-prometheus-server -o yaml
    • 查看命名空间Prometheus下的pod信息描述

      kubectl get pod -n prometheus
      kubectl describe pods <pod名>  -n prometheus
    • 删除命名空间下的pod

      kubectl delete pod -n prometheus <pod名>
      kubectl delete pod -n prometheus <pod名> --force --grace-period=0    # 强制删除
    • 更新helm

      helm list 
      helm upgrade -f values.yaml <name> .
  • # 验证 prometheus

    查看 prometheus pod 运行状态,发现 prometheus-server 并没有启动成功:

    kubectl get pod -n prometheus
  • # [可选]修改 service type

    将 prometheus service type 修改为 NodePort 类型:

    kubectl patch svc -n prometheus prometheus-prometheus-server -p '{"spec": {"type": "NodePort"}}'
    kubectl get svc -n prometheus
    
    kubectl edit svc -n prometheus prometheus-prometheus-server

    将 prometheus-prometheus-alertmanager 修改为 NodePort 类型:

    kubectl patch svc -n prometheus prometheus-prometheus-alertmanager -p '{"spec": {"type": "NodePort"}}'
    
    kubectl edit svc -n prometheus prometheus-prometheus-alertmanager 

    将 prometheus-prometheus-pushgateway 修改为 NodePort 类型:

    kubectl patch svc -n prometheus prometheus-prometheus-pushgateway -p '{"spec": {"type": "NodePort"}}'
    
    kubectl edit svc -n prometheus prometheus-prometheus-pushgateway 
  • [推荐]编写 Ingress

    创建 ingress 资源文件 prometheus-ingress.yaml :

    apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
      name: prometheus-ingress
      namespace: prometheus
      labels:
        app.kubernetes.io/name: ingress-nginx
        app.kubernetes.io/part-of: ingress-nginx
    spec:
      rules:
        - host: www.prometheus.com
          http:
            paths:
            - path: /
              backend:
                serviceName: prometheus-prometheus-server
                servicePort: 9090
    
        - host: alertmanager.prometheus.com
          http:
            paths:
            - path: /
              backend:
                serviceName: prometheus-prometheus-alertmanager
                servicePort: 9093
    
        - host: pushgateway.prometheus.com
          http:
            paths:
            - path: /
              backend:
                serviceName: prometheus-prometheus-pushgateway
                servicePort: 9091
    kubectl apply -f prometheus-ingress.yaml
    • 查看prometheus命名空间下的Ingress

      kubectl get ingresses. -n prometheus
      [root@k8s-master01 helm-prometheus]# kubectl get ingresses. -n prometheus
      NAME                 HOSTS                                                                       ADDRESS        PORTS   AGE
      prometheus-ingress   www.prometheus.com,alertmanager.prometheus.com,pushgateway.prometheus.com   10.104.97.57   80      66s
  • 访问 prometheus

    www.prometheus.com
    alertmanager.prometheus.com
    pushgateway.prometheus.com