天天看點

雲原生(三十五) | Prometheus入門和安裝​Prometheus入門和安裝

​Prometheus入門和安裝

一、入門

Prometheus是一個開源監控解決方案,用于收集和聚合名額作為時間序列資料。更簡單地說,Prometheus 商店中的每個項目都是一個名額事件,并帶有它發生的時間戳。

Prometheus 最初是在 Soundcloud 開發的,但現在是一個由Cloud Native Computing Foundation (CNCF)支援的社群項目。在過去十年中,它迅速發展壯大,因為它結合了查詢功能和雲原生架構,使其成為現代應用程式的理想監控堆棧。

1、docker版本

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           

複制

# 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']           

複制

# 運作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           

複制

#主機監控,參照如下網址
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           

複制

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下載下傳

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、配置定制化檔案

vi override.yaml  #直接Ingress部署的           

複制

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、安裝

kubectl create ns monitor
helm install -f values.yaml -f override.yaml prometheus-stack ./ -n monitor           

複制