1
|
[root@localhost ~]
# yum install-y gcc gcc-c++ makepcre pcre-devel kernel-devel openssl-devel libnl-devel popt-devel
|
1
2
3
4
|
wget http:
//www
.linuxvirtualserver.org
/software/kernel-2
.6
/ipvsadm-1
.26.
tar
.gz
tar
zxvf ipvsadm-1.26.
tar
.gz
cd
ipvsadm-1.26
make
&&
make
install
|
1
2
3
4
5
|
wget http:
//www
.keepalived.org
/software/keepalived-1
.2.7.
tar
.gz
tar
zxvf keepalived-1.2.7.
tar
.gz
cd
keepalived-1.2.7
.
/configure
--prefix=
/usr/local/keepalived
make
&&
make
install
|
1
2
3
4
5
|
cp
/usr/local/keepalived/etc/rc
.d
/init
.d
/keepalived
/etc/init
.d/
cp
/usr/local/keepalived/etc/sysconfig/keepalived
/etc/sysconfig/
mkdir
/etc/keepalived/
cp
/usr/local/keepalived/etc/keepalived/keepalived
.conf
/etc/keepalived/
cp
/usr/local/keepalived/sbin/keepalived
/usr/sbin/
|
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
|
[root@localhost ~]
# vi /etc/keepalived/keepalived.conf
! Configuration File forkeepalived
global_defs {
notification_email {
test
@sina.com
#故障接受联系人
}
notification_email_from admin@
test
.com
#故障发送人
smtp_server 127.0.0.1
#本机发送邮件
smtp_connect_timeout 30
router_id LVS_MASTER
#BACKUP上修改为LVS_BACKUP
}
vrrp_instance VI_1 {
state MASTER
#BACKUP上修改为BACKUP
interface eth0
virtual_router_id 51
#虚拟路由标识,主从相同
priority 100
#BACKUP上修改为90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
#主从认证密码必须一致
}
virtual_ipaddress {
#Web虚拟IP(VTP)
172.0.0.10
}
}
virtual_server 172.0.0.10 80 {
#定义虚拟IP和端口
delay_loop 6
#检查真实服务器时间,单位秒
lb_algo rr
#设置负载调度算法,rr为轮训
lb_kind DR
#设置LVS负载均衡DR模式
persistence_timeout 50
#同一IP的连接60秒内被分配到同一台真实服务器
protocol TCP
#使用TCP协议检查realserver状态
real_server 172.0.0.13 80 {
#第一个web服务器
weight 3
#节点权重值
TCP_CHECK {
#健康检查方式
connect_timeout 3
#连接超时
nb_get_retry 3
#重试次数
delay_before_retry 3
#重试间隔/S
}
}
real_server 172.0.0.14 80 {
#第二个web服务器
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@localhost ~]
# service keepalived restart #启动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
|
[root@localhost ~]
# vi /etc/init.d/real.sh
#description : start realserver
VIP=172.0.0.10
.
/etc/init
.d
/functions
case
"$1"
in
start)
/sbin/ifconfig
lo:0 $VIP broadcast $VIP netmask 255.255.255.255 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
echo
"LVS RealServer Start OK"
;;
stop)
/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
echo
"LVS RealServer Stoped OK"
;;
*)
echo
"Usage: $0 {start|stop}"
exit
1
esac
|
1
2
3
4
|
[root@localhost ~]
# chmod +x /etc/init.d/real.sh
[root@localhost ~]
# /etc/init.d/real.sh start
LVS RealServer Start OK
[root@localhost ~]
# echo "/etc/init.d/real.sh start" >> /etc/rc.local
|
1
2
3
4
5
|
[root@localhost ~]
# service httpd start
[root@localhost ~]
# echo “192.168.0.30″ > /var/www/html/index.html
[root@localhost ~]
# echo “192.168.0.40″ > /var/www/html/index.html
[root@localhost ~]
# service iptables stop #关闭防火墙
[root@localhost ~]
# setenforce 0 #临时关闭selinux
|