首页 > 代码库 > zabbix 3.2高可用
zabbix 3.2高可用
说明
1. 基础架构为LAMP环境,采用keepalived实现zabbix服务器高可用,保证主server的mysql或者httpd宕掉后能切换到从server
2、数据库做主主同步,保证两边服务器数据的一致性,实现数据库的高可用。
环境
zabbix01:192.168.8.117
zabbix02:192.168.8.118
vip:192.168.8.111
OS:CentOS release 6.9 X86_64
MYSQL:Percona-Server-5.6
NGINX:tengine-2.2.0
PHP: php-7.0.16
ZABBIX:zabbix3.2.6
zlib:zlib-1.2.11
openssl:openssl-1.0.2
pcre:pcre-8.39
安装依赖
yum update
yum groupinstall ‘开发工具‘
vim /etc/selinux/config
selinux=disabled
:wq
iptables -F
/etc/init.d/iptables save
/etc/init.d/iptables stop
chkconfig iptables off
/etc/init.d/postfix stop && chkconfig postfix off
yum install gcc gcc-c++ libxml2 libxml2-devel libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libmcrypt libmcrypt-devel openssl-devel bzip2-devel recode-devel openldap openldap-devel net-snmp net-snmp-devel libcurl-devel libcurl vim wget ntp jemalloc -y
reboot
LNMP环境安装
安装pcre
tar xf pcre-8.39.tar.gz
cd pcre-8.39
./configure --prefix=/usr/local/pcre/ && make && make install
openssl安装
tar xf openssl-1.0.2.tar.gz
cd openssl-1.0.2
./config enable-shared --prefix=/usr/local/openssl/ && make && make test && make install
zlib安装
tar xf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure --prefix=/usr/local/zlib && make &&make install
nginx安装
groupadd web
useradd -s /sbin/nologin -g web web
tar xf tengine-2.2.0.tar.gz
./configure --prefix=/usr/local/nginx --user=web --group=web --with-pcre=/root/pcre-8.39 --with-openssl=/root/openssl-1.0.2 --with-zlib=/root/zlib-1.2.11 --with-file-aio --with-http_gzip_static_module --with-http_realip_module --with-http_stub_status_module
make && make install
vim /etc/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:" | sed ‘s/[^*]*--user=\([^ ]*\).*/\1/g‘ -`
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep ‘configure arguments:‘`
for opt in $options; do
if [ `echo $opt | grep ‘.*-temp-path‘` ]; then
value=http://www.mamicode.com/`echo $opt | cut -d"=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
chkconfig nginx on
ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
chmod +x /etc/init.d/nginx
php安装
tar xf php-7.0.16.tar.gz
cd php-7.0.16
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-bz2 --with-curl --enable-ftp --enable-sockets --disable-ipv6 --with-gd --with-jpeg-dir=/usr/local --with-png-dir=/usr/local --with-freetype-dir=/usr/local --enable-gd-native-ttf --with-iconv-dir=/usr/local/lib/libconv --enable-mbstring --enable-calendar --with-gettext --with-libxml-dir=/usr/local --with-zlib --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --enable-dom --enable-xml --enable-fpm --with-libdir=lib64 --enable-bcmath --enable-zip --enable-pcntl --enable-exif --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-opcache --enable-session --enable-mbregex --with-fpm-user=vagrant --with-fpm-group=nogroup --enable-wddx --with-mcrypt --enable-gd-native-ttf --enable-gd-jis-conv --with-openssl --with-zlib=/usr --with-recode=/usr --with-xmlrpc --enable-inline-optimization --disable-debug --disable-rpath --with-ldap
make
make test
make install
vim /etc/init.d/php-fpm
#! /bin/sh
### BEGIN INIT INFO
# Provides: php-fpm
# Required-Start: $remote_fs $network
# Required-Stop: $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts php-fpm
# Description: starts the PHP FastCGI Process Manager daemon
### END INIT INFO
prefix=/usr/local/php
exec_prefix=${prefix}
php_fpm_BIN=${exec_prefix}/sbin/php-fpm
php_fpm_CONF=${prefix}/etc/php-fpm.conf
php_fpm_PID=${prefix}/var/run/php-fpm.pid
php_opts="--fpm-config $php_fpm_CONF --pid $php_fpm_PID"
wait_for_pid () {
try=0
while test $try -lt 35 ; do
case "$1" in
‘created‘)
if [ -f "$2" ] ; then
try=‘‘
break
fi
;;
‘removed‘)
if [ ! -f "$2" ] ; then
try=‘‘
break
fi
;;
esac
echo -n .
try=`expr $try + 1`
sleep 1
done
}
case "$1" in
start)
echo -n "Starting php-fpm "
$php_fpm_BIN --daemonize $php_opts
if [ "$?" != 0 ] ; then
echo " failed"
exit 1
fi
wait_for_pid created $php_fpm_PID
if [ -n "$try" ] ; then
echo " failed"
exit 1
else
echo " done"
fi
;;
stop)
echo -n "Gracefully shutting down php-fpm "
if [ ! -r $php_fpm_PID ] ; then
echo "warning, no pid file found - php-fpm is not running ?"
exit 1
fi
kill -QUIT `cat $php_fpm_PID`
wait_for_pid removed $php_fpm_PID
if [ -n "$try" ] ; then
echo " failed. Use force-quit"
exit 1
else
echo " done"
fi
;;
status)
if [ ! -r $php_fpm_PID ] ; then
echo "php-fpm is stopped"
exit 0
fi
PID=`cat $php_fpm_PID`
if ps -p $PID | grep -q $PID; then
echo "php-fpm (pid $PID) is running..."
else
echo "php-fpm dead but pid file exists"
fi
;;
force-quit)
echo -n "Terminating php-fpm "
if [ ! -r $php_fpm_PID ] ; then
echo "warning, no pid file found - php-fpm is not running ?"
exit 1
fi
kill -TERM `cat $php_fpm_PID`
wait_for_pid removed $php_fpm_PID
if [ -n "$try" ] ; then
echo " failed"
exit 1
else
echo " done"
fi
;;
restart)
$0 stop
$0 start
;;
reload)
echo -n "Reload service php-fpm "
if [ ! -r $php_fpm_PID ] ; then
echo "warning, no pid file found - php-fpm is not running ?"
exit 1
fi
kill -USR2 `cat $php_fpm_PID`
echo " done"
;;
*)
echo "Usage: $0 {start|stop|force-quit|restart|reload|status}"
exit 1
;;
esac
chmod +x /etc/init.d/php-fpm
chkconfig php-fpm on
ln -s /usr/local/php/bin/php /usr/local/bin/php
ln -s /usr/local/php/bin/php-config /usr/local/bin/php-config
ln -s /usr/local/php/bin/phpize /usr/local/bin/phpize
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
cp php.ini-development /usr/local/php/etc/php.ini
ln -s /user/local/php/etc/php.ini /etc
vim www.conf
将user和group替换为web
nginx和php整合
cat /user/local/nginx/conf/nginx.conf
mysql安装
tar xf Percona-Server-5.6.33-79.0-r2084bdb-el6-x86_64-bundle.tar
yum localinstall Percona-Server-*
/etc/init.d/mysql start
ps_tokudb_admin --enable -uroot
更改数据引擎
vim /etc/my.cnf
在[mysqld]下添加
default-storage-engine=TokuDB
:wq
/etc/init.d/mysql restart
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------------------+--------------+------+------------+
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| TokuDB | DEFAULT | Percona TokuDB Storage Engine with Fractal Tree(tm) Technology | YES | YES | YES |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| InnoDB | YES | Percona-XtraDB, Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
+--------------------+---------+----------------------------------------------------------------------------+--------------+------+------------+
10 rows in set (0.03 sec)
chkconfig mysql on
启动服务
/etc/init.d/nginx restart
/etc/ini.d/php-fpm restart
安装zabbix
groupadd -g 201 zabbix
useradd -g zabbix -u 201 -s /sbin/nologin zabbix
tar xf zabbix-3.2.6.tar.gz
cd zabbix-3.2.6
./configure --prefix=/usr/local/zabbix --enable-server --enable-proxy --enable-agent --with-mysql=/usr/bin/mysql_config --with-net-snmp --with-libcurl --with-ldap --with-libxml2
make && make install
创建并导入数据(注意顺序)
mysql -e "create database zabbix default charset utf8;"
mysql -e "grant all on zabbix.* to zabbix@localhost identified by ‘zabbix‘;"
mysql -uzabbix -pzabbix zabbix<./database/mysql/schema.sql
mysql -uzabbix -pzabbix zabbix<./database/mysql/images.sql
mysql -uzabbix -pzabbix zabbix<./database/mysql/data.sql
mkdir /var/log/zabbix
chown -R zabbix.zabbix /var/log/zabbix
软链
ln -s /usr/local/zabbix/etc/ /etc/zabbix
ln -s /usr/local/zabbix/bin/* /usr/bin/
ln -s /usr/local/zabbix/sbin/* /usr/sbin/
ln -s /usr/local/zabbix/sbin/* /usr/local/sbin/
ln -s /usr/local/zabbix/bin/* /usr/local/bin/
cp misc/init.d/fedora/core/zabbix_* /etc/init.d/
cp -r frontends/php/ /usr/local/nginx/html/zabbix/
chmod 755 /etc/init.d/zabbix_*
sed -i "s@BASEDIR=/usr/local@BASEDIR=/usr/local/zabbix@g" /etc/init.d/zabbix_server
sed -i "s@BASEDIR=/usr/local@BASEDIR=/usr/local/zabbix@g“ /etc/init.d/zabbix_agentd
sed -i "s@#DBPassword=@DBPassword=zabbix@g" /etc/zabbix/zabbix_server.conf
sed -i "s@# DBPassword=@DBPassword=zabbix@g" /etc/zabbix/zabbix_server.conf
sed -i "s@Server=127.0.0.1@Server=127.0.0.1,192.168.8.118@g" /etc/zabbix/zabbix_agentd.conf
sed -i "s@ServerActive=127.0.0.1@ServerActive=$IP: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 "s@^# UnsafeUserParameters=0@UnsafeUserParameters=1\n@g" /etc/zabbix/zabbix_agentd.conf
cp -r frontends/php/ /usr/local/nginx/html/zabbix/
/etc/init.d/zabbix_server start
sed -i ‘s/max_execution_time = 30/max_execution_time = 300/‘ /etc/php.ini
sed -i ‘s/max_input_time = 60/max_input_time = 300/‘ /etc/php.ini
sed -i ‘s/post_max_size = 8M/post_max_size = 16M/‘ /etc/php.ini
sed -i ‘s/;date.timezone =/date.timezone = PRC/‘ /etc/php.ini
sed -i ‘s/;always_populate_raw_post_data/always_populate_raw_post_data/‘ /etc/php.ini
修改zabbix_server.conf参数:
SourceIP=192.168.8.111
DBName=zabbix 数据库名称
DBUser=zabbix 连接数据库的账号,与Mysql授权账号对应
DBPassword=zabbix 连接数据库的密码,与Mysql授权账号的密码对应
修改zabbix_agentd.conf参数:
Server=192.168.8.111
ServerActive=10.10.1.98
Hostname=Zabbix1 #zabbix2修改为自己的hostname
将server1和server2的监控在web界面中添加
安装keepalived实现主从切换
yum install keepalived
创建keepalived状态检查脚本:
vim /etc/keepalived/keepalived_check.sh
#!/bin/bash
mysql_check=`ps -C mysqld --no-header | wc -l`
httpd_check=`ps -C httpd --no-header | wc -l`
case $1 in
mysqld)
if [ $mysql_check -eq 0 ];then
service mysqld start
sleep 3
if [ $mysql_check -eq 0 ];then
service keepalived stop
fi
fi
;;
httpd)
if [ $httpd_check -eq 0 ];then
service nginx start
sleep 3
if [ $httpd_check -eq 0 ];then
service keepalived stop
fi
fi
;;
esac
chmod 755 /etc/keepalived/keepalived_check.sh
创建zabbix重启脚本(主从切换的时候执行此脚本,必须在keepalived配置文件中增加此脚本,否则,主从切换后,web查看host的状态是错误,但是后台可以获得数据)
vi /etc/keepalived/keepalived_zabbix.sh
#!/bin/bash
sleep 60
/etc/init.d/zabbix_server restart
chmod 777 /etc/keepalived/keepalived_zabbix.sh
以上内容server1和server2配置均相同
修改keepalived配置文件(注意zabbix1/zabbix2的状态和优先级)
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc #设置报警地址,可以设置多个,每行1个
sysadmin@firewall.loc #需安装sendmail,并开启邮件报警
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1 #设置SMTP Server地址
smtp_connect_timeout 30
router_id okooo
}
vrrp_script mysqld #vrrp脚本命名
{
script "/etc/keepalived/keepalived_check.sh mysqld" #要执行的脚本
interval 2 #脚本指定间隔,秒
weight -30 #优先级(如果脚本执行结果为0,并且weight配置的值大于0,则优先级相应的增加,如果脚本执行结果非0,并且weight配置的值小于0,则优先级相应的减少)
fall 2 #尝试两次都成功才成功
rise 2 #尝试两次都失败才失败
}
vrrp_script httpd
{
script "/etc/keepalived/keepalived_check.sh nginx"
interval 2
weight -20
fall 2 #尝试两次都成功才成功
rise 2 #尝试两次都失败才失败
}
vrrp_instance zabbix_server{
state MASTER/BACKUP #指定Keepalived的角色,MASTER为主,BACKUP为备
interface eth0 #绑定真实的物网卡
virtual_router_id 1 #虚拟路由编号,主备要一致
priority 150/100 #定义优先级,数字越大,优先级越高,主DR必须大于备用DR
advert_int 1 #检查间隔,默认为1s
authentication {
auth_type PASS #认证方式,PASS或AH两种
auth_pass okooo #认证密码,主备服务器要一致
}
track_script #调用上面定义的检查脚本
{
mysqld
}
track_script
{
nginx
}
virtual_ipaddress {
192.168.8.111 #定义虚拟IP(VIP),可多设,每行一个
}
notify_master "/etc/keepalived/keepalived_zabbix.sh" #指定当切换到master时,执行的脚本
notify_backup "/etc/keepalived/keepalived_zabbix.sh" #指定当切换到backup时,执行的脚本
notify_fault "/sbin/service zabbix_server stop" #故障时执行的脚本
}
/etc/init.d/keepalived start
chkconfig keepalived on
Mysq主主同步
修改数据库配置文件(zabbix1和zabbix2的server-id不同)
[root@zabbix02 ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
default-storage-engine=TokuDB
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
server-id=2 #(zabbix2 设置为2)
log-bin=mysql-bin # 开启bin日志
expire_logs_days=365 # bin日志过期清理时间
max_binlog_size=314572800 # 每个bin日志大小
auto-increment-increment = 2 #字段变化增量值
auto-increment-offset = 1 #初始字段ID为1
slave-skip-errors = all #忽略所有复制产生的错误
binlog-do-db=zabbix # 同步原数据库名
binlog-ignore-db=mysql # 不同步数据库名
binlog-ignore-db=test
binlog-ignore-db=performance_schema
binlog-ignore-db=information_schema
replicate-do-db=zabbix
replicate-ignore-db=mysql
replicate-ignore-db=test
replicate-ignore-db=performance_schema
replicate-ignore-db=information_schema
[mysqld_safe]
thp-setting=never
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
建立同步数据同步账号
server1
grant replication slave on *.* to ‘tongbu‘@‘192.168.8.118‘ identified by ‘tongbu@123‘;
flush privileges;
server2
grant replication slave on *.* to ‘tongbu‘@‘192.168.8.117‘ identified by ‘tongbu@123‘;
flush privileges;
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select user,host from user;
+--------+---------------+
| user | host |
+--------+---------------+
| root | 127.0.0.1 |
| tongbu | 192.168.8.117 |
| root | ::1 |
| | localhost |
| root | localhost |
| zabbix | localhost |
| | zabbix02 |
| root | zabbix02 |
+--------+---------------+
8 rows in set (0.00 sec)
zabbix02
mysql> show master status;
+------------------+----------+--------------+--------------------------------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+--------------------------------------------------+-------------------+
| mysql-bin.000001 | 3305 | zabbix | mysql,test,performance_schema,information_schema | |
+------------------+----------+--------------+--------------------------------------------------+-------------------+
1 row in set (0.00 sec)
mysql> change master to master_host=‘192.168.8.117‘, master_user=‘tongbu‘, master_password=‘tongbu@123‘, master_log_file=‘mysql-bin.000001‘,master_log_pos=4290;
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.8.117
Master_User: tongbu
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 76847
Relay_Log_File: zabbix02-relay-bin.000002
Relay_Log_Pos: 72840
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: zabbix
Replicate_Ignore_DB: mysql,test,performance_schema,information_schema
Replicate_Do_Table:
Replicate_Ignore_Table:
zabbix01
mysql> show master status;
+------------------+----------+--------------+--------------------------------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+--------------------------------------------------+-------------------+
| mysql-bin.000001 | 4290 | zabbix | mysql,test,performance_schema,information_schema | |
+------------------+----------+--------------+--------------------------------------------------+-------------------+
1 row in set (0.00 sec)
mysql> change master to master_host=‘192.168.8.118‘, master_user=‘tongbu‘, master_password=‘tongbu@123‘, master_log_file=‘mysql-bin.000001‘,master_log_pos=3305;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.8.118
Master_User: tongbu
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 127078
Relay_Log_File: zabbix01-relay-bin.000002
Relay_Log_Pos: 124056
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: zabbix
Replicate_Ignore_DB: mysql,test,performance_schema,information_schema
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
重启服务:
# /etc/init.d/nginx restart
# /etc/init.d/zabbix_agentd restart
# /etc/init.d/zabbix_server restart
本文出自 “Dr小白” 博客,请务必保留此出处http://1213503.blog.51cto.com/1203503/1940925
zabbix 3.2高可用