通过saltstack部署zabbix
一. 案例简介
使用Saltstack部署zabbix,主要包括zabbix-server、zabbix-agent、zabbix-web以及mysql。其中,zabbix的版本使用4.0,使用本地自己搭建的zabbix仓库,加快部署效率。
两台虚拟机作为示例,分别是server2和server3。
部署架构
[[email protected] srv]# tree
.
└── salt
├── top.sls
├── zabbix-agent
│ ├── init.sls
│ └── zabbix_agentd.conf
├── zabbix-db
│ └── init.sls
├── zabbix-server
│ ├── init.sls
│ └── zabbix_server.conf
└── zabbix-web
├── init.sls
└── zabbix.conf
5 directories, 8 files
二.部署过程
1 本地zabbix仓库搭建
[email protected] ~]# vim /etc/yum.repos.d/dvd.repo
[[email protected] ~]# cat /etc/yum.repos.d/dvd.repo
[dvd]
name=dvd
baseurl=http://172.25.7.250/rhel7.6
gpgcheck=0
[saltstack]
name=saltstack
baseurl=http://172.25.7.250/3000
gpgcheck=0
[zabbix]
name=zabbix
baseurl=http://172.25.7.250/4.0
gpgcheck=0
2 创建数据库
创建目录并且编写init.sls文件
[email protected] zabbix-db]# vim init.sls
[[email protected] zabbix-db]# cat init.sls
db-install:
pkg.installed:
- pkgs:
- mariadb-server
- mariadb
- MySQL-python
service.running:
- name: mariadb
cmd.run:
- name: mysql -pwestos && create database zabbix character set utf8 collate utf8_bin; && create user 'zabbix'@'localhost' identified by 'westos'; && grant all privileges on zabbix.* to 'zabbix'@'localhost'; && quit && cd /usr/share/doc/ && cd zabbix-server-mysql-4.0.5/ && zcat create.sql.gz | mysql -pwestos zabbix
推送:
[[email protected] zabbix-db]# salt server2 state.sls zabbix-db
在server2中查看,
发现推送的文件
3 为server2部署zabbix-server
编写init.sls文件
[[email protected] zabbix-server]# cat init.sls
server-install:
pkg.installed:
- pkgs:
- zabbix-server-mysql
- zabbix-agent
file.managed:
- name: /etc/zabbix/zabbix_server.conf
- source: salt://zabbix-server/zabbix_server.conf
- template: jinja
- context:
dbpasswd: westos
service.running:
- name: zabbix-server
- watch:
- file: server-install
zabbix-agent:
service.running
修改配置文件
/srv/salt/zabbix-server/zabbix_server.conf
推送
4 为server2,server3配置agent
创建目录并且编写init.sls文件
[[email protected] salt]# mkdir zabbix-agent
[[email protected] salt]# cd zabbix-agent/
[[email protected] zabbix-agent]# vim init.sls
[[email protected] zabbix-agent]# cat init.sls
agent-install:
pkg.installed:
- name: zabbix-agent
file.managed:
- name: /etc/zabbix/zabbix_agentd.conf
- source: salt://zabbix-agent/zabbix_agentd.conf
- template: jinja
- context:
{% if grains['fqdn'] == 'server2' %}
zabbixserver: 172.25.7.2
{% elif grains['fqdn'] == 'server3' %}
zabbixserver: 172.25.7.3
{% endif %}
hostname: {{ grains['fqdn'] }}
service.running:
- name: zabbix-agent
- watch:
- file: agent-install
修改配置文件
[[email protected] zabbix-agent]# cat zabbix_agentd.conf | grep zabbixserver
Server= {{ zabbixserver }}
ServerActive= {{ zabbixserver }}
推送:
[[email protected] zabbix-agent]# salt '*' state.sls zabbix-agent
server3:
----------
ID: agent-install
Function: pkg.installed
Name: zabbix-agent
Result: True
Comment: All specified packages are already installed
Started: 06:10:31.439168
Duration: 844.035 ms
Changes:
----------
ID: agent-install
Function: file.managed
Name: /etc/zabbix/zabbix_agentd.conf
Result: True
Comment: File /etc/zabbix/zabbix_agentd.conf updated
Started: 06:10:32.286875
Duration: 47.639 ms
Changes:
----------
diff:
---
+++
@@ -95,7 +95,7 @@
# Default:
# Server=
-Server= 172.25.7.2
+Server= 172.25.7.3
### Option: ListenPort
# Agent will listen on this port for connections from the server.
@@ -136,7 +136,7 @@
# Default:
# ServerActive=
-ServerActive= 172.25.7.2
+ServerActive= 172.25.7.3
### Option: Hostname
# Unique, case sensitive hostname.
----------
ID: agent-install
Function: service.running
Name: zabbix-agent
Result: True
Comment: Service restarted
Started: 06:10:32.409923
Duration: 64.286 ms
Changes:
----------
zabbix-agent:
True
Summary for server3
------------
Succeeded: 3 (changed=2)
Failed: 0
------------
Total states run: 3
Total run time: 955.960 ms
server2:
----------
ID: agent-install
Function: pkg.installed
Name: zabbix-agent
Result: True
Comment: All specified packages are already installed
Started: 06:10:32.247200
Duration: 889.695 ms
Changes:
----------
ID: agent-install
Function: file.managed
Name: /etc/zabbix/zabbix_agentd.conf
Result: True
Comment: File /etc/zabbix/zabbix_agentd.conf updated
Started: 06:10:33.141290
Duration: 43.863 ms
Changes:
----------
diff:
---
+++
@@ -95,7 +95,7 @@
# Default:
# Server=
-Server=127.0.0.1
+Server= 172.25.7.2
### Option: ListenPort
# Agent will listen on this port for connections from the server.
@@ -136,7 +136,7 @@
# Default:
# ServerActive=
-ServerActive=127.0.0.1
+ServerActive= 172.25.7.2
### Option: Hostname
# Unique, case sensitive hostname.
@@ -147,7 +147,7 @@
# Default:
# Hostname=
-Hostname=Zabbix server
+Hostname= server2
### Option: HostnameItem
# Item used for generating Hostname if it is undefined. Ignored if Hostname is defined.
----------
ID: agent-install
Function: service.running
Name: zabbix-agent
Result: True
Comment: Started Service zabbix-agent
Started: 06:10:33.186499
Duration: 124.787 ms
Changes:
----------
zabbix-agent:
True
Summary for server2
------------
Succeeded: 3 (changed=2)
Failed: 0
------------
Total states run: 3
Total run time: 1.058 s
5 为server2部署zabbix-web界面
[[email protected] zabbix-web]# cat init.sls
web-install:
pkg.installed:
- pkgs:
- zabbix-web-mysql
- zabbix-web
- httpd
- php
- php-mysql
file.managed:
- name: /etc/httpd/conf.d/zabbix.conf
- source: salt://zabbix-web/zabbix.conf
service.running:
- name: httpd
- watch:
- file: web-install
修改时区
[[email protected] zabbix-web]# vim zabbix.conf
推送:
[[email protected] zabbix-web]# salt server2 state.sls zabbix-web
server2:
----------
ID: web-install
Function: pkg.installed
Result: True
Comment: All specified packages are already installed
Started: 06:22:37.085842
Duration: 733.787 ms
Changes:
----------
ID: web-install
Function: file.managed
Name: /etc/httpd/conf.d/zabbix.conf
Result: True
Comment: File /etc/httpd/conf.d/zabbix.conf updated
Started: 06:22:37.823486
Duration: 38.106 ms
Changes:
----------
diff:
---
+++
@@ -17,7 +17,7 @@
php_value max_input_time 300
php_value max_input_vars 10000
php_value always_populate_raw_post_data -1
- # php_value date.timezone Europe/Riga
+ php_value date.timezone Asia/shanghai
</IfModule>
</Directory>
----------
ID: web-install
Function: service.running
Name: httpd
Result: True
Comment: Service restarted
Started: 06:22:37.922469
Duration: 1196.386 ms
Changes:
----------
httpd:
True
Summary for server2
------------
Succeeded: 3 (changed=2)
Failed: 0
------------
Total states run: 3
Total run time: 1.968 s
6 编写top文件
[[email protected] salt]# vim top.sls
[[email protected] salt]# cat top.sls
base:
'server2':
- zabbix-db
- zabbix-server
- zabbix-web
- zabbix-agent
'server3':
- zabbix-agent
推送:
[[email protected] salt]# salt '*' state.highstate
7 测试
web端访问