keepalived整合lvs实现nginx高可用负载均衡

1.简介

LVS(Linux Virtual Server) 是Linux虚拟服务器,可以把多台服务器虚拟为一个虚拟IP,同时实现各种负载均衡算法

Keepalived是基于LVS,并与LVS高度融合的,监控LVS下所有真实服务器健康状态,并通过健康健康状态动态调整LVS的复杂均衡转发权重和规则


2,拓扑图

193952209.jpg


3,IP规划

名称
IP
备注
VIP
eth2:192.168.46.200/24
虚拟IP
主调度器(director server )
eth2:192.168.46.131/24 负责lvs调度
辅调度器(director server )
eth2:192.168.46.132/24 负责lvs调度

真实服务器1

(Real Server )

eth2:192.168.46.133/24 nginx服务器

真实服务器2

(Real Server )

eth2:192.168.46.134/24 nginx服务器


4,安装lvs和keepalived包

这里不在介绍了,大家可以看看我的另外的两篇文章

1
2
http: //wolfword.blog.51cto.com/blog/4892126/1218416
http: //wolfword.blog.51cto.com/blog/4892126/1217585


5,配置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
主调度器 192.168 . 46.131 上安装keepalived,辅助调度器 192.168 . 46.132 上安装keepalived,不需要在real server上安装keepalived
vim   /etc/keepalived/keepalived.conf
global_defs {
   notification_email {
   root@localhost
   }
   notification_email_from  root@localhost
   smtp_server  127.0 . 0.1
   smtp_connect_timeout  30
   router_id LVS_DEVEL_131
}
vrrp_sync_group VG1 {
        group {
               VI_1
        }
}
vrrp_instance VI_1 {
   state MASTER    //辅助调度器为BACKUP
   interface  eth2
   virtual_router_id  51
   priority  100   //辅助调度器为90
   advert_int  1
   authentication {
     auth_type PASS
     auth_pass  1111
   }
   virtual_ipaddress {
     192.168 . 46.200 / 24
   }
}
virtual_server  192.168 . 46.200   80   {
    delay_loop  6
    lb_algo  rr
    lb_kind  DR
    protocol TCP
    real_server  192.168 . 46.133  80  {
    weight  100
    TCP_CHECK {
     connect_timeout   3
     nb_get_retry   3
     delay_beforce_retry   3
      connect_port   80
    }
}
    real_server  192.168 . 46.134  80  {
     weight   100
     TCP_CHECK {
       connect_timeout  3
       nb_get_retry  3
       delay_beforce_retry   3
       connect_port   80
     }
   }
}

6,配置lvs

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
主辅调度器上的lvs脚本
#!/bin/bash
VIP= 192.168 . 46.200
RIP1= 192.168 . 46.133
RIP2= 192.168 . 46.134
case  "$1"  in
start)
echo  "start LVS of DirectorServer DR"
/sbin/iptables -F
/sbin/ipvsadm -C
/sbin/ifconfig eth2: 0  $VIP  broadcast $VIP  netmask  255.255 . 255.0   up
/sbin/ipvsadm  -A -t $VIP: 80   -s rr
/sbin/ipvsadm  -a -t $VIP: 80   -r  $RIP1 -g
/sbin/ipvsadm  -a -t $VIP: 80   -r  $RIP2 -g
/sbin/ipvsadm
;;
stop)
echo  "stop  LVS of DirectorServer DR"
echo  "0"  >/proc/sys/net/ipv4/ip_forward
/sbin/ipvsadm -C
/sbin/ifconfig eth2: 0  down
;;
*)
echo  "argements error"
;;
esac
真实服务器real server  上的lvs脚本
#!/bin/bash
#DR server
VIP= 192.168 . 46.200
case  "$1"  in
start)
echo  "start LVS of DR"
/sbin/ifconfig lo: 0  $VIP broadcast $VIP netmask  255.255 . 255.255  up
/sbin/route add  -host  $VIP dev lo: 0
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
;;
stop)
echo  "stop LVS of DR"
/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  "Usage:$0 {start|stop}"
esac
exit  1


7,日志讲解

1
2
3
4
5
6
7
8
Sep  30  20 : 50 : 24  test2 Keepalived_vrrp: Opening file  '/etc/keepalived/keepalived.conf' .
Sep  30  20 : 50 : 24  test2 Keepalived_vrrp: Configuration  is  using :  65227  Bytes
Sep  30  20 : 50 : 24  test2 Keepalived_vrrp: Using LinkWatch kernel netlink reflector...
Sep  30  20 : 50 : 24  test2 Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
Sep  30  20 : 50 : 24  test2 Keepalived_vrrp: VRRP sockpool: [ifindex( 4 ), proto( 112 ), fd( 10 , 11 )]
Sep  30  20 : 51 : 50  test2 Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Sep  30  20 : 51 : 50  test2 Keepalived_vrrp: VRRP_Group(VG1) Syncing instances to MASTER state
Sep  30  20 : 51 : 51  test2 Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE


8,功能测试

停掉主调度器keepalived服务,来看看辅助调度器上的日志

1
2
3
4
5
6
7
Oct   1  21 : 02 : 07  test2 avahi-daemon[ 4276 ]: Received response from host  192.168 . 139.1  with  invalid source port  56132  on  interface  'eth1.0'
Oct   1  21 : 02 : 09  test2 avahi-daemon[ 4276 ]: Received response from host  192.168 . 139.1  with  invalid source port  56132  on  interface  'eth1.0'
Oct   1  21 : 02 : 09  test2 Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Oct   1  21 : 02 : 10  test2 Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Oct   1  21 : 02 : 10  test2 Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Oct   1  21 : 02 : 10  test2 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth2  for  192.168 . 46.200
Oct   1  21 : 02 : 11  test2 avahi-daemon[ 4276 ]: Received response from host  192.168 . 139.1  with  invalid source port  56132  on  interface  'eth1.0'