目录
ansible端部署
使用ansible配置zabbix-mysql端
使用ansible配置zabbix-server端
使用ansible配置zabbix-agent端
一键部署zabbix
Ansible是一款开源的自动化运维工具,可以通过SSH协议远程自动化地执行一些复杂的IT工作,例如程序部署、配置管理、应用运维等等。Ansible基于Python编写,采用YAML格式语言来描述自动化任务,具有高度的可读性和易维护性,使用起来非常方便。
Zabbix是一款开源的企业级监控系统,用于监测服务器、网络设备及其他应用程序的运行状态和性能指标。Zabbix可以采集、处理和存储各种类型的监控数据,并提供可视化的前端界面和各种告警和报警机制,帮助管理员及时掌握系统状态的变化以及服务质量的情况,并对异常进行排查和处理。
使用Ansible部署Zabbix监控平台可以大大简化部署流程,减少错误和时间浪费,有助于提高运维效率和平台的稳定性。
本次实验基本配置:
ansible主机
zabbix-server
zabbix-mysql
zabbix-agent
ansible端部署
ansible服务器给另外三台做ssh免密
ssh-keygen
ssh-copy-id +ip地址
ansible安装在之前博客有讲到:
(268条消息) Ansible学习记录-基本使用学习_fx_872431785的博客-CSDN博客
添加主机清单
vim /etc/ansible/hosts
[zabbix_server]
192.168.52.149
[zabbix_mysql]
192.168.52.148
[zabbix_agent]
192.168.52.150
测试连通性
使用ansible配置zabbix-mysql端
创建zabbix-mysql角色,并下载好数据库仓库软件包放在files目录下
ansible-galaxy init zabbix-mysql
wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
编辑zabbix-mysql/tasks/main.yml
- name: Upload and Install MariaDB RPM
hosts: zabbix_mysql
tasks:
- name: uploadk
copy:
src: mysql80-community-release-el7-3.noarch.rpm
dest: /root/
- name: installrepo
command: rpm -ivh /root/mysql80-community-release-el7-3.noarch.rpm
- name: install-yum.utils
yum:
name: yum-utils
state: present
- name: five
command: yum-config-manager --enable mysql57-community
- name: eight
command: yum-config-manager --disable mysql80-community
- name: check
command: sed -i 's/gpgcheck=1/gpgcheck=0/' /etc/yum.repos.d/mysql-community.repo
- name: installmysql
yum:
name: mysql,mysql-server
state: present
- name: mysqldstart
command: systemctl start mysqld
- name: copysh
copy:
src: mysql.sh
dest: /root/
- name: runsh
command: bash /root/mysql.sh
脚本如下
#!/bin/bash
begin() {
systemctl start mysqld
sun=`cat /var/log/mysqld.log |grep password |awk '{print $NF}'` #数据库初始化
mysqladmin -u root -p"$Feng" password 'Fengxin@123'
echo "数据库初始化成功"
}
peizhi() {
mysql -uroot -pFengxin@123 -e "create database zabbix character set utf8 collate utf8_bin;"
mysql -uroot -pFengxin@123 -e "grant all on zabbix.* to zabbix@'%' identified by 'Sunsweet@123';"
mysql -uroot -pFengxin@123 -e "set global log_bin_trust_function_creators = 1;"
echo "数据库配置成功"
}
main() {
begin
peizhi
}
main
执行main.yml文件
ansible-playbook mysql.yml
使用ansible配置zabbix-server端
zabbix官网:www.zabbix.com 使用的是zabbix 5.0版本
创建zabbix-server角色,下载好zabbix仓库文件放在files目录下
ansible-galaxy init zabbix-server
yum -y install https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
编辑zabbix-server/tasks/main.yml
- name: installrpm
copy:
src: zabbix.repo
dest: /etc/yum.repos.d/
- name: installservice
yum:
name: zabbix-server-mysql,zabbix-agent,centos-release-scl,yum-utils
state: present
- name: update
command: yum-config-manager --enable zabbix-frontend
- name: installweb
yum:
name: zabbix-web-mysql-scl,zabbix-apache-conf-scl
state: present
- name: installrpm
yum:
name: mariadb
state: present
- name: inputdata
shell: "zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix - pSunsweet@123 zabbix -h 10.0.0.228"
- name: copyfile
copy:
src: zabbix_server.conf
dest: /etc/zabbix/zabbix_server.conf
- name: copyfiles
copy:
src: zabbix.conf
dest: /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf
- name: startservice
command: systemctl restart zabbix-server zabbix-agent httpd rh-php72-php-fpm
创建server.yml文件并写入
---
- name: useserver
hosts: zabbix_server
roles:
- zabbix-server
执行server.yml文件
ansible-playbook server.yml
使用浏览器访问:http://192.168.52.149/zabbix
配置数据库信息
使用ansible配置zabbix-agent端
创建zabbix-agent角色并准备好zabbix.repo放入files目录下
ansible-galaxy init zabbix-agent
编辑zabbix-agent/tasks/main.yaml
- name: repo
copy:
src: zabbix.repo
dest: /etc/yum.repos.d/
- name: installagent
yum:
name: zabbix-agent
state: present
- name: copyfile
copy:
src: zabbix_agentd.conf
dest: /etc/zabbix/zabbix_agentd.conf
- name: startagent
command: systemctl restart zabbix-agent
编辑agent.yml
---
- name: agent
hosts: zabbix_agent
roles:
- zabbix-agent
一键部署zabbix
yml文件内容如下:
--- 注意:调用角色的顺序
- name: zabbixmysql
hosts: zabbix_mysql
roles:
- zabbix-mysql
- name: zabbixagent
hosts: zabbix_agent
roles:
- zabbix-agent
- name: zabbixserver
hosts: zabbix_server
roles:
- zabbix-server
综上,使用Ansible部署Zabbix监控平台可以大大简化部署流程,减少错误和时间浪费,有助于提高运维效率和平台的稳定性。