Install Prometheus By Ansible
如题所示,本文将呈现如何通过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脚本安装过程如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
[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的简单部署完成。
赞一个