基于Docker搭建zabbix监控服务

环境

操作系统:centos6.5  

Docker version 1.7.1

Docker的安装以及pull镜像这里暂且不介绍

[root@gmdai ~]# docker p_w_picpaths

REPOSITORY         TAG                 IMAGE ID            CREATED             VIRTUAL SIZE

centos6-lamp       latest             601481552a9d        4 minutesago       1.498 GB

centos6-ssh-mysql  latest             8fe2459b59e5        16 hoursago        464 MB

tomcat             latest             430e8c6b6fad        6 days ago          357.3 MB

nginx              latest             c0e6aba9c87a        6 daysago          182.6 MB

centos7            latest             0e0217391d41        7 weeksago         196.6 MB

consol/tomcat-7.0  latest              1439b1fbf911        10 months ago       601.2 MB

centos6-ssh        latest             b78c71c001db        20 monthsago       296.9 MB

 

我们将用centos6-lamp镜像构建zabbix镜像,注意docker pull镜像时推荐pull centos6的镜像,不要pull默认的centos,因为那是centos7,对于centos7,本人感觉和centos6有好多出入,并不是简简单单的升级,所以推荐docker search centos6

本人是拿pull下来的centos6一键yum安装的lamp环境然后commit为一个新的镜像

Yum安装lamp命令

yum -y install gcc gcc-c++ autoconf  httpd php mysql mysql-server php-mysqlhttpd-manual mod_ssl

mod_perl mod_auth_mysql php-gd php-xml php-mbstring php-ldapphp-pear php-xmlrpc php-bcmath

mysql-connector-odbc mysql-devel libdbi-dbd-mysqlnet-snmp-devel curl-devel unixODBC-devel OpenIPMI-devel java-devel

 

创建zabbix容器,创建后docker ps可以查看容器的一些基本信息

[root@gmdai ~]# docker run -itd --name zabbix -v /mnt/:/mnt/ -p10050:10050 -p 10051:10051 centos6-lamp

fae23d1c5f1c64670ea50bc91f5a444c54ffd823b194187deff279a5ba4ea469

[root@gmdai ~]# docker ps

CONTAINER ID       IMAGE               COMMAND                CREATED             STATUS              PORTS                                          NAMES

fae23d1c5f1c       centos6-lamp        "/bin/sh-c '/usr/sb   14 seconds ago      Up 11 seconds       22/tcp, 0.0.0.0:10050-10051->10050-10051/tcp   zabbix 

 

拷贝宿主主机的zabbix安装包到创建容器时所挂载的数据卷目录

[root@gmdai ~]# cp zabbix-2.0.15.tar.gz /mnt/

[root@gmdai ~]# docker exec -it zabbix /bin/bash     //进入docker容器

bash-4.1# cp /mnt/zabbix-2.0.15.tar.gz /root/    

bash-4.1# cd root/

bash-4.1# ls

zabbix-2.0.15.tar.gz

bash-4.1# tar -xzvf zabbix-2.0.15.tar.gz

 

创建zabbix账户,用户组,yum安装编译安装时需要的依赖包

并编译安装zabbix

bash-4.1# groupadd zabbix -g 201

bash-4.1# useradd -g zabbix -u 201 -m zabbix

bash-4.1# yum install php-pecl-ssh2.x86_64libssh2-devel.x86_64 openldap openldap-devel

bash-4.1# tar -xzvf zabbix-2.0.15.tar.gz

bash-4.1#./configure --prefix=/usr --sysconfdir=/etc/zabbix--enable-server --enable-proxy --enable-agent

--enable-ipv6 --with-mysql=/usr/bin/mysql_config--with-net-snmp --with-libcurl --with-openipmi

--with-unixodbc --with-ldap --with-ssh2 --enable-java    //配置

bash-4.1# make && make install                        //编译&安装

 

运行mysql服务,配置root用户密码

bash-4.1# mysqladmin -uroot password "root"

mysqladmin: connect to server at 'localhost' failed

error: 'Can't connect to local MySQL server through socket'/var/lib/mysql/mysql.sock' (111)'

Check that mysqld is running and that the socket:'/var/lib/mysql/mysql.sock' exists!             //这个错误提示mysql服务是否启动

bash-4.1# service mysqld status

mysqld dead but pid file exists                  //mysql服务已死,重启就行了

bash-4.1# service mysqld restart

Stopping mysqld:                                          [FAILED]

Starting mysqld:                                          [  OK  ]

bash-4.1# service mysqld status

mysqld (pid  8224) isrunning...

bash-4.1# mysqladmin -uroot password "root"     //更改root密码

 

创建zabbix数据库,并创建zabbix数据库用户以及授权

bash-4.1# mysql -uroot -proot

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 4

Server version: 5.1.73 Source distribution

 

Copyright (c) 2000, 2013, Oracle and/or its affiliates. Allrights reserved.

 

Oracle is a registered trademark of Oracle Corporation and/orits

affiliates. Other names may be trademarks of their respective

owners.

 

Type 'help;' or '\h' for help. Type '\c' to clear the currentinput statement.

 

mysql> create database zabbix character set utf8;

Query OK, 1 row affected (0.00 sec)

 

mysql> grant all privileges on zabbix.* tozabbix@localhost identified by "zabbix";

Query OK, 0 rows affected (0.00 sec)

 

mysql> flush privileges;

Query OK, 0 rows affected (0.01 sec)

 

 

导入zabbix数据库到mysql

bash-4.1# mysql -uzabbix -pzabbix zabbix <database/mysql/schema.sql

bash-4.1# mysql -uzabbix -pzabbix zabbix <database/mysql/p_w_picpaths.sql

bash-4.1# mysql -uzabbix -pzabbix zabbix <database/mysql/data.sql  

 

拷贝zabbix服务启动脚本到/etc/init.d,并授权

bash-4.1# cp misc/init.d/fedora/core/zabbix_* /etc/init.d/

bash-4.1# chmod 755 /etc/init.d/zabbix_*

 

修改服务脚本参数

bash-4.1# sed -i "s#BASEDIR=/usr/local#BASEDIR=/usr/#g"/etc/init.d/zabbix_server

bash-4.1# sed -i "s#BASEDIR=/usr/local#BASEDIR=/usr/#g"/etc/init.d/zabbix_agentd

 

创建zabbix日志目录,并授权

bash-4.1# mkdir /var/log/zabbix

bash-4.1# chown zabbix:zabbix/var/log/zabbix/

配置PHP 环境,修改PHP.INI

vim /etc/php.ini (修改这些参数的目的是zabbix web 代码要求要这么配置才能正

常运行,所以,不修改饿后果就是安装无法获得正常的环境,关于php.ini 这些参数的具体

意思,请读者自行参考php 相关资料)

date.timezone = Asia\/Shanghai

max_execution_time = 300

post_max_size = 32M

max_input_time = 300

memory_limit = 128M

mbstring.func_overload = 0

 

另外直接sed命令更改配置文件也行

sed -i "s/;date.timezone =/date.timezone =Asia\/Shanghai/g" /etc/php.ini

sed -i "s#max_execution_time = 30#max_execution_time =300#g" /etc/php.ini

sed -i "s#post_max_size = 8M#post_max_size = 32M#g"/etc/php.ini

sed -i "s#max_input_time = 60#max_input_time =300#g" /etc/php.ini

sed -i "s#memory_limit = 128M#memory_limit =128M#g" /etc/php.ini

sed -i "/;mbstring.func_overload =2/ambstring.func_overload = 0\n" /etc/php.ini

 

设置HTTPDMYSQLD开机启动并开启服务

chkconfig mysqld on

chkconfig httpd on

service mysqld start

service httpd start

 

配置ZABBIX_SERVER.CONF文件

SED更改

sed -i"s/DBUser\=root/DBUser\=zabbix/g"/etc/zabbix/zabbix_server.conf

sed -i "/#DBPassword=/aDBPassword=zabbix\n"/etc/zabbix/zabbix_server.conf

sed -i" s#tmp/zabbix_server.log#var/log/zabbix/zabbix_server.log#g"/etc/zabbix/zabbix_server.conf

 

主要的4个参数,vim 更改/etc/zabbix_server.conf文件

LogFile=/tmp/zabbix_server.log

DBName=zabbix

DBUser=zabbix

DBPassword=zabbix

DBSocket=/var/lib/mysql/mysql.sock

 

配置ZABBIX_AGENTD.CONF文件

Sed配置

sed -i "s/Server\=127.0.0.1/Server\=127.0.0.1,192.168.1.89/g"

/etc/zabbix/zabbix_agentd.conf

sed -i"s/ServerActive\=127.0.0.1/ServerActive\=192.168.1.89:10051/g"

/etc/zabbix/zabbix_agentd.conf

sed -i"s#tmp/zabbix_agentd.log#var/log/zabbix/zabbix_agentd.log#g"

/etc/zabbix/zabbix_agentd.conf

sed -i"#UnsafeUserParameters=0#aUnsafeUserParameters=1\n"

/etc/zabbix/zabbix_agentd.conf

建议的参考配置

 

vim /etc/zabbix/zabbix_agentd.conf

LogFile=/var/log/zabbix/zabbix_agentd.log

EnableRemoteCommands=0

Server=127.0.0.1,192.168.1.89

StartAgents=8

ServerActive=192.168.1.89:10051

Hostname=nat.kx1d.com

Timeout=30

Include=/etc/zabbix/zabbix_agentd.conf.d/

UnsafeUserParameters=1

 

拷贝网页文件到网站目录,并启动服务

bash-4.1# cp -r frontends/php/* /var/www/html/

bash-4.1# /etc/init.d/zabbix_server start

Starting zabbix_server:                                    [  OK  ]

bash-4.1# /etc/init.d/zabbix_agentd start

Starting zabbix_agentd:  zabbix_agentd [8376]:/usr/local/etc/zabbix_agentd.userparams.conf: [2] No such file or directory

 

                                                           [  OK  ]

 

按常规来走,已经可以访问访问http://宿主主机-ip/zabbix进行配置,但是因为创建容器时忘记映射80端口所以现在是不能访问配置的,不得不docker commit成新的镜像,然后利用新镜像创建新容器

[root@gmdai ~]# docker commit zabbix centos6-zabbix-v1

8567daabbc620ac4314135412385ffa017eb3f96a04c43a854c6733f269de86a

[root@gmdai ~]# docker p_w_picpaths

REPOSITORY         TAG                 IMAGE ID            CREATED             VIRTUAL SIZE

centos6-zabbix-v1  latest             8567daabbc62        43 secondsago      1.693 GB

[root@gmdai ~]# docker rm -f zabbix 

Zabbix    //删除原来的zabbix容器,因为容器名字是唯一的,不删除会报错

 

创建新容器,10050端口映射可选(如果zabbix对外开发,那么就映射,只是在docker容器之间用的话,可以不选)

root@gmdai ~]# docker run -itd --name zabbix -v /mnt/:/mnt/ -p10050:10050  -p 80:80 centos6-lamp

cb65aed2b86af4d7d8fad75fd97f37c38249dc5387e2effc53003ee7d101f2a7

[root@gmdai ~]# docker exec -it zabbix /bin/bash

 

重启mysqld httpd这些服务,修改zabbix-agentd-conf参数为Server=127.0.0.1,192.168.1.89 ServerActive=192.168.1.89:10051新容器启动时的ip地址

重启zabbix-server zabbix-agentd服务

再次访问进行配置就行了

 

 

配置zabbix时出现date(): Itis not safe to rely on the system's timezone settings的报错,网上找了答案是在php.ini中设置date.timezone的值为PRC,设置好以后的为:date.timezone=PRC,同时取消这一行代码的注释,即去掉前面的分号就可以了。

 

 

遗留问题docker容器的ip不是固定的每次重启都会重新分配