当前位置: 澳门新濠3559 > 操作系统 > 正文

采用keepalived实现zabbix服务器高可用,3》Master2操

时间:2019-12-08 05:50来源:操作系统
1》环境 1、构造划设想计图   Master1 10.0.0.201 2、设计表达 1.幼功布局为LAMP情状,选择keepalived完结zabbix服务器高可用,保险主server的mysql可能httpd宕掉后能切换成从server。 2、数据库做主

1》环境

1、构造划设想计图

澳门新濠3559 1

 

Master1 10.0.0.201

2、设计表达

1. 幼功布局为LAMP情状,选择keepalived完结zabbix服务器高可用,保险主server的mysql可能httpd宕掉后能切换成从server。

2、数据库做主主同步,保证两侧服务器数据的大器晚成致性,达成数据库的高可用。

3、选取unison同步软件保证不管校正这台服务器配置,zabbix配置目录及web目录内容的均等,完毕文件双向同步。

 

Master2 10.0.0.202

3、互连网情形

zabbix01:192.168.8.117
zabbix02:192.168.8.118
vip:192.168.8.111

2》Master1操作授权

4、软件版本

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

CAML:ocaml-3.10.2

UNISON:unison-2.13.16

INOTIFY-TOOLS:inotify-tools-3.13 

 

  mysql>grant replication slave on *.* to 'admin'@'10.0.0.202' identified by '123456';授权

5、根基调节和测量检验

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

  mysql>show master statusG;

6、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=`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

3》Master2操作授权

7、安装zabbix(zabbix01和zabbix02相同)

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=1n@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分界面中增加

  mysql->grant replication slave on *.* to 'admin'@'10.0.0.201' identified by '123456';授权
  mysql->show master statusG;

8、安装keepalived达成主题切换

yum install keepalived

创办keepalived状态检查脚本:
vim /etc/keepalived/keepalived_check.sh
#!/bin/bash
mysql_check=`ps -C mysqld --no-header | wc -l`
nginx_check=`ps -C nginx --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
;;
nginx)
if [ $nginx_check -eq 0 ];then
service nginx start
sleep 3
if [ $nginx_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 nginx
{
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 #概念优先级,数字越大,优先级越高,主D奥迪Q3必得超过备用DWrangler 
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

4》主1-201上操作

9、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                                                           #(zabbix01 设置为1)
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                                      #在slave库中钦定同步那壹个库的binlog日志   
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)

 

获取zabbix binlog文件和ID

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)
zabbix02
mysql> show master status;
+------------------+----------+--------------+--------------------------------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+--------------------------------------------------+-------------------+
| mysql-bin.000001 | 3305 | zabbix | mysql,test,performance_澳门新濠3559,schema,information_schema | |
+------------------+----------+--------------+--------------------------------------------------+-------------------+
1 row in set (0.00 sec)

 

运维互为大旨格局

zabbix01

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 statusG
*************************** 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:

zabbix02

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 statusG
*************************** 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:

重启服务:

# /etc/init.d/nginx restart

# /etc/init.d/zabbix_agentd restart

# /etc/init.d/zabbix_server restart

 

  change master to
  master_host='10.0.0.202',
  master_user='admin',
  master_password='123456',
  master_log_file='mysql-bin.000006',
  master_log_pos=242;
  mysql->start slave;
  mysql->show slave statusG;

10、落成web目录双向实时同步

 

第风华正茂两台server间供给产生ssh的免密登录

* 5》*主2-202上操作

10.1、安装unison同步工具(zabbix1同zabbix2)

tar zxvf ocaml-3.10.2.tar.gz 
cd ocaml-3.10.2 
./configure 
make world opt 
make install

tar zxvf unison-2.13.16.tar.gz 
cd unison-2.13.16 
make UISTYLE=text THREADS=true STATIC=true 
cp ./unison /usr/local/bin/
/usr/local/bin/unison #扭转的那些文件自此,首先须求奉行一遍,会在方今登录的客户家目录下生成多少个.unison的目录,而且在目录下生成贰个defualt.prf文件,以往unison的利用都是透过纠正稿配置文件,实行unison那些命令实行。

 

*  change master to
  master_host='10.0.0.201',
  master_user='admin',
  master_password='123456',
  master_log_file='mysql-bin.000002',
  master_log_pos=242;
  mysql->start slave;
  mysql->show slave statusG;*

10.2、安装inotify工具inotify-tools

[root@zabbix01 log]# uname -a
Linux zabbix01 2.6.32-696.3.1.el6.x86_64 #1 SMP Tue May 30 19:52:55 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
[root@zabbix01 log]# ll /proc/sys/fs/inotify
total 0
-rw-r--r-- 1 root root 0 Jun 22 15:23 max_queued_events
-rw-r--r-- 1 root root 0 Jun 22 15:23 max_user_instances
-rw-r--r-- 1 root root 0 Jun 22 15:23 max_user_watches
上述输出代表系统基本暗中认可有inotify,接下去安装插件就可以

tar xvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure
make
make install
[root@zabbix01 log]# ll /usr/local/bin/inotifywa*
-rwxr-xr-x 1 root root 38582 Jun 22 15:36 /usr/local/bin/inotifywait
-rwxr-xr-x 1 root root 40353 Jun 22 15:36 /usr/local/bin/inotifywatch
inotify-tools安装到位后,会生成inotifywait和inotifywatch三个指令,个中,inotifywait用于等待文件或文件集上的二个一定事件,它能够监督任何文件和目录设置,何况能够递归地监察和控制整个目录树。inotifywatch用于搜罗被监督的文件系统计算数据,包罗每种inotify事件产生多少次等新闻。

 6》主201 和202同有时候设置keepalived

10.3、更正unison配置文件,编写同步脚本举办实行监督同步(注意目录和IP外zabbix1同zabbix2)

漫天进程是通过使用innotify对目录和文书实行检查,然后依照变动调用unison工具实行联合
[root@zabbix01 .unison]# cat zabbix.prf
#同步zabbix配
# Unison preferences file
root=/usr/local/zabbix/
root=ssh://root@192.168.8.118//usr/local/zabbix/ #zabbix2换成zabbix1的IP
batch = true
owner = true
group = true
perms = -1
fastcheck = false
rsync = false
sshargs = -C
xferbycopying = true
#confirmbigdel = true
log = true
logfile = /root/.unison/unison.log
maxthreads = 300
retry = 3
path=etc
path=sbin
path=lib
path=share/zabbix/
path=bin/

[root@zabbix01 .unison]# cat webapps.prf
root=/usr/local/nginx/html/
#本土文件夹
root=ssh://root@192.168.8.118//usr/local/nginx/html
#长途文件夹zabbix2换到zabbix1的IP
batch = true
#活动情势,采纳并执行暗许动作
owner = true
#代表维持同步的文本属主消息
group = true
#代表维持同步的文件属组新闻
perms = -1
#代表维持同步的公文读写权限。
fastcheck = false
#true代表同步时通过文件的创马上间来相比较两半夏件;false表示同步时经过相比两半夏件内容。
rsync = false
#暗中认可值是true,用于激活rsync传输方式。
sshargs = -C
#接纳ssh的裁减传输方式。
xferbycopying = true
#优化传输参数,暗许值为true。
#confirmbigdel = false
#暗中同意值为true,表示当需求联合的五个目录有三个为空时,unison将告一段落。设置为false能够保险当供给协同的某些目录为空时,unison不会终止运作。
log = true
#意味着在终点输出运维消息。
logfile = /root/.unison/unison.log
#钦命同偶尔候输出写入log文件。
maxthreads = 300
#钦定同步时最大线程数
repeat = 1
#代表间距1秒后开首一次新的一同检查
retry = 3
#钦点失利重试次数
path=zabbix/
#一齐钦赐的子目录及文件,是相对路径

  # tar -xvf keepalived-1.1.20.tar.gz
  # cd keepalived-1.1.20
  #./configure --prefix=/usr/local/keepalived
  # make && make install
  # cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
  # cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
  # mkdir /etc/keepalived
  # cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
  # cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

10.4、创立同步脚本(zabbix1同zabbix2)

[root@zabbix01 .unison]# cat /scripts/unison.sh
#!/bin/bash
dir1="/usr/local/nginx/html/zabbix/" #内需检验的目录#
dir2="/usr/local/zabbix/" #亟需检测的目录#
/usr/local/bin/inotifywait -mrq -e create,delete,modify,move $dir1 $dir2 | while read line; do
#通过inotifywait来检验那么些三个目录的转移情状#
/usr/local/bin/unison webapps #调用配置文件webapps.prf同步#
/usr/local/bin/unison zabbix #调用配置文件zabbix.prf同步#
echo -n "`whoami` `date | cut -d " " -f1-5` " >> /var/log/inotify.log #记录目录被改革的岁月到日志,并记下当前登入的顾客#
echo "$line" >> /var/log/inotify.log #笔录目录哪些文件被退换情状#
done

后台执行脚本:
/usr/bin/nohup bash /scripts/unison.sh & 
echo "/usr/bin/nohup bash /scripts/unison.sh &" >>/etc/rc.local

 

*    7》keepalived配置文件**

**! Configuration File for keepalived

global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id MYSQLHA_DEVEL
}

vrrp_script check_mysqld {
script "/etc/keepalived/mysqlcheck/check_mysql.sh" #自己商酌MYSQL 复制状态脚本
interval 2
weight 2
}

vrrp_instance VI_1 {
state BACKUP #这里主201和主202都写上BACKUP
interface eth1 #vip对外的网络接口
virtual_router_id 80 #标暗记。。主重要统一同来
priority 100 #优先级。主202上改成90
advert_int 2
nopreempt #不抢占格局。在预先级高的那台设置此参数,平常在主上设置,主的初期级高

authentication {
auth_type PASS
auth_pass 1111
}

track_script {
check_mysqld #调用MYSQL脚本的函数
}

virtual_ipaddress {
10.0.2.204/16
}
}*

*    当master down了,backup接管了,master再度兴起,不会再形成master。**要不然master恢复生机了再接管的话。如若现身问题了,则切换五遍对生站业务以来并倒霉。*

*   解决办法是:
  state 都安装为Backup,在事情未发生前级高的那台设置参数nopreempt.*

*8》**监察和控制MYSQL 复制状态的剧本*

*#!/bin/bash
#Totle:check_mysql
#Description:Check mysql status
#system:Use Linux
#Author:Alvin
#Version:1.0
#DateTime:2014-09-09
#======================================
#Set mysql host
#各样机器改成温馨的IP地址和长间隔授权的顾客
Host_S=10.0.0.201
User_S=admintest
Pwss_S=123456
Port_S=3306
#======================================
#Function-->Check_mysql_IO
#=====================================
Check_mysql_IO()
{

Check_IO=`mysql -u$User_S -p$Pwss_S -h $Host_S -P $Port_S -e "show slave statusG" | grep "Runnin" | sed 's/ //g' | grep "IO" | awk -F: '{print $NF}' | grep "Yes" | wc -l`

Check_SQL=`mysql -u$User_S -p$Pwss_S -h $Host_S -P $Port_S -e "show slave statusG" | grep "Runnin" | sed 's/ //g' | grep "SQL" | awk -F: '{print $NF}' | grep "Yes" | wc -l`

if [ $Check_IO -ne 1 -o $Check_SQL -ne 1 ]
then
/etc/init.d/keepalived stop
return 1
else
Check_PID=`/etc/init.d/keepalived status | grep "pid" | wc -l`
if [ $Check_PID -eq 1 ]
then
echo "OK"
exit 0
else
/etc/init.d/keepalived start
if [ $? -eq 0 ]
then
echo "keepalived start ok" >/tmp/checkmysql.log
exit 0
else
echo "keepalived start fail..">/tmp/checkmysql.log
return 1
fi
fi
fi
}

#======================================
#Function-->Main
#=====================================
Main()
{

Check_mysql_IO
if [ $? -eq 1 ]
then
echo "Mysql $Host_S IO or SQL error" | mail -s "Mysql IO error" 50738846@qq.com
exit 1
fi

}
Main;

9》**在201和20第22中学间距授权VIP登录顾客*

*  >grant all on *.* to admintest@'%' identified by '123456';*

*10》**末尾测验*

*    经常VIP情势是在201地方的,咱们在202测量试验一下,。用VIP登入是不是观察是的201 ,
    # mysql –u admintest –p 123456 –h 10.0.2.204 –P 3306
       mysqlàshow variables like '%hostname%';
       mysqlàshow variables like '%server_id%';

  接下去。大家测量试验这几个主主切换。是或不是OK?大家在201上。将SLAVE IO 给stop掉。
    mysql->slave stop;
  然后退出来。看看201的VIP还是否存在?借使不设有。那么202上是或不是早就接管了VIP?


编辑:操作系统 本文来源:采用keepalived实现zabbix服务器高可用,3》Master2操

关键词: