基於Docker搭建zabbix服務

基於Docker搭建zabbix監控服務php

環境

操做系統:centos6.5   html

Docker version 1.7.1java

Docker的安裝以及pull鏡像這裏暫且不介紹mysql

[root@gmdai ~]# docker p_w_picpathsnginx

REPOSITORY         TAG                 IMAGE ID            CREATED             VIRTUAL SIZEc++

centos6-lamp       latest             601481552a9d        4 minutesago       1.498 GBweb

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

tomcat             latest             430e8c6b6fad        6 days ago          357.3 MBdocker

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不是固定的每次重啓都會從新分配

相關文章
相關標籤/搜索