prometheus-operator部署

Prometheus学习整理-Prometheus-operator 阅读详情

一、前言

prometheus-operator相比较传统的prometheus主要是使用CRD的方式实现部署的自动化,还可以自动发现监控的节点、自动发现监控规则并更新,不需要像传统的方式一样需要手动执行命令热加载,本篇文章使用prometheus-operator的方式部署prometheus、alertmange、servicemonitor、prometheusrule并且使用prometheusalert组件告警到企业微信机器人,本人使用的是k8s 1.23.0版本

二、实施

创建命名空间

kubectl create ns prometheus

创建私有仓库拉取凭据

 kubectl create secret docker-registry monitor-secret --namespace=prometheus --docker-server=https://xxx.xxx.com --docker-username=xxx --docker-password=xxx

部署prometheus-operator

在github上下载yaml部署文件,根据自己的k8s集群版本选择对应的operator版本

参考连接:https://github.com/prometheus-operator/prometheus-operator/blob/main/bundle.yaml

下载完成后可以自定义镜像、以及命名空间

#需要更改namespace的位置
kind: ClusterRoleBinding
  namespace: prometheus
kind: Deployment
  namespace: prometheus
kind: ServiceAccount
  namespace: prometheus
kind: Service
  namespace: prometheus

#需要更改image的位置
kind: Deployment
  image: xxx.xxx.com/monitor/prometheus-operator:v0.66.0
  - --prometheus-config-reloader=xxx.xxx.com/monitor/prometheus-config-reloader:v0.66.0

#更改私有仓库镜像后需要增加拉取镜像凭据配置

  imagePullSecrets:
  - name: monitor-secret

更改完成后直接执行以下命令部署

#需要使用create命令,使用apply命令会报错
kubectl create -f bundle.yaml

部署完成后可以看到创建的operator

kubectl get pod -n prometheus

部署prometheus,prometheus通过创建yaml文件,然后由rbc识别自动部署

deployment.yaml

apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
  name: prometheus
  namespace: prometheus
spec:
  replicas: 1
  # 1. 使用私有仓库镜像
  image: harbor.ylbeijing.com/monitor/prometheus:latest  # 替换为你的私有仓库地址
  imagePullPolicy: IfNotPresent
  
  # 2. 私有仓库认证(如果需要)
  imagePullSecrets:
  - name: monitor-secret  # 需要提前创建 docker-registry secret
  
  #配置altermanage告警
  alerting:
    alertmanagers:
    - namespace: prometheus
      name: alertmanager-operated
      port: 9093

  #用于发现k8s集群外的监控服务
  additionalScrapeConfigs:
    name: additional-scrape-configs
    key: prometheus-additional.yaml

  # 3. 使用 PVC 持久化存储
  storage:
    volumeClaimTemplate:
      spec:
        accessModes: ["ReadWriteMany"]
        resources:
          requests:
            storage: 30Gi
        # 如果使用方式B的本地存储,取消下面的注释
        storageClassName: nfs

  #使用root模式,因为nfs配置的目录权限是root,用其他角色权限不够报错
  securityContext:
    fsGroup: 0
    runAsUser: 0
    runAsNonRoot: false
 
  # 4. 基础配置
  serviceAccountName: prometheus
  replicas: 1
  retention: 30d  # 数据保留30天
  retentionSize: 30GB  # 可选:限制存储大小
  
  # 5. ServiceMonitor 选择器
  serviceMonitorSelector:
    matchLabels: {}
  serviceMonitorNamespaceSelector: {}
  
  #prometheus告警规则自动发现
  ruleSelector:
    matchLabels: {}
  ruleNamespaceSelector: {}

  # 6. 资源配置
  resources:
    requests:
      memory: 1Gi
      cpu: 1000m
    limits:
      memory: 2Gi
      cpu: 2000m
  
  # 7. 节点亲和性(使用本地存储时必须指定)
  # nodeSelector:
  #   kubernetes.io/hostname: your-node-name

rbac.yaml

apiVersion: v1
kind: ServiceAccount
metadata:
  name: prometheus
  namespace: prometheus
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: prometheus
rules:
- apiGroups:
  - ""
  resources:
  - nodes
  - services
  - endpoints
  - pods
  - nodes/proxy
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - "extensions"
  resources:
    - ingresses
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - ""
  resources:
  - configmaps
  - nodes/metrics
  verbs:
  - get
- nonResourceURLs:
  - /metrics
  verbs:
  - get
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: prometheus
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: prometheus
subjects:
- kind: ServiceAccount
  name: prometheus
  namespace: prometheus

pv.yaml 

apiVersion: v1
kind: PersistentVolume
metadata:
  name: "prometheus-data-pv"
  labels:
    name: prometheus-data-pv
    release: stable
spec:
  capacity:
    storage: 30Gi
  accessModes:
    - ReadWriteMany            #配置读写方式
  persistentVolumeReclaimPolicy: Retain  #配置回收策略为Retain,既删除pv保留数据
  storageClassName: nfs       #使用nfs存储
  nfs:         #配置nfs路径信息
    path: /xxx/xxx/xxxx
    server: xx.xx.xx.xx

pvc.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: prometheus-data-pvc
  namespace: prometheus
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 30Gi
  selector:
    matchLabels:
      name: prometheus-data-pv
      release: stable
  storageClassName: nfs

service.yaml

kind: Service
apiVersion: v1
metadata:
  annotations:
    prometheus.io/scrape: 'true'
  labels:
    app.kubernetes.io/name: prometheus
  name: prometheus-service
  namespace: prometheus
spec:
  ports:
    - port: 9090
      targetPort: 9090
      nodePort: 30090
  selector:
    app.kubernetes.io/name: prometheus
  type: NodePort

部署servicemonitor 用于pod自动发现

servicemonitor.yaml

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: servicemonitor          # 给这个监控任务起个名字
  namespace: prometheus          # 建议与 Prometheus 实例所在命名空间一致
  labels:
    # 【关键】这里的标签需要能被你的 Prometheus 实例的 serviceMonitorSelector 选中
    # 你可以通过以下命令查看你的 Prometheus 实例需要什么标签
    # kubectl describe prometheus prometheus -n prometheus | grep "Service Monitor Selector"
    release: prometheus          # 这是一个常见示例,请根据你的实际配置修改
spec:
  # 选择要监控的 Service
  selector:
    matchLabels:
      #app: my-app               # 匹配 labels 为 app: my-app 的 Service
      monitoring: "enabled"  # 所有服务只要打了这个标签就被监控
  # 配置采集端点
  endpoints:
  - port: metrics               # 指定 Service 上定义的端口名称,必须与要自动发现的svc端口名称对应不然识别不到
    interval: 30s               # 每30秒采集一次
  # 指定要监控的 Service 所在的命名空间
  namespaceSelector:
    #matchNames:
    #- my-namespace              # 替换为你的应用所在的命名空间
    any: true  # 监控所有命名空间

部署prometheusrule用于prometheus自动发现告警规则

以下规则仅供参考,需要其他的告警规则可以参考以下网站

prometheus告警规则网站:Awesome Prometheus Alerts | Copy-pasteable Prometheus alerting rules

prometheusrule.yaml 

apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
  name: prometheusrule
  namespace: prometheus            # 通常放在和 Prometheus 相同的命名空间
  labels:
    # 同样,这里的 labels 需要被你的 Prometheus 实例的 ruleSelector 选中
    release: prometheus
spec:
  groups:
  - name: nodealerts
    rules:
    - alert: 内存使用率过高
      expr: 100 - (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) * 100 > 85
      for: 3m
      labels:
        severity: critical
      annotations:
        summary: "{{ $labels.instance }} 内存使用率过高, 请尽快处理!"
        description: "{{ $labels.instance }}内存使用率超过85%,当前使用率{{ $value }}%."
    - alert: 服务器宕机
      expr: up == 0
      for: 1s
      labels:
        severity: critical
      annotations:
        summary: "{{$labels.instance}} 服务器宕机, 请尽快处理!"
        description: "{{$labels.instance}} 服务器延时超过3分钟,当前状态{{ $value }}. "
    - alert: CPU高负荷
      expr: 100 - (avg by (instance,job)(irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 80
      for: 5m
      labels:
        severity: critical
      annotations:
        summary: "{{$labels.instance}} CPU使用率过高,请尽快处理!"
        description: "{{$labels.instance}} CPU使用大于80%,当前使用率{{ $value }}%. "
      
    - alert: 磁盘IO性能
      expr: avg(irate(node_disk_io_time_seconds_total[1m])) by(instance,job)* 100 > 80
      for: 5m
      labels:
        severity: critical
      annotations:
        summary: "{{$labels.instance}} 流入磁盘IO使用率过高,请尽快处理!"
        description: "{{$labels.instance}} 流入磁盘IO大于80%,当前使用率{{ $value }}%."
 
 
    - alert: 网络流入
      expr: ((sum(rate (node_network_receive_bytes_total{device!~'tap.*|veth.*|br.*|docker.*|virbr*|lo*'}[5m])) by (instance,job)) / 100) > 102400
      for: 5m
      labels:
        severity: critical
      annotations:
        summary: "{{$labels.instance}} 流入网络带宽过高,请尽快处理!"
        description: "{{$labels.instance}} 流入网络带宽持续5分钟高于100M. RX带宽使用量{{$value}}."
 
    - alert: 网络流出
      expr: ((sum(rate (node_network_transmit_bytes_total{device!~'tap.*|veth.*|br.*|docker.*|virbr*|lo*'}[5m])) by (instance,job)) / 100) > 102400
      for: 5m
      labels:
        severity: critical
      annotations:
        summary: "{{$labels.instance}} 流出网络带宽过高,请尽快处理!"
        description: "{{$labels.instance}} 流出网络带宽持续5分钟高于100M. RX带宽使用量{$value}}."
  
    - alert: TCP连接数
      expr: node_netstat_Tcp_CurrEstab > 10000
      for: 2m
      labels:
        severity: critical
      annotations:
        summary: " TCP_ESTABLISHED过高!"
        description: "{{$labels.instance}} TCP_ESTABLISHED大于100%,当前使用率{{ $value }}%."
 
    - alert: 磁盘容量
      expr: 100-(node_filesystem_free_bytes{fstype=~"ext4|xfs"}/node_filesystem_size_bytes {fstype=~"ext4|xfs"}*100) > 80
      for: 1m
      labels:
        severity: critical
      annotations:
        summary: "{{$labels.mountpoint}} 磁盘分区使用率过高,请尽快处理!"
        description: "{{$labels.instance}} 磁盘分区使用大于80%,当前使用率{{ $value }}%."
  - name: winalerts
    rules:
    - alert: WindowsServerServiceStatus
      expr: windows_service_status{status="ok"} != 1
      for: 1m
      labels:
        severity: critical
      annotations:
        summary: Windows Server service Status (instance {{ $labels.instance }})
        description: "Windows Service state is not OK\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"
    - alert: WindowsServerCPUUsage
      expr: 100 - (avg by (instance) (rate(windows_cpu_time_total{mode="idle"}[2m])) * 100) > 80
      for: 0m
      labels:
        severity: warning
      annotations:
        summary: Windows Server CPU Usage (instance {{ $labels.instance }})
        description: "CPU Usage is more than 80%\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"
    - alert: WindowsServerMemoryUsage
      expr: 100 - ((windows_os_physical_memory_free_bytes / windows_cs_physical_memory_bytes) * 100) > 80
      for: 2m
      labels:
        severity: warning
      annotations:
        summary: Windows Server memory Usage (instance {{ $labels.instance }})
        description: "Memory usage is more than 80%\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"
    - alert: WindowsServerDiskSpaceUsage
      expr: 100 - 100 * (windows_logical_disk_free_bytes / windows_logical_disk_size_bytes) > 80 and windows_logical_disk_size_bytes > 0
      for: 2m
      labels:
        severity: critical
      annotations:
        summary: Windows Server disk Space Usage (instance {{ $labels.instance }})
        description: "Disk usage is more than 80%\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"
  - name: k8salerts
    rules:
    - alert: KubernetesNodeNotReady
      expr: kube_node_status_condition{condition="Ready",status="true"} == 0
      for: 10m
      labels:
        severity: critical
      annotations:
        summary: Kubernetes Node not ready (instance {{ $labels.instance }})
        description: "Node {{ $labels.node }} has been unready for a long time\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"
    - alert: KubernetesNodeSchedulingDisabled
      expr: kube_node_spec_taint{key="node.kubernetes.io/unschedulable"} == 1
      for: 30m
      labels:
        severity: warning
      annotations:
        summary: Kubernetes Node scheduling disabled (instance {{ $labels.instance }})
        description: "Node {{ $labels.node }} has been marked as unschedulable for more than 30 minutes.\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"
    - alert: KubernetesStatefulSetDown
      expr: kube_statefulset_replicas != kube_statefulset_status_replicas_ready > 0
      for: 1m
      labels:
        severity: critical
      annotations:
        summary: Kubernetes StatefulSet down (instance {{ $labels.instance }})
        description: "StatefulSet {{ $labels.namespace }}/{{ $labels.statefulset }} went down\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"
    - alert: KubernetesPodNotHealthy
      expr: sum by (namespace, pod) (kube_pod_status_phase{phase=~"Pending|Unknown|Failed"}) > 0
      for: 15m
      labels:
        severity: critical
      annotations:
        summary: Kubernetes Pod not healthy (instance {{ $labels.instance }})
        description: "Pod {{ $labels.namespace }}/{{ $labels.pod }} has been in a non-running state for longer than 15 minutes.\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"
    - alert: KubernetesPodCrashLooping
      expr: increase(kube_pod_container_status_restarts_total[1m]) > 3
      for: 2m
      labels:
        severity: warning
      annotations:
        summary: Kubernetes pod crash looping (instance {{ $labels.instance }})
        description: "Pod {{ $labels.namespace }}/{{ $labels.pod }} is crash looping\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"
    - alert: KubernetesDeploymentReplicasMismatch
      expr: kube_deployment_spec_replicas != kube_deployment_status_replicas_available
      for: 10m
      labels:
        severity: warning
      annotations:
        summary: Kubernetes Deployment replicas mismatch (instance {{ $labels.instance }})
        description: "Deployment {{ $labels.namespace }}/{{ $labels.deployment }} replicas mismatch\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"
    - alert: KubernetesStatefulSetReplicasMismatch
      expr: kube_statefulset_status_replicas_ready != kube_statefulset_status_replicas
      for: 10m
      labels:
        severity: warning
      annotations:
        summary: Kubernetes StatefulSet replicas mismatch (instance {{ $labels.instance }})
        description: "StatefulSet does not match the expected number of replicas.\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"
    - alert: KubernetesClientCertificateExpiresNextWeek
      expr: apiserver_client_certificate_expiration_seconds_count{job="apiserver"} > 0 and histogram_quantile(0.01, sum by (job, le) (rate(apiserver_client_certificate_expiration_seconds_bucket{job="apiserver"}[5m]))) < 7*24*60*60
      for: 0m
      labels:
        severity: warning
      annotations:
        summary: Kubernetes client certificate expires next week (instance {{ $labels.instance }})
        description: "A client certificate used to authenticate to the apiserver is expiring next week.\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"
  - name: mysqlalerts
    rules:
    - alert: MySQLDown
      expr: mysql_up == 0
      for: 1m
      labels:
        severity: critical
      annotations:
        summary: MySQL down (instance {{ $labels.instance }})
        description: "MySQL instance is down on {{ $labels.instance }}\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"
    - alert: MySQLTooManyConnections(>80%)
      expr: max_over_time(mysql_global_status_threads_connected[1m]) / mysql_global_variables_max_connections * 100 > 80 and mysql_global_variables_max_connections > 0
      for: 2m
      labels:
        severity: warning
      annotations:
        summary: MySQL too many connections (> 80%) (instance {{ $labels.instance }})
        description: "More than 80% of MySQL connections are in use on {{ $labels.instance }}\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"
    - alert: MySQLHighThreadsRunning
      expr: max_over_time(mysql_global_status_threads_running[1m]) / mysql_global_variables_max_connections * 100 > 60 and mysql_global_variables_max_connections > 0
      for: 2m
      labels:
        severity: warning
      annotations:
        summary: MySQL high threads running (instance {{ $labels.instance }})
        description: "More than 60% of MySQL connections are in running state on {{ $labels.instance }}\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"
    - alert: MySQLSlowQueries
      expr: delta(mysql_global_status_slow_queries[1m]) > 0
      for: 2m
      labels:
        severity: warning
      annotations:
        summary: MySQL slow queries (instance {{ $labels.instance }})
        description: "MySQL server has some new slow queries ({{ $value }} in the last minute).\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"
  - name: blackboxalerts
    rules:
    - alert: BlackboxProbeFailed
      expr: probe_success == 0
      for: 1m
      labels:
        severity: critical
      annotations:
        summary: Blackbox probe failed (instance {{ $labels.instance }})
        description: "Probe failed\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"
    - alert: BlackboxProbeHTTPFailure
      expr: probe_http_status_code <= 199 OR probe_http_status_code >= 400
      for: 1m
      labels:
        severity: critical
      annotations:
        summary: Blackbox probe HTTP failure (instance {{ $labels.instance }})
        description: "HTTP status code is not 200-399\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"
    - alert: BlackboxSSLCertificateWillExpireVerySoon
      expr: 0 <= round((last_over_time(probe_ssl_earliest_cert_expiry[10m]) - time()) / 86400, 0.1) < 3
      for: 0m
      labels:
        severity: critical
      annotations:
        summary: Blackbox SSL certificate will expire very soon (instance {{ $labels.instance }})
        description: "SSL certificate expires in less than 3 days\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"

部署用于发现k8s集群外的监控数据接口服务

vm-scrape-secret.yaml

#用于发现k8s集群外的监控数据接口
apiVersion: v1
kind: Secret
metadata:
  name: additional-scrape-configs
  namespace: prometheus

stringData:
  prometheus-additional.yaml: |
    - job_name: 'vm-node-exporter'   #用于发现集群外的node_exporter数据接口

      static_configs:
      - targets:
        - xxx.x.xxx.xxx:9100
        - xxx.xxx.xxx.xxx:9100
        - xxx.xxx.xxx.xxx:9101
    
    - job_name: 'blackbox-http'    #用于发现集群外的blackbox_exporter数据接口

      metrics_path: /probe

      params:
        module:
        - http_2xx

      static_configs:
      - targets:
        - https://xxx.xxxx.com
        - http://xxx.xxxx.com

      relabel_configs:

      - source_labels:
        - __address__
        target_label: __param_target

      - source_labels:
        - __param_target
        target_label: instance

      - target_label: __address__
        replacement: blackbox-exporter.prometheus.svc:9115

执行以下命令部署以上yaml文件的服务

kubectl apply -f ./

部署alertmange服务用于接收prometheus的告警

deployment.yaml

#指定镜像,用rbc自动部署
apiVersion: monitoring.coreos.com/v1
kind: Alertmanager
metadata:
  name: alertmanager
  namespace: prometheus

spec:
  replicas: 1
  image: xxx/xxx/alertmanager:latest  # 替换为你的私有仓库地址
  imagePullPolicy: IfNotPresent
  
  # 2. 私有仓库认证(如果需要)
  imagePullSecrets:
  - name: monitor-secret  # 需要提前创建 docker-registry secret

service.yaml 

apiVersion: v1
kind: Service
metadata:
  name: svc-alertmanager
  namespace: prometheus
spec:
  type: NodePort
  ports:
  - port: 9093
    protocol: TCP
    targetPort: 9093
    nodePort: 30011
  selector:
    app.kubernetes.io/name: alertmanager

secret.yaml 

apiVersion: v1
kind: Secret
metadata:
  name: alertmanager-alertmanager
  namespace: prometheus

stringData:
  alertmanager.yaml: |
    global:
      resolve_timeout: 1m
    route:
      group_by: ['instance']
      group_wait: 30s    #告警等待发送时间
      group_interval: 5m
      repeat_interval: 15m   #重复告警的再次发送时间
      receiver: prometheusalert       #调用的告警组件,下面配置的组件名称
    receivers:
    - name: 'prometheusalert'
      webhook_configs:
      - "url": "http://prometheus-alert-svc.prometheus:8080/prometheusalert?type=wx&tpl=prometheus-wx&wxurl=https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxx"
#以上url使用的是prometheusalert,其中使用企业微信机器人的模式发送告警
        "send_resolved": true # 告警结束后是否通知已恢复

 执行以下命令部署以上yaml文件的服务

kubectl apply -f ./

部署prometheusalert服务用于接收alermange的告警然后发送到企业微信机器人

部署参考:https://feiyu563.gitbook.io/prometheusalert/system/system-prometheus

配置文件参考:https://github.com/feiyu563/PrometheusAlert/blob/master/conf/app-example.conf

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: prometheus-alert
  namespace: prometheus
spec:
  replicas: 1
  selector:
    matchLabels:
      app: prometheus-alert
  template:
    metadata:
      labels:
        app: prometheus-alert
    spec:
      containers:
      - name: prometheus-alert
        image: xxxx/xxx/prometheus-alert:v4.9.2
        ports:
        - containerPort: 8080
        volumeMounts:
        - name: config
          mountPath: /app/conf/app.conf
          subPath: app.conf
      volumes:
      - name: config
        configMap:
          name: prometheus-alert-conf
      imagePullSecrets:
      - name: monitor-secret

configmap.yaml 

apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-alert-conf
  namespace: prometheus # 根据你的命名空间调整
data:
  app.conf: |
    #登录用户名
    login_user=prometheusalert
    #登录密码
    login_password=prometheusalert
    #监听地址
    httpaddr = "0.0.0.0"
    #监听端口
    httpport = 8080
    runmode = dev
    copyrequestbody = true
    title=PrometheusAlert
    logtype=file
    logpath=logs/prometheusalertcenter.log
    prometheus_cst_time=0
    db_driver=sqlite3
    AlertRecord=1
    RecordLive=1
    RecordLiveDay=30
    maxIdleConns=100
    open-hotreload=0
    open-weixin=1
    wxurl=https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxx
    open-alertgroup=0

service.yaml

apiVersion: v1
kind: Service
metadata:
  name: prometheus-alert-svc
  namespace: prometheus
spec:
  type: NodePort
  selector:
    app: prometheus-alert
  ports:
  - port: 8080
    targetPort: 8080
    nodePort: 30012
    protocol: TCP

 执行以下命令部署以上yaml文件的服务

kubectl apply -f ./

部署node_exporter服务监控主机数据

监控k8s集群主机

daemonset.yaml

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: node-exporter
  namespace: prometheus
  labels:
    app: node-exporter

spec:
  selector:
    matchLabels:
      app: node-exporter

  template:
    metadata:
      labels:
        app: node-exporter
        monitoring: "enabled"

    spec:
      hostNetwork: true
      hostPID: true
      hostIPC: true

      tolerations:
      - operator: Exists

      containers:
      - name: node-exporter
        image: harbor.ylbeijing.com/monitor/node-exporter:v1.7.0

        args:
        - --path.procfs=/host/proc
        - --path.sysfs=/host/sys
        - --path.rootfs=/host/root

        ports:
        - name: metrics
          containerPort: 9100

        securityContext:
          privileged: true

        volumeMounts:
        - name: proc
          mountPath: /host/proc
          readOnly: true

        - name: sys
          mountPath: /host/sys
          readOnly: true

        - name: root
          mountPath: /host/root
          readOnly: true

      volumes:
      - name: proc
        hostPath:
          path: /proc

      - name: sys
        hostPath:
          path: /sys

      - name: root
        hostPath:
          path: /
      imagePullSecrets:
      - name: monitor-secret

service.yaml

apiVersion: v1
kind: Service
metadata:
  name: node-exporter
  namespace: prometheus

  labels:
    monitoring: "enabled"   #用于servicemonitor自动发现

spec:
  selector:
    app: node-exporter

  ports:
  - name: metrics
    port: 9100
    targetPort: 9100

 执行以下命令部署以上yaml文件的服务

kubectl apply -f ./

监控虚拟机,使用脚本部署(我这里把自动下载安装包注释掉了,需要自行下载上传)

node_exporter.sh

#!/bin/bash
#mkdir /opt/node_exporter && cd /opt/node_exporter
# 下载并解压
#wget https://github.com/prometheus/node_exporter/releases/download/v1.7.0/node_exporter-1.7.0.linux-amd64.tar.gz
tar -xvf node_exporter-1.7.0.linux-amd64.tar.gz
sudo mv node_exporter-1.7.0.linux-amd64/node_exporter /usr/local/bin/

# 创建 systemd 服务 (可选,用于开机自启)
sudo tee /etc/systemd/system/node_exporter.service > /dev/null <<EOF
[Unit]
Description=Node Exporter
After=network.target

[Service]
User=nobody
ExecStart=/usr/local/bin/node_exporter

[Install]
WantedBy=default.target
EOF

# 启动服务
sudo systemctl daemon-reload
sudo systemctl start node_exporter
sudo systemctl enable node_exporter

部署完成后即可在prometheus中看到监控的虚拟机

部署kube-state-metrics服务监控k8s集群数据

部署文件参考:https://github.com/kubernetes/kube-state-metrics/tree/v2.7.0/examples/standard

重点需要更改service.yaml使得servicemonitor可以自动发现

service.yam

apiVersion: v1
kind: Service
metadata:
  labels:
    app.kubernetes.io/component: exporter
    app.kubernetes.io/name: kube-state-metrics
    app.kubernetes.io/version: 2.7.0
    monitoring: enabled   #增加servicemonitor自动发现的标签
  name: kube-state-metrics
  namespace: prometheus
spec:
  clusterIP: None
  ports:
  - name: metrics
    port: 8080
    targetPort: 8080
  - name: telemetry
    port: 8081
    targetPort: 8081
  selector:
    app.kubernetes.io/name: kube-state-metrics

部署mysql-exporter服务监控mysql数据

需要提前在数据库创建用户

CREATE USER 'exporter'@'%' IDENTIFIED BY '你的密码' WITH MAX_USER_CONNECTIONS 3;
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'%';
FLUSH PRIVILEGES;

deployment.yaml 

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-exporter
  namespace: prometheus
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql-exporter
  template:
    metadata:
      labels:
        app: mysql-exporter
    spec:
      containers:
      - name: mysql-exporter
        image: harbor.ylbeijing.com/monitor/mysqld-exporter:latest  # 建议使用较新版本
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 9104
          name: metrics
        args:
        - "--config.my-cnf=/etc/mysql/.my.cnf"

        volumeMounts:
        - name: mysql-config
          mountPath: /etc/mysql
          readOnly: true

      volumes:
      - name: mysql-config
        secret:
          secretName: mysql-exporter-secret
      imagePullSecrets:
      - name: monitor-secret
---
apiVersion: v1
kind: Service
metadata:
  name: mysql-exporter
  namespace: prometheus
  labels:
    app: mysql-exporter
    monitoring: enabled
spec:
  selector:
    app: mysql-exporter
  ports:
  - protocol: TCP
    port: 9104
    targetPort: 9104
    name: metrics

secret.yaml 

apiVersion: v1
kind: Secret
metadata:
  name: mysql-exporter-secret
  namespace: prometheus  # 建议和Prometheus在同一个命名空间
type: Opaque
stringData:
  .my.cnf: |
    [client]
    user=exporter
    password=xxxxx
    host=xxx.xx.xxx.xxxx
    port=xxx

部署blackbox-exporter服务监控网站可用性

还需要在prometheus配置集群外的自动发现,即上面编辑的vm-scrape-secret.yaml文件

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: blackbox-exporter
  namespace: prometheus

spec:
  replicas: 1

  selector:
    matchLabels:
      app: blackbox-exporter

  template:
    metadata:
      labels:
        app: blackbox-exporter

    spec:
      containers:
      - name: blackbox-exporter

        image: harbor.ylbeijing.com/monitor/blackbox-exporter:v0.27.0

        args:
        - --config.file=/etc/blackbox/blackbox.yml

        ports:
        - containerPort: 9115
          name: metrics

        volumeMounts:
        - name: config
          mountPath: /etc/blackbox

      volumes:
      - name: config
        configMap:
          name: blackbox-config
      imagePullSecrets:
      - name: monitor-secret

service.yaml 

apiVersion: v1
kind: Service
metadata:
  name: blackbox-exporter
  namespace: prometheus

  labels:
    monitoring: enabled

spec:
  selector:
    app: blackbox-exporter

  ports:
  - name: metrics
    port: 9115
    targetPort: 9115

configmap.yaml 

apiVersion: v1
kind: ConfigMap
metadata:
  name: blackbox-config
  namespace: prometheus

data:
  blackbox.yml: |
    modules:

      http_2xx:
        prober: http
        timeout: 5s
        http:
          preferred_ip_protocol: ip4

      tcp_connect:
        prober: tcp
        timeout: 5s

      icmp:
        prober: icmp
        timeout: 5s

Prometheus Operator(一) Prometheus Operator(一) 之前的文章介绍过传统方式安装Prometheus来监控集群,但是这种方式有一定的缺陷虽然完全可以用自定义方式来实现,但是不够灵活,不具有通用性。在自定义监控时还需要不断的更新Prometheus的配置。如果是kubernetes集群完全可以使用一种更原始的方式来实现kubernetes集群监控,即采用Prometheus Operator Operator是由CoreOS公司开发的用来扩展Kubernetes API的特定应用程序控制器,用来创建、配置和管理复 阅读详情

相关推荐

Prometheus Operator部署管理

kube-prometheus 是一整套监控解决方案,它使用 Prometheus 采集集群指标,Grafana 做展示,包含如下组件Grafana✔✔✗✗xxx✗✔✔✗xxx✗✗✔✔xxx✗✗✗x✔✔✔main✗✗✗xx✔✔Prometheus整体监控结构略微复杂,一个个部署并不简单,kube-prometheus大大提升了部署的方式通过自定义资源CRD维护简单,不用再次维护大量的configmap配置文件,操作流程大大简化。

s1440350254的博客 1220

总结:Prometheus Operator

一、Operator介绍 Operator是由CoreOS公司开发的用来扩展Kubernetes API的特定应用程序控制器,用来创建、配置和管理复杂的有状态应用,例如数据库、缓存和监控系统。Operator基于kubernetes的资源和控制器概念上构建,但同时又包含了应用程序特定的领域知识。创建Operator的关键是CRD(自定义资源)的设计。 Operator是将运维人员对软件操作的知识代码化,同时利用Kubernetes强大的抽象来管理大规模的软件应用。目前CoreOS官方提供了几种Opera

w2009211777的专栏 6648

Prometheus Operator部署和使用

说明 Prometheus-Operator官方提供的架构图: Operator是最核心的部分,作为一个控制器,他会去创建Prometheus、PodMonitor、ServiceMonitor、AlertManager以及PrometheusRule这5个CRD资源对象,然后会一直监控并维持这5个资源对象的状态。 其中创建的Prometheus这种资源对象就是作为Prometheus ...

twingao的专栏 1万+

prometheus-operator介绍

架构 这是官方的架构图 prometheus-operator的作用主要是用来创建prometheus的相关资源以及监视与管理它创建出来的资源对象。 Operator: 根据自定义资源(Custom Resource Definition / CRDs)来部署和管理 Prometheus Server,同时监控这些自定义资源事件的变化来做相应的处理,是整个系统的控制中心。 Prometheus:声明 Prometheus deployment 期望的状态,Operator 确保这个 deploy

lyh24k的博客 6446

Prometheus Operator 部署与配置实战指南

Prometheus Operator 是 Kubernetes 社区推出的一个管理 Prometheus 监控系统组件的项目。它提供了一种声明式的监控配置方法,通过定义 Kubernetes 自定义资源(Custom Resource Definitions,CRDs)来自动化监控系统的部署和管理。Prometheus Operator 监控动态变化的Kubernetes服务,提供了一种更加灵活且适应云原生架构的方式来管理和扩展 Prometheus。

weixin_29859471的博客 1192

从稳压管到LDO芯片:一张图看懂线性电源的‘内卷’进化史

本文追溯了从稳压管到LDO芯片的线性电源技术进化历程,详细解析了各阶段的技术突破与局限。重点探讨了现代LDO在超低功耗、高精度控制等方面的创新,并提供了实战选型的黄金法则,帮助工程师在电子设备小型化与低功耗化趋势中做出最优选择。

weixin_42525551的博客 206

prometheus operator

Prometheus 开源的系统监控和报警框架,本身也是一个时序数据库(TSDB)

qq_31055683的博客 5047

某二级支行网络的设计与实现——配置脚本(1)

【代码】某二级支行网络的设计与实现——配置脚本(1)

u010311846的博客 577

Prometheus Operator部署

文章目录一、Prometheus Operator的部署1、安装配置2、介绍1)Prometheus2)Alertmanager3)ThanosRuler4)ServiceMonitor5)PodMonitor6)Probe7)PrometheusRule8)AlertmanagerConfig3、安装4、配置二、自定义监控报警etcd 监控配置 PrometheusRule配置报警三、高级配置自动发现配置数据持久化 一、Prometheus Operator的部署 1、安装配置 前面的章节中我们学习了用自

weixin_46837396的博客 1202

SpringBoot多线程查询实战-查询库中所有数据多线程实现

但是,在数据量较大且服务器资源足够的情况下,多线程方法通常能够显著提高查询效率。此外,由于它是同步执行的,没有利用并行处理的能力,所以在处理大量数据时可能会比较慢。这个方法采用了分页查询的方式,每次只获取一部分数据,从而避免了内存溢出的问题。然而,它也是同步执行的,没有利用多线程或并行处理的能力。如果每次分页查询的数据量仍然很大,或者需要查询的轮数很多,那么这种方法的效率可能仍然会受到限制。​ ​ ​在比较这三种方式的效率时,我们需要考虑几个关键因素:数据量的大小、查询操作的复杂性、以及并行处理的能力。

勇敢牛牛,不怕困难! 4809

DAC60501与STM32的I2C通信实战:从寄存器配置到电压输出

本文详细介绍了如何使用STM32通过I2C通信驱动DAC60501数模转换器,实现精确的模拟电压输出。内容涵盖从硬件连接到软件模拟I2C驱动编写,再到DAC寄存器配置与电压计算的全流程实战指南,并提供了关键的驱动实例代码和常见问题调试技巧,帮助开发者快速掌握这一嵌入式开发中的实用技能。

weixin_29197835的博客 368

基于 Operator 部署 Prometheus 实现 K8S 监控

在 Kubernetes 集群的运维管理中,监控是必不可少的一环。Prometheus 作为一款优秀的开源监控系统,结合 Operator 方式进行部署,可以更便捷地实现对 K8S 的监控。 这种自动监控 K8S 集群的能力,使得基于 Operator 部署 Prometheus 成为一种高效、便捷且符合 Kubernetes 原生理念的监控解决方案,极大地降低了在 Kubernetes 环境中搭建和维护监控系统的复杂性,同时也提高了监控系统的可靠性和可扩展性,以下是基于 Operator 部署 Prom

成为一个内心平静的人,要多久 1577

前言:为什么要学习爬虫和逆向,该如何学习?

学习爬虫和逆向技术是当今数字化时代的重要技能,尤其在数据分析、安全研究、自动化工具开发等领域有广泛应用。以下是详细的学习理由、路径和方法:

数据知道的博客 1万+

Prometheus-Operator:快速部署

本博客讲解了Prometheus的简介,以及如何通过Prometheues Opeartor方式进行快速部署Prometheues以及Grafana模版使用等内容。

m0_60125201的博客 3533

C#部署YOLO三种方案深度实测:OpenCV DNN vs ONNX Runtime vs TensorRT

在工业视觉项目落地阶段,C#生态下部署YOLO模型主要有三条技术路线。很多开发者仅凭经验选型,往往在部署后期踩中性能瓶颈、兼容故障、维护成本超标的坑。本文基于工控机常见硬件配置,针对YOLOv8系列模型做控制变量实测,从推理性能、部署难度、工业稳定性、综合成本等多个维度做横向对比,给出可直接落地的选型结论。

这里只有干货:从 Modbus 通信到 MES 对接,从 YOLO 训练到工控机部署,带你搞定工业软件开发全流程。 466

如何解决群晖Docker注册表查询失败/无法拉取镜像等问题

如何解决群晖Docker注册表查询失败/无法拉取镜像等问题

XiaoqiangClub的博客 1万+

prometheus-operator手动部署

自定义报警使用钉钉推送消息安装配置方法修改 alertmanager 配置,接入 dingtalk-hook# 先将之前的 secret 对象删除 $ kubectl delete secret alertmanager-main -n monitoring。

运维老生常谈 1085

安装glibc出现../sysdeps/nptl/pthread.h:744:47:error:argument 1 of type ‘struct __jmp_buf_tag *‘ 解决方案

本文主要介绍了安装glibc出现…/sysdeps/nptl/pthread.h:744:47:error:argument 1 of type ‘struct __jmp_buf_tag *’ 解决方案,希望能对使用Linux的同学们有所帮助。 文章目录 1. 问题描述 2. 解决方案

weixin_43178406的博客 12万+

k8s1.23 部署Prometheus-Operator集群监控

kubernetes自定义资源:使用kubernetes CRD 来部署和管理Prometheus,Alertmanager和相关组件简化的部署配置:直接通过kubernetes资源清单配置Prometheus,比如版本,持久化,副本,保留策略等等配置Prometheus监控目标配置:基于熟知的kubernetes标签查询自动生成监控目标配置,无需学习prometheus特地的配置。

2228

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

Prometheus-Operator实战部署指南

Prometheus-Operator实战部署指南 【免费下载链接】prometheus-operator 项目地址: https://gitcode.com/gh_mirrors/pro/prometheus-operator...

gitblog_00666的博客 1186

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

在K8S中部署Prometheus-Operator

Prometheus-Operator 通过 CRD 将 Prometheus 的配置与 Kubernetes 集成,使得监控和报警变得更加高效和自动化。结合 ServiceMonitor 和 PrometheusRule,可以动态发现监控目标并管理报警规则,Alertmanager 和 Grafana 则分别负责告警通知和数据可视化。这套基于 CRD 的监控栈大大降低了 Kubernetes 监控的复杂度,是生产环境中不可或缺的一部分。

海鸥 868

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Prometheus Operator手动部署

一、prometheus-operator 介绍和功能prometheus-operator 介绍当今Cloud Native概念流行,对于容器、服务、节点以及集群的监控变得越来越重要。...

qq_24794401的博客 499

Prometheus Operator部署/管理Prometheus Server

Prometheus Operator & Kube-Prometheus & Helm chart 部署区别Prometheus Operator 是 Kubernetes 原生的工具,它通过将 Prometheus 资源定义为 Kubernetes 对象(CRD)来简化 Prometheus 集群的管理...

weixin_35866747的博客 819

kubernetes部署prometheus-operator

kubernetes1.22.9 部署prometheus-operator

scdn_fm的博客 660

1. 部署Prometheus Operator

定义了 9 条告警规则,覆盖 APIServer 的性能延迟错误率证书过期服务可用性四大类风险,按严重程度分为warning(警告)和critical(致命)两个级别,确保你能按优先级处理问题。

qq_55269107的博客 728

Kubernetes用operator部署prometheus

Operator 模式 Operator 是 Kubernetes 的扩展软件,它利用定制资源管理应用及其组件。 Operator 遵循 Kubernetes 的理念,特别是在控制器方面。 Operator的场景就是专门给有状态应用而设计的。 为什么只给有状态应用? 因为无状态应用简单啊,没有服务间的交互,要再开一家火锅店,跟k8s说一声,开一家一样的就可以了。 有状态不一样,你开了一家火锅店以后,客户的信息怎么同步,就涉及到与别的火锅店交涉的问题,当然你也可以写个别的程序做这个数据同步的操...

南宫乘风-Linux运维-虚拟化容器-Python编程 ownit.top 904
上一篇: windows中病毒处理
下一篇: 笔记本硬盘替换升级
Apex   Predator
博客等级 码龄6年 9371粉丝 289原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值