关于LVS基础不多介绍直接操练起来。
1.环境准备
首先准备4台机器(VM,Vbox...)
node1 --> Director1 192.168.137.31 vip=192.168.137.10
node2 --> Director2 192.168.137.32 vip=192.168.137.10
node3 --> RServer1 192.168.137.33
node4 --> RServer2 192.168.137.34
基于DR负载均衡模式,设置了一个VIP(Virtual IP)为192.168.137.10,用户只需要访问这个IP地址即可获得网页服务。其中,负载均衡MASTER-Server1 192.168.137.31,BACKUP-Server2 192.168.137.32 ;RServer1为192.168.137.33,RServer2为192.168..137.34
拓扑:
2.RealServer 配置
RS1配置
|
1
2
3
|
# yum install -y httpd# echo "<h1>The page from node3</h1>" > /var/www/html/index.html# service httpd start |
编写脚本setka.sh配置内核参数
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
[[email protected] ~]# cat setka.sh
#!/bin/bashvip=192.168.137.10case $1 in
start)echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
;;stop)ifconfig lo:0 down
echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
;;esac |
|
1
2
3
4
5
6
7
8
9
10
11
|
# bash setka.sh start ##运行该脚本设置相关内核参数及VIP地址。# cat /proc/sys/net/ipv4/conf/lo/arp_ignore ##内核参数设置成功1# cat /proc/sys/net/ipv4/conf/all/arp_announce2# ifconfig lo:0 ##VIP设置成功lo:0 Link encap:Local Loopbackinet addr:192.168.137.10 Mask:255.255.255.255UP LOOPBACK RUNNING MTU:65536 Metric:1# scp setka.sh [email protected]:/root ##拷贝一份给RS2setka.sh 100% 547 0.5KB/s 00:00
|
RS2配置
|
1
2
3
4
5
|
# yum install -y httpd# echo "<h1>The page from node4</h1>" > /var/www/html/index.html# service start httpd# bash setka.sh ##执行脚本,检查设置是否生效两台都准备妥当后。 |
3.Director 配置
在配置之前,先要把4机器的时间同步一下
以node1为ntp-server
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
[[email protected] ~]# for i in 31 32 33 34;do ssh 192.168.137.$i 'date';done ##提前给4台机器做了**认证
Wed Nov 8 23:50:28 CST 2017Wed Nov 8 23:50:28 CST 2017Wed Nov 8 23:50:28 CST 2017Wed Nov 8 23:50:28 CST 2017# yum install -y ipvsadm# route add -host 192.168.137.10/32 dev eth0添加IPVS规则# ipvsadm -A -t 192.168.137.10:80 -s rr# ipvsadm -a -t 1192.168.137.10:80 -r 192.168.137.37 -g -w 1# ipvsadm -a -t 192.168.137.10:80 -r 192.168.137.37 -g -w 1# ipvsadm -a -t 192.168.137.10:80 -r 192.168.137.33 -g -w 2# ipvsadm -L -n使用另一台Director访问VIPcurl http://192.168.137.10
[[email protected] ~]# curl http://192.168.137.10
<h1>The page from node3</h1>
[[email protected] ~]# curl http://192.168.137.10
<h1>The page from node4</h1>
[[email protected] ~]# curl http://192.168.137.10
|
两台Director进行IPVS测试之后,清除ipvs规则删除route条目
# ipvsadm -C
# route del -host 192.168.137.10/32 dev eth0
开始给Director 配置keepalived
MASTER 配置(node1)
# yum install -y keepalived httpd
# echo "<h1>Sorry,Under maintances(31).</h1>" > /var/www/html/index.html ##添加sorry_server页面
# service httpd start
# 编辑配置文件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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
! Configuration File for keepalived
global_defs {notification_email {[email protected] #本地email地址
}notification_email_from [email protected]smtp_server 127.0.0.1smtp_connect_timeout 30router_id LVS_DEVELvrrp_mcast_group4 224.0.1.118 #配置VRRP组播域
}#健康状态检测脚本检查vrrp_script chk_mt {script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
interval 1weight -20}#实例vrrp_instance VI_1 {state MASTERinterface eth0virtual_router_id 151 #虚拟路由ID
priority 100 #权重100
advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.137.10/16 dev eth0 label eth0:1 #虚拟IP地址这里建议使用16为掩码
}track_script { #追踪健康状态脚本
chk_mt}notify_master "/etc/keepalived/notify.sh master" #配置通知脚本
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}#虚拟服务器及RealServer配置virtual_server 192.168.137.10 80 {delay_loop 6lb_algo wrr #weighted rr (表示通过权重来轮询调度)
lb_kind DR #LVS DR 模型
nat_mask 255.255.255.0protocol TCPsorry_server 127.0.0.1 80 #本地sorry_server服务器配置
real_server 192.168.137.33 80 {weight 1HTTP_GET {url {path /status_code 200}connect_timeout 3nb_get_retry 3delay_before_retry 3}}real_server 192.168.137.34 80 {weight 2HTTP_GET {url {path /status_code 200}connect_timeout 3nb_get_retry 3delay_before_retry 3}}} |
通知脚本(/etc/keepalived/notify.sh):
|
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
|
#!/bin/bash# Author: [email protected]# description: An example of notify script#vip=192.168.137.10contact='[email protected]'
notify() {mailsubject="`hostname` to be $1: $vip floating"
mailbody="`date '+%F %H:%M:%S'`: vrrp transition, `hostname` changed to be $1"
echo $mailbody | mail -s "$mailsubject" $contact
}case "$1" in
master)notify masterexit 0
;;backup)notify backupexit 0
;;fault)notify faultexit 0
;;*)echo 'Usage: `basename $0` {master|backup|fault}'
exit 1
;;esac注:通知脚本需要拷一份至node2 |
BACKUP配置(node2)
|
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# yum install -y keepalived# yum install -y httpd# echo "<h1>Sorry,Under maintances(32).</h1>" > /var/www/html/index.html ##添加sorry_server页面# service httpd start# 配置keepalived.conf! Configuration File for keepalived
global_defs {notification_email {}notification_email_from [email protected]smtp_server 127.0.0.1smtp_connect_timeout 30router_id LVS_DEVELvrrp_mcast_group4 224.0.1.118}vrrp_script chk_mt {script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
interval 1weight -20}vrrp_instance VI_1 {state BACKUPinterface eth0virtual_router_id 151priority 99advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.137.10/16 dev eth0 label eth0:1
}track_script {chk_mt}notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}virtual_server 192.168.137.10 80 {delay_loop 6lb_algo wrrlb_kind DRnat_mask 255.255.255.0protocol TCPsorry_server 127.0.0.1 80real_server 192.168.137.33 80 {weight 1HTTP_GET {url {path /status_code 200}connect_timeout 3nb_get_retry 3delay_before_retry 3}}real_server 192.168.137.34 80 {weight 2HTTP_GET {url {path /status_code 200}connect_timeout 3nb_get_retry 3delay_before_retry 3}}} |
4.启动keepalived并测试功能
在node1上
1、启动keepalived
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
# service keepalived restart ; ssh 192.168.137.32 'service keepalived restart'# ifconfig[[email protected]~]# ifconfig ##可以看到eth0:1地址已经获取到
eth0 Link encap:Ethernet HWaddr 00:0C:29:AA:09:30inet addr:192.168.137.31 Bcast:192.168.137.255 Mask:255.255.255.0inet6 addr: fe80::20c:29ff:feaa:930/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1RX packets:46005 errors:0 dropped:0 overruns:0 frame:0TX packets:59292 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:1000RX bytes:5151611 (4.9 MiB) TX bytes:5965319 (5.6 MiB)eth0:1 Link encap:Ethernet HWaddr 00:0C:29:AA:09:30inet addr:192.168.137.10 Bcast:0.0.0.0 Mask:255.255.0.0UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 |
2、测试访问web
|
1
2
3
4
|
[[email protected]~]# curl http://192.168.137.10
<h1>The page from node3</h1>
[[email protected]~]# curl http://192.168.137.10
<h1>The page from node4</h1>
|
3、健康状态检测
手动在(MASTER)/etc/keepalived中新建一个down文件
# touch down
# 脚本检测到down文件存在权重减20,则vip会自动的floating 到BACKUP的主机上,删除down文件MASTER会将vip抢回来(处于主备的抢占模式下)。
测试如下:
|
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
|
[[email protected] keepalived]# touch down
[[email protected] keepalived]#
[[email protected] keepalived]# ls
down keepalived.conf notify.sh在node2上发现VIP设置成功[[email protected] ~]# ifconfig eth0:1
eth0:1 Link encap:Ethernet HWaddr 00:0C:29:CB:63:DF inet addr:192.168.137.10 Bcast:0.0.0.0 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
在看看mail,此处说明VIP漂至node2 U 10 root Wed Nov 8 21:50 19/723 "node2.localdomain to be master: 192.168.137.10 floating"
访问VIP:[[email protected] keepalived]# curl 192.168.137.10
<h1>The page from node4</h1>
[[email protected] keepalived]# curl 192.168.137.10
<h1>The page from node3</h1>
删除node1keepalived上的down文件。#rm -rf /etc/keepalived/down[[email protected] keepalived]# ifconfig eth0:1
eth0:1 Link encap:Ethernet HWaddr 00:0C:29:AA:09:30 inet addr:192.168.137.10 Bcast:0.0.0.0 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
mail邮件>N 9 root Wed Nov 8 22:01 18/713 "node1.localdomain to be master: 192.168.137.10 floating"
查看网站是否正常访问[[email protected] ~]# curl 192.168.137.10
<h1>The page from node4</h1>
[[email protected] ~]# curl 192.168.137.10
<h1>The page from node3</h1>
这里可看到地址又被node1抢回来了(因为我们配置的是抢占模式),网站正常被调度,测试完毕!!! |
4、Sorry_Server测试
在RealServer(node3)
停掉两台RealServer服务器
# service httpd stop ; ssh 192.168.137.34 'service httpd stop'
[[email protected] ~]# curl http://192.168.137.10
<h1>Sorry,Under maintances(31).</h1>
注释:这里会用涉及到ntp时钟同步,Openssl的**的认证,
# ntpdate IP(ntp时钟服务器地址)
*/5 * * * * root /usr/sbin/ntpdate 192.168.137.31 &>/dev/null;hwclock -w
# ssh-****** -t rsa -P '' -f "/root/.ssh/id_rsa"
# ssh-copy-id -i .ssh/id_rsa.pub [[email protected]]machine
至此keepalived+lvs_dr+healthcheck 高可用集群就这样部署完毕。
本文转自 ljohnmail 51CTO博客,原文链接:http://blog.51cto.com/ljohn/1980547,如需转载请自行联系原作者