SaltSack自动化运维:Zabbix的部署
Posted Tuki_a
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SaltSack自动化运维:Zabbix的部署相关的知识,希望对你有一定的参考价值。
要在部署好的saltstack上实现自动化部署zabbix,用两台主机来实现,一台作是salt-mater,另一台是salt-minion,在master端配置实现minion端自动化部署zabbix
实验环境
两台台虚拟机主机名/ip/角色如下所示:
server5 192.168.122.15 salt-master
server6 192.168.122.16 salt-minion
server5配置salt源,server6配置salt源和zabbix源
server5已安装并初步配置好salt
##为了方便,可以在真机ssh连接虚拟机操作
部署步骤
创建相关目录及文件
[root@server5 ~]# mkdir /srv/salt/
[root@server5 ~]# mkdir /srv/pillar/
[root@server5 ~]# cd /srv/salt/
[root@server5 salt]# mkdir mariadb
[root@server5 salt]# mkdir zabbix-server
[root@server5 salt]# mkdir zabbix-agent
[root@server5 salt]# mkdir zabbix-web
编写部署zabbix-server的文件
[root@server5 salt]# cd zabbix-server/
[root@server5 zabbix-server]# pwd
/srv/salt/zabbix-server
[root@server5 zabbix-server]# vim 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: {{ pillar['DBPasswd'] }}
service.running:
- name: zabbix-server
- watch:
- file: server-install
zabbix-agent:
service.running
编写部署zabbix-agent的文件
[root@server5 salt]# cd zabbix-agent/
[root@server5 zabbix-agent]# pwd
/srv/salt/zabbix-agent
[root@server5 zabbix-agent]# vim 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:
zabbixserver: {{ pillar['Zabbix-server'] }}
hostname: {{ grains['fqdn'] }}
service.running:
- name: zabbix-agent
- watch:
- file: agent-install
编写部署zabbix-web的文件
[root@server5 salt]# cd zabbix-web/
[root@server5 zabbix-web]# pwd
/srv/salt/zabbix-web
[root@server5 zabbix-web]# vim 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
编写部署mariadb的文件
[root@server5 salt]# cd mariadb/
[root@server5 mariadb]# pwd
/srv/salt/mariadb
[root@server5 mariadb]# vim init.sls
##文件内容:
db-install:
pkg.installed:
- pkgs:
- mariadb-server
- mariadb
- MySQL-python
service.running:
- name: mariadb
cmd.run:
- name: mysql -e "DELETE FROM mysql.user WHERE User='';" && mysql -e "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');" && mysql -e "DROP DATABASE IF EXISTS test;" && mysql -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\\\_%'" && mysql -e "FLUSH PRIVILEGES;"
- onlyif: mysql -e "show databases;"
db-configure:
file.managed:
- name: /mnt/create.sql.gz
- source: salt://mariadb/create.sql.gz
mysql_database.present:
- name: zabbix
- character_set: utf8
- collate: utf8_bin
- connection_user: root
- connection_pass: westos
mysql_user.present:
- name: zabbix
- host: localhost
- password: westos
- connection_user: root
- connection_pass: westos
mysql_grants.present:
- grant: all privileges
- database: zabbix.*
- user: zabbix
- connection_user: root
- connection_pass: westos
cmd.wait:
- name: zcat /mnt/create.sql.gz | mysql -uzabbix -pwestos zabbix
- watch:
- mysql_database: db-configure
编写/srv/salt/下的top文件
[root@server5 salt]# pwd
/srv/salt
[root@server5 salt]# vim top.sls
##文件内容:
base:
'server6':
- mariadb
- zabbix-server
- zabbix-web
- zabbix-agent
编写/srv/pillar下的zabbix.sls文件
[root@server5 pillar]# pwd
/srv/pillar
[root@server5 pillar]# vim zabbix.sls
##文件内容:
##设置zabbix监控端监控自己
{% if grains['fqdn'] == 'server6' %}
DBPasswd: westos
Zabbix-server: 127.0.0.1
{% endif %}
编写/srv/pillar下的top文件
[root@server5 pillar]# pwd
/srv/pillar
[root@server5 pillar]# vim top.sls
##文件内容:
base:
'*':
- zabbix
开一台全新干净的虚拟机(不是部署用的那两台主机),下载zabbix相关软件来获取相关的配置文件
[root@server4 yum.repos.d]# yum install -y zabbix-agent.x86_64 zabbix-server-mysql.x86_64 zabbix-web.noarch
接下来把需要的配置文件拷贝到salt-master端
[root@server4 yum.repos.d]# cd /etc/zabbix/
[root@server4 zabbix]# scp zabbix_agentd.conf root@192.168.122.15:/srv/salt/zabbix-agent/
[root@server4 zabbix]# scp zabbix_server.conf root@192.168.122.15:/srv/salt/zabbix-server/
[root@server4 zabbix]# cd /etc/httpd/conf.d
[root@server4 conf.d]# ls
autoindex.conf php.conf README userdir.conf welcome.conf zabbix.conf
[root@server4 conf.d]# scp zabbix.conf root@192.168.122.15:/srv/salt/zabbix-web/
[root@server4 conf.d]# scp /usr/share/doc/zabbix-server-mysql-4.0.5/create.sql.gz root@192.168.122.15:/srv/salt/mariadb/
此时我们查看下/srv的结构,如下图所示:
接下来开始在master端编辑配置文件(使用变量定义)
修改zabbix-server的配置文件
[root@server5 srv]# cd salt/
[root@server5 salt]# ls
mariadb top.sls zabbix-agent zabbix-server zabbix-web
[root@server5 salt]# cd zabbix-server/
[root@server5 zabbix-server]# ls
init.sls zabbix_server.conf
[root@server5 zabbix-server]# vim zabbix_server.conf
DBPassword={{ DBPasswd }} #引用变量定义server端的DBPassword
修改zabbix-agent的配置文件
[root@server5 zabbix-server]# cd ..
[root@server5 salt]# cd zabbix-agent/
[root@server5 zabbix-agent]# ls
init.sls zabbix_agentd.conf
[root@server5 zabbix-agent]# vim zabbix_agentd.conf
ServerActive={{ zabbixserver }}
Hostname={{ hostname }}
Server={{ zabbixserver }}
修改zabbix-web的配置文件(修改时区)
[root@server5 zabbix-agent]# cd ..
[root@server5 salt]# cd zabbix-web/
[root@server5 zabbix-web]# ls
init.sls zabbix.conf
[root@server5 zabbix-web]# vim zabbix.conf
php_value date.timezone Asia/Shanghai
master推送给minion端配置zabbix
[root@server5 pillar]# salt server6 state.highstate
可以看到没有报错,接下来进入浏览器访问
http://192.168.122.16/zabbix
访问成功!
填入数据库用户zabbix的密码
用管理员账号登录,用户名Admin,密码:zabbix
成功进入监控页面!minion端监控自己成功!
至此部署成功完成!
以上是关于SaltSack自动化运维:Zabbix的部署的主要内容,如果未能解决你的问题,请参考以下文章