【kubernetes搭建(三)】搭建Promethus+grafana监控平台

本文档详述了如何在Kubernetes集群中部署Prometheus监控系统和Grafana可视化工具。首先介绍了Prometheus、Exporter和Grafana的基本概念。接着,展示了安装NFS服务器作为共享存储的步骤。然后,逐步指导安装node-exporter以收集节点指标,以及部署Prometheus和配置监控规则。最后,部署Grafana并提供了几个预设的监控面板链接,以帮助建立一个全面的集群监控平台。

一、介绍

1、Prometheus(中文名:普罗米修斯)

是由SoundCloud开发的开源监控报警系统和时序列数据库(TSDB). Prometheus使用Go语言开发, 是Google BorgMon监控系统的开源版本。

Prometheus的基本原理是通过HTTP协议周期性抓取被监控组件的状态, 任意组件只要提供对应的HTTP接口就可以接入监控. 不需要任何SDK或者其他的集成过程。输出被监控组件信息的HTTP接口被叫做exporter,目前开发常用的组件大部分都有exporter可以直接使用, 比如Nginx、MySQL、Linux系统信息、Mongo、ES等

2、exporter

可以理解为一个数据库+数据抓取工具, 工具从各处抓来统一的数据, 放入prometheus这一个时间序列数据库中. 那如何保证各处的数据格式是统一的呢?就是通过这个exporter. Exporter是一类数据采集组件的总称. Exporter负责从目标处搜集数据, 并将其转化为Prometheus支持的格式, 它开放了一个http接口(以便Prometheus来抓取数据). 与传统的数据采集组件不同的是, Exporter并不向中央服务器发送数据, 而是等待中央服务器(如Prometheus等)主动前来抓取。https://github.com/prometheus 有很多写好的exporter,可以直接下载使用。

3、Grafana

是一个图形化工具, 它可以从很多种数据源(例如Prometheus)中读取数据信息, 使用很漂亮的图表来展示数据, 并且有很多开源的dashborad可以使用,可以快速地搭建起一个非常精美的监控平台。它与Prometheus的关系就类似于Kibana与ElasticSearch。

二、安装nfs(用于共享存储)

(最好单独用一个服务器用于共享存储服务器,类似于ftp)

1、NFS服务器:

yum install nfs-utils rpcbind -y
systemctl start rpcbind
systemctl enable rpcbind
systemctl start nfs
systemctl enable nfs
或
systemctl enable nfs-server
systemctl start nfs-server

mkdir -p /nfs/prometheus/data
mkdir -p /nfs/grafana/data
echo "/nfs/prometheus/data  192.168.16.0/24(rw,no_root_squash,sync,insecure)" >>/etc/exports
echo "/nfs/grafana/data  192.168.16.0/24(rw,no_root_squash,sync,insecure)" >>/etc/exports

#测试一下连接
showmount -e 49.233.42.34

(最后的命令重要,否则会报错The PersistentVolume "prometheus-data-pv" is invalid: spec.persistentvolumesource: Forbidden)

注意: 192.168.16.0/24网段,根据自己的ip addr结果去设置,否则下面Promethus和grafana连不上

修改后/etc/exports文件后,需要重启nfs

systemctl restart nfs
systemctl restart rpcbind

2、其他NFS客户端(k8s节点):

如果是其他k8s节点直接启动rpcbind并且挂载目录就可以

yum install nfs-utils rpcbind -y
#如果没安装nfs-utils,挂载时会报错:Output: mount: wrong fs type, bad option, bad superblock on ……
systemctl start rpcbind
systemctl enable rpcbind
mkdir /nfs/prometheus/data -p
mount -t nfs 192.168.16.100:/data/k8s /data/k8s

三、创建namespace 

vim ~/ns-monitor-namespace.yml (复制下面内容即可)

apiVersion: v1
kind: Namespace
metadata: 
  name: ns-monitor
  labels:
    name: ns-monitor

kubectl create -f ns-monitor-namespace.yml

四、安装node-exporter(用于采集kubernetes集群中各个节点的物理指标)

vim ~/node-exporter.yaml(复制下面内容即可)

kind: DaemonSet
apiVersion: apps/v1
metadata: 
  labels:
    app: node-exporter
  name: node-exporter
  namespace: ns-monitor
spec:
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: node-exporter
  template:
    metadata:
      labels:
        app: node-exporter
    spec:
      containers:
        - name: node-exporter
          image: prom/node-exporter:v0.16.0
          ports:
            - containerPort: 9100
              protocol: TCP
              name:	http
      hostNetwork: true
      hostPID: true
      tolerations:
        - effect: NoSchedule
          operator: Exists

---
kind: Service
apiVersion: v1
metadata:
  labels:
    app: node-exporter
  name: node-exporter-service
  namespace: ns-monitor
spec:
  ports:
    - name:	http
      port: 9100
      nodePort: 31672
      protocol: TCP
  type: NodePort
  selector:
    app: node-exporter

kubectl create -f node-exporter.yaml

五、安装prometheus

vim ~/prometheus.yaml (复制下面内容即可)

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: prometheus
rules:
  - apiGroups: [""] # "" indicates the core API group
    resources:
      - nodes
      - nodes/proxy
      - services
      - endpoints
      - pods
    verbs:
      - get
      - watch
      - list
  - apiGroups:
      - extensions
    resources:
      - ingresses
    verbs:
      - get
      - watch
      - list
  - nonResourceURLs: ["/metrics"]
    verbs:
      - get
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: prometheus
  namespace: ns-monitor
  labels:
    app: prometheus
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: prometheus
subjects:
  - kind: ServiceAccount
    name: prometheus
    namespace: ns-monitor
roleRef:
  kind: ClusterRole
  name: prometheus
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-conf
  namespace: ns-monitor
  labels:
    app: prometheus
data:
  prometheus.yml: |-
    # my global config
    global:
      scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
      evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
      # scrape_timeout is set to the global default (10s).

    # Alertmanager configuration
    alerting:
      alertmanagers:
      - static_configs:
        - targets:
          # - alertmanager:9093

    # Load rules once and periodically evaluate them according to the global 'evalua
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值