0%

zabbix的部署

zabbix提供了监控与告警的全套解决方案,在许多大型企业进行了深度实践,功能强大、产品成熟而稳定

选用5.0.4版本,主要的组件有:

  • zabbix-server,拉取/接收zabbix-agent的数据,计算触发器条件、向用户发送通知等核心功能
  • zabbix-agent,客户端,收集本地监控项数据,并通过主动/被动方式发送到zabbix-server
  • zabbix-proxy,可选,负责从一个或多个受监控设备采集监控数据并将信息发送到zabbix-server,以分担zabbix-server的压力

此外,根据选用的部署组件版本,zabbix-server如果不包含web功能,还需要部署zabbix-web。zabbix使用Mysql关系型数据库作为底层存储(可选mysql、postgresql、sqlite等),在数据量大的时候对数据库的压力较大,限制了大规模集群中 zabbix 的应用,对告警信息汇总运算时性能较差;并且,zabbix为监控主机、网络设备等基础设置而设计,对于容器监控等场景,zabbix只能通过集成定制化脚本的方式实现且功能有限,在云原生环境下的应用受到限制

本次部署使用的机器:

  • 10.70.20.33,使用docker部署zabbix-server、zabbix-web,部署mysql-5.7
  • 10.70.20.20,部署zabbix-agent

mysql部署与配置

选用mysql-5.7作为zabbix底层存储

建议为zabbix部署单独的mysql,因为zabbix对数据库压力较大,尤其是执行housekeeper的时候;并且,监控数据并不敏感,mysql不必使用主从复制或者备份等手段,也减少了性能损耗和空间占用

mysql-5.7部署具体可以参考quick_launch_tools仓库提供的mysql安装脚本

使用mysql客户端连接到数据库,创建zabbix使用的用户zabbix,指定具有zabbix这一db下的权限(zabbix数据库下将在zabbix-server启动中自动创建数据表)

1
2
3
4
5
6
7
8
9
10
11
12
-- 创建zabbix数据库,注意character及collate
create database zabbix character set utf8 collate utf8_bin;

-- 创建zabbix@10.70.20.%用户,可由10.70.20.1/24网络下登录
create user 'zabbix'@'10.70.20.%' identified by 'zbx.123';
grant all privileges on zabbix.* to "zabbix"@'10.70.20.%';

-- 创建zabbix@172.17.0.%用户,可由172.17.0.1/24网络下登录(zabbix-server/zabbix-web使用docker部署,使用docker网络IP)
create user 'zabbix'@'172.17.0.%' identified by 'zbx.123';
grant all privileges on zabbix.* to "zabbix"@'172.17.0.%';

flush privileges;

部署zabbix-server及zabbix-web

zabbix-server及zabbix-web使用docker启动,使用docker部署更快捷,可以省去安装众多依赖包、基础环境的步骤:

1
2
3
4
5
# 启动zabbix-server
docker run -e DB_SERVER_HOST="10.70.20.33" -e DB_SERVER_PORT="3306" -e MYSQL_USER="zabbix" -e MYSQL_PASSWORD="zbx.123" -e MYSQL_DATABASE="zabbix" -p 10051:10051 --init -d zabbix/zabbix-server-mysql:alpine-5.0.4

# 启动zabbix-web
docker run -e DB_SERVER_HOST="10.70.20.33" -e DB_SERVER_PORT="3306" -e MYSQL_USER="zabbix" -e MYSQL_PASSWORD="zbx.123" -e MYSQL_DATABASE="zabbix" -e ZBX_SERVER_HOST="10.70.20.33" -e ZBX_SERVER_PORT="10051" -e PHP_TZ="Asia/Shanghai" -p 8081:8080 -d zabbix/zabbix-web-nginx-mysql:alpine-5.0.4

环境变量如下:

  • DB_SERVER_HOST,使用的数据库host
  • DB_SERVER_PORT,使用的数据库port
  • MYSQL_DATABASE,使用的数据database
  • MYSQL_USER,连接数据库的user
  • MYSQL_PASSWORD,连接数据库的password
  • ZBX_SERVER_HOST,zabbix-server服务的host
  • ZBX_SERVER_PORT,zabbix-server服务的port
  • PHP_TZ,设置zabbix-web使用的时区,选用PHP支持的时区

这里使用docker部署,并将10051端口映射到了主机的10051端口,因此使用10.70.20.33:10051指定zabbix-server地址;而zabbix-web的容器内8080端口被映射到了主机的8081端口,无他,操作的时候主机的8080端口已被占用了

启动后,查看zabbix-server docker容器日志

1
2
** Database 'zabbix' already exists. Please be careful with database COLLATE!
** Creating 'zabbix' schema in MySQL

可以看到zabbix-server自动完成了zabbix数据表的创建,可以前往数据库确认是否初始化成功

zabbix-web访问路径修改

启动成功后,zabbix-web默认的访问url为:http://10.70.20.33:8081,我们可能希望使用:http://10.70.20.33:8081/zabbix

以上使用的zabbix-web-nginx-mysql镜像集成了nginx,可以直接进入容器修改访问根路径:

1
2
3
4
# docker exec进入zabbix-web容器内
docker exec -it <container-id> bash
# 进入nginx配置文件所在目录
cd /etc/nginx

修改/etc/nginx/conf.d/nginx.conf文件,将zabbix-web的文件根路径由

1
set $webroot '/usr/share/zabbix';

修改为

1
set $webroot '/usr/share';

修改后在容器内使用’nginx -s reload’重新加载nginx配置,或者重启容器,即可使用http://10.70.20.33:8081/zabbix 访问;容器重启后仍将生效;也可以考虑commit container为image以固化修改,或者改造dockerfile以支持环境变量控制

zabbix-agent部署及配置

zabbix-agent部署

以下示例rpm包部署和docker部署

  1. rpm包部署

下载zabbix-agent 5.0.4 rpm包并安装:

1
2
rpm -ivh --nodeps zabbix-agent-5.0.4-1.el7.x86_64.rpm
zabbix_agentd -V

启动zabbix-agent:

1
zabbix_agentd

将zabbix-agent添加为开机启动:

1
2
3
which zabbix_agentd
# 根据which命令的输出修改
ln -s /usr/sbin/zabbix_agentd /etc/init.d/zabbix_agentd
  1. docker部署

zabbix-agent同样可以使用docker部署,但需要注意,docker通过ns进行资源隔离——也就是说,zabbix-agent默认采集到的进程、网络等数据,是自己所在的容器挂载的ns下的数据,因此需要在容器启动时挂载主机ns,可参考这个issue how to monitor host processes with zabbix 3.4.12 in docker

zabbix-agent被动/主动模式配置

这里的被动、主动是针对zabbix-agent来说的

被动模式是zabbix数据收集的默认模式,即zabbix-server向zabbix-agent发起拉取数据的指令,zabbix-agent将数据返回到zabbix-server;主动模式则是zabbix-agent推送监控数据到zabbix-server

被动模式需要zabbix-server拉取数据,zabbix-agent数量大的情况下压力较大;主动模式需要为每个zabbix-agent指定zabbix-server的地址,并且需要保证地址固定且可用

如果zabbix-server无法访问zabbix-agent的地址(比如,不在同一内网且zabbix-agent无公网IP),那么只能选择主动模式

  1. zabbix-agent被动模式配置

修改/etc/zabbix/zabbix_agentd.conf,关键项为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
### Option: LogFileSize
# Maximum size of log file in MB.
# 0 - disable automatic log rotation.
#
# Mandatory: no
# Range: 0-1024
# Default:
# LogFileSize=1

# 修改LogFileSize,设置日志文件大小
LogFileSize=1024


### Option: Server
# List of comma delimited IP addresses, optionally in CIDR notation, or DNS names of Zabbix servers and Zabbix proxies.
# Incoming connections will be accepted only from the hosts listed here.
# If IPv6 support is enabled then '127.0.0.1', '::127.0.0.1', '::ffff:127.0.0.1' are treated equally
# and '::/0' will allow any IPv4 or IPv6 address.
# '0.0.0.0/0' can be used to allow any IPv4 address.
# Example: Server=127.0.0.1,192.168.1.0/24,::1,2001:db8::/32,zabbix.example.com
#
# Mandatory: yes, if StartAgents is not explicitly set to 0
# Default:
# Server=

# zabbix-server/zabbix-proxy的地址,可以指定多个;如果不固定可以按照注释设置为子网范围,zabbix-agent将只允许范围Server指定的IP/子网范围内的请求
# 这里放行了zabbix-server所在主机IP和所在容器内网IP,可以查看docker0网卡确认容器子网范围
Server=10.70.20.33,172.17.0.1/16


### Option: Hostname
# Unique, case sensitive hostname.
# Required for active checks and must match hostname as configured on the server.
# Value is acquired from HostnameItem if undefined.
#
# Mandatory: no
# Default:
# Hostname=
# zabbix-agent在zabbix-server中使用的hostname,可以指定为所在主机的IP方便识别
Hostname=10.70.20.20

启动zabbix-agent:

1
zabbix_agentd

按照配置文件指定的日志路径(默认为/var/log/zabbix/zabbix_agentd.log)查看启动是否成功

  1. zabbix-agent主动模式配置

修改/etc/zabbix/zabbix_agentd.conf,关键项为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
### Option: LogFileSize
# Maximum size of log file in MB.
# 0 - disable automatic log rotation.
#
# Mandatory: no
# Range: 0-1024
# Default:
# LogFileSize=1

# 修改LogFileSize,设置日志文件大小
LogFileSize=1024


### Option: Server
# List of comma delimited IP addresses, optionally in CIDR notation, or DNS names of Zabbix servers and Zabbix proxies.
# Incoming connections will be accepted only from the hosts listed here.
# If IPv6 support is enabled then '127.0.0.1', '::127.0.0.1', '::ffff:127.0.0.1' are treated equally
# and '::/0' will allow any IPv4 or IPv6 address.
# '0.0.0.0/0' can be used to allow any IPv4 address.
# Example: Server=127.0.0.1,192.168.1.0/24,::1,2001:db8::/32,zabbix.example.com
#
# Mandatory: yes, if StartAgents is not explicitly set to 0
# Default:
# Server=

# zabbix-server/zabbix-proxy的地址,可以指定多个;如果不固定可以按照注释设置为子网范围,zabbix-agent将只允许范围Server指定的IP/子网范围内的请求
# 这里放行了zabbix-server所在主机IP和所在容器内网IP,可以查看docker0网卡确认容器子网范围
Server=10.70.20.33,172.17.0.1/16

### Option: StartAgents
# Number of pre-forked instances of zabbix_agentd that process passive checks.
# If set to 0, disables passive checks and the agent will not listen on any TCP port.
#
# Mandatory: no
# Range: 0-100
# Default:
# StartAgents=3

# 调整为0,关闭zabbix-agent被动模式,zabbix-agent也将不监听任何端口
StartAgents=0

### Option: ServerActive
# List of comma delimited IP:port (or DNS name:port) pairs of Zabbix servers and Zabbix proxies for active checks.
# If port is not specified, default port is used.
# IPv6 addresses must be enclosed in square brackets if port for that host is specified.
# If port is not specified, square brackets for IPv6 addresses are optional.
# If this parameter is not specified, active checks are disabled.
# Example: ServerActive=127.0.0.1:20051,zabbix.domain,[::1]:30051,::1,[12fc::1]
#
# Mandatory: no
# Default:
# ServerActive=

# zabbix-server/zabbix-proxy的地址,可以指定多个,用于指定zabbix-agent推送数据的地址
ServerActive=10.70.20.33:10051

### Option: Hostname
# Unique, case sensitive hostname.
# Required for active checks and must match hostname as configured on the server.
# Value is acquired from HostnameItem if undefined.
#
# Mandatory: no
# Default:
# Hostname=
# zabbix-agent在zabbix-server中使用的hostname,可以指定为所在主机的IP方便识别
Hostname=10.70.20.20

zabbix-server添加监控项

在zabbix-web添加host,为host关联templete;也可通过zabbix提供的Discovery定义配置自动发现

在此不演示,可以参考zabbix文档或者网络资料如:zabbix agent的主动模式配置

添加主机监控后,查看zabbix-agent、zabbix-server日志,确认是否成功获取监控数据,在zabbix-web也可以看到zabbix-agent状态