配置 LVS DR 负载均衡

  1. Director Server: 192.168.31.33/24
  2. Real Server 1: 192.168.31.31/24
  3. Real Server 2: 192.168.31.32/24
  4. VIP: 192.168.31.30/32

Director Server 上安装 ipvsadm

1
[email protected]:~# apt install ipvsadm

配置 vip

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
[email protected]:~# ip addr add 192.168.31.30/32 brd 192.168.31.30 dev ens32
[email protected]:~# ip addr show ens32
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:af:9c:1f brd ff:ff:ff:ff:ff:ff
    inet 192.168.31.33/24 brd 192.168.31.255 scope global ens32
       valid_lft forever preferred_lft forever
    inet 192.168.31.30/32 brd 192.168.31.30 scope global ens32
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:feaf:9c1f/64 scope link
       valid_lft forever preferred_lft forever

配置 ipvs 规则

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# 清空 ipvsadm 配置表
[email protected]:~# ipvsadm -C
# 设置连接超时值
[email protected]:~# ipvsadm --set 30 5 60
# 添加虚拟服务器
[email protected]:~# ipvsadm -A -t 192.168.31.30:80 -s rr -p 20
# 向虚拟服务器添加 RealServer
[email protected]:~# ipvsadm -a -t 192.168.31.30:80 -r 192.168.31.31:80 -g -w 1
[email protected]:~# ipvsadm -a -t 192.168.31.30:80 -r 192.168.31.32:80 -g -w 1
# 查看 lvs 状态
[email protected]:~# ipvsadm -L -n --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port               Conns   InPkts  OutPkts  InBytes OutBytes
  -> RemoteAddress:Port
TCP  192.168.31.30:80                    0        0        0        0        0
  -> 192.168.31.31:80                    0        0        0        0        0
  -> 192.168.31.32:80                    0        0        0        0        0

参数说明

  • -C: 清空 ipvs 所有规则
  • -A: 添加虚拟服务器
  • -t: 使用 tcp 协议, 后接虚拟服务器的 ip 地址和端口
  • -s: 使用的调度算法
  • -p: 定义持久时间,能够实现将来自同一个地址的请求始终发往同一个 RealServer
  • -a: 添加 RealServer
  • -r: 指定 RealServer 地址和端口
  • -g: 使用的 DR 实现方式
  • -w: RealServer 服务器的权重
1
[email protected]:~# ip addr add 192.168.31.30/32 brd 192.168.31.30 dev lo

禁止发送 arp 广播,由于 vip 在多台机器上配置了,如果不抑制广播会造成 ip 地址冲突。

1
2
3
4
[email protected]:~# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
[email protected]:~# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
[email protected]:~# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
[email protected]:~# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
1
curl 192.168.31.30

注意: 至此 lvs 负载均衡集群配置完成,由于 lvs 服务器本身不支持高可用,存在单点故障, 可以配合 keepalived 一起使用

lvs 的配置是终端上配置的,机器重启后会丢失,那么该如何管理配置呢? 有两种方法:

  1. ipvsadm 包自带配置信息管理工具,ipvsadm-save, ipvsadm-restore
  2. 开发脚本管理
  • ipvsadm-save: 用于导出 lvs 配置
  • ipvsadm-restore: 用于从文件中恢复 lvs 配置

也可以使用 ipvsadm 包中的脚本管理 /etc/init.d/ipvsadm

1
2
3
4
5
6
7
8
9
[email protected]:~# ipvsadm-save > /etc/ipvsadm.rules   # 等价于 /etc/init.d/ipvsadm save
[email protected]:~# ipvsadm-restore < /etc/ipvsadm.rules  # 等价于 /etc/init.d/ipvsadm load
[email protected]:~# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.31.30:80 rr persistent 20
  -> 192.168.31.31:80             Route   1      0          0
  -> 192.168.31.32:80             Route   1      0          0
 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
#!/bin/bash

INTERFACE=ens32
PORT=80
VIP=192.168.31.30
RIP=(
192.168.31.31
192.168.31.32
)

IPVSADM=/usr/sbin/ipvsadm

start() {
    ip addr add ${VIP}/32 brd ${VIP} dev $INTERFACE
    $IPVSADM -C
    $IPVSADM --set 30 5 60
    $IPVSADM -A -t ${VIP}:${PORT} -s rr -p 20
    for (( i = 0; i < echo ${#RIP[*]}; i++ )); do
        $IPVSADM -a -t ${VIP}:${PORT} -r ${RIP[$i]}:${PORT} -g -w 1
    done
    echo "The lvs Server is start!"
}

stop() {
    $IPVSADM -C
    ip addr del ${VIP}/32 brd ${VIP} dev $INTERFACE
    echo "The lvs Server is stop!"
}

status() {
    $IPVSADM -L -n --stats
}

case $1 in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop 
        sleep 1
        start
        ;;
    status)
        status
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart}"
        exit 2
        ;;
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
#!/bin/bash

VIP=192.168.31.30
INTERFACE=lo

case $1 in
  start)
      echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
      echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
      echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
      echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
      ip addr add ${VIP}/32 brd ${VIP} dev $INTERFACE
      echo "The RS Server is Ready!"
      ;;
      
  stop)
      ip addr del ${VIP}/32 brd ${VIP} dev $INTERFACE
      echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
      echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
      echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
      echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
      echo "The RS Server is Canceled!"
      ;;

  *)
      echo $"Usage: $0 {start|stop|restart}"
      exit 1
      ;;
esac