首页 > 其他分享 >k8s集群部署prometheus

k8s集群部署prometheus

时间:2024-07-26 16:39:23浏览次数:8  
标签:name rules labels Prometheus prometheus 集群 2.1 k8s

目录

一、Prometheus简介

1.1、前言

官网地址:https://prometheus.io/docs/prometheus/latest/getting_started/

  • 灵活的时间序列数据库;
  • 定制各式各样的监控规则;
  • Prometheus的开发人员和用户社区非常活跃;
  • 独立的开源项目,不依赖于任何公司;
  • 继Kurberntes之后第二个入驻的项目;

1.2、prometheus架构

image.png
Prometheus 的工作原理主要分为五个步骤:

    1. 数据采集(Exporters):Prometheus 定期通过HTTP请求从目标资源中拉取数据。目标资源可以是应用程序、系统、服务或其他资源。
  1. 数据存储(Storage):Prometheus 将采集到的数据存储在本地存储引擎中。存储引擎以时间序列方式存储数据,其中每个时间序列都由指标名称和一组键值对组成。
  2. 数据聚合(PromQL):Prometheus 通过查询表达式聚合数据。PromQL 是 Prometheus 的查询语言,它允许用户通过查询表达式从存储引擎中检索指标的特定信息。
  3. 告警处理(Alertmanager):Prometheus 可以根据用户指定的规则对数据进行警报。当指标的值超出特定阈值时,Prometheus 向 Alertmanager 发送警报。Alertmanager 可以帮助用户对警报进行分组、消除和路由,并将警报发送到相应的接收器,例如邮件、企微、钉钉等。
  4. 数据大盘(Grafana):帮助用户通过可视化方式展示 Prometheus 的数据,包括仪表盘、图表、日志和警报等。

1.3、prometheus时间序列数据

1.3.1、什么是序列数据?

时间序列数据(TimeSeries Data):按照时间顺序记录系统、设备状态变化的数据被称为时序数据。

1.3.2、时间序列数据特点

  • 性能好:关系型数据库对于大规模数据的处理性能糟糕。NOSQL 可以比较好的处理大规模数据,依然比不上时间序列数据库。
  • 存储成本低:高效的压缩算法,节省存储空间,有效降低 IO。

官方数据:Prometheus 有着非常高效的时间序列数据存储方法,每个采样数据仅仅占用 3.5byte 左右空间,上百万条时间序列,30 秒间隔,保留 60 天,大概200多G。

1.3.3、Promethues适合场景

Prometheus 非常适合记录任何纯数字时间序列。它既适合以机器为中心的监控,也适合监控高度动态的面向服务的体系架构。

二、部署配置

整个监控体系涉及的技术栈较多,几乎可覆盖真实企业中的所有场景。主要技术栈如下:

  • Prometheus:监控主服务
  • node-exporter:数据采集器
  • kube-state-metrics:数据采集器
  • metrics-server:数据采集器
  • Consul:自动发现
  • blackbox:黑盒拨测
  • Alertmanager:监控告警服务
  • Grafana:数据展示服务
  • prometheusAlert:告警消息转发服务

2.1、Prometheus部署

部署对外可访问Prometheus:

  1. 首先需要创建Prometheus所在命名空间;
  2. 然后创建Prometheus使用的RBAC规则;
  3. 创建Prometheus的configmap来保存配置文件;
  4. 创建service暴露Prometheus服务;
  5. 创建deployment部署Prometheus容器;
  6. 最后创建Ingress实现外部域名访问Prometheus。

2.1.1、创建命名空间

kubectl create namespace monitor

2.1.2、创建RBAC规则

创建RBAC规则,包含ServiceAccount、ClusterRole、ClusterRoleBinding三类YAML文件。


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

确认验证:


kubectl get sa prometheus -n monitor
 
kubectl get clusterrole prometheus
 
kubectl get clusterrolebinding prometheus

2.1.3、创建ConfigMap类型的Prometheus配置文件

apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-config
  namespace: monitor
data:
  prometheus.yml: |
    global:
      scrape_interval:     15s
      evaluation_interval: 15s
      external_labels:
        cluster: "kubernetes"
        
    ############ 数据采集job ###################
    scrape_configs:
    - job_name: prometheus
      static_configs:
      - targets: ['127.0.0.1:9090']
        labels:
          instance: prometheus
 
    ############ 指定告警规则文件路径位置 ###################
    rule_files:
    - /etc/prometheus/rules/*.rules

确认验证:


$ kubectl get cm prometheus-config -n monitor

2.1.4、创建ConfigMap类型的prometheus rules配置文件

使用ConfigMap方式创建prometheus rules配置文件:
包含的内容是两块,分别是general.rules和node.rules。使用以下命令创建Prometheus的另外两个配置文件:


apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-rules
  namespace: monitor
data:
  general.rules: |
    groups:
    - name: general.rules
      rules:
      - alert: InstanceDown
        expr: |
          up{job=~"k8s-nodes|prometheus"} == 0
        for: 1m
        labels:
          severity: critical
        annotations:
          summary: "Instance {{ $labels.instance }} 停止工作"
          description: "{{ $labels.instance }} 主机名:{{ $labels.hostname }} 已经停止1分钟以上."
 
  node.rules: |
    groups:
    - name: node.rules
      rules:
      - alert: NodeFilesystemUsage
        expr: |
          100 - (node_filesystem_avail_bytes / node_filesystem_size_bytes) * 100 > 85
        for: 1m
        labels:
          severity: warning
        annotations:
          summary: "Instance {{ $labels.instance }} : {{ $labels.mountpoint }} 分区使用率过高"
          description: "{{ $labels.instance }} 主机名:{{ $labels.hostname }} : {{ $labels.mountpoint }} 分区使用大于85% (当前值: {{ $value }})"

确认验证:

 kubectl get cm -n monitor prometheus-rules

2.1.5、创建prometheus svc


apiVersion: v1
kind: Service
metadata:
  name: prometheus
  namespace: monitor
  labels:
    k8s-app: prometheus
spec:
  type: ClusterIP
  ports:
  - name: http
    port: 9090
    targetPort: 9090
  selector:
    k8s-app: prometheus

2.1.6、创建prometheus pvc

由于Prometheus需要对数据进行持久化,以便在重启后能够恢复历史数据。所以这边我们通过早先课程部署的NFS做存储来实现持久化。
当前我们使用NFS提供的StorageClass来做数据存储。


apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: prometheus-data-pvc
  namespace: monitor
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: "nfs-storage"
  resources:
    requests:
      storage: 10Gi

2.1.7、创建proemtheus deploy


apiVersion: apps/v1
kind: Deployment
metadata:
  name: prometheus
  namespace: monitor
  labels:
    k8s-app: prometheus
spec:
  replicas: 1
  selector:
    matchLabels:
      k8s-app: prometheus
  template:
    metadata:
      labels:
        k8s-app: prometheus
    spec:
      serviceAccountName: prometheus
      containers:
      - name: prometheus
        image: prom/prometheus:v2.36.0
        imagePullPolicy: IfNotPresent
        ports:
        - name: http
          containerPort: 9090
        securityContext:
          runAsUser: 65534
          privileged: true
        command:
        - "/bin/prometheus"
        args:
        - "--config.file=/etc/prometheus/prometheus.yml"
        - "--web.enable-lifecycle"
        - "--storage.tsdb.path=/prometheus"
        - "--storage.tsdb.retention.time=10d"
        - "--web.console.libraries=/etc/prometheus/console_libraries"
        - "--web.console.templates=/etc/prometheus/consoles"
        resources:
          limits:
            cpu: 2000m
            memory: 2048Mi
          requests:
            cpu: 1000m
            memory: 512Mi
        readinessProbe:
          httpGet:
            path: /-/ready
            port: 9090
          initialDelaySeconds: 5
          timeoutSeconds: 10
        livenessProbe:
          httpGet:
            path: /-/healthy
            port: 9090
          initialDelaySeconds: 30
          timeoutSeconds: 30
        volumeMounts:
        - name: data
          mountPath: /prometheus
          subPath: prometheus
        - name: config
          mountPath: /etc/prometheus
        - name: prometheus-rules
          mountPath: /etc/prometheus/rules
      - name: configmap-reload
        image: jimmidyson/configmap-reload:v0.5.0
        imagePullPolicy: IfNotPresent
        args:
        - "--volume-dir=/etc/config"
        - "--webhook-url=http://localhost:9090/-/reload"
        resources:
          limits:
            cpu: 100m
            memory: 100Mi
          requests:
            cpu: 10m
            memory: 10Mi
        volumeMounts:
        - name: config
          mountPath: /etc/config
          readOnly: true
      volumes:
      - name: data
        persistentVolumeClaim:
          claimName: prometheus-data-pvc
      - name: prometheus-rules
        configMap:
          name: prometheus-rules
      - name: config
        configMap:
          name: prometheus-config

部署的 Deployment 资源文件中的 containers 部分配置了两个容器,分别是:

  • prometheus: Prometheus 容器是主容器,用于运行 Prometheus 进程。
  • configmap-reload: 用于监听指定的 ConfigMap 文件中的内容,如果内容发生更改,则执行 webhook url 请求,因为 Prometheus 支持通过接口重新加载配置文件,所以这里使用这个容器提供的机制来完成 Prometheus ConfigMap 配置文件内容一有更改,就执行 Prometheus 的 /-/reload 接口,进行更新配置操作。

上面资源文件中 Prometheus 参数说明:

  • --web.enable-lifecycle: 启用 Prometheus 用于重新加载配置的 /-/reload 接口
  • --config.file: 指定 Prometheus 配置文件所在地址,这个地址是相对于容器内部而言的
  • --storage.tsdb.path: 指定 Prometheus 数据存储目录地址,这个地址是相对于容器而言的
  • --storage.tsdb.retention.time: 指定删除旧数据的时间,默认为 15d
  • --web.console.libraries: 指定控制台组件依赖的存储路径
  • --web.console.templates: 指定控制台模板的存储路径

确认验证:


$ kubectl get deploy  -n monitor
$ kubectl get pods -n monitor

2.1.8、创建prometheus ingress实现外部域名访问


apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  namespace: monitor
  name: prometheus-ingress
spec:
  ingressClassName: nginx
  rules:
  - host: prometheus.k8s.cn
    http:
      paths:
        - pathType: Prefix
          backend:
            service:
              name: prometheus
              port:
                number: 9090
          path: /

访问验证:


# curl prometheus.k8s.cn
<a href="/graph">Found</a>.

标签:name,rules,labels,Prometheus,prometheus,集群,2.1,k8s
From: https://www.cnblogs.com/Unstoppable9527/p/18325698

相关文章

  • KingbaseES 集群运维典型案例 03 --“双主” sys_rewind恢复机制
    案例说明:主库主机系统重启,触发failover切换,原主库系统启动后,数据库服务被启动,出现“双主”,使用“repmgrnoderejoin--force-rewind”恢复集群。通过本案例了解,集群“双主”产生的原因及解决方案,并熟悉sys_rewind在集群恢复中的应用。数据库版本:KingbaseESV8R6一、集群架构......
  • 在K8S中,headless服务是什么?
    在Kubernetes(K8S)中,headless服务(无头服务)是一种特殊类型的服务,它与普通的K8S服务有所不同。通常情况下,服务(Service)的作用是为一组后端Pod提供一个稳定的网络入口,使得这些Pod可以通过服务的名称或IP地址被访问到。Headless服务的主要特点如下:不分配ClusterIP:......
  • 在K8S中,集群可以做哪些优化?
    在Kubernetes(K8S)集群中进行优化是一个多方面的任务,涉及从硬件层面到软件层面的诸多考虑。以下是一些常见的优化领域和技术:1.硬件优化选择合适的节点类型:根据工作负载的特点选择合适的计算、内存和存储资源。使用具有高I/O性能的SSD存储,对于I/O密集型工作负载尤......
  • 在K8S中,replicaset 和deploy有何区别?
    在Kubernetes(K8S)中,ReplicaSet和Deployment是两种非常重要的资源对象,它们都用于管理Pod的副本数量。尽管它们有一些相似之处,但在功能和用途上还是存在显著差异。下面详细介绍它们之间的区别:1.ReplicaSet定义:ReplicaSet是一种确保运行指定数量的Pod副本的Kuber......
  • 在K8S中,calico有哪些组件?都是做什么的?
    Calico是一个广泛使用的Kubernetes网络插件,它提供了一个高性能、可扩展的网络解决方案,用于连接和保护容器化的应用。Calico支持多种网络模型,包括BGP(BorderGatewayProtocol)和eBPF(ExtendedBerkeleyPacketFilter)技术,使其成为企业级应用的理想选择之一。以下是Cal......
  • 在K8S中,hpa原理是什么?
    在Kubernetes(K8S)中,HorizontalPodAutoscaler(HPA)是一种自动扩缩容机制,它可以根据预定义的指标自动调整Pod的数量。HPA的主要目的是确保应用程序能够根据实际负载自动伸缩,从而提高资源利用率和系统的弹性。1.HPA的工作原理定义目标指标:用户首先需要定义扩缩容......
  • 从K8s的“临时容器”看K8s设计的厉害之处
    本文分享自华为云社区《从K8s的“临时容器”看K8s设计的厉害之处》,作者:tsjsdbd。从一个容器的不足说起容器概念出现时,有个非常重要的理念:容器中极简。即容器里面只保留需要运行的进程就可以,其他一律不要安装。这也是为什么Docker出现的那时,有一篇文章《为什么不需要在Docker容......
  • 通过rancher部署k8s集群
    一、Rancher介绍Rancher是为使用容器的公司打造的容器管理平台。Rancher简化了使用Kubernetes的流程,开发者可以随处运行Kubernetes(RunKubernetesEverywhere),满足IT需求规范,赋能DevOps团队。Rancher可以创建来自Kubernetes托管服务提供商的集群,自动创建节点并安装K......
  • k8s证书续期10年
    现象通过kubeadm安装kubernetes集群时会存在一个证书问题:由kubeadm生成的客户端证书在1年后到期。随着kubernetes集群的使用,某一天证书过期了,此时kubernetes集群将无法正常使用,比如:kubectl命令执行会产生错误(Youmustbeloggedintotheserver(unauthorized))、......
  • 关于k8s中的pv和pvc无法绑定
    在一次申明pv和pvc时,发现两者怎么都无法绑定,以下是两个文件申明pvapiVersion:v1kind:PersistentVolumemetadata:name:my-pvspec:capacity:storage:5GiaccessModes:-ReadWriteManypersistentVolumeReclaimPolicy:RecyclestorageClassName:nfs......