ansible--promethues+pushgateway部署

ansible自动部署:

相关安装包下载链接:

链接:https://pan.baidu.com/s/1UaT9JAGM7HDXVFwsprntbA 
提取码:7571
# prometheus-2.33.1.linux-amd64.tar.gz
# pushgateway-1.4.2.linux-amd64.tar.gz

目录结构:

[root@localhost ansible]# tree promethues_install
promethues_install
├── conf
│   ├── prometheus.yml
│   └── pro_restart.sh
├── id_rsa
├── inv
├── promethues.yml
├── README.md
└── run.sh
promethues.yml配置文件
- name: promethues安装
  hosts: all
  gather_facts: no
  remote_user: sa
  become_user: root
  serial: 1
  vars:
  tasks:
    - name: 下载安装包 '{{ prometheus_package }}',存储到 '{{ download_dir }}'
      get_url:
        url: "{{ http_url }}{{ prometheus_package }}"
        dest: "{{ download_dir }}"
        mode: 0644
        force: no
        checksum: "{{ pro_md }}"
      become: yes
    - name: 下载安装包 '{{ pushgateway_package }}',存储到 '{{ download_dir }}'
      get_url:
        url: "{{ http_url }}{{ pushgateway_package }}"
        dest: "{{ download_dir }}"
        mode: 0644
        force: no
        checksum: "{{ push_md }}"
      become: yes
    - name: 解压promethues安装包到 '{{ Unpack }}'
      unarchive:
        src: "{{ download_dir }}{{ prometheus_package }}"
        dest: "{{ Unpack }}"
        remote_src: yes
      args:
        creates: "{{ pro_file }}"
      become: yes
    - name: 解压pushgateway安装包到
      unarchive:
        src: "{{ download_dir }}{{ pushgateway_package }}"
        dest: "{{ Unpack }}"
        remote_src: yes
      args:
        creates: "{{ push_file }}"
      become: yes
    - name: 传输promethues配置文件
      template:
        src: '{{ item.src }}'
        dest: "{{ pro_dir }}"
        mode: '{{ item.mode }}'
      with_items:
        - { src: ./conf/prometheus.yml ,mode: 0644}
        - { src: ./conf/pro_restart.sh, mode: 0770}
      become: yes 
    - name: 启动promethues服务
      shell:  nohup  ./prometheus --web.listen-address=0.0.0.0:9090 --web.enable-lifecycle --web.enable-admin-api >/dev/null 2>&1 &
      args:
        chdir: "{{ pro_dir }}"
      become: yes
    - name: 启动pushgateway服务
      shell: nohup ./pushgateway &
      args:
        chdir: "{{ push_dir }}"
      become: yes


run.sh配置文件
cd `dirname $0`

## 安装包源
http_url='http://192.168.1.230/packages/'
## prometheus安装包名称
prometheus_package='prometheus-2.33.1.linux-amd64.tar.gz'
## prometheus安装包md5值
pro_md='md5:3cd2a8a85141316435b8b6670ff3e143'
## pushgateway安装包名称
pushgateway_package='pushgateway-1.4.2.linux-amd64.tar.gz'
## pushgateway安装包md5值
push_md='md5:85c3b9a00667f4862d7b86165fcd0493'
## 解包路径
Unpack='/data/op/'
## 拉包存放位置
download_dir='/tmp/'
## prometheus解压完成路径
pro_dir='/data/op/prometheus-2.33.1.linux-amd64'
## prometheus解压完成标识
pro_file='/data/op/prometheus-2.33.1.linux-amd64/prometheus'
## pushgateway解压完成路径
push_dir='/data/op/pushgateway-1.4.2.linux-amd64'
## pushgateway解压完成标识
push_file='/data/op/pushgateway-1.4.2.linux-amd64/pushgateway'
## 获取prometheus安装服务器
pro_ip=`cat inv`


ansible_options="
    -e http_url='$http_url'
    -e prometheus_package='$prometheus_package'
    -e pro_md='$pro_md'
    -e pushgateway_package='$pushgateway_package'
    -e push_md='$push_md'
    -e Unpack='$Unpack'
    -e download_dir='$download_dir'
    -e pro_dir='$pro_dir'
    -e pro_file='$pro_file'
    -e push_dir='$push_dir'
    -e push_file='$push_file'
    -e pro_ip='$pro_ip'
    "


ansible-playbook -i ./inv \
    $ansible_options \
    --key-file "./id_rsa"\
    promethues.yml $*


inv配置文件
10.10.80.176

id_rsa配置文件
将访问机器的秘钥拷贝过来

conf目录下文件:

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 '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'.

    static_configs:
      - targets: ["localhost:9090"]
  - job_name: 'pushgateway'
    static_configs:
      - targets: ['localhost:9091']
        labels:
          instance: pushgateway

pro_restart.sh配置文件
## 重新加载配置文件
curl -X POST http://{{ pro_ip }}:9090/-/reload



## --------停止服务,重启服务

# cd `dirname $0`
# > nohup.out 
# kill -9  `ps -ef|grep prometheus|grep 9090|grep -v grep|awk '{print $2}'`
# nohup ./prometheus --web.listen-address=0.0.0.0:9090 --web.enable-lifecycle --web.enable-admin-api &
# ps -ef|grep prometheus|grep 9090|grep -v grep


执行示例:

image

posted @ 2022-02-09 17:07  咖啡馆  阅读(87)  评论(0编辑  收藏  举报