服务器 | IP | VIP | 系统 |
Director | 10.0.0.12 | 10.0.0.88 | CentOS 6.x X64 |
Realserver1 | 10.0.0.9 | 10.0.0.88 | CentOS 6.x X64 |
Realserver2 | 10.0.0.20 | 10.0.0.88 | CentOS 6.x X64 |
Realserver3 | 10.0.0.21 | 10.0.0.88 | CentOS 6.x X64 |
1
|
yum
install
ipvsadm -y
|
1
|
ipvsadm --help
|
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
|
#!/bin/bash
#
# LVS script for VS/DR
# chkconfig: - 90 10
# description: LVS DR director
.
/etc/rc
.d
/init
.d
/functions
#
VIP=10.0.0.88
DIP=10.0.0.12
RIP1=10.0.0.9
RIP2=10.0.0.20
RIP3=10.0.0.21
PORT=80
RSWEIGHT1=2
RSWEIGHT2=5
RSWEIGHT3=5
#
case
"$1"
in
start)
/sbin/ifconfig
eth0:0 $VIP up
/sbin/route
add -host $VIP dev eth0:0
# Since this is the Director we must be able to forward packets
echo
1 >
/proc/sys/net/ipv4/ip_forward
# Clear all iptables rules.
/sbin/iptables
-F
# Reset iptables counters.
/sbin/iptables
-Z
# Clear all ipvsadm rules/services.
/sbin/ipvsadm
-C
# Add an IP virtual service for VIP 192.168.0.219 port 80
# In this recipe, we will use the round-robin scheduling method.
# In production, however, you should use a weighted, dynamic scheduling method.
/sbin/ipvsadm
-A -t $VIP:80 -s wrr
# Now direct packets for this VIP to
# the real server IP (RIP) inside the cluster
/sbin/ipvsadm
-a -t $VIP:80 -r $RIP1 -g -w $RSWEIGHT1
/sbin/ipvsadm
-a -t $VIP:80 -r $RIP2 -g -w $RSWEIGHT2
/sbin/ipvsadm
-a -t $VIP:80 -r $RIP3 -g -w $RSWEIGHT3
/sbin/ipvsadm
--
set
11 15 300
/bin/touch
/var/lock/subsys/ipvsadm
&>
/dev/null
;;
stop)
# Stop forwarding packets
echo
0 >
/proc/sys/net/ipv4/ip_forward
# Reset ipvsadm
/sbin/ipvsadm
-C
# Bring down the VIP interface
/sbin/ifconfig
eth0:0 down
/sbin/route
del $VIP
/bin/rm
-f
/var/lock/subsys/ipvsadm
echo
"ipvs is stopped..."
;;
status)
if
[ ! -e
/var/lock/subsys/ipvsadm
];
then
echo
"ipvsadm is stopped ..."
else
echo
"ipvs is running ..."
ipvsadm -L -n
fi
;;
*)
echo
"Usage: $0 {start|stop|status}"
;;
esac
|
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
|
#!/bin/bash
#
# Script to start LVS DR real server.
# chkconfig: - 90 10
# description: LVS DR real server
#
.
/etc/rc
.d
/init
.d
/functions
VIP=10.0.0.88
host=`
/bin/hostname
`
case
"$1"
in
start)
# Start LVS-DR real server on this machine.
/sbin/ifconfig
lo down
/sbin/ifconfig
lo up
echo
1 >
/proc/sys/net/ipv4/conf/lo/arp_ignore
echo
2 >
/proc/sys/net/ipv4/conf/lo/arp_announce
echo
1 >
/proc/sys/net/ipv4/conf/all/arp_ignore
echo
2 >
/proc/sys/net/ipv4/conf/all/arp_announce
/sbin/ifconfig
lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route
add -host $VIP dev lo:0
;;
stop)
# Stop LVS-DR real server loopback device(s).
/sbin/ifconfig
lo:0 down
echo
0 >
/proc/sys/net/ipv4/conf/lo/arp_ignore
echo
0 >
/proc/sys/net/ipv4/conf/lo/arp_announce
echo
0 >
/proc/sys/net/ipv4/conf/all/arp_ignore
echo
0 >
/proc/sys/net/ipv4/conf/all/arp_announce
;;
status)
# Status of LVS-DR real server.
islothere=`
/sbin/ifconfig
lo:0 |
grep
$VIP`
isrothere=`
netstat
-rn |
grep
"lo:0"
|
grep
$VIP`
if
[ !
"$islothere"
-o !
"isrothere"
];
then
# Either the route or the lo:0 device
# not found.
echo
"LVS-DR real server Stopped."
else
echo
"LVS-DR real server Running."
fi
;;
*)
# Invalid entry.
echo
"$0: Usage: $0 {start|status|stop}"
exit
1
;;
esac
|
1
2
3
4
5
6
7
8
9
|
[root@lvs1 ~]
# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.0.0.88:80 wrr
-> 10.0.0.9:80 Route 2 3162 387
-> 10.0.0.10:80 Route 1 3906 28
-> 10.0.0.20:80 Route 5 8010 1011
-> 10.0.0.21:80 Route 6 9492 1215
|