通過saltstack部署zabbix
一. 案例簡介
使用Saltstack部署zabbix,主要包括zabbix-server、zabbix-agent、zabbix-web以及mysql,其中,zabbix的版本使用4.0,使用本地自己搭建的zabbix倉庫,加快部署效率,
兩臺虛擬機作為示例,分別是server2和server3,
部署架構
[root@server1 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倉庫搭建
root@server1 ~]# vim /etc/yum.repos.d/dvd.repo
[root@server1 ~]# 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檔案
root@server1 zabbix-db]# vim init.sls
[root@server1 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

推送:
[root@server1 zabbix-db]# salt server2 state.sls zabbix-db

在server2中查看,
發現推送的檔案

3 為server2部署zabbix-server
撰寫init.sls檔案
[root@server1 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檔案
[root@server1 salt]# mkdir zabbix-agent
[root@server1 salt]# cd zabbix-agent/
[root@server1 zabbix-agent]# vim init.sls
[root@server1 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

修改組態檔
[root@server1 zabbix-agent]# cat zabbix_agentd.conf | grep zabbixserver
Server= {{ zabbixserver }}
ServerActive= {{ zabbixserver }}



推送:
[root@server1 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界面
[root@server1 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

修改時區
[root@server1 zabbix-web]# vim zabbix.conf

推送:
[root@server1 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檔案
[root@server1 salt]# vim top.sls
[root@server1 salt]# cat top.sls
base:
'server2':
- zabbix-db
- zabbix-server
- zabbix-web
- zabbix-agent
'server3':
- zabbix-agent
推送:
[root@server1 salt]# salt '*' state.highstate


7 測驗
web端訪問

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/289420.html
標籤:其他
上一篇:云計算的高可用性
