Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6659521
  • 博文数量: 1005
  • 博客积分: 8199
  • 博客等级: 中将
  • 技术积分: 13071
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-25 20:19
个人简介

脚踏实地、勇往直前!

文章分类

全部博文(1005)

文章存档

2020年(2)

2019年(93)

2018年(208)

2017年(81)

2016年(49)

2015年(50)

2014年(170)

2013年(52)

2012年(177)

2011年(93)

2010年(30)

分类: Mysql/postgreSQL

2016-05-23 19:34:12


1.1 环境部署
                                                                        

 节点角色

ip

master

192.168.56.111

backup

192.168.56.112

vip

192.168.56.199








1.2 keepalived下载
下载地址:


1.3 安装依赖包
openssl-0.9.8e-20.el5
openssl-devel-0.9.8e-20.el5
popt-1.10.2.3-22.el5
libnl-1.0-0.10.pre5.5
libnl-devel-1.0-0.10.pre5.5

1.4 keepalived安装

1.4.1  master上安装
[root@host01 keepalive]# tar -xvf keepalived-1.2.13.tar.gz 
[root@host01 keepalive]# cd keepalived-1.2.13
[root@host01]# ./configure --prefix=/usr/local/keepalived --disable-fwmark
[root@host01 keepalived-1.2.13]# make
[root@host01 keepalived-1.2.13]# make install
[root@host01 sbin]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
[root@host01]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@host01 sysconfig]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
[root@host01 sysconfig]# cd /etc/init.d/
[root@host01 init.d]# chkconfig --add keepalived
[root@host01 init.d]# chkconfig keepalived on
[root@host01 init.d]# mkdir -p /etc/keepalived

keepalived-1.3.8安装到指定的目录
./configure --prefix=/opt/keepalived
make PREFIX=/opt/keepalived
make install PREFIX=/opt/keepalived

keepalived-1.3.8安装步骤
[root@pxc01]# tar -xvf keepalived-1.3.8.tar.gz  
[root@pxc01]# cd keepalived-1.3.8
[root@pxc01]# ./configure --prefix=/usr/local/keepalived
[root@pxc01]# make
[root@pxc01]# make install
[root@pxc01]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
[root@pxc01]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@pxc01]# mkdir -p /etc/keepalived
[root@pxc01]# cd /soft/keepalived-1.3.8/keepalived/etc/init.d
[root@pxc01]# cp keepalived /etc/init.d/
[root@host01]# cd /etc/init.d/
[root@host01]# chkconfig --add keepalived ##添加服务
[root@host01]# chkconfig keepalived on ##开机启动


1.4.2  master配置
vi /etc/keepalived/keepalived.conf
内容如下:

! Configuration File for keepalived


global_defs {
 notification_email {
 slnngk@163.com
 }
 notification_email_from slnngk@163.com
 smtp_server 192.168.56.1
 smtp_connect_timeout 30
 router_id LVS_DEVEL
}


vrrp_instance VI_1 {
 state MASTER
 interface eth0
 virtual_router_id 51
 priority 100
 advert_int 1
 authentication {
 auth_type PASS
 auth_pass 1111
 }
 virtual_ipaddress {
 192.168.56.199/24 dev eth0 scope global
 }
}

1.4.3  backup上安装
[root@host02 keepalive]# tar -xvf keepalived-1.2.13.tar.gz 
[root@host02 keepalive]# cd keepalived-1.2.13
[root@host02]# ./configure --prefix=/usr/local/keepalived --disable-fwmark
[root@host02 keepalived-1.2.13]# make
[root@host02 keepalived-1.2.13]# make install
[root@host02 sbin]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
[root@host02]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@host02 sysconfig]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
[root@host02 sysconfig]# cd /etc/init.d/
[root@host02 init.d]# chkconfig --add keepalived
[root@host02 init.d]# chkconfig keepalived off
[root@host02 init.d]# mkdir -p /etc/keepalived

1.4.4  backup上配置
vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
 notification_email {
 slnngk@163.com
 }
 notification_email_from slnngk@163.com
 smtp_server 192.168.56.1
 smtp_connect_timeout 30
 router_id LVS_DEVEL
}


vrrp_instance VI_1 {
 state BACKUP
 interface eth0
 virtual_router_id 51
 priority 99
 advert_int 1
 authentication {
 auth_type PASS
 auth_pass 1111
 }
 virtual_ipaddress {
192.168.56.199/24 dev eth0 scope global 
}
}

1.4.5  启动
分别在master和backup机器上执行如下命令启动keepalived

[root@host01 keepalived-1.2.13]# service keepalived start
Starting keepalived: [  OK  ]

[root@host02 keepalived-1.2.13]# service keepalived start
Starting keepalived: [  OK  ]

1.5 keepalived验证
1.5.1  master上执行
[root@host01 keepalived]# ip addr
1: lo: mtu 16436 qdisc noqueue 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 08:00:27:d1:2c:51 brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.111/24 brd 192.168.56.255 scope global eth0
    inet 192.168.56.199/24 scope global secondary eth0
3: eth1: mtu 1500 qdisc noop qlen 1000
link/ether 08:00:27:b9:1e:c3 brd ff:ff:ff:ff:ff:ff

1.5.2  backup上执行
关闭master上的keepalived服务后在backup上查看
[root@host02 keepalived]# ip addr
1: lo: mtu 16436 qdisc noqueue 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 08:00:27:70:5c:2d brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.112/24 brd 192.168.56.255 scope global eth0
    inet 192.168.56.199/24 scope global secondary eth0
3: eth1: mtu 1500 qdisc noop qlen 1000
    link/ether 08:00:27:49:31:51 brd ff:ff:ff:ff:ff:ff

1.6 keepalived启动指定配置文件和日志文件
分别在master和backup机器上编辑该文件
/usr/local/keepalived/etc/sysconfig/ keepalived,修改红色部分的内容

 [root@host01 sysconfig]# more keepalived
# Options for keepalived. See `keepalived --help' output and keepalived(8) and
# keepalived.conf(5) man pages for a list of all options. Here are the most
# common ones :
#
# --vrrp               -P    Only run with VRRP subsystem.
# --check              -C    Only run with Health-checker subsystem.
# --dont-release-vrrp  -V    Dont remove VRRP VIPs & VROUTEs on daemon stop.
# --dont-release-ipvs  -I    Dont remove IPVS topology on daemon stop.
# --dump-conf          -d    Dump the configuration data.
# --log-detail         -D    Detailed log messages.
# --log-facility       -S    0-7 Set local syslog facility (default=LOG_DAEMON)
#

KEEPALIVED_OPTIONS="-D -d -S 0 -f /etc/keepalived/keepalived.conf"
-f 指定了自定义的配置文件
-S指定了日志文件,我们需要在/etc/syslog.conf文件添加如下红色标识的内容

CentOS 7下该文件为/etc/rsyslog.conf

 [root@host01 etc]# more /etc/syslog.conf
# Log all kernel messages to the console.
# Logging much else clutters up the screen.
#kern.*                                                 /dev/console
 # Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
*.info;mail.none;authpriv.none;cron.none                /var/log/messages

 # The authpriv file has restricted access.
authpriv.*                                              /var/log/secure

 # Log all the mail messages in one place.
mail.*                                                  -/var/log/maillog
 # Log cron stuff
cron.*                                                  /var/log/cron
 # Everybody gets emergency messages
*.emerg                                                 *
 # Save news errors of level crit and higher in a special file.
uucp,news.crit                                          /var/log/spooler
 # Save boot messages also to boot.log
local7.*                                                /var/log/boot.log
 # keepalived -S 0 
local0.*                                                /var/log/keepalived.log

1.7 配置检查脚本
 我们在做mysql高可用的时候,检查mysql停掉后,相应的keepalived也停掉,让其切换到另一台机器
 在master上操作,修改配置文件,添加红色部分的内容
vi /etc/keepalived/keepalived.conf
内容如下:

! Configuration File for keepalived
 global_defs {
 notification_email {
 slnngk@163.com
 }
 notification_email_from slnngk@163.com
 smtp_server 192.168.56.1
 smtp_connect_timeout 30
 router_id LVS_DEVEL
}
vrrp_script check_mysql {
  script  "/etc/keepalived/check_mysql.sh"
  interval 2
}
 vrrp_instance VI_1 {
 state MASTER
 interface eth0
 virtual_router_id 51
 priority 100
 advert_int 1
 authentication {
 auth_type PASS
 auth_pass 1111
 }
 virtual_ipaddress {
 192.168.56.199/24 dev eth0 scope global
 }

track_script {       ##注意这里{前面必须有空格,否则不会生效
     check_mysql
}

}

要是不在keepalived配置文件中定义执行脚本的话,可以在crontab定时任务中每时每刻执行检查脚本。
### MySQL_HA
* * * * * /etc/keepalived/check_mysql.sh  >/dev/null 2>&1
* * * * * sleep 10;/etc/keepalived/check_mysql.sh >/etc/keepalived/mycheck.log 2>&1
* * * * * sleep 20;/etc/keepalived/check_mysql.sh >/etc/keepalived/mycheck.log 2>&1
* * * * * sleep 30;/etc/keepalived/check_mysql.sh >/etc/keepalived/mycheck.log 2>&1
* * * * * sleep 40;/etc/keepalived/check_mysql.sh >/etc/keepalived/mycheck.log 2>&1
* * * * * sleep 50;/etc/keepalived/check_mysql.sh >/etc/keepalived/mycheck.log 2>&1


check_mysql.sh脚本内容如下:
 [root@host01 keepalived]# more check_mysql.sh
#!/bin/bash

 MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=mysql
SOCK_FILE=/db/mysqlmha/mysql.sock
CHECK_TIME=3
now_date=`date '+%Y-%m-%d %H:%M:%S'`

#mysql is working MYSQL_OK is 0 , mysql down MYSQL_OK is 1
MYSQL_OK=1


function check_mysql_helth (){
mysql -h $MYSQL_HOST -u $MYSQL_USER -p$MYSQL_PASSWORD -S $SOCK_FILE -e "show status;" > /etc/keepalived/mycheck.log 2>&1
if [ $? = 0 ] ;then
MYSQL_OK=0
else
MYSQL_OK=1
fi
return $MYSQL_OK
}


while [ $CHECK_TIME -ne 0 ] 
do
let "CHECK_TIME -= 1"


check_mysql_helth
echo $now_date>>/etc/keepalived/mycheck.log

if [ $MYSQL_OK = 0 ] ; then
CHECK_TIME=0
exit 0
fi


if [ $MYSQL_OK -eq 1 ] && [ $CHECK_TIME -eq 1 ]
then
    ##pkill keepalived
/sbin/service keepalived stop 
exit 1
fi


sleep 1


done

-- The End --
阅读(3710) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~