云原生(三十五) | Prometheus入门和安装

2022-07-25 00:26:20 浏览数 (4)

Prometheus入门和安装

一、入门

Prometheus是一个开源监控解决方案,用于收集和聚合指标作为时间序列数据。更简单地说,Prometheus 商店中的每个项目都是一个指标事件,并带有它发生的时间戳。

Prometheus 最初是在 Soundcloud 开发的,但现在是一个由Cloud Native Computing Foundation (CNCF)支持的社区项目。在过去十年中,它迅速发展壮大,因为它结合了查询功能和云原生架构,使其成为现代应用程序的理想监控堆栈。

1、docker版本

代码语言:javascript复制
mkdir -p /app/prometheus/config
# 自行配置 prometheus.yml
docker run -d --name prometheus --restart=always 
    -p 9090:9090 
    -v /app/prometheus/config:/etc/prometheus 
    prom/prometheus

代码语言:javascript复制
# 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 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"
 
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'
    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
    scrape_interval: 5s
    static_configs:
    - targets: ['127.0.0.1:9090']
 
  - job_name: 'docker'
    scrape_interval: 10s
    static_configs:
      - targets: ['10.120.82.4:8080'] ## 这个安装了下面cadvisor才有,自动访问/metrics
  - job_name: 'node'  ## 准备一个程序能暴露 /metrics   k v; node_exporter
    scrape_interval: 5s
    static_configs:
    - targets: ['10.120.82.4:9100']

代码语言:javascript复制
# 运行cadvisor 导出docker节点数据,访问 8080/metrics即可
docker run -v /:/rootfs:ro 
-v /var/run:/var/run:rw 
-v /sys:/sys:ro 
-v /var/lib/docker/:/var/lib/docker:ro 
-p 8080:8080 -d  --name=cadvisor  google/cadvisor

代码语言:javascript复制
#主机监控,参照如下网址
https://grafana.com/grafana/dashboards/13978?src=grafana_gettingstarted&pg=dashboards&plcmt=featured-sub1

#创建开机启动服务.为啥不行???? 可以用 nohup node_exporter &
#nohup node_exporter >> node_exporter.output.log 2>&1 &
vi /etc/systemd/system/node-exporter.service
## 内容如下,如下的配置算了
[Unit]
Description=Node Exporter

[Service]
User=node-exporter
ExecStart=/usr/local/bin/node_exporter  --config.file=agent-config.yaml
##ExecStart=/usr/local/bin/node_exporter
Restart=always

[Install]
WantedBy=multi-user.target

##创建用户
useradd --no-create-home --shell /bin/false node-exporter

## 启动
sudo systemctl daemon-reload
sudo systemctl enable node-exporter.service --now
sudo systemctl start node-exporter.service
sudo systemctl status node-exporter.service

代码语言:javascript复制
docker run -d --name=grafana --restart=always -p 3000:3000 grafana/grafana

二、安装

kube-prometheus-stack

  • prometheus
  • stack-charts
  • prometheus-community/kube-state-metrics
  • prometheus-community/prometheus-node-exporter
  • grafana/grafana

1、charts下载

代码语言:javascript复制
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
helm pull prometheus-community/kube-prometheus-stack --version 16.0.0

2、定制化配置

2.1、配置ingress访问证书

全局做过的就可以跳过; 只需要给全局加上域名即可

2.2、配置定制化文件

代码语言:javascript复制
vi override.yaml  #直接Ingress部署的

代码语言:javascript复制
alertmanager:
  ingress: 
    enabled: true
    ingressClassName: nginx
    hosts:
      - alertmanager.itdachang.com
    paths:
      - /
    pathType: Prefix
    tls: 
     - secretName: itdachang.com
       hosts:
        - alertmanager.itdachang.com
grafana:
  enabled: true
  defaultDashboardsEnabled: true
  adminPassword: Admin123456
  ingress: 
    enabled: true
    hosts: 
    - grafana.itdachang.com
    path: /
    pathType: Prefix
    tls:
      - secretName: itdachang.com
        hosts:
          - grafana.itdachang.com 
  
prometheus: 
  ingress: 
    enabled: true
    hosts: [prometheus.itdachang.com]
    paths:
      - / 
    pathType: Prefix
    tls:
      - secretName: itdachang.com
        hosts:
          - prometheus.itdachang.com
    additionalPodMonitors:
      - name: registry.cn-hangzhou.aliyuncs.com/lanson_k8s_images/kube-state-metrics:v2.0.0

3、安装

代码语言:javascript复制
kubectl create ns monitor
helm install -f values.yaml -f override.yaml prometheus-stack ./ -n monitor

0 人点赞