MYSQL + MHA +keepalive + VIP安装配置(三)--keepalived安装配置
一、概述
keepalived介绍:Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web 服务器从系统中剔除,当web服务器工作正常后Keepalived自动将web服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人 工做的只是修复故障的web服务器。
二、环境
1
2
3 |
vip: 192.168 . 1.203 \ 204 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
1 |
shell>wget http: //www.keepalived.org/software/keepalived-1.2.12.tar.gz |
2、安装环境
1 |
yum -y install openssl-devel |
否则会报如下错误
1
2
3 |
configure: error: !!! OpenSSL is not properly installed on your system. !!! !!! Can not include OpenSSL headers files. |
3、安装
1
2
3
4
5 |
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可以通过
1 |
shell>uname -r 命令查看到 |
4、配置
1
2
3
4
5 |
# 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、启动/停止
1
2
3 |
shell>service keepalived start shell>service keepalived stop shell>service keepalived restart |
四、keepalived配置
1、mysql-master:192.168.1.231的配置
1 |
shell>vim /etc/keepalived/keepalived.conf |
配置如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 |
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的配置
1 |
shell>vim /etc/keepalived/keepalived.conf |
配置如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 |
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这个选下也得注意,这个是不抢占资源在优先级高的机器上配置就可以。
看下这个脚本的内容:
1
2
3
4 |
# 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的绑定情况,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13 |
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,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 |
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 优先级高。
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,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 |
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 + MHA +keepalive + VIP安装配置(三)--keepalived安装配置,古老的榕树,5-wow.com
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。