# LVS

《LVS + Keepalived + DR》

* LVS主机：192.168.2.100
* RS主机 ：192.168.2.104，192.168.2.105
* VIP：192.168.2.110

## LVS主机

### 1、安装ipvsadm

```
$ yum -y install ipvsadm
```

### 2、安装keepalived

```
$ yum -y install keepalived
```

### 3、配置keepalived

编辑`/etc/keepalived/keepalived.conf`文件，内容如下

```
global_defs {
    notification_email {
        acassen@firewall.loc
        failover@firewall.loc
        sysadmin@firewall.loc
    }
    notification_email_from Alexandre.Cassen@firewall.loc
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER      #备用服务器上为 BACKUP
    interface eth0    # 网卡
    virtual_router_id 110    # 注意不要和其他keepalived集群冲突
    priority 100      #优先级，数值越大优先级越高；备用服务器上为90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.2.110
    }
}

virtual_server 192.168.2.110 80 {
    delay_loop 6            #(每隔6秒查询realserver状态，是否存活)
    lb_algo wlc                 #(加权轮询算法)
    lb_kind DR                #(DR模式)
    persistence_timeout 0    #(同一IP的连接多少秒内被分配到同一台realserver，0表示不连接)
    protocol TCP             #(用TCP协议检查realserver状态)

    real_server 192.168.2.104 80 {
        weight 100            #(权重)
        TCP_CHECK {
            connect_timeout 10     #(10秒无响应超时)
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
    real_server 192.168.2.105 80 {
         weight 100
         TCP_CHECK {
             connect_timeout 10
             nb_get_retry 3
             delay_before_retry 3
             connect_port 80
         }
    }
}

virtual_server 192.168.2.110 81 {
    delay_loop 6
    lb_algo wlc
    lb_kind DR
    persistence_timeout 0
    protocol TCP

    real_server 192.168.2.104 81 {
        weight 100
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 81
        }
    }
    real_server 192.168.2.105 81 {
         weight 100
         TCP_CHECK {
             connect_timeout 10
             nb_get_retry 3
             delay_before_retry 3
             connect_port 81
         }
    }
}
```

### 4、启动keepalived

```
$ systemctl enable keepalived && systemctl start keepalived
```

## RS主机

编辑`/etc/sysconfig/network-scripts/ifcfg-lo`，内容更改为如下，把lo网卡的IP配置为VIP

```
DEVICE=lo
IPADDR=192.168.2.110
NETMASK=255.255.255.255
BROADCAST=192.168.2.110
ONBOOT=yes
NAME=loopback
```

然后添加路由（实践中发现该步骤不操作也没问题）

```
$ route add -host 192.168.2.110 dev lo
```

修改`/etc/sysctl.conf`，在文件尾部添加如下内容，然后执行命令

```
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_announce = 2
```

然后执行以下命令生效

```
$ sysctl -p
```

## Reference

* <https://www.cnblogs.com/MacoLee/p/5856858.html>
* <https://blog.51cto.com/cuchadanfan/1661576>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://pshizhsysu.gitbook.io/linux/lvs.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
