prometheus监控获取数据的接口:prometheus实战之一,用ansible部署

人气:318 ℃/2023-12-20 20:52:32

欢迎访问我的GitHub

这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos

关于《prometheus实战》本篇概览

环境信息
  1. 操作系统:ubuntu 22.04 LTS
  2. prometheus:2.37.7
  3. node_exporter:1.5.0
  4. ansible:2.10.8
使用ansible安装

hostname

身份

IP地址

ansible

ansible执行主机

192.168.25.130

prometheus

prometheus服务器(部署了prometheus)

192.168.25.129

working001

应用服务器(部署了node_exporter)

192.168.25.128

梳理整个部署过程

新建prometheus账号(prometheus服务器、应用服务器)

shellgroupadd prometheus \&& useradd -d /home/prometheus -g prometheus -m prometheus \&& usermod -s /bin/bash prometheus

准备ansible(ansible电脑)

shellsudo apt-get install -y ansible sshpass

编写ansible用到的配置和脚本(ansible电脑)

文件名

类别

作用

hosts

配置文件

prometheus服务器和应用服务器的IP地址、账号、密码等配置

ansible.cfg

配置文件

ansible的配置信息,如hosts文件位置、超时设置等

vars.yml

配置文件

脚本中用到的参数信息,例如prometheus下载地址、版本号等

common_setup.yml

脚本文件

通用的设置脚本,如apt更新、设置时区等

install_prometheus.yml

脚本文件

部署prometheus的脚本,用在prometheus服务器

install_node_exporter.yml

脚本文件

部署node_exporter的脚本,用在应用服务器

shell[prometheus-group]prometheus ansible_host=192.168.25.129 ansible_port=22 ansible_user=prometheus ansible_password=888888working001 ansible_host=192.168.25.128 ansible_port=22 ansible_user=prometheus ansible_password=888888

shell[defaults]inventory = ~/playbooks/hostshost_key_checking = Falsetimeout = 30

ymlprometheus_user_home: /home/prometheusprometheus_base_path: '{{prometheus_user_home}}/prometheus'prometheus_url: https://github.com/prometheus/prometheus/releases/downloadprometheus_version: 2.37.7prometheus_deploy_path: '{{prometheus_base_path}}/prometheus-{{prometheus_version}}.linux-amd64'node_exporter_base_path: '{{prometheus_user_home}}/node_exporter'node_exporter_url: https://github.com/prometheus/node_exporter/releases/downloadnode_exporter_version: 1.5.0node_exporter_deploy_path: '{{node_exporter_base_path}}/node_exporter-{{node_exporter_version}}.linux-amd64'

yml- name: 部署前的准备工作(通用) hosts: prometheus-group gather_facts: True vars_files: - vars.yml tasks: - name: update apt apt: update_cache: yes cache_valid_time: 86400 become: yes - name: get timelocal shell: ls -l /etc/localtime | awk '{print $11}' register: timelocal - name: delete loacltime shell: rm -f /etc/localtime when: timelocal.stdout != "/usr/share/zoneinfo/Asia/Shanghai" become: yes - name: create asia time shell: ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime when: timelocal.stdout != "/usr/share/zoneinfo/Asia/Shanghai" become: yes - name: 安装时钟同步工具 apt: name: ntpdate state: present become: yes - name: 同步时钟 command: ntpdate ntp.aliyun.com become: yes

yml- name: 部署prometheus hosts: prometheus gather_facts: False vars_files: - vars.yml tasks: - name: 停止残留的prometheus ignore_errors: yes systemd: name: prometheus state: stopped become: yes - name: 清理可能的prometheus service残留文件 file: path: /etc/systemd/system/prometheus.service state: absent become: yes - name: 清理可能的prometheus残留文件夹 file: path: '{{prometheus_base_path}}' state: absent - name: 新建部署文件夹 file: path: '{{prometheus_base_path}}' state: directory mode: '0755' - name: 下载并解压文件prometheus-{{prometheus_version}}.linux-amd64.tar.gz ansible.builtin.unarchive: src: '{{prometheus_url}}/v{{prometheus_version}}/prometheus-{{prometheus_version}}.linux-amd64.tar.gz' dest: '{{prometheus_base_path}}' remote_src: yes - name: 生成systemd的service文件 shell: | tee /etc/systemd/system/prometheus.service <<-'EOF' [Unit] Description=Prometheus Server Documentation=https://prometheus.io/docs/introduction/overview/ After=network-online.target [Service] User=prometheus Restart=on-failure #Change this line if you download the #Prometheus on different path user ExecStart={{prometheus_deploy_path}}/prometheus --config.file={{prometheus_deploy_path}}/prometheus.yml --storage.tsdb.path={{prometheus_base_path}}/data [Install] WantedBy=multi-user.target EOF become: yes - name: 刷新服务配置 systemd: daemon_reload: true become: yes - name: 将prometheus服务设置为自启动 systemd: name: prometheus enabled: true masked: no become: yes - name: 启动prometheus systemd: state: started name: prometheus become: yes

yml- name: 部署node_exporter hosts: working001 gather_facts: False vars_files: - vars.yml tasks: - name: 停止残留的node_exporter ignore_errors: yes systemd: name: node_exporter state: stopped become: yes - name: 清理可能的node_exporter service残留文件 file: path: /etc/systemd/system/node_exporter.service state: absent become: yes - name: 清理可能的node_exporter残留文件夹 file: path: '{{node_exporter_base_path}}' state: absent - name: 新建部署文件夹 file: path: '{{node_exporter_base_path}}' state: directory mode: '0755' - name: 下载并解压文件node_exporter-{{node_exporter_version}}.linux-amd64.tar.gz ansible.builtin.unarchive: src: '{{node_exporter_url}}/v{{node_exporter_version}}/node_exporter-{{node_exporter_version}}.linux-amd64.tar.gz' dest: '{{node_exporter_base_path}}' remote_src: yes - name: 生成systemd的service文件 shell: | tee /etc/systemd/system/node_exporter.service <<-'EOF' [Unit] Description=Node exporter Server Documentation=https://prometheus.io/docs/introduction/overview/ After=network-online.target [Service] User=prometheus Restart=on-failure ExecStart={{node_exporter_deploy_path}}/node_exporter --web.listen-address=:9100 [Install] WantedBy=multi-user.target EOF become: yes - name: 刷新服务配置 systemd: daemon_reload: true become: yes - name: 将node_exporter服务设置为自启动 systemd: name: node_exporter enabled: true masked: no become: yes - name: 启动node_exporter systemd: state: started name: node_exporter become: yes

执行ansible脚本(ansible电脑)

shell# 公共脚本,prometheus服务器和应用服务器都会执行ansible-playbook common_setup.yml# 部署node_exporter脚本,prometheus服务器和应用服务器都会执行ansible-playbook install_prometheus.yml# 部署node_exporter脚本,prometheus服务器和应用服务器都会执行ansible-playbook install_node_exporter.yml

检查服务部署情况

配置prometheus,添加监控任务

yml # 新增任务,从应用服务器采集数据 - job_name: "node-resource-working001" # 抓取时间间隔 scrape_interval: 15s # 抓取超时时间 scrape_timeout: 10s # 抓取地址 static_configs: - targets: ["192.168.25.128:9100"]

shellsystemctl restart prometheus

问题备忘

shellWarning: Error fetching server time: Detected 72.22200012207031 seconds time difference between your browser and the server. Prometheus relies on accurate time and time drift might cause unexpected query results.

脚本下载

名称

链接

备注

项目主页

https://github.com/zq2599/blog_demos

该项目在GitHub上的主页

git仓库地址(https)

https://github.com/zq2599/blog_demos.git

该项目源码的仓库地址,https协议

git仓库地址(ssh)

git@github.com:zq2599/blog_demos.git

该项目源码的仓库地址,ssh协议

欢迎关注头条号:程序员欣宸

百科

More+
首页/电脑版/网名
© 2026 NiBaKu.Com All Rights Reserved.