Zabbix系列之(七):监控Docker容器


title: Zabbix系列之(七):监控Docker容器
categories: Linux
tags:
- Zabbix
- Docker
timezone: Asia/Shanghai
date: 2019-02-25


环境

[root@centos181001 run]# cat /etc/centos-release
CentOS Linux release 7.6.1810 (Core)

[root@centos181001 run]# docker -v
Docker version 18.09.1, build 4c52b90

[root@centos181003 ~]# zabbix_server -V
zabbix_server (Zabbix) 3.0.25
Revision 89098 28 January 2019, compilation time: Jan 28 2019 10:49:12

Copyright (C) 2019 Zabbix SIA
License GPLv2+: GNU GPL version 2 or later .
This is free software: you are free to change and redistribute it according to
the license. There is NO WARRANTY, to the extent permitted by law.

This product includes software developed by the OpenSSL Project
for use in the OpenSSL Toolkit (http://www.openssl.org/).

Compiled with OpenSSL 1.0.1e-fips 11 Feb 2013
Running with OpenSSL 1.0.1e-fips 11 Feb 2013

Zabbix Server 3.4   centos181001    11.11.11.61
Docker manager     centos181002     11.11.11.62
Docker worker1     centos181003     11.11.11.63

第零步:关闭系统默认防火墙(by all)

setenforce 0
sed -i -r "/^SELINUX=/c SELINUX=disabled" /etc/selinux/config
which systemctl && systemctl stop firewalld
which systemctl && systemctl disable firewalld
which systemctl && systemctl stop iptables || service iptables stop
which systemctl && systemctl disable iptables || chkconfig iptables off

第一步:安装Zabbix(by Zabbix Server)

1.安装并初始化MariaDB

# 1.安装mariadb
yum install mariadb mariadb-server mariadb-libs mariadb-devel -y

# 2.启动MariaDB并设置开机自动启动
systemctl start mariadb
systemctl status mariadb
systemctl enable mariadb

# 3.初始化数据库
mysql_secure_installation

2.安装Zabbix Server

a.配置官方yum源

rpm -Uvh https://repo.zabbix.com/zabbix/3.0/rhel/7/x86_64/zabbix-release-3.0-1.el7.noarch.rpm

b.安装Zabbix server,Web前端,agent,mysql

yum install -y zabbix-server-mysql zabbix-web-mysql zabbix-agent

c.创建初始数据库

[root@localhost home]# mysql -u root -pxiaoliu

create database zabbix character set utf8 collate utf8_bin;
grant all privileges on zabbix.* to zabbix@localhost identified by 'xiaoliu';
quit

# 导入初始架构和数据,系统将提示您输入新创建的密码。
zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix

d.为Zabbix server配置数据库密码

vim /etc/zabbix/zabbix_server.conf

DBPassword=xiaoliu

e.为Zabbix前端配置PHP时区参数

# 增加以下行
vim /etc/httpd/conf.d/zabbix.conf

php_value date.timezone Asia/Shanghai

f.启动Zabbix server和agent进程,并为它们设置开机自启

systemctl restart zabbix-server zabbix-agent httpd
systemctl enable zabbix-server zabbix-agent httpd
systemctl status zabbix-server zabbix-agent httpd

现在您的Zabbix server已经安装完成并开始运行!打开以下页面初始化Zabbix Server前端

http://11.11.11.61/zabbix

第二步:Docker环境设置

1.安装Docker CE(所有Docker节点)

## 安装Docker CE
yum install -y audit-libs-python-2.8.4-4.el7.x86_64.rpm \
checkpolicy-2.5-8.el7.x86_64.rpm \
containerd.io-1.2.2-3.el7.x86_64.rpm \
container-selinux-2.74-1.el7.noarch.rpm \
docker-ce-18.09.1-3.el7.x86_64.rpm \
docker-ce-cli-18.09.1-3.el7.x86_64.rpm \
libcgroup-0.41-20.el7.x86_64.rpm \
libsemanage-python-2.5-14.el7.x86_64.rpm \
policycoreutils-python-2.5-29.el7.x86_64.rpm \
python-IPy-0.75-6.el7.noarch.rpm \
setools-libs-3.3.8-4.el7.x86_64.rpm && \

## 安装Docker命令自动补全
yum install -y bash-completion && \
source /usr/share/bash-completion/completions/docker && \
bash /usr/share/bash-completion/bash_completion && \
systemctl start docker && \
systemctl enable docker && \
systemctl status docker && \
exit

2.初始化Docker Swarm集群

Docker manager节点:在管理节点初始化集群

docker swarm init --advertise-addr 11.11.11.62

Docker worker节点:在worker执行以下命令加入集群

docker swarm join --token SWMTKN-1-3y41mzyy6760l9luw9wqfwbcm7u8cb6s7hu85a9txbd6p514n2-ezugts5zrfqysz5m693znv7up 11.11.11.62:2377

3.启动一个容器

docker pull nginx
docker service create --replicas 3 -p 80:80 --name nginx nginx

第三步:配置Docker/LXC containers monitoring

https://www.zabbix.com/cn/integrations/docker

https://github.com/monitoringartist/Zabbix-Docker-Monitoring

1.Zabbix Server节点配置

## 1.下载模板
https://raw.githubusercontent.com/monitoringartist/zabbix-docker-monitoring/master/template/Zabbix-Template-App-Docker-active.xml

## 2.导入模板

    登录zabbix前段管理页面 - 配置 - 模板 - 导入 - 选择文件 - 导入

## 3.分别添加监控主机

    登录zabbix前段管理页面 - 配置 - 主机 - 创建主机
        主机名称    必须填写被监控主机名

## 4.为被监控主机配置监控模板

    登录zabbix前段管理页面 - 配置 - 主机 - 选择被监控主机 - 模板 - 选择 - 选择刚导入的模板 - 添加 - 更新

2.所有Docker节点设置

11.11.11.61为Zabbix Server节点IP地址

## 以容器形式运行
docker run \
  --name=dockbix-agent-xxl \
  --net=host \
  --privileged \
  -v /:/rootfs \
  -v /var/run:/var/run \
  --restart unless-stopped \
  -e "ZA_Server=11.11.11.61" \
  -e "ZA_ServerActive=11.11.11.61" \
  -d monitoringartist/dockbix-agent-xxl-limited:latest

## 查看日志,输出``Secured``为成功
docker logs -f dockbix-agent-xxl

3.登录Zabbix Server前端查看监控状态

4.模板内容汉化

UPDATE  items SET `name` = replace(name, 'Container', '容器 ');
UPDATE  items SET `name` = replace(name, 'Used swap', '使用 swap ');
UPDATE  items SET `name` = replace(name, 'CPU utilization', 'CPU 利用率');
UPDATE  items SET `name` = replace(name, 'Used RSS memory', '物理内存占用');
UPDATE  items SET `name` = replace(name, 'Used cache memory', '内存 Cache 占用');

你可能感兴趣的:(Zabbix系列之(七):监控Docker容器)