Lvs+Keepalived+Bind+web构建高可用负载均衡系统
-------------------------------一、前言
二、环境
三、
1.安装keepalived及ipvsadm
-------------------------------
一、前言
二、环境
系统:CentOS6.4 32位 6台
拓扑图:
IP划分:
三、
LB-Master及LB-Backup安装keepalived及ipvsadm
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
# yum groupinstall "Additional Development" //安装开发工具
# yum groupinstall "Development tools"
# tar -zxvf keepalived-1.2.1.tar.gz -C /usr/local/src/
# cd /usr/local/src/keepalived-1.2.1
# ./cnfigure
Keepalived configuration
------------------------
Keepalived version : 1.2.1
Compiler : gcc
Compiler flags : -g -O2
Extra Lib : -lpopt -lssl -lcrypto
Use IPVS Framework : No //配置出现错误
IPVS sync daemon support : No
Use VRRP Framework : Yes
Use Debug flags : No
解决方法:
# yum install kernel-devel ipvsadm
# ln -s /usr/src/kernels/2.6.32-358.el6.i686/ /usr/src/linux
# ./cnfigure //再次配置环境
# make //编译
# make install //安装
# cd /usr/local/etc //keepalived默认安装路径
# ll
drwxr-xr-x. 3 root root 4096 May 24 00:37 keepalived
drwxr-xr-x. 3 root root 4096 May 24 00:29 rc.d
drwxr-xr-x. 2 root root 4096 May 24 00:29 sysconfig
配置以系统方式service启动
# cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
# cp /usr/local/etc/sysconfig/keepalived/etc/sysconfig/
# mkdir /etc/keepalived
# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
# cp /usr/local/sbin/keepalived /usr/sbin/
LB-Master主配置文档
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# cat /etc/keepalived/keepalived.conf
#guration File for keepalived
#global define
global_defs {
router_id Haweb_1
}
vrrp_sync_group VGM {
group {
VI_WEB
}
}
vrrp_sync_group VGN {
group {
HA_DNS
}
}
# vvrp_instance define #
vrrp_instance VI_WEB {
state MASTER
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 55
priority 100
advert_int 5
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.2.200/24 dev eth0
}
}
##########LVS###########
virtual_server 192.168.2.200 80 {
delay_loop 6
lb_algo rr
lb_kind DR
# persistence_timeout 20
protocol TCP
real_server 192.168.2.50 80 {
weight 100
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.2.60 80 {
weight 100
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
### HA DNS START ###
vrrp_instance HA_DNS {
state BACKUP
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 56
priority 90
advert_int 5
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.2.100/24 dev eth0
}
}
##########LVS DNS###########
virtual_server 192.168.2.100 53 {
delay_loop 6
lb_algo rr
lb_kind DR
# persistence_timeout 6
protocol UDP
real_server 192.168.2.30 53 {
weight 100
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 53
}
}
real_server 192.168.2.40 53 {
weight 100
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 53
}
}
}
##################################
LB-Backup主配置文档
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# cat /etc/keepalived/keepalived.conf
#guration File for keepalived
#global define
global_defs {
router_id Haweb_1
}
vrrp_sync_group VGM {
group {
VI_WEB
}
}
vrrp_sync_group VGN {
group {
HA_DNS
}
}
# vvrp_instance define #
vrrp_instance VI_WEB {
state BACKUP
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 55
priority 90
advert_int 5
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.2.200/24 dev eth0
}
}
##########LVS###########
virtual_server 192.168.2.200 80 {
delay_loop 6
lb_algo rr
lb_kind DR
# persistence_timeout 20
protocol TCP
real_server 192.168.2.50 80 {
weight 100
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.2.60 80 {
weight 100
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
### HA DNS START ###
vrrp_instance HA_DNS {
state MASTER
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 56
priority 100
advert_int 5
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.2.100/24 dev eth0
}
}
##########LVS DNS###########
virtual_server 192.168.2.100 53 {
delay_loop 6
lb_algo rr
lb_kind DR
# persistence_timeout 6
protocol UDP
real_server 192.168.2.30 53 {
weight 100
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 53
}
}
real_server 192.168.2.40 53 {
weight 100
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 53
}
}
}
##################################
DNS-Master及DNS-Backup安装bind
1
2
3
4
5
6
7
8
9
# yum install bind bind-chroot
# vim /etc/sysctl.conf //添加以下两行
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
# sysctl -p
# ll slaves/
-rw-r-----. 1 root named 186 May 27 06:06 abc.com.local
-rw-r-----. 1 root named 229 May 27 06:06 abc.com.zone
1
2
3
4
5
6
# yum install httpd
# echo "This is web1.">/var/www/html/index.html
# vim /etc/sysctl.conf //添加以下两行
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
# sysctl -p
客户端测试:
客户端IP配置:
客户端浏览器访问http://www.abc.com,并不断刷新。
LB-Master:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP192.168.2.200:http rr
-> 192.168.2.50:http Route 100 0 4
-> 192.168.2.60:http Route 100 0 4
UDP192.168.2.100:domain rr
-> 192.168.2.30:domain Route 100 0 0
-> 192.168.2.40:domain Route 100 0 0
# ip addr
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 UNKNOWN qlen 1000
link/ether 00:0c:29:1c:d4:8d brd ff:ff:ff:ff:ff:ff
inet 192.168.2.10/24 brd 192.168.2.255 scope global eth0
inet 192.168.2.200/24 scope global secondary eth0
inet6 fe80::20c:29ff:fe1c:d48d/64 scope link
valid_lft forever preferred_lft forever
LB-Backup:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP192.168.2.200:http rr
-> 192.168.2.50:http Route 100 0 0
-> 192.168.2.60:http Route 100 0 0
UDP192.168.2.100:domain rr
-> 192.168.2.30:domain Route 100 0 6
-> 192.168.2.40:domain Route 100 0 6
# ip addr
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 UNKNOWN qlen 1000
link/ether 00:0c:29:22:3d:01 brd ff:ff:ff:ff:ff:ff
inet 192.168.2.20/24 brd 192.168.2.255 scope global eth0
inet 192.168.2.100/24 scope global secondary eth0
inet6 fe80::20c:29ff:fe22:3d01/64 scope link
valid_lft forever preferred_lft forever
页:
[1]