自动化部署zabbix
要在server3安装数据库mariadb
在server1上写salt脚本来部署zabbix服务
[root@server1 ~]# cd /srv/salt
[root@server1 salt]# mkdir mysql/files/ -p
[root@server1 salt]# cd mysql/
[root@server1 mysql]# vim init.sls
mysql-install:
pkg.installed:
- pkgs:
- mariadb-server
- MySQL-python
file.managed:
- name: /etc/my.cnf
- source: salt://mysql/files/my.cnf
service.running:
- name: mariadb
- enable: true
- watch:
- file: mysql-install
mysql-config:
mysql_database.present:
- name: zabbix
mysql_user.present:
- name: zabbix
- host: '%'
- password: "westos"
mysql_grants.present:
- grant: all privileges
- database: zabbix.*
- user: zabbix
- host: '%'
file.managed:
- name: /mnt/create.sql
- source: salt://mysql/files/create.sql
cmd.run:
- name: mysql zabbix < /mnt/create.sql && touch /mnt/zabbix.lock
- creates: /mnt/zabbix.lock
我们在server1的files目录下需要有两个文件
create.sql
my.cnf
需要在server2上复制到server1上
server2
cd /usr/share/doc/zabbix-server-mysql-4.0.5/
ls
scp create.sql.gz server1:/srv/salt/mysql/files
在server1上需要手动解压
[root@server1 files]# gunzip create.sql.gz
[root@server1 files]# tree
├── create.sql
└── my.cnf
0 directories, 2 files
[root@server1 files]# vim my.cnf
log-bin=mysql-bin ##开启二进制日志
character-set-server=utf8 ##mysql默认需要的字符类型
server2上安装zabbix-server zabbix-agent zabbix-web
[root@server1 salt]# mkdir zabbix-server/
[root@server1 salt]# cd zabbix-server/
[root@server1 zabbix-server]# vim init.sls
zabbix-server:
pkgrepo.managed:
- name: zabbix
- humanname: zabbix 4.0
- baseurl: http://172.25.254.250/pub/docs/zabbix/4.0
- gpgcheck: 0
pkg.installed:
- pkgs:
- zabbix-server-mysql
- zabbix-agent
- zabbix-web-mysql
file.managed:
- name: /etc/zabbix/zabbix_server.conf
- source: salt://zabbix-server/files/zabbix_server.conf
service.running:
- name: zabbix-server
- enable: true
- watch:
- file: zabbix-server
zabbix-agent:
service.running
zabbix-web:
file.managed:
- name: /etc/httpd/conf.d/zabbix.conf
- source: salt://zabbix-server/files/zabbix.conf
service.running:
- name: httpd
- enable: true
- watch:
- file: zabbix-web
/etc/zabbix/web/zabbix.conf.php:
file.managed:
- source: salt://zabbix-server/files/zabbix.conf.php
我们server1上的/srv/salt/zabbix-server/files
下有三个文件:zabbix.conf.php暂时没有需要到后面的操作才会出现!
我们需要从server2传过来
[root@server2 conf.d]# scp zabbix.conf server1:/srv/salt/zabbix-server/files
[root@server2] scp /etc/zabbix/zabbix_server.conf server1:/srv/salt/zabbix-server/files
vim /srv/salt/zabbix-server/files/zabbix.conf
传过来之后,在server1上修改zabbix.conf里面的时区
vim /srv/salt/zabbix-server/files/zabbix_server.conf
DBHost=172.25.1.3
DBPasswoed=westos
写top文件
[root@server1 salt]# vim top.sls
base:
'roles:apache':
- match: grain
- apache
- keepalived
- zabbix-server
'roles:nginx':
- match: grain
- nginx
- keepalived
- mysql
[root@server1 salt]# salt '*' state.highstate
执行完之后会出现一个报错,这是因为缺少一个文件,初始化完成之后才会生成
接着访问172.25.1.2/zabbix
mysql来源写172.25.1.3 因为数据库在server3上
zabbix在本地写localhost就可以!
从server2中拿到zabbix.conf.php
[root@server2 ~]# cd /etc/zabbix/web/
[root@server2 web]# ls
maintenance.inc.php zabbix.conf.php
[root@server2 web]# scp zabbix.conf.php server1:/srv/salt/zabbix-server/files
再次执行之后就不会出现报错,到这个zabbix安装配置完成
root@server1 salt]# salt '*' state.highstate
再次访问172.25.1.2/zabbix可以看到成功部署了!!