用Saltstack自动部署zabbix
实验设置:
用server1主机作satlstack的master端,server2主机作saltstack的minion端,server2作zabbix的server端和agent端
一、Saltstack的安装
1、在server1主机安装saltstack的master端
yum install salt-master.noarch -y
systemctl enable --now salt-master.service
systemctl start salt-master.service
2、在server2主机安装并配置saltstack的minion端
yum install salt-minion.noarch -y
vim /etc/salt/minion ##设置master主机的ip
systemctl enable --now salt-minion.service
systemctl start salt-minion.service
3、master端执行命令允许minion连接
salt-key -L ##显示连接
salt-key -A ##整体添加
二、自动配置zabbix
- 安装tree,查看整体布局结构
yum install tree
1、搭建zabbix的repo源
2、repo模块
mkdir /srv/salt
mkdir /srv/salt/repo
vim /srv/salt/repo/init.sls
repo:
pkgrepo.managed:
- name : zabbix
- baseurl: http://172.25.36.250/4.0
- gpgcheck: 0
3、zabbix-server模块
zabbix_server.conf
为安装好zabbix-server-mysql后/etc/zabbix
下的配置文件,将其复制过来使用
mkdir /srv/salt/zabbix-server
cp zabbix_server.conf /srv/zabbix/
vim /srv/salt/zabbix-server/init.sls
zabbix-server-install:
pkg.installed:
- pkgs:
- zabbix-server-mysql
file.managed:
- name: /etc/zabbix/zabbix_server.conf
- source: salt://zabbix-server/zabbix_server.conf
- template: jinja
- context:
dbpasswd: {{ pillar['DBPASSWD'] }}
zabbix-server:
service.running:
- enable: true
- reload: true
- watch:
- file: zabbix-server-install
vim /srv/zabbix-server/zabbix_server.conf
124 DBPassword={{ dbpasswd }}
4、zabbix-server模块
zabbix_agentd.conf
为安装好zabbix-agent后/etc/zabbix
下的配置文件,将其复制过来使用
mkdir /srv/salt/zabbix-agent
cp zabbix_agentd.conf /srv/salt/zabbix-agent
vim /srv/salt/zabbix-agent/init.sls
zabbix-agent-install:
pkg.installed:
- pkgs:
- zabbix-agent
file.managed:
- name: /etc/zabbix/zabbix_agentd.conf
- source: salt://zabbix-agent/zabbix_agentd.conf
- template: jinja
- context:
server: {{ pillar['ZABBIX-SERVER'] }}
hostname: {{ grains['fqdn'] }}
zabbix-agent:
service.running:
- enable: true
- reload: true
- watch:
- file: zabbix-agent-install
vim /srv/salt/zabbix-agent/zabbix_agent.conf
98 Server={{ server }}
139 ServerActive={{ server }}
150 Hostname={{ hostname }}
5、zabbix-web模块
zabbix.conf
为安装zabbix-web
后/etc/httpd/conf.d/
下的配置文件,将其复制过来使用
mkdir /srv/salt/zabbix-web
cp zabbix.conf /srv/salt/zabbix-web
vim /srv/salt/zabbix-web/init.sls
zabbix-web-install:
pkg.installed:
- pkgs:
- zabbix-web-mysql
- httpd
file.managed:
- name: /etc/httpd/conf.d/zabbix.conf
- source: salt://zabbix-web/zabbix.conf
httpd:
service.running:
- enable: true
- reload: true
- watch:
- file: zabbix-web-install
vim /srv/salt/zabbix-web/zabbix.conf
20 php_value date.timezone Asia/Shanghai
6、mysql模块
create.sql.gz
为数据模板,将zabbix-server数据模板导入数据库
mkdir /srv/salt/mysql
cp create.sql.gz /srv/salt/mysql
vim /srv/salt/mysql/init.sls
db-install:
pkg.installed:
- pkgs:
- mariadb-server
- mariadb
- MySQL-python
service.running:
- name: mariadb
db-configure:
file.managed:
- name: /mnt/create.sql.gz
- source: salt://mysql/create.sql.gz
mysql_database.present:
- name: zabbix
- character_set: utf8
- collate: utf8_bin
- connection_user: root
- connection_pass: {{ pillar['password'] }}
mysql_user.present:
- name: zabbix
- host: localhost
- password: westos
- connection_user: root
- connection_pass: {{ pillar['password'] }}
mysql_grants.present:
- grant: all privileges
- database: zabbix.*
- user: zabbix
- connection_user: root
- connection_pass: {{ pillar['password'] }}
cmd.wait:
- name: zcat /mnt/create.sql.gz | mysql -uroot zabbix
- watch:
- mysql_database: db-configure
7、pillar模块
mkdir /srv/pillar
vim /srv/pillar/zabbix.sls
{% if grains['fqdn'] == 'server2' %}
DBPASSWD: westos
ZABBIX-SERVER: 127.0.0.1
password: westos
{% else %}
{% endif %}
vim /srv/pillar/top.sls
base:
'*':
- zabbix
8、top模块
vim /srv/salt/top.sls
base:
'server2':
- repo
- zabbix-server
- zabbix-agent
- zabbix-web
- mysql
三、执行批量处理
salt server2 state.highstate
1、在server2上检测能否收到信息
yum install zabbix-get -y
zabbix_get -s 127.0.0.1 -p 10050 -k 'mysql.ping'
zabbix_get -s 127.0.0.1 -p 10050 -k 'mysql.version'
2、web页面访问:http://172.25.36.2/zabbix