Linux下部署LVS(NAT)+keepalived

Linux

LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统。keepalives主要用作RealServer的健康状态检查以及LoadBalance主机和BackUP主机之间failover的实现。我们完全可以用这两个免费软件配合实现网站高性能高可用负载均衡。

director(高可用引导服务器主从)
1、安装ipvsadm和keepalive

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@CentOS-LVS ~]# yum -y install popt popt-devel libnl libnl-devel popt-static openssl-devel ipvsadm
[root@CentOS-LVS ~]# yum install keepalived
######### 将keepalived做成启动服务,方便管理##########
[root@CentOS-LVS ~]# wget http://www.keepalived.org/software/keepalived-1.2.4.tar.gz
[root@CentOS-LVS ~]# tar zxvf keepalived-1.2.4.tar.gz
[root@CentOS-LVS ~]# cd keepalived-1.2.4
[root@CentOS-LVS keepalived-1.2.4]# ./configure && make && make install
[root@CentOS-LVS ~]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
[root@CentOS-LVS ~]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
[root@CentOS-LVS ~]# mkdir /etc/keepalived/
[root@CentOS-LVS ~]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
[root@CentOS-LVS_MASTER ~]# cp /usr/local/sbin/keepalived /usr/sbin/
[root@CentOS-LVS_MASTER ~]# service keepalived start | stop

2、开启路由转发

1
2
3
[root@CentOS-LVS ~]# vi /etc/sysctl.conf
net.ipv4.ip_forward = 1
[root@CentOS-LVS ~]# sysctl -p

3、配置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
[root@CentOS-LVS ~]# less /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
router_id LVS_MASTER #BACKUP上修改为LVS_BACKUP
}

vrrp_instance VI_1 {
state MASTER #BACKUP上修改为BACKUP
interface eth0
virtual_router_id 51
priority 100 #BACKUP上修改为80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.227
}
}

vrrp_instance LAN_GATEWAY {
state MASTER #BACKUP上修改为LVS_BACKUP
interface eth1
virtual_router_id 52
priority 100 #BACKUP上修改为80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.10.10
}
}

virtual_server 10.0.0.227 80 {
delay_loop 6
lb_algo rr
lb_kind NAT
# persistence_timeout 5
protocol TCP

real_server 192.168.10.4 80 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}

real_server 192.168.10.5 80 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}

BACKUP服务器同上配置,先安装lvs再安装keepalived,然后配置/etc/keepalived/keepalived.conf,只需将批注部分改一下即可。

4、分别在2台Real Server上面设置网关

1
2
3
[root@CentOS-LVS ~]# /sbin/ifconfig lo:0 10.0.0.227 broadcast 10.0.0.227 netmask 255.255.255.255 up
[root@CentOS-LVS  ~]# echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@CentOS-LVS ~]# echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce

5、在2台RealServer中分别配置HTTP

1
2
3
4
5
[root@CentOS-LVS ~]# yum -y install httpd
[root@CentOS-LVS ~]# cd /var/www/html/
[root@CentOS-LVS html]# cat index.html
<h1>WEB1/192.168.10.4</h1>
[root@raykaeso html]# /etc/init.d/httpd start

另一台机器配置一样,过程略。

6、分别在CentOS-LVS_MASTER、CentOS-LVS_BACKUP上执行service keepalived start启动keepalived就可实现负载均衡及高可用集群了;

1
[root@CentOS-LVS keepalived]# service keepalived start

来源:Linux下部署LVS(NAT)+keepalived

发表评论

电子邮件地址不会被公开。 必填项已用*标注