前提:此文档仅支持在CentOS上安装zabbix_agent。
环境:
salt-master: 172.16.0.197
salt-minions: 172.16.0.225, 172.16.0.233, 172.16.0.234, 172.16.0.226, 172.16.0.236
一、准备工作
salt-master
- 安装salt-master
curl -L https://bootstrap.saltstack.com -o install_salt.sh
sudo sh install_salt.sh -P -M
- 编辑/etc/salt/master
file_roots:
base:
- /srv/salt/base/
pillar_roots:
base:
- /srv/pillar/base/
- 启动salt-master
centos6
service salt-master start
centos7
systemctl start salt-master
- 首先在每台需要安装zabbix_agent的机器上部署salt-minion
curl -L https://bootstrap.saltstack.com -o install_salt.sh
sudo sh install_salt.sh -P
- 修改/etc/salt/minion文件
#master: salt
master: 172.16.0.197
-
启动salt-minion
centos6
service salt-minion start
centos7
systemctl start salt-minion
-
在salt-master上用salt-key -A将各minion添加认证
然后用salt '*' test.ping测试,都显示为True
[root@localhost ~]# salt '*' test.ping
172.16.0.225:
True
172.16.0.233:
True
172.16.0.234:
True
172.16.0.236:
True
172.16.0.226:
True
二、部署
- 准备目录
mkdir -p /srv/salt/base/init/files
init目录用来表示这个目录下的文件是用来系统初始化的,也就是没个系统必须具备的
files目录用来存放一些安装包和配置文件
将zabbix-3.4.8.tar.gz和zabbix_agentd.conf保存到files目录下,tar包可以官网下载,zabbix_agentd.conf可以从已经安装过的机器中拷贝过来 - 通过Pillar来设置Zabbix Server值
[root@localhost ~]# vim /srv/pillar/base/top.sls
base:
'*':
- zabbix
[root@localhost ~]# vim /srv/pillar/base/zabbix.sls
zabbix-agent:
Zabbix_Server: 172.16.0.174
- 创建启动zabbix_agentd的用户
[root@localhost ~]# mkdir /srv/salt/base/user
[root@localhost ~]# vim /usr/salt/base/user/zabbix.sls
zabbix-user-group:
group.present:
- name: zabbix
- gid: 1000
user.present:
- name: zabbix
- fullname: zabbix
- shell: /sbin/nologin
- uid: 1000
- gid: 1000
- 安装依赖包
[root@localhost ~]# vim /srv/salt/base/init/pkg-init.sls
pkg-init:
pkg.installed:
- names:
- gcc
- gcc-c++
- glibc
- openssl
- openssl-devel
- pcre-devel
这里还可以添加你需要安装的一些基础包
- 编写安装文件
[root@mycat ~]# cat /srv/salt/base/init/zabbix_agent.sls
include:
- user.zabbix
zabbix-install:
file.managed:
- name: /usr/local/src/zabbix-3.4.8.tar.gz
- source: salt://init/files/zabbix-3.4.8.tar.gz
- mode: 755
- user: zabbix
- group: zabbix
cmd.run:
- name: cd /usr/local/src && tar zxf zabbix-3.4.8.tar.gz && cd zabbix-3.4.8 && ./configure --prefix=/usr/local/zabbix --enable-agent && make install
- unless: test -d /usr/local/zabbix
- require:
- user: zabbix-user-group
- file: zabbix-install
zabbix-agent:
file.managed:
- name: /usr/local/zabbix/etc/zabbix_agentd.conf
- source: salt://init/files/zabbix_agentd.conf
- template: jinja
- defaults:
Server: {{ pillar['zabbix-agent']['Zabbix_Server'] }}
Hostname: {{ grains['fqdn'] }}
- require:
- file: zabbix-install
zabbix_agentd.conf.d:
file.directory:
- name: /usr/local/zabbix/etc/zabbix_agentd.conf.d
- watch_in:
- service: zabbix-agent
- require:
- file: zabbix-agent
zabbix_agentd-start:
cmd.run:
- name: echo "/usr/local/zabbix/sbin/zabbix_agentd -c /usr/local/zabbix/etc/zabbix_agentd.conf" >> /etc/rc.d/rc.local && /usr/local/zabbix/sbin/zabbix_agentd -c /usr/local/zabbix/etc/zabbix_agentd.conf
- require:
- file: zabbix-install
- watch:
- file: zabbix-agent
- 修改zabbix_agentd.conf文件
[root@mycat ~]# vim /srv/salt/base/init/files/zabbix_agentd.conf
Server={{ Server }} #修改此行
Hostname= {{ Hostname }} #修改此行
Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d #添加或者修改此行
- 创建env_init.sls把初始环境的.sls文件都包含进来,这边我们只有一个zabbix和一组依赖包
[root@localhost ~]# vim /srv/salt/base/init/env_init.sls
include:
- init.pkg-init
- init.zabbix_agent
- 在top.sls里面给Minion指定状态
[root@localhost ~]# vim /srv/salt/base/top.sls
base:
'*':
- init.env_init
- 执行命令
在执行前,先进行测试
[root@localhost ~]# salt '*' state.highstate test=True
在所有Minion上执行状态
[root@localhost ~]# salt '*' state.highstate
网友评论