Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2112091
  • 博文数量: 317
  • 博客积分: 5670
  • 博客等级: 大校
  • 技术积分: 3677
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-10 17:51
文章分类

全部博文(317)

文章存档

2016年(2)

2015年(44)

2014年(68)

2013年(42)

2012年(23)

2011年(51)

2010年(67)

2009年(17)

2008年(3)

分类: LINUX

2014-09-12 01:44:37

一、概述

  keepalived介绍:Keepalived的作用是检测web服务器 的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web 服务器从系统中剔除,当web服务器工作正常后Keepalived自动将web服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人 工做的只是修复故障的web服务器。

二、环境

vip:192.168.1.203\204

mysql-master:192.168.1.231

mysql-slave:192.168.1.232

三、keepalived安装

安装步骤两台机mysql-mastermysql-slave一样,如下:

1、下载地址:

shell>wget

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配置

1mysql-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-slaveLVS上修改成80

     advert_int 1

     nopreempt #不抢占资源,意思就是它活了之后也不会再把主抢回来

   

     authentication {

     # 认证方式,可以是PASSAH两种认证方式

     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

         }

     }

}

 2mysql-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-masterLVS上修改成100

     advert_int 1

     authentication {

     # 认证方式,可以是PASSAH两种认证方式

     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

         }

     }

}

 3myslq_down.sh配置,两台机都要配置这一步!!!

    这里需要注意的是,notify_down /root/mysql_down.sh这个选项,这个是是在keepalived检测不到mysql的时候要执行的脚本,从上面的配置文件来看real 服务器只有本机。那么,keeaplived如果启动,客户端也只是访问本机的mysqlnopreempt这个选下也得注意,这个是不抢占资源在优先 级高的机器上配置就可以。
看下这个脚本的内容:

# 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脚本,等于自杀。

4mysql-master(231)启动后查看IP的绑定情况,如下:

shell> ip a

1: lo: 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: 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

 五、测试

1231232两台机同时启动MYSQLKeepalived.

   通过win cmd 命令行的客户端(或程序)连接VIP192.168.1.203204,如下:

C:\Users\Duncan>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 优先级高。

2mysql-slave(232)的机同时关闭MYSQLKeepalived.这时关闭上以上的是一样的。因为他没有走这个库。

3mysql-master(231)同时关闭MYSQLKeepalived,mysql-slave(232)同时启动MYSQLKeepalived

    切换很快,大概在23秒之间!

   通过win cmd 命令行的客户端(或程序)连接VIP192.168.1.203204,如下:

C:\Users\Duncan>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>

 成功!

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