• K8S 搭建 Prometheus (一) 部署 nodeexporter, prometheusserver


    版本信息:

    软件 版本
    K8S 1.20.5
    containerd 1.4.4

    部署 node-exporter

    node-exporter 组件用来采集机器(物理机、虚拟机、云主机等)的监控指标数据,能够采集到的指标包括CPU, 内存,磁盘,网络,文件数等信息

    # 在 master 上操作
    
    kubectl create ns monitor-sa
    
    mkdir /data/yaml/monitor-sa/export -p
    
    cd /data/yaml/monitor-sa/export
    
    cat node-export.yaml 
    apiVersion: apps/v1
    kind: DaemonSet  #可以保证k8s集群的每个节点都运行完全一样的pod
    metadata:
      name: node-exporter
      namespace: monitor-sa
      labels:
        name: node-exporter
    spec:
      selector:
        matchLabels:
         name: node-exporter
      template:
        metadata:
          labels:
            name: node-exporter
        spec:
          hostPID: true
          hostIPC: true
          hostNetwork: true
          containers:
          - name: node-exporter
            image: harbor.junengcloud.com/monitor/node-exporter:v0.16.0
            ports:
            - containerPort: 9100
            resources:
              requests:
                cpu: 0.15
            securityContext:
              privileged: true
            args:
            - --path.procfs
            - /host/proc
            - --path.sysfs
            - /host/sys
            - --collector.filesystem.ignored-mount-points
            - '"^/(sys|proc|dev|host|etc)($|/)"'
            volumeMounts:
            - name: dev
              mountPath: /host/dev
            - name: proc
              mountPath: /host/proc
            - name: sys
              mountPath: /host/sys
            - name: rootfs
              mountPath: /rootfs
          tolerations:
          - key: "node-role.kubernetes.io/master"
            operator: "Exists"
            effect: "NoSchedule"
          volumes:
            - name: proc
              hostPath:
                path: /proc
            - name: dev
              hostPath:
                path: /dev
            - name: sys
              hostPath:
                path: /sys
            - name: rootfs
              hostPath:
                path: /
    
    kubectl apply -f node-export.yaml
    
    # 检查
    kubectl get pods -n monitor-sa
    

    通过 http://主机ip:9100/metrics 查看当前主机获取到的所有监控数据

    部署 prometheus server

    # 在 master 上操作
    # 创建一个sa账号
    kubectl create serviceaccount monitor -n monitor-sa  
    
    # 把sa账号monitor通过clusterrolebing绑定到clusterrole上
    kubectl create clusterrolebinding monitor-clusterrolebinding -n monitor-sa --clusterrole=cluster-admin  --serviceaccount=monitor-sa:monitor
    
    # 创建数据目录
    # 在k8s集群的一个node节点 k8s-node1,所以在 k8s-node1 上操作如下命令:
    mkdir /data/prometheus-data -p
    cd /data/
    chmod 777 prometheus-data
    
    # 创建一个configmap存储卷,用来存放prometheus配置信息
    # 在 master 上操作
    mkdir -p /data/yaml/monitor-sa/prometheus
    
    cd /data/yaml/monitor-sa/prometheus
    
    cat prometheus-cfg.yaml
    ---
    kind: ConfigMap
    apiVersion: v1
    metadata:
      labels:
        app: prometheus
      name: prometheus-config
      namespace: monitor-sa
    data:
      prometheus.yml: |
        global:
          scrape_interval: 15s
          scrape_timeout: 10s
          evaluation_interval: 1m
        scrape_configs:
        - job_name: 'kubernetes-node'
          kubernetes_sd_configs:
          - role: node
          relabel_configs:
          - source_labels: [__address__]
            regex: '(.*):10250'
            replacement: '${1}:9100'
            target_label: __address__
            action: replace
          - action: labelmap
            regex: __meta_kubernetes_node_label_(.+)
        - job_name: 'kubernetes-node-cadvisor'
          kubernetes_sd_configs:
          - role:  node
          scheme: https
          tls_config:
            ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
          bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
          relabel_configs:
          - action: labelmap
            regex: __meta_kubernetes_node_label_(.+)
          - target_label: __address__
            replacement: kubernetes.default.svc:443
          - source_labels: [__meta_kubernetes_node_name]
            regex: (.+)
            target_label: __metrics_path__
            replacement: /api/v1/nodes/${1}/proxy/metrics/cadvisor
        - job_name: 'kubernetes-apiserver'
          kubernetes_sd_configs:
          - role: endpoints
          scheme: https
          tls_config:
            ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
          bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
          relabel_configs:
          - source_labels: [__meta_kubernetes_namespace, __meta_kubernetes_service_name, __meta_kubernetes_endpoint_port_name]
            action: keep
            regex: default;kubernetes;https
        - job_name: 'kubernetes-service-endpoints'
          kubernetes_sd_configs:
          - role: endpoints
          relabel_configs:
          - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scrape]
            action: keep
            regex: true
          - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scheme]
            action: replace
            target_label: __scheme__
            regex: (https?)
          - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_path]
            action: replace
            target_label: __metrics_path__
            regex: (.+)
          - source_labels: [__address__, __meta_kubernetes_service_annotation_prometheus_io_port]
            action: replace
            target_label: __address__
            regex: ([^:]+)(?::\d+)?;(\d+)
            replacement: $1:$2
          - action: labelmap
            regex: __meta_kubernetes_service_label_(.+)
          - source_labels: [__meta_kubernetes_namespace]
            action: replace
            target_label: kubernetes_namespace
          - source_labels: [__meta_kubernetes_service_name]
            action: replace
            target_label: kubernetes_name 
    
    kubectl apply -f prometheus-cfg.yaml
    
    # 部署 prometheus
    cat prometheus-deploy.yaml 
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: prometheus-server
      namespace: monitor-sa
      labels:
        app: prometheus
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: prometheus
          component: server
      template:
        metadata:
          labels:
            app: prometheus
            component: server
          annotations:
            prometheus.io/scrape: 'false'
        spec:
          nodeName: k8s-node1
          serviceAccountName: monitor
          containers:
          - name: prometheus
            image: harbor.junengcloud.com/monitor/prometheus:v2.2.1
            imagePullPolicy: IfNotPresent
            command:
              - prometheus
              - --config.file=/etc/prometheus/prometheus.yml
              - --storage.tsdb.path=/prometheus
              - --storage.tsdb.retention=720h
              - --web.enable-lifecycle
            ports:
            - containerPort: 9090
              protocol: TCP
            volumeMounts:
            - mountPath: /etc/prometheus/prometheus.yml
              name: prometheus-config
              subPath: prometheus.yml
            - mountPath: /prometheus/
              name: prometheus-storage-volume
          volumes:
            - name: prometheus-config
              configMap:
                name: prometheus-config
                items:
                  - key: prometheus.yml
                    path: prometheus.yml
                    mode: 0644
            - name: prometheus-storage-volume
              hostPath:
               path: /data/prometheus-data
               type: Directory
    
    kubectl apply -f prometheus-deploy.yaml 
    
    # 部署 svc
    cat prometheus-svc.yaml 
    apiVersion: v1
    kind: Service
    metadata:
      name: prometheus
      namespace: monitor-sa
      labels:
        app: prometheus
    spec:
      type: NodePort
      ports:
        - port: 9090
          targetPort: 9090
          nodePort: 30090
          protocol: TCP
      selector:
        app: prometheus
        component: server
    
    kubectl apply -f prometheus-svc.yaml 
    

    访问 http://192.168.1.116:30090/targets

    热更新 prometheus

    为了每次修改配置文件可以热加载prometheus,也就是不停止prometheus,就可以使配置生效,修改了prometheus-cfg.yaml文件中prometheus的配置,通过kubectl apply -f prometheus-cfg.yaml和kubectl apply -f prometheus-deploy.yaml更新资源清单文件,想要使配置生效可用如下热加载命令:

    kubectl get pod -n monitor-sa -o wide |grep  prometheus-server
    # 172.16.36.67 为 pod prometheus-server 的 ip
    curl -X POST http://172.16.36.67:9090/-/reload
    
  • 相关阅读:
    "此页的状态信息无效,可能已损坏。”的解决办法 dodo
    C#AdServer相关操作 dodo
    E: 有未能满足的依赖关系。请尝试不指明软件包的名字来运行“aptget f install”(也可以指定一个解决办法)。 dodo
    通过ASP.NET连接Oracle数据库 dodo
    System.Runtime.InteropServices.ExternalException: GDI+ 中发生一般性错误 dodo
    FAT32转NTFS无法输入正确卷标 dodo
    Virtual PC 虚拟机虚拟硬盘压缩 dodo
    Destination host unreachable 一般解决办法 dodo
    15个优秀的第三方 Web 技术集成 dodo
    asp.net 2.0关于NavigateUrl中绑定Eval()方法时出现"服务器标记的格式不正确"的解决方法 dodo
  • 原文地址:https://www.cnblogs.com/klvchen/p/15035400.html
Copyright © 2020-2023  润新知