关于Prometheus在K8S中的部署方案如何选择,以及分享手工部署的YAML

简介: 关于Prometheus在K8S中的部署方案如何选择,以及分享手工部署的YAML

关于Prometheus部署方案的选择

在以往的分享中,有分享过使用Prometheus Operator来管理Prometheus。但,在此同时,又抛出了个问题:是手工将Prometheus部署到 Kubernetes 比较好还是使用Prometheus Operator来部署比较好?

对于技术的选型,往往是没有规定死是要用哪一项技术的,而是需要结合业务的需求、运维场景、自身对某项技术的掌握程度、以及其它更多的考量因素来共同决定的:

  1. 如果对 Kubernetes 中的 Prometheus 的自动化部署、管理和配置不是很熟悉,或者需要部署 Prometheus 集群和实现高可用性,那么使用 Prometheus Operator 是更好的选择。
  2. Prometheus Operator 提供了简化 Prometheus 在 Kubernetes 中部署的功能,可以自动处理很多繁琐的任务,如自动部署 Prometheus 和 Alertmanager、自动创建监控目标和规则等。这样可以显著降低部署和维护 Prometheus 的难度和工作量,并增强 Prometheus 在 Kubernetes 中的可靠性和可用性。
  3. 如果有丰富的 Kubernetes 和 Prometheus 的经验,并且需要更加个性化的定制和控制,那么手工将 Prometheus 部署到 Kubernetes 中也是一个不错的选择。
  4. 手工部署虽然相对更复杂,但是也可以充分发挥 Kubernetes 的灵活性和可定制性,例如自定义 Kubernetes Service 和 Endpoints、更加细致的管理数据存储和备份等。这样可以满足更加个性化和定制化的需求,同时增加对 Prometheus 系统的深度理解和掌握。

所以,选择手工部署还是 Prometheus Operator,应该基于具体场景和需求进行综合考虑,以便更好地满足业务和运维的要求。

分享手工将Prometheus部署到K8S(供参考)

下面分享手工将Prometheus部署到 Kubernetes 的yaml,关于使用Prometheus Operator部署可参考我之前的分享或者参考官方文档即可。

提示:本案例中使用Prometheus的数据目录所在的后端存储是rook-ceph,可将其修改为您已有的后端存储,如原生的ceph、nfs等等。

apiVersion: v1
kind: Namespace
metadata:
  name: monitor
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-config
  namespace: monitor
data:
  prometheus.yml: |
    global:
      scrape_interval: 15s
      evaluation_interval: 15s
    alerting:
      alertmanagers:
        - static_configs:
            - targets:
              # - alertmanager:9093
    rule_files:
      # - "first_rules.yml"
      # - "second_rules.yml"
    scrape_configs:
      - job_name: "prometheus"
        static_configs:
          - targets: ["localhost:9090"]
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: prometheus-pvc
  namespace: monitor
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 100Gi
  storageClassName: rook-ceph-block
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: prometheus
  name: prometheus
  namespace: monitor
spec:
  replicas: 1
  selector:
    matchLabels:
      app: prometheus
  template:
    metadata:
      labels:
        app: prometheus
    spec:
      initContainers:
      - name: "change-prometheus-data-dir-perm"
        image: busybox
        command: ["/bin/sh"]
        args: ["-c", "chown -R 65534:65534 /prometheus"]
        securityContext:
          privileged: true
        volumeMounts:
          - name: prometheus-storage
            mountPath: /prometheus
      containers:
      - image: prom/prometheus:latest
        name: prometheus
        ports:
        - containerPort: 9090
          protocol: TCP
        volumeMounts:
        - name: prometheus-storage
          mountPath: /prometheus
        - name: prometheus-config
          mountPath: /etc/prometheus
          readOnly: true
      volumes:
        - name: prometheus-config
          configMap: 
            name: prometheus-config
        - name: prometheus-storage
          persistentVolumeClaim:
            claimName: prometheus-pvc
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: prometheus
  name: prometheus
  namespace: monitor
spec:
  ports:
  - name: http-port
    nodePort: 30090
    port: 9090
    protocol: TCP
    targetPort: 9090
  selector:
    app: prometheus
  type: NodePort

注意:在上面的yaml中,initContainers 的作用是确保 /prometheus 目录以及其子目录的权限正确,因为 Prometheus 进程通常需要以非特权用户运行。同时,由于该 initContainers 是以特权模式运行的,因此可以确保 Prometheus 容器能够以正确的方式访问挂载的卷,而不会因为权限问题导致运行异常。

相关实践学习
容器服务Serverless版ACK Serverless 快速入门:在线魔方应用部署和监控
通过本实验,您将了解到容器服务Serverless版ACK Serverless 的基本产品能力,即可以实现快速部署一个在线魔方应用,并借助阿里云容器服务成熟的产品生态,实现在线应用的企业级监控,提升应用稳定性。
云原生实践公开课
课程大纲 开篇:如何学习并实践云原生技术 基础篇: 5 步上手 Kubernetes 进阶篇:生产环境下的 K8s 实践 相关的阿里云产品:容器服务 ACK 容器服务 Kubernetes 版(简称 ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情: https://www.aliyun.com/product/kubernetes
相关文章
|
9天前
|
Kubernetes 监控 Cloud Native
Kubernetes自动伸缩方案的终极指南
【4月更文挑战第18天】
20 0
Kubernetes自动伸缩方案的终极指南
|
3月前
|
Prometheus 监控 Kubernetes
如何用 Prometheus Operator 监控 K8s 集群外服务?
如何用 Prometheus Operator 监控 K8s 集群外服务?
|
5月前
|
Kubernetes Cloud Native Docker
云原生|kubernetes|网络插件flannel二进制部署和calico的yaml清单部署总结版
云原生|kubernetes|网络插件flannel二进制部署和calico的yaml清单部署总结版
141 0
|
5月前
|
Prometheus Kubernetes 监控
云原生|kubernetes |使用Prometheus监控k8s cAdvisor篇(进阶篇--- 一)(centos操作系统)
云原生|kubernetes |使用Prometheus监控k8s cAdvisor篇(进阶篇--- 一)(centos操作系统)
447 0
|
3月前
|
Prometheus 监控 Kubernetes
监控 Kubernetes 集群证书过期时间的三种方案
监控 Kubernetes 集群证书过期时间的三种方案
|
3天前
|
存储 Kubernetes 调度
K8S常见的持久化(存储)方案用法详解
K8S常见的持久化(存储)方案用法详解
|
20天前
|
JSON Kubernetes Go
IDEA使用Kubernetes插件编写YAML
IDEA使用Kubernetes插件编写YAML
32 0
IDEA使用Kubernetes插件编写YAML
|
3月前
|
Prometheus Kubernetes 监控
|
3月前
|
存储 Kubernetes 安全
加密 K8s Secrets 的几种方案
加密 K8s Secrets 的几种方案
|
4月前
|
Prometheus Kubernetes Cloud Native
kubernetes安装Prometheus
##### 安装 在目标集群上,执行如下命令: ```shell kubectl apply -f https://github.com/512team/dhorse/raw/main/conf/kubernetes-prometheus.yml

推荐镜像

更多