如题所示,本文将呈现如何通过Ansible的方式,安装与部署Prometheus监控。

本文所包含的代码将发布在:
https://gitee.com/leviathan-litan/ansible-prometheus

Prometheus 官方网站:
https://prometheus.io/

官方下载页:
https://prometheus.io/download/
最新版本是:【 2.25.2 / 2021-03-16

Prometheus | 架构图:

使用Ansible脚本安装过程如下:

[root@center ansible-prometheus]# rz
rz waiting to receive.
Starting zmodem transfer.  Press Ctrl+C to cancel.
Transferring ansible-prometheus.zip...
  100%  147847 KB    73923 KB/sec    00:00:02       0 Errors  

[root@center ansible-prometheus]# 
[root@center ansible-prometheus]# unzip ansible-prometheus.zip 
Archive:  ansible-prometheus.zip
   creating: ansible-prometheus/inventory/
   creating: ansible-prometheus/inventory/group_vars/
  inflating: ansible-prometheus/inventory/group_vars/all  
 extracting: ansible-prometheus/inventory/group_vars/grafana  
 extracting: ansible-prometheus/inventory/group_vars/prometheus  
  inflating: ansible-prometheus/inventory/hosts  
   creating: ansible-prometheus/playbooks/
  inflating: ansible-prometheus/playbooks/deploy_prometheus.yml  
   creating: ansible-prometheus/playbooks/roles/
   creating: ansible-prometheus/playbooks/roles/alert_manager/
   creating: ansible-prometheus/playbooks/roles/alert_manager/files/
   creating: ansible-prometheus/playbooks/roles/alert_manager/files/install_media/
  inflating: ansible-prometheus/playbooks/roles/alert_manager/files/install_media/alertmanager-0.21.0.linux-amd64.tar.gz  
   creating: ansible-prometheus/playbooks/roles/alert_manager/tasks/
  inflating: ansible-prometheus/playbooks/roles/alert_manager/tasks/alert_manager_install.yml  
 extracting: ansible-prometheus/playbooks/roles/alert_manager/tasks/main.yml  
   creating: ansible-prometheus/playbooks/roles/alert_manager/templates/
   creating: ansible-prometheus/playbooks/roles/alert_manager/templates/alert_manager/
  inflating: ansible-prometheus/playbooks/roles/alert_manager/templates/alert_manager/alertmanager.service.j2  
  inflating: ansible-prometheus/playbooks/roles/alert_manager/templates/alert_manager/alertmanager.yml.j2  
   creating: ansible-prometheus/playbooks/roles/alert_manager/vars/
  inflating: ansible-prometheus/playbooks/roles/alert_manager/vars/main.yml  
   creating: ansible-prometheus/playbooks/roles/example/
   creating: ansible-prometheus/playbooks/roles/example/vars/
  inflating: ansible-prometheus/playbooks/roles/example/vars/main.yml  
   creating: ansible-prometheus/playbooks/roles/grafana/
   creating: ansible-prometheus/playbooks/roles/grafana/files/
   creating: ansible-prometheus/playbooks/roles/grafana/files/install_media/
  inflating: ansible-prometheus/playbooks/roles/grafana/files/install_media/grafana-7.4.5-1.x86_64.rpm  
   creating: ansible-prometheus/playbooks/roles/grafana/tasks/
  inflating: ansible-prometheus/playbooks/roles/grafana/tasks/grafana_install.yml  
 extracting: ansible-prometheus/playbooks/roles/grafana/tasks/main.yml  
   creating: ansible-prometheus/playbooks/roles/grafana/templates/
   creating: ansible-prometheus/playbooks/roles/grafana/templates/grafana/
  inflating: ansible-prometheus/playbooks/roles/grafana/templates/grafana/grafana.ini.j2  
   creating: ansible-prometheus/playbooks/roles/grafana/vars/
  inflating: ansible-prometheus/playbooks/roles/grafana/vars/main.yml  
   creating: ansible-prometheus/playbooks/roles/influxdb/
   creating: ansible-prometheus/playbooks/roles/influxdb/files/
   creating: ansible-prometheus/playbooks/roles/influxdb/files/install_media/
   creating: ansible-prometheus/playbooks/roles/influxdb/tasks/
   creating: ansible-prometheus/playbooks/roles/influxdb/templates/
   creating: ansible-prometheus/playbooks/roles/influxdb/vars/
   creating: ansible-prometheus/playbooks/roles/linux/
   creating: ansible-prometheus/playbooks/roles/linux/files/
   creating: ansible-prometheus/playbooks/roles/linux/tasks/
  inflating: ansible-prometheus/playbooks/roles/linux/tasks/linux_directory.yml  
  inflating: ansible-prometheus/playbooks/roles/linux/tasks/linux_firewall.yml  
  inflating: ansible-prometheus/playbooks/roles/linux/tasks/linux_selinux.yml  
  inflating: ansible-prometheus/playbooks/roles/linux/tasks/linux_user_group.yml  
  inflating: ansible-prometheus/playbooks/roles/linux/tasks/linux_yum.yml  
  inflating: ansible-prometheus/playbooks/roles/linux/tasks/main.yml  
   creating: ansible-prometheus/playbooks/roles/linux/templates/
   creating: ansible-prometheus/playbooks/roles/linux/vars/
  inflating: ansible-prometheus/playbooks/roles/linux/vars/main.yml  
   creating: ansible-prometheus/playbooks/roles/node_exporter/
   creating: ansible-prometheus/playbooks/roles/node_exporter/files/
   creating: ansible-prometheus/playbooks/roles/node_exporter/files/install_media/
  inflating: ansible-prometheus/playbooks/roles/node_exporter/files/install_media/node_exporter-1.1.2.linux-amd64.tar.gz  
   creating: ansible-prometheus/playbooks/roles/node_exporter/tasks/
 extracting: ansible-prometheus/playbooks/roles/node_exporter/tasks/main.yml  
  inflating: ansible-prometheus/playbooks/roles/node_exporter/tasks/node_exporter_install.yml  
   creating: ansible-prometheus/playbooks/roles/node_exporter/templates/
   creating: ansible-prometheus/playbooks/roles/node_exporter/templates/node_exporter/
  inflating: ansible-prometheus/playbooks/roles/node_exporter/templates/node_exporter/node_exporter.service.j2  
   creating: ansible-prometheus/playbooks/roles/node_exporter/vars/
  inflating: ansible-prometheus/playbooks/roles/node_exporter/vars/main.yml  
   creating: ansible-prometheus/playbooks/roles/prometheus_server/
   creating: ansible-prometheus/playbooks/roles/prometheus_server/files/
   creating: ansible-prometheus/playbooks/roles/prometheus_server/files/install_media/
  inflating: ansible-prometheus/playbooks/roles/prometheus_server/files/install_media/prometheus-2.25.2.linux-amd64.tar.gz  
   creating: ansible-prometheus/playbooks/roles/prometheus_server/tasks/
 extracting: ansible-prometheus/playbooks/roles/prometheus_server/tasks/main.yml  
  inflating: ansible-prometheus/playbooks/roles/prometheus_server/tasks/prometheus_server_install.yml  
   creating: ansible-prometheus/playbooks/roles/prometheus_server/templates/
   creating: ansible-prometheus/playbooks/roles/prometheus_server/templates/prometheus/
  inflating: ansible-prometheus/playbooks/roles/prometheus_server/templates/prometheus/prometheus.service.j2  
  inflating: ansible-prometheus/playbooks/roles/prometheus_server/templates/prometheus/prometheus.yml.j2  
   creating: ansible-prometheus/playbooks/roles/prometheus_server/vars/
  inflating: ansible-prometheus/playbooks/roles/prometheus_server/vars/main.yml  
[root@center ansible-prometheus]# 
[root@center ansible-prometheus]# cd ansible-prometheus
[root@center ansible-prometheus]# 
[root@center ansible-prometheus]# ansible-playbook -i inventory/hosts playbooks/deploy_prometheus.yml 

PLAY [操作系统:Linux] ******************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************
ok: [192.168.232.12]
ok: [192.168.232.11]
ok: [192.168.232.13]

TASK [linux : 创建Linux系统组:MySQL] ****************************************************************************************************************
changed: [192.168.232.11] => (item={u'gid': 5000, u'group_name': u'leviathan'})
changed: [192.168.232.13] => (item={u'gid': 5000, u'group_name': u'leviathan'})
changed: [192.168.232.12] => (item={u'gid': 5000, u'group_name': u'leviathan'})
changed: [192.168.232.11] => (item={u'gid': 5001, u'group_name': u'prometheus'})
changed: [192.168.232.13] => (item={u'gid': 5001, u'group_name': u'prometheus'})
changed: [192.168.232.12] => (item={u'gid': 5001, u'group_name': u'prometheus'})

TASK [linux : 创建Linux系统用户:MySQL] ***************************************************************************************************************
changed: [192.168.232.12] => (item={u'username': u'leviathan', u'comment': u'Leviathan', u'shell': u'/bin/bash', u'group': u'leviathan', u'uid': 5000, u'groups': [u'root'], u'password': u'oracle', u'dir_home': u'/home/leviathan'})
changed: [192.168.232.13] => (item={u'username': u'leviathan', u'comment': u'Leviathan', u'shell': u'/bin/bash', u'group': u'leviathan', u'uid': 5000, u'groups': [u'root'], u'password': u'oracle', u'dir_home': u'/home/leviathan'})
changed: [192.168.232.11] => (item={u'username': u'leviathan', u'comment': u'Leviathan', u'shell': u'/bin/bash', u'group': u'leviathan', u'uid': 5000, u'groups': [u'root'], u'password': u'oracle', u'dir_home': u'/home/leviathan'})
changed: [192.168.232.12] => (item={u'username': u'prometheus', u'comment': u'Prometheus Monitor System', u'shell': u'/sbin/nologin', u'group': u'prometheus', u'uid': 5001, u'groups': [u'root', u'prometheus'], u'password': u'oracle', u'dir_home': u'/home/prometheus'})
changed: [192.168.232.13] => (item={u'username': u'prometheus', u'comment': u'Prometheus Monitor System', u'shell': u'/sbin/nologin', u'group': u'prometheus', u'uid': 5001, u'groups': [u'root', u'prometheus'], u'password': u'oracle', u'dir_home': u'/home/prometheus'})
changed: [192.168.232.11] => (item={u'username': u'prometheus', u'comment': u'Prometheus Monitor System', u'shell': u'/sbin/nologin', u'group': u'prometheus', u'uid': 5001, u'groups': [u'root', u'prometheus'], u'password': u'oracle', u'dir_home': u'/home/prometheus'})

TASK [linux : 创建目录] ****************************************************************************************************************************
changed: [192.168.232.12] => (item={u'owner': u'root', u'path': u'/upload', u'group': u'root'})
changed: [192.168.232.11] => (item={u'owner': u'root', u'path': u'/upload', u'group': u'root'})
changed: [192.168.232.13] => (item={u'owner': u'root', u'path': u'/upload', u'group': u'root'})
changed: [192.168.232.11] => (item={u'owner': u'root', u'path': u'/temp_me', u'group': u'root'})
changed: [192.168.232.12] => (item={u'owner': u'root', u'path': u'/temp_me', u'group': u'root'})
changed: [192.168.232.13] => (item={u'owner': u'root', u'path': u'/temp_me', u'group': u'root'})
changed: [192.168.232.11] => (item={u'owner': u'root', u'path': u'/backup', u'group': u'root'})
changed: [192.168.232.12] => (item={u'owner': u'root', u'path': u'/backup', u'group': u'root'})
changed: [192.168.232.13] => (item={u'owner': u'root', u'path': u'/backup', u'group': u'root'})
changed: [192.168.232.12] => (item={u'owner': u'root', u'path': u'/project_base', u'group': u'root'})
changed: [192.168.232.11] => (item={u'owner': u'root', u'path': u'/project_base', u'group': u'root'})
changed: [192.168.232.13] => (item={u'owner': u'root', u'path': u'/project_base', u'group': u'root'})
changed: [192.168.232.12] => (item={u'owner': u'root', u'path': u'/data', u'group': u'root'})
changed: [192.168.232.11] => (item={u'owner': u'root', u'path': u'/data', u'group': u'root'})
changed: [192.168.232.13] => (item={u'owner': u'root', u'path': u'/data', u'group': u'root'})
changed: [192.168.232.11] => (item={u'owner': u'root', u'path': u'/iso', u'group': u'root'})
changed: [192.168.232.12] => (item={u'owner': u'root', u'path': u'/iso', u'group': u'root'})
changed: [192.168.232.13] => (item={u'owner': u'root', u'path': u'/iso', u'group': u'root'})

TASK [linux : YUM:文件:/etc/yum.conf] ************************************************************************************************************
changed: [192.168.232.13]
changed: [192.168.232.12]
changed: [192.168.232.11]

TASK [linux : YUM:安装包] *************************************************************************************************************************
changed: [192.168.232.11]
changed: [192.168.232.13]
changed: [192.168.232.12]

TASK [linux : 防火墙:禁用] **************************************************************************************************************************
changed: [192.168.232.11]
changed: [192.168.232.12]
changed: [192.168.232.13]

TASK [linux : SELINUX:禁用] **********************************************************************************************************************
ok: [192.168.232.13]
ok: [192.168.232.11]
ok: [192.168.232.12]

TASK [linux : SELINUX:宽容模式] ********************************************************************************************************************
fatal: [192.168.232.12]: FAILED! => {"changed": true, "cmd": "setenforce 0", "delta": "0:00:00.003303", "end": "2021-03-25 08:16:14.509891", "msg": "non-zero return code", "rc": 1, "start": "2021-03-25 08:16:14.506588", "stderr": "setenforce: SELinux is disabled", "stderr_lines": ["setenforce: SELinux is disabled"], "stdout": "", "stdout_lines": []}
...ignoring
fatal: [192.168.232.13]: FAILED! => {"changed": true, "cmd": "setenforce 0", "delta": "0:00:00.003009", "end": "2021-03-25 08:16:14.508180", "msg": "non-zero return code", "rc": 1, "start": "2021-03-25 08:16:14.505171", "stderr": "setenforce: SELinux is disabled", "stderr_lines": ["setenforce: SELinux is disabled"], "stdout": "", "stdout_lines": []}
...ignoring
fatal: [192.168.232.11]: FAILED! => {"changed": true, "cmd": "setenforce 0", "delta": "0:00:00.003274", "end": "2021-03-25 08:16:14.511847", "msg": "non-zero return code", "rc": 1, "start": "2021-03-25 08:16:14.508573", "stderr": "setenforce: SELinux is disabled", "stderr_lines": ["setenforce: SELinux is disabled"], "stdout": "", "stdout_lines": []}
...ignoring

TASK [linux : SELINUX:查询] **********************************************************************************************************************
changed: [192.168.232.13]
changed: [192.168.232.12]
changed: [192.168.232.11]

TASK [linux : SELINUX:结果] **********************************************************************************************************************
ok: [192.168.232.11] => {
    "msg": [
        "SELinux status:                 disabled"
    ]
}
ok: [192.168.232.12] => {
    "msg": [
        "SELinux status:                 disabled"
    ]
}
ok: [192.168.232.13] => {
    "msg": [
        "SELinux status:                 disabled"
    ]
}

PLAY [Grafana] *********************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************
ok: [192.168.232.11]

TASK [grafana : 创建目录] **************************************************************************************************************************
changed: [192.168.232.11] => (item={u'owner': u'root', u'path': u'/upload/grafana', u'group': u'root'})

TASK [grafana : 上传安装介质] ************************************************************************************************************************
changed: [192.168.232.11]

TASK [grafana : 安装] ****************************************************************************************************************************
[WARNING]: Consider using the yum, dnf or zypper module rather than running 'rpm'.  If you need to use command because yum, dnf or zypper is
insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
changed: [192.168.232.11]

TASK [grafana : 启动服务] **************************************************************************************************************************
changed: [192.168.232.11]

TASK [grafana : 检查服务:运行用户] *********************************************************************************************************************
changed: [192.168.232.11]

TASK [grafana : 检查服务:进程] ***********************************************************************************************************************
changed: [192.168.232.11]

TASK [grafana : 检查服务:服务监听端口] *******************************************************************************************************************
changed: [192.168.232.11]

TASK [grafana : 检查服务:结果] ***********************************************************************************************************************
ok: [192.168.232.11] => {
    "msg": "运行用户【grafana】进程ID【3049】监听端口【:::3000】"
}

PLAY [Prometheus | Node Exporter] **************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************
ok: [192.168.232.11]
ok: [192.168.232.12]
ok: [192.168.232.13]

TASK [node_exporter : 创建目录] ********************************************************************************************************************
changed: [192.168.232.12] => (item={u'owner': u'prometheus', u'path': u'/upload/node_exporter', u'group': u'prometheus'})
changed: [192.168.232.11] => (item={u'owner': u'prometheus', u'path': u'/upload/node_exporter', u'group': u'prometheus'})
changed: [192.168.232.13] => (item={u'owner': u'prometheus', u'path': u'/upload/node_exporter', u'group': u'prometheus'})
changed: [192.168.232.12] => (item={u'owner': u'prometheus', u'path': u'/usr/local/node_exporter', u'group': u'prometheus'})
changed: [192.168.232.11] => (item={u'owner': u'prometheus', u'path': u'/usr/local/node_exporter', u'group': u'prometheus'})
changed: [192.168.232.13] => (item={u'owner': u'prometheus', u'path': u'/usr/local/node_exporter', u'group': u'prometheus'})

TASK [node_exporter : 上传安装介质并解压] ***************************************************************************************************************
changed: [192.168.232.12]
changed: [192.168.232.13]
changed: [192.168.232.11]

TASK [node_exporter : 移动到软件根目录] ****************************************************************************************************************
changed: [192.168.232.12]
changed: [192.168.232.11]
changed: [192.168.232.13]

TASK [node_exporter : Linux:服务文件] **************************************************************************************************************
changed: [192.168.232.13]
changed: [192.168.232.11]
changed: [192.168.232.12]

TASK [node_exporter : 启动:Node Exporter] ********************************************************************************************************
changed: [192.168.232.12]
changed: [192.168.232.11]
changed: [192.168.232.13]

PLAY [Prometheus | Alert Manager] **************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************
ok: [192.168.232.11]

TASK [alert_manager : 创建目录] ********************************************************************************************************************
changed: [192.168.232.11] => (item={u'owner': u'prometheus', u'path': u'/upload/alert_manager', u'group': u'prometheus'})
changed: [192.168.232.11] => (item={u'owner': u'prometheus', u'path': u'/usr/local/alert_manager', u'group': u'prometheus'})
changed: [192.168.232.11] => (item={u'owner': u'prometheus', u'path': u'/data/alert_manager', u'group': u'prometheus'})

TASK [alert_manager : 上传安装介质并解压] ***************************************************************************************************************
changed: [192.168.232.11]

TASK [alert_manager : 移动到软件根目录] ****************************************************************************************************************
changed: [192.168.232.11]

TASK [alert_manager : Alert Manager:配置文件] ******************************************************************************************************
changed: [192.168.232.11]

TASK [alert_manager : Linux:服务文件] **************************************************************************************************************
changed: [192.168.232.11]

TASK [alert_manager : 启动:Alert Manager] ********************************************************************************************************
changed: [192.168.232.11]

PLAY [Prometheus | Prometheus Server] **********************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************
ok: [192.168.232.11]

TASK [prometheus_server : 创建目录] ****************************************************************************************************************
changed: [192.168.232.11] => (item={u'owner': u'prometheus', u'path': u'/upload/prometheus_server', u'group': u'prometheus'})
changed: [192.168.232.11] => (item={u'owner': u'prometheus', u'path': u'/usr/local/prometheus_server', u'group': u'prometheus'})
changed: [192.168.232.11] => (item={u'owner': u'prometheus', u'path': u'/data/prometheus_server', u'group': u'prometheus'})

TASK [prometheus_server : 上传安装介质并解压] ***********************************************************************************************************
changed: [192.168.232.11]

TASK [prometheus_server : 移动到软件根目录] ************************************************************************************************************
changed: [192.168.232.11]

TASK [prometheus_server : Prometheus Server:配置文件] **********************************************************************************************
changed: [192.168.232.11]

TASK [prometheus_server : Prometheus Server:配置文件 / 修改] *****************************************************************************************
[WARNING]: Consider using the replace, lineinfile or template module rather than running 'sed'.  If you need to use command because replace,
lineinfile or template is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid
of this message.
changed: [192.168.232.11]

TASK [prometheus_server : Linux:服务文件] **********************************************************************************************************
changed: [192.168.232.11]

TASK [prometheus_server : 启动:Prometheus Server] ************************************************************************************************
changed: [192.168.232.11]

PLAY RECAP *************************************************************************************************************************************
192.168.232.11             : ok=41   changed=33   unreachable=0    failed=0    skipped=0    rescued=0    ignored=1   
192.168.232.12             : ok=17   changed=13   unreachable=0    failed=0    skipped=0    rescued=0    ignored=1   
192.168.232.13             : ok=17   changed=13   unreachable=0    failed=0    skipped=0    rescued=0    ignored=1   

[root@center ansible-prometheus]# 

脚本运行完成后,Prometheus就可以访问了:
http://192.168.232.11:9090/targets

可以看到,稍等片刻,Prometheus的Targets的各个服务都是【UP】状态了。

Grafana:
http://192.168.232.11:3000

添加刚刚的Prometheus的数据源:

导入官方的Prometheus的面板,编号:8919

至此,Prometheus的简单部署完成。


Subscribe
提醒
guest

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

0 评论
Inline Feedbacks
View all comments
隐藏
变装
0
Would love your thoughts, please comment.x
()
x