实验目的:
通过Keepalive来构建Mysql的高可用。Mysql已做主从,当一台Mysql宕机的时候不影响对Mysql的调用。
实验环境:
两台Mysql服务器(基于LAMP且已做主从),两台Mysql服务器都通过Apache发布Discuz论坛(discuz调用的是主mysql地址)。
身份 主机名 IP地址
主Mysql master 192.168.1.3
从Myslq slave 192.168.1.4
LAMP平台请参见<<搭建Discuz论坛的两种方式>>
Mysql主从参见<<构建Mysql三部曲之三 主从案例>>
实验步骤:
一、在主mysql上安装配置keepalive
1
|
[root@master ~] # bash keepalive_install.sh
|
“脚本”内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
[root@master ~] # cat keepalive_install.sh
#!/bin/bash yum -y install kernel-devel openssl-devel popt-devel &&
wgethttp: //www .keepalived.org /software/keepalived-1 .1.15. tar .gz &&
tar -zxf keepalived-1.1.15. tar .gz &&
cd keepalived-1.1.15 &&
. /configure--with-kernel-dir = /usr/src/kernels/2 .6.32-431.el6.x86_64/ &&
make && make install &&
DIR= /usr/local/
cp $DIR /etc/rc .d /init .d /keepalived /etc/rc .d /init .d/ &&
cp $DIR /etc/sysconfig/keepalived /etc/sysconfig/ && mkdir -p /etc/keepalived &&
cp $DIR /sbin/keepalived/usr/sbin/ &&
cp /usr/local/etc/keepalived/keepalived .conf /etc/keepalived/ &&
/etc/init .d /keepalived restart
if [ $? - eq 0 ]; then
echo -e '\e[32mInstall Keepalive Successfully!\e[0m'
else echo -e '\e[31mInstall Keepalive Failure!\e[0m'
fi |
加载ip_vs的内核模块
1
2
3
|
[root@master ~] # modprobe ip_vs
[root@master~] # modprobe ip_vs_wrr
[root@master~] # modprobe ip_vs_rr
|
修改keepalived.conf配置文件如下:
1
2
|
[root@master ~] # vim /etc/keepalived/keepalived.conf
[root@master ~] # cat/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
|
! Configuration File for keepalived
global_defs { notification_email {
654001593@qq.com
}
notification_email_from root@localhost.com
smtp_server 127.0.0.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.1.77
}
} virtual_server 192.168.1.77 3306 { delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.1.3 3306 {
weight 1
notify_down /data/sh/ keepalive_stop.sh
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
} |
1
2
3
4
5
6
7
8
9
|
[root@master ~] # mkdir -p /data/sh
[root@master ~] # vim/data/sh/keepalive_stop.sh
[root@master ~] # chmod +x /data/sh/keepalive_stop.sh
[root@master ~] # /etc/init.d/keepalivedreload
Reloading keepalived: [ OK ] [root@master ~] # cat/data/sh/keepalive_stop.sh
#!/bin/bash /etc/init .d /keepalived stop
[root@master ~] #
|
二、在从mysql上安装配置keepalive
1
|
[root@slave ~] # bash keepalive_install.sh
|
“脚本”内容如下,同master上执行的脚本一致(非严格意义上的脚本,安装命令放一起而已)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
[root@slave ~] # cat keepalive_install.sh
#!/bin/bash yum -y install kernel-developenssl-devel popt-devel &&
wget http: //www .keepalived.org /software/keepalived-1 .1.15. tar .gz&&
tar -zxf keepalived-1.1.15. tar .gz &&
cd keepalived-1.1.15 &&
. /configure--with-kernel-dir = /usr/src/kernels/2 .6.32-431.el6.x86_64/ &&
make && make install &&
DIR= /usr/local/
cp $DIR /etc/rc .d /init .d /keepalived /etc/rc .d /init .d/ &&
cp $DIR /etc/sysconfig/keepalived/etc/sysconfig/ && mkdir -p /etc/keepalived &&
cp $DIR /sbin/keepalived/usr/sbin/ &&
cp /usr/local/etc/keepalived/keepalived .conf /etc/keepalived/ &&
/etc/init .d /keepalived restart
if [ $? - eq 0 ]; then
echo -e '\e[32mInstall Keepalive Successfully!\e[0m'
else echo -e '\e[31mInstall Keepalive Failure!\e[0m'
fi [root@slave ~] #
|
加载ip_vs的内核模块
1
2
3
|
[root@master ~] # modprobe ip_vs
[root@master ~] # modprobe ip_vs_wrr
[root@master ~] # modprobe ip_vs_rr
|
修改keepalived.conf配置文件如下
1
2
|
[root@slave ~] # vim/etc/keepalived/keepalived.conf
[root@slave ~] # cat/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
|
! Configuration File for keepalived
global_defs { notification_email {
654001593@qq.com
}
notification_email_from root@localhost.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
} vrrp_instance VI_1 { state BACKUP !此处同master不一样
interface eth0
virtual_router_id 51
priority 90 !优先级比master上低
advert_int 1
authentication{
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.77
}
} virtual_server 192.168.1.77 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.4 3306 { !Slave上的IP
weight 1
notify_down /data/sh/keepalive_stop .sh
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
} |
1
2
3
4
5
6
7
8
9
10
|
[root@slave ~] #
root@slave ~] # mkdir -p /data/sh
[root@slave ~] # vim/data/sh/keepalive_stop.sh
[root@slave ~] # chmod +x/data/sh/keepalive_stop.sh
[root@slave ~] # /etc/init.d/keepalivedreload
Reloading keepalived: [ OK ] [root@slave ~] # cat/data/sh/keepalive_stop.sh
#!/bin/bash /etc/init .d /keepalived stop
[root@slave ~] #
|
三、修改Discuz论坛数据库连接IP为VIP
直接访问master上的192.168.1.3论坛是可以访问的,且直接访问slave上的192.168.1.4论坛也是可以访问的,现在我们直接访问VIP(虚拟IP) 192.168.1.77依旧可以访问论坛。
这个时候因为discuz调用的是master上的数据库IP,所以我们应该让其调用VIP 192.168.1.77,这样才能保证在主挂的时候自动切换到从上。
在master上
1
2
|
[root@master ~] #sed -i 's/192.168.1.3/192.168.1.77/'/usr/local/apache/htdocs/config/config_global.php
[root@master ~] #/usr/local/apache/bin/apachectl restart
|
在slave上
1
2
|
[root@slave ~] #sed -i 's/192.168.1.3/192.168.1.77/'/usr/local/apache/htdocs/config/config_global.php
[root@slave ~] #/usr/local/apache/bin/apachectl restart
|
四、测试
上边两个图很明显可以看出此时VIP在master上。
手动模拟master上的mysql关闭
VIP自动漂到slave上
论坛依旧可以访问
本文转自Jacken_yang 51CTO博客,原文链接:http://blog.51cto.com/linuxnote/1653242,如需转载请自行联系原作者