-------------------------------html
1、前言linux
2、環境web
3、配置vim
1.LB-Master及LB-Backup配置後端
(1)LB-Master及LB-Backup安裝keepalived和ipvsadm瀏覽器
(2)LB-Master的keepalived主配置文檔bash
(3)LB-Backup的keepalived主配置文檔服務器
2.DNS-Master及DNS-Backup配置負載均衡
(1)DNS-Master及DNS-Backup網卡設置dom
(2)DNS-Master及DNS-Backup安裝bind及ARP設置
(3)DNS-Master配置
(4)DNS-Backup配置
3.web1及web2配置
4、測試
5、故障模擬
模擬LB-Master失效
模擬DNS-Master失效
模擬web1失效
-------------------------------
1、前言
這個實驗是實現企業高可用負載均衡的web案例,不管Master或Backup宕掉任意一臺,業務照常可用。分析拓撲,其中LB-Master負責web的調度,LB-Master負責DNS的調度,實現負載均衡,若是其中一臺出現故障,那麼web和DNS服務會當即轉移到另外一臺服務器上,DNS分爲主輔同步,實現高可用,web在生產環境下頁面應該是一致的,爲了試驗效果,web頁面設置不同,在實際環境下,web後端應架設共享存儲,實現web頁面的一致性。
2、環境
系統:CentOS6.4 32位 6臺
拓撲圖:
IP劃分:
3、配置
1.LB-Master及LB-Backup配置
(1)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
|
# 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/
|
(2)LB-Master的keepalived主配置文檔
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
}
}
}
##################################
|
(3)LB-Backup的keepalived主配置文檔
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
}
}
}
##################################
|
2.DNS-Master及DNS-Backup配置
(1)DNS-Master及DNS-Backup網卡設置
DNS-Master及DNS-Backup在各自網卡基礎上再添加lo:0端口。
(2)DNS-Master及DNS-Backup安裝bind及ARP設置
1
2
3
4
5
|
# yum install bind bind-chroot
# vim /etc/sysctl.conf //添加如下兩行
net.ipv4.conf.all.arp_announce = 2
//
關閉全部接口的ARP響應和發佈
net.ipv4.conf.all.arp_ignore = 1
# sysctl -p
|
(3)DNS-Master配置
1
2
3
4
5
6
|
# service named start
Generating
/etc/rndc
.key: [ OK ]
Starting named: [ OK ]
# cd /var/named/chroot/etc/
# vim named.conf //編輯主配置文檔
|
1
|
# vim named.rfc1912.zones //編輯區域聲明文件[object Object]
|
1
2
3
4
5
|
# scp named.conf named.rfc1912.zones 192.168.2.40:/var/named/chroot/etc/
# cd /var/named/chroot/var/named/
# cp -p named.localhost abc.com.zone
# cp -p named.localhost abc.com.local
# vim abc.com.zone
|
1
|
# vim abc.com.local
|
(4)DNS-Backup配置
1
2
3
|
# cd /var/named/chroot/etc/
# chgrp named named.conf named.rfc1912.zones
# vim named.rfc1912.zones
|
1
2
3
4
5
6
|
# service named start
# cd /var/named/chroot/var/named/slaves
# ll //區域文件已經同步
-rw-r--r--. 1 named named 325 May 27 19:31 abc.com.
local
-rw-r--r--. 1 named named 340 May 27 19:39 abc.com.zone
# vim abc.com.zone
|
1
|
# vim abc.com.local
|
3.web1及web2配置
在原有網卡基礎上再添加一塊lo:0網卡
1
2
3
4
5
6
7
8
|
# yum install httpd
# echo "This is web1.">/var/www/html/index.html //web1配置
# echo "This is web2.">/var/www/html/index.html //web2配置
# vim /etc/sysctl.conf //添加如下兩行
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
# sysctl -p
# service httpd start
|
4、測試:
測試客戶端IP配置:
客戶端瀏覽器訪問http://www.abc.com,並不斷刷新,能夠看出web1和web2交替不斷出現,在實際生產環境下web內容是同樣的 。
LB-Master(負責web輪詢):
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
TCP 192.168.2.200:http rr
-> 192.168.2.50:http Route 100 0 4
-> 192.168.2.60:http Route 100 0 4
UDP 192.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(負責DNS輪詢):
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
TCP 192.168.2.200:http rr
-> 192.168.2.50:http Route 100 0 0
-> 192.168.2.60:http Route 100 0 0
UDP 192.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
|
5、故障模擬
模擬LB-Master失效
(1)停掉LB-Master的keepalived服務
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
# service keepalived stop
Stopping keepalived: [ OK ]
# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
# 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
inet6 fe80::20c:29ff:fe1c:d48d
/64
scope link
valid_lft forever preferred_lft forever
|
(2)查看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 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
inet 192.168.2.200
/24
scope global secondary eth0
inet6 fe80::20c:29ff:fe22:3d01
/64
scope link
valid_lft forever preferred_lft forever
# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.2.200:http rr
-> 192.168.2.50:http Route 100 0 9
-> 192.168.2.60:http Route 100 0 10
UDP 192.168.2.100:domain rr
-> 192.168.2.30:domain Route 100 0 6
-> 192.168.2.40:domain Route 100 0 6
|
2.模擬DNS-Master失效
(1)停掉DNS-Master的named服務,並恢復LB-Master的服務
1
2
|
# service named stop
Stopping named: . [ OK ]
|
(2)查看LB-Master狀態(不斷在測試客戶端刷新網頁)
1
2
3
4
5
6
7
8
9
|
# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.2.200:http rr
-> 192.168.2.50:http Route 100 0 13
-> 192.168.2.60:http Route 100 0 14
UDP 192.168.2.100:domain rr
-> 192.168.2.40:domain Route 100 0 0
|
(3)查看LB-Backup狀態
1
2
3
4
5
6
7
8
9
|
# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.2.200:http rr
-> 192.168.2.50:http Route 100 0 0
-> 192.168.2.60:http Route 100 0 0
UDP 192.168.2.100:domain rr
-> 192.168.2.40:domain Route 100 0 6
|
3.模擬web1失效
停掉web1的httpd服務便可測試,這步比較簡單,再也不贅述。
本文出自 「一言既出;駟馬難追」 博客,請務必保留此出處http://hatech.blog.51cto.com/8360868/1417899