CentOS6.2下搭建LVS(NAT)+Keepalived实现高性能高可用负载均衡服务器

时间:2021-05-19 19:47:25

上一篇文档写的是LVS(DR)+ Keepalived实现高性能高可用负载均衡服务器,这次主要写的是基于LVS(NAT)+ Keepalived实现高性能高可用负载均衡服务器。
其实NAT模式在企业用的不多,我和很多朋友都沟通过这个事情,我们搭建这个环节主要是测试一下整个性能与DR的具体的差距。但是在搭建这个环境过程中遇到一点小问题,就是内网的VIP不知道如何去设置,这个问题困扰了我很长时间,因为LVS也只是刚刚接触,期间给我一个之前的学员电话他给我提供了一个方法,昨天晚上回去测试OK~!今天上午在线上也测试了,没有问题。以后要继续努力~!朝着我的目标前进~!同时希望各位在参考时遇到什么问题和我及时沟通,以便更好的完善文档!~

软件下载:
keepalived:http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
ipvsadm: http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gz 注意:(for kernel 2.6.28-rc3 or later)
popt-static:http://mirror.centos.org/centos/6/os/x86_64/Packages/popt-static-1.13-7.el6.x86_64.rpm
 

实验拓扑图
CentOS6.2下搭建LVS(NAT)+Keepalived实现高性能高可用负载均衡服务器
1、在2台Director Server上分别配置LVS+Keepalived
[root@host1 ~]# rpm -ivh popt-static-1.13-7.el6.x86_64.rpm
#看我前一篇文章http://crazylinux.blog.51cto.com/259244/811591
[root@host1 ~]# yum -y install kernel-devel make gcc openssl-devel lftp libnl* popt*
[root@host1 ~]# ln -s /usr/src/kernels/2.6.18-194.el5-x86_64/ /usr/src/linux
[root@host1 ~]# tar -zxvf ipvsadm-1.26.tar.gz -C /usr/src/
[root@host1 ~]# cd /usr/src/ipvsadm-1.26/
[root@host1 ipvsadm-1.26]# make
[root@host1 ipvsadm-1.26]# make install
[root@host1 ~]# tar -zxvf keepalived-1.2.2.tar.gz -C /usr/src/
[root@host1 ~]# cd /usr/src/keepalived-1.2.2/
[root@host1 keepalived-1.2.2]# ./configure
[root@host1 keepalived-1.2.2]# make
[root@host1 keepalived-1.2.2]# make install
[root@host1 keepalived-1.2.2]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
[root@host1 keepalived-1.2.2]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
[root@host1 keepalived-1.2.2]# mkdir /etc/keepalived
[root@host1 keepalived-1.2.2]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
[root@host1 keepalived-1.2.2]# cp /usr/local/sbin/keepalived /usr/sbin/
2 、开启路由转发
[root@host1 ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
[root@host1 ~]# sysctl -p
3、配置Keepalived
[root@host1 ~]# less /etc/keepalived/keepalived.conf

  
 
 
  1. ! Configuration File for keepalived  
  2. global_defs {  
  3.    router_id LVS_MASTER         #备上修改LVS_BACKUP  
  4. }  
  5. vrrp_instance VI_1 {  
  6.     state MASTER                #备上修改BACKUP  
  7.     interface eth0  
  8.     virtual_router_id 51 
  9.     priority 100                #备上修改80  
  10.     advert_int 1 
  11.     authentication {  
  12.         auth_type PASS  
  13.         auth_pass 1111 
  14.     }  
  15.     virtual_ipaddress {  
  16.         10.0.0.10 
  17.     }  
  18. }  
  19.  
  20. vrrp_instance LAN_GATEWAY {  
  21.     state MASTER                 #备上修改BACKUP  
  22.     interface eth1  
  23.     virtual_router_id 52 
  24.     priority 100                 #备上修改80  
  25.     advert_int 1 
  26.     authentication {  
  27.         auth_type PASS  
  28.         auth_pass 1111 
  29.     }  
  30.     virtual_ipaddress {  
  31.         192.168.10.10 
  32.     }  
  33. }  
  34.  
  35. virtual_server 10.0.0.10 80 {  
  36.     delay_loop 6 
  37.     lb_algo rr  
  38.     lb_kind NAT  
  39. #    nat_mask 255.255.255.0  
  40. #    persistence_timeout 50  
  41.     protocol TCP  
  42.  
  43.     real_server 192.168.10.3 80 {  
  44.         weight 1 
  45.         TCP_CHECK {  
  46.         connect_timeout 10 
  47.         nb_get_retry 3 
  48.         connect_port 80 
  49.         }  
  50.     }  
  51.     real_server 192.168.10.4 80 {  
  52.         weight 1 
  53.         TCP_CHECK {  
  54.         connect_timeout 10 
  55.         nb_get_retry 3 
  56.         connect_port 80 
  57.         }  
  58.     }  
  59. }  

 BACKUP服务器同上配置,先安装lvs再按装keepalived,然后配置/etc/keepalived/keepalived.conf,只需将批注部分改一下即可。
4、分别在2台Real Server上面设置网关
把网关都设置成:192.168.10.10
5、在2台RealServer中分别配置HTTP
[root@host3 ~]# yum -y install httpd
[root@host3 ~]# cd /var/www/html/
[root@host3 html]# cat index.html
<h1>RealServer for 192.168.10.3</h1>
[root@host3 html]# /etc/init.d/httpd start
另一台机器配置一样,过程略、、、
6、测试:
分别启动Keepalived
[root@host1 ~]# chkconfig keepalived on
[root@host1 ~]# /etc/init.d/keepalived restart
[root@host1 ~]# 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.10:80 rr
  -> 192.168.10.3:80              Masq    1      0          0        
  -> 192.168.10.4:80              Masq    1      0          0 
先关闭LVS_Master的Keepalived,观察LVS_Backup的日志
[root@host2 ~]# tailf /var/log/messages
Mar 22 22:39:16 host2 Keepalived_vrrp: VRRP_Instance(LAN_GATEWAY) Transition to MASTER STATE
Mar 22 22:39:16 host2 Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Mar 22 22:39:17 host2 Keepalived_vrrp: VRRP_Instance(LAN_GATEWAY) Entering MASTER STATE
Mar 22 22:39:17 host2 Keepalived_vrrp: VRRP_Instance(LAN_GATEWAY) setting protocol VIPs.
Mar 22 22:39:17 host2 Keepalived_vrrp: VRRP_Instance(LAN_GATEWAY) Sending gratuitous ARPs on eth1 for 192.168.10.10
Mar 22 22:39:17 host2 Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Mar 22 22:39:17 host2 Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Mar 22 22:39:17 host2 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 10.0.0.10
Mar 22 22:39:17 host2 Keepalived_healthcheckers: Netlink reflector reports IP 192.168.10.10 added
Mar 22 22:39:17 host2 Keepalived_healthcheckers: Netlink reflector reports IP 10.0.0.10 added
Mar 22 22:39:22 host2 Keepalived_vrrp: VRRP_Instance(LAN_GATEWAY) Sending gratuitous ARPs on eth1 for 192.168.10.10
Mar 22 22:39:22 host2 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 10.0.0.10
在测试机*问:
在2台RealServer中分别配置HTTP
[root@host3 ~]# yum -y install httpd
[root@host3 ~]# cd /var/www/html/
[root@host3 html]# cat index.html
<h1>RealServer for 192.168.10.3</h1>
[root@host3 html]# /etc/init.d/httpd start
另一台机器配置一样,过程略、、、
7、测试:
分别启动Keepalived
[root@host1 ~]# chkconfig keepalived on
[root@host1 ~]# /etc/init.d/keepalived restart
[root@host1 ~]# 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.10:80 rr
  -> 192.168.10.3:80              Masq    1      0          0        
  -> 192.168.10.4:80              Masq    1      0          0 
先关闭LVS_Master的Keepalived,观察LVS_Backup的日志
[root@host2 ~]# tailf /var/log/messages
Mar 22 22:39:16 host2 Keepalived_vrrp: VRRP_Instance(LAN_GATEWAY) Transition to MASTER STATE
Mar 22 22:39:16 host2 Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Mar 22 22:39:17 host2 Keepalived_vrrp: VRRP_Instance(LAN_GATEWAY) Entering MASTER STATE
Mar 22 22:39:17 host2 Keepalived_vrrp: VRRP_Instance(LAN_GATEWAY) setting protocol VIPs.
Mar 22 22:39:17 host2 Keepalived_vrrp: VRRP_Instance(LAN_GATEWAY) Sending gratuitous ARPs on eth1 for 192.168.10.10
Mar 22 22:39:17 host2 Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Mar 22 22:39:17 host2 Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Mar 22 22:39:17 host2 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 10.0.0.10
Mar 22 22:39:17 host2 Keepalived_healthcheckers: Netlink reflector reports IP 192.168.10.10 added
Mar 22 22:39:17 host2 Keepalived_healthcheckers: Netlink reflector reports IP 10.0.0.10 added
Mar 22 22:39:22 host2 Keepalived_vrrp: VRRP_Instance(LAN_GATEWAY) Sending gratuitous ARPs on eth1 for 192.168.10.10
Mar 22 22:39:22 host2 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 10.0.0.10
在测试机*问:
CentOS6.2下搭建LVS(NAT)+Keepalived实现高性能高可用负载均衡服务器

启动LVS_Master,在查看LVS_Backup日志
[root@host2 ~]# tailf /var/log/messages
Mar 22 22:51:50 host2 Keepalived_vrrp: VRRP_Instance(LAN_GATEWAY) Received higher prio advert
Mar 22 22:51:50 host2 Keepalived_vrrp: VRRP_Instance(LAN_GATEWAY) Entering BACKUP STATE
Mar 22 22:51:50 host2 Keepalived_vrrp: VRRP_Instance(LAN_GATEWAY) removing protocol VIPs.
Mar 22 22:51:50 host2 Keepalived_vrrp: VRRP_Instance(VI_1) Received higher prio advert
Mar 22 22:51:50 host2 Keepalived_healthcheckers: Netlink reflector reports IP 192.168.10.10 removed
Mar 22 22:51:50 host2 Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
Mar 22 22:51:50 host2 Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs.
Mar 22 22:51:50 host2 Keepalived_healthcheckers: Netlink reflector reports IP 10.0.0.10 removed
一切访问正常~!

本文出自 “CrazyLinux工作室” 博客,请务必保留此出处http://crazylinux.blog.51cto.com/259244/813154