[转帖]容器监控实践—Prometheus部署方案

容器,监控,实践,prometheus,部署,方案 · 浏览次数 : 0

小编点评

## Container Monitor Practice—Prometheus Deployment Scheme This document outlines a deployment scheme for Prometheus using the Prometheus Operator, focusing on the containerized approach. **Key Concepts:** * **Prometheus Operator:** A Kubernetes operator that provides a unified management interface for Prometheus and Kubernetes. * **Prometheus:** A monitoring and alert system for Kubernetes that can be deployed on Kubernetes. * **CRD (Cluster Resource Definition):** A Kubernetes resource that defines the configuration for a particular Kubernetes resource. **Deployment Steps:** 1. **Create a namespace for the deployment:** - `kubectl create namespace monitoring` 2. **Define the Prometheus target list:** - `imagegrafana/prometheus-operator/manifestsprometheus/targetList.yaml` - This file defines the IP addresses and ports of the Prometheus servers. 3. **Create a Prometheus service:** - `kubectl create service -n monitoring -port 9090 -target-port 9090 -type LoadBalancer` 4. **Create a Prometheus deployment:** - `kubectl create deployment -n monitoring -spec PrometheusDeployment.yaml` - This YAML file defines the desired number of pods, image, ports, and configuration for the Prometheus deployment. 5. **Add a serviceMonitor to the namespace:** - `kubectl create serviceMonitor -n monitoring -port 9090 --tcp-port 9090 --name prometheus-service-monitor` - This serviceMonitor will monitor the `kubelet` service and expose the metrics on the Prometheus service port. 6. **Set up Alertmanager:** - `kubectl create namespace monitoring -alias alertmanager` - `kubectl create service -n monitoring -port 8085 --name alertmanager -port 8086 --target-port 8085 --type LoadBalancer` - `kubectl create deployment -n monitoring -spec AlertmanagerDeployment.yaml` - This configuration creates an Alertmanager cluster that will receive alerts from the Prometheus service. 7. **Start the Prometheus and Alertmanager services:** - `kubectl rollout restart deployment monitoring -n monitoring --parallel 3` - `kubectl rollout restart deployment alertmanager -n monitoring --parallel 3` **Benefits:** * **Simplified management:** The Prometheus Operator provides a unified UI for managing both Prometheus and Kubernetes resources. * **Containerized deployment:** Prometheus is deployed in containers, ensuring portability and isolation. * **Scalability and resilience:** The operator automatically scales the number of pods based on resource demands. * **Alerting capabilities:** Prometheus and Alertmanager provide comprehensive alerting and notification mechanisms. **Additional Notes:** * The `metrics-server.json` file is a configuration file for the Prometheus server. * The `kubelet.service` and `kubelet.yaml` files handle authentication and configuration for the `kubelet` service. * This is a high-level overview of the deployment process. For detailed instructions, refer to the official Prometheus Operator documentation.

正文

容器监控实践—Prometheus部署方案

https://www.jianshu.com/p/ae0500f70acf

 

一.单独部署

二.在K8S中部署

如果在Kubernetes中部署Prometheus,可以使用prometheus in kubernetes,含exporter、grafana等组件。

安装方式:

kubectl apply \
  --filename https://raw.githubusercontent.com/giantswarm/kubernetes-prometheus/master/manifests-all.yaml

卸载方式:

kubectl delete namespace monitoring

该方式为大多数用户和云厂商使用的方式,可以基于Prometheus的服务发现:在annotation中设置prometheus.io/scrape为true,就可以把K8S的所有服务都加入到监控中,但在使用的过程中会有一些问题:

  • 1.如果增加了新的exporter,如nginx-exporter,需要修改prometheus配置并重启
  • 2.服务本身和监控配置没有分离
  • 3.监控集群多实例的状态不好管理
  • 4.报警配置也包含在prometheus的配置中,监控与报警没有分离,添加规则麻烦

以上问题一般的处理方式为:在prometheus上加一个控制台,来动态配置target、报警规则,并向后端server发起修改、重启操作。同时有权限控制、日志审计、整体配置过期时间等功能。

但如果使用了Prometheus Operator,就可以将以上大多数操作抽象为k8s中的资源提交、修改,减少上层封装的工作量。

三.Prometheus Operator部署

Prometheus-Operator是一套为了方便整合prometheus和kubernetes的开源方案,使用Prometheus-Operator可以非常简单的在kubernetes集群中部署Prometheus服务,用户能够使用简单的声明性配置来配置和管理Prometheus实例,这些配置将响应、创建、配置和管理Prometheus监控实例。

  • 官方地址:https://github.com/coreos/prometheus-operator
  • 目前状态:beta状态,还不够完整,但向后兼容。将成为趋势
  • 前置条件:要求k8s的版本>=1.8.0(应该是因为metric api和CRD支持的限制)

Operator的核心思想是将Prometheus的部署与它监控的对象的配置分离,做到部署与监控对象的配置分离之后,就可以轻松实现动态配置。使用Operator部署了Prometheus之后就可以不用再管Prometheus Server了,以后如果要添加监控对象或者添加告警规则,只需要编写对应的ServiceMonitor和Prometheus资源就可以,不用再重启Prometheus服务,Operator会动态的观察配置的改动,并将其生成为对应的prometheus配置文件其中Operator可以部署、管理Prometheus Service

 
image

四种CRD作用如下:

  • Prometheus: 由 Operator 依据一个自定义资源kind: Prometheus类型中,所描述的内容而部署的 Prometheus Server 集群,可以将这个自定义资源看作是一种特别用来管理Prometheus Server的StatefulSets资源。

  • ServiceMonitor: 一个Kubernetes自定义资源(和kind: Prometheus一样是CRD),该资源描述了Prometheus Server的Target列表,Operator 会监听这个资源的变化来动态的更新Prometheus Server的Scrape targets并让prometheus server去reload配置(prometheus有对应reload的http接口/-/reload)。而该资源主要通过Selector来依据 Labels 选取对应的Service的endpoints,并让 Prometheus Server 通过 Service 进行拉取(拉)指标资料(也就是metrics信息),metrics信息要在http的url输出符合metrics格式的信息,ServiceMonitor也可以定义目标的metrics的url。

  • Alertmanager:Prometheus Operator 不只是提供 Prometheus Server 管理与部署,也包含了 AlertManager,并且一样通过一个 kind: Alertmanager 自定义资源来描述信息,再由 Operator 依据描述内容部署 Alertmanager 集群。

  • PrometheusRule:对于Prometheus而言,在原生的管理方式上,我们需要手动创建Prometheus的告警文件,并且通过在Prometheus配置中声明式的加载。而在Prometheus Operator模式中,告警规则也编程一个通过Kubernetes API 声明式创建的一个资源.告警规则创建成功后,通过在Prometheus中使用想servicemonitor那样用ruleSelector通过label匹配选择需要关联的PrometheusRule即可。

安装方式:

prometheus的target列表:

 
image

grafana的自带监控图列表:

 
image
 
image

常见问题:

因为要operator中要支持聚合api,在某些版本的集群上可能需要一些配置,如下:


cfssl gencert -ca=/etc/kubernetes/pki/ca.pem -ca-key=/etc/kubernetes/pki/ca-key.pem -config=/etc/kubernetes/pki/ca-config.json -profile=jpaas metrics-server-csr.json | cfssljson -bare metrics-server


{
  "CN": "aggregator",
  "host": [],
  "key": {
    "algo": "rsa",
    "size": 2048
  },
  "names": [
    {
      "C": "CN",
      "ST": "BeiJing",
      "L": "BeiJing",
      "O": "k8s",
      "OU": "cloudnative"
    }
  ]
}
  • 配置master组件参数,以支持metric-server
vim /etc/systemd/system/kube-apiserver.service

--requestheader-client-ca-file=/etc/kubernetes/pki/ca.pem \
--requestheader-allowed-names="aggregator" \
--requestheader-extra-headers-prefix="X-Remote-Extra-" \
--requestheader-group-headers=X-Remote-Group \
--requestheader-username-headers=X-Remote-User \
--proxy-client-cert-file=/etc/kubernetes/pki/metrics-server.pem \
--proxy-client-key-file=/etc/kubernetes/pki/metrics-server-key.pem \
--runtime-config=api/all=true \
--enable-aggregator-routing=true \


systemctl daemon-reload
systemctl restart kube-apiserver.service
systemctl status kube-apiserver.service


vim /etc/systemd/system/kube-controller.service
--horizontal-pod-autoscaler-use-rest-clients=true
systemctl daemon-reload
systemctl restart kube-controller.service
systemctl status kube-controller.service
  • 启动成功后,prometheus的target中,kubelet没有值,401报错
vim /etc/systemd/system/kubelet.service

--authentication-token-webhook=true
--authorization-mode=Webhook

systemctl daemon-reload

systemctl restart kubelet.service

参考文档:

本文为容器监控实践系列文章,完整内容见:container-monitor-book

与[转帖]容器监控实践—Prometheus部署方案相似的内容:

[转帖]容器监控实践—Prometheus部署方案

容器监控实践—Prometheus部署方案 https://www.jianshu.com/p/ae0500f70acf 一.单独部署 二进制安装各版本下载地址:https://prometheus.io/download/ Docker运行运行命令:docker run --name promet

[转帖]容器监控实践-Grafana

容器监控实践-Grafana https://www.jianshu.com/p/cdda94e5b9e2 概述 Grafana 是一个开源的,可以用于大规模指标数据的可视化项目,甚至还能对指标进行报警。基于友好的 Apache License 2.0 开源协议,目前是prometheus监控展示的

[转帖]零信任策略下K8s安全监控最佳实践(K+)

https://developer.aliyun.com/article/1009607?spm=a2c6h.24874632.expert-profile.126.3b0b506fysVD76 简介: 本文重点将围绕监控防护展开,逐层递进地介绍如何在复杂的分布式容器化环境中借助可观测性平台,持续监

[转帖]linux 调优各项监控指标小记

https://z.itpub.net/article/detail/8A4E4E96522BD59D45AB5A4CA442EDB3 自开始负责生产环境部署,中间遇到了若干线上环境内存以及CPU的问题。由于微服务以及容器的流行,现在已经可以很方便的使用 K8s + prometheus + gra

[转帖]Docker 常见问题处理技巧

https://zhuanlan.zhihu.com/p/517495884 1、Docker 迁移存储目录 默认情况系统会将 Docker 容器存放在 /var/lib/docker 目录下 [问题起因] 今天通过监控系统,发现公司其中一台服务器的磁盘快慢,随即上去看了下,发现 /var/lib/

[转帖]zabbix监控进程和监控日志

监控日志:大家可以监控系统日志、nginx、Apache、业务日志。想用好用对,不是辣么容易。zabbix最主要的是监控日志文件中有没有某个字符串的表达式,支持日志文件正则和关键字正则,其是把日志文件中符合关键字的日志过滤出来入库,不包含的日志不采集,且只支持主动模式。 1、 zabbix日志监控表

【转帖】10个Linux 系统性能监控命令行工具

引言: 系统一旦跑起来,我们就希望它能够稳定运行,不要宕机,不出现速度变慢。因此,对于Linux 系统管理员来说每天监控和调试 Linux 系统的性能问题是一项繁重却又重要的工作。监控和保持系统启动并运行是很不容易的一件事。 下面是小编总结的十个实用的 Linux 系统监控命令,让你轻松保持系统的实

[转帖]解码Redis最易被忽视的CPU和内存占用高问题

https://ost.51cto.com/posts/12514 我们在使用Redis时,总会碰到一些redis-server端CPU及内存占用比较高的问题。下面以几个实际案例为例,来讨论一下在使用Redis时容易忽视的几种情形。 一、短连接导致CPU高 某用户反映QPS不高,从监控看CPU确实偏

[转帖]容器化 TCP Socket 缓存、接收窗口参数

https://blog.mygraphql.com/zh/notes/low-tec/network/tcp-mem/ 最近需要支持一个单 POD 的 TCP 连接数上 10k 的基础服务(Cassandra)的容器化。需要对其使用的资源(特别是TCP缓存内存),以及对相邻 Pod(同一 work

[转帖] 容器内的Linux诊断工具0x.tools

https://www.cnblogs.com/codelogs/p/16242999.html 简介# Linux上有大量的问题诊断工具,如perf、bcc等,但这些诊断工具,虽然功能强大,但却需要很高的权限才可以使用。 而0x.tools这个工具提供了一个很好的思路,通过采样/proc目录来诊断