一、概述
keepalived介绍:Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web 服务器从系统中剔除,当web服务器工作正常后Keepalived自动将web服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人 工做的只是修复故障的web服务器。
二、环境
vip:192.168.1.203204 mysql-master:192.168.1.231 mysql-slave:192.168.1.232
三、keepalived安装
安装步骤两台机mysql-master、mysql-slave一样,如下:
1、下载地址:http://www.keepalived.org/software/keepalived-1.2.12.tar.gz
shell>wget http://www.keepalived.org/software/keepalived-1.2.12.tar.gz
2、安装环境
yum -y install openssl-devel
否则会报如下错误
configure: error: !!! OpenSSL is not properly installed on your system. !!! !!! Can not include OpenSSL headers files.
3、安装
shell>tar -zxvf keepalived-1.2.12.tar.gz shell>cd keepalived-1.2.12 shell>./configure --prefix=/opt/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.x86_64/ shell>make shell>make install
说明:
--prefix:安装路径
--with-kernel-dir:这个是重要的参数,这个参数并不表示我们要把Keepalived统进内核,而是指使用内核源码里面的头文件,也就是include目录。
2.6.32-431.el6.x86_64可以通过
shell>uname -r 命令查看到
4、配置
# cp /opt/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/ # cp /opt/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ # mkdir /etc/keepalived # cp /opt/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ # cp /opt/keepalived/sbin/keepalived /usr/sbin/
5、启动/停止
shell>service keepalived start shell>service keepalived stop shell>service keepalived restart
四、keepalived配置
1、mysql-master:192.168.1.231的配置
shell>vim /etc/keepalived/keepalived.conf
配置如下:
global_defs { router_id mysql-master #修改为自己的主机名 notification_email { mengtao10@163.com #接收邮件,可以有多个,一行一个 } #当主、备份设备发生改变时,通过邮件通知 notification_email_from lzyangel@126.com #发送邮箱服务器 smtp_server stmp.163.com #发送邮箱超时时间 smtp_connect_timeout 30 } ##################第一部分################### vrrp_instance VI_1 { state BACKUP #都修改成BACKUP interface eth0 #绑定的网卡 virtual_router_id 60 #默认51 主从都修改为60 priority 100 #优先级,在mysql-slave上LVS上修改成80 advert_int 1 nopreempt #不抢占资源,意思就是它活了之后也不会再把主抢回来 authentication { # 认证方式,可以是PASS或AH两种认证方式 auth_type PASS # 认证密码 auth_pass 1111 } virtual_ipaddress { 192.168.1.203 192.168.1.204 #这可以增加多个VIP } } ##################第二部分################### virtual_server 192.168.1.203 3306 { delay_loop 6 lb_algo wrr lb_kind DR nat_mask 255.255.255.0 persistence_timeout 50 protocol TCP real_server 192.168.1.231 3306 { weight 1 notify_down /root/mysql_down.sh TCP_CHECK { connect_timeout 10 nb_get_retry 3 connect_port 3306 } } }
2、mysql-master:192.168.1.232的配置
shell>vim /etc/keepalived/keepalived.conf
配置如下:
global_defs { router_id mysql-master #修改为自己的主机名 notification_email { mengtao10@163.com #接收邮件,可以有多个,一行一个 } #当主、备份设备发生改变时,通过邮件通知 notification_email_from lzyangel@126.com #发送邮箱服务器 smtp_server stmp.163.com #发送邮箱超时时间 smtp_connect_timeout 30 } ##################第一部分################### vrrp_instance VI_1 { state BACKUP #都修改成BACKUP interface eth0 #绑定的网卡 virtual_router_id 60 #默认51 主从都修改为60 priority 80 #优先级,在mysql-master上LVS上修改成100 advert_int 1 authentication { # 认证方式,可以是PASS或AH两种认证方式 auth_type PASS # 认证密码 auth_pass 1111 } virtual_ipaddress { 192.168.1.203 192.168.1.204 #这可以增加多个VIP } } ##################第二部分################### virtual_server 192.168.1.203 3306 { delay_loop 6 lb_algo wrr lb_kind DR nat_mask 255.255.255.0 persistence_timeout 50 protocol TCP real_server 192.168.1.232 3306 { weight 1 notify_down /root/mysql_down.sh TCP_CHECK { connect_timeout 10 nb_get_retry 3 connect_port 3306 } } }
3、myslq_down.sh配置,两台机都要配置这一步!!!
这里需要注意的是,notify_down /root/mysql_down.sh这个选项,这个是是在keepalived检测不到mysql的时候要执行的脚本,从上面的配置文件来看real服务器只有本机。那么,keeaplived如果启动,客户端也只是访问本机的mysql。nopreempt这个选下也得注意,这个是不抢占资源在优先级高的机器上配置就可以。
看下这个脚本的内容:
# vim /root/mysql_down.sh #!/bin/bash pkill keepalived # chmod +x /root/mysql_down.sh #授权可执行权限
脚本内容就一条命令:pkill keepalived,主要作用是如果本机的mysql挂掉了,那么同时会杀死本机的keepalived,这样另外一台就会接替他工作,虚拟IP也会被另一台接管,如果不杀死keepalived虚拟IP不会被另一台接管,mysql访问也就不会切换过去。
注意:必须要先启动MYSQL,再启动keepalived,否则keepalived启动后会运行mysql_down.sh脚本,等于自杀。
4、mysql-master(231)启动后查看IP的绑定情况,如下:
shell> ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 08:00:27:cc:92:22 brd ff:ff:ff:ff:ff:ff inet 192.168.1.232/24 brd 192.168.1.255 scope global eth0 inet 192.168.1.203/32 scope global eth0 inet 192.168.1.204/32 scope global eth0 inet6 fe80::a00:27ff:fecc:9222/64 scope link valid_lft forever preferred_lft forever
五、测试
1、231、232两台机同时启动MYSQL及Keepalived.
通过win cmd 命令行的客户端(或程序)连接VIP:192.168.1.203或204,如下:
C:UsersDuncan>mysql -h192.168.1.203 -usunney -psunney Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 1440 Server version: 5.5.37-log MySQL Community Server (GPL) Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sunney | | test | +--------------------+ 5 rows in set (0.04 sec) mysql>
注意:这时存储的数据库是mysql-master(192.168.1.231)因为他是主库。配置 priority 100 优先级高。
2、mysql-slave(232)的机同时关闭MYSQL及Keepalived.这时关闭上以上的是一样的。因为他没有走这个库。
3、mysql-master(231)同时关闭MYSQL及Keepalived,mysql-slave(232)同时启动MYSQL及Keepalived
切换很快,大概在2到3秒之间!
通过win cmd 命令行的客户端(或程序)连接VIP:192.168.1.203或204,如下:
C:UsersDuncan>mysql -h192.168.1.203 -usunney -psunney Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 1440 Server version: 5.5.37-log MySQL Community Server (GPL) Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sunney | | test | +--------------------+ 5 rows in set (0.04 sec) mysql>
成功!