---
title: "Lvs 笔记"
date: 2019-10-30T12:55:50+08:00
lastmod: 2019-10-30T12:55:50+08:00
tags: ["lvs", "负载均衡"]
categories: ["ha/lb"]
---

## 环境
角色 | 地址
---- | ----
负载分配服务器 | 192.168.1.209
tomcat 服务器1 | 192.168.1.207
tomcat 服务器2 | 192.168.1.208
VIP | 192.168.1.250

## LVS-DR
- 配置负载分配服务器(192.168.1.209)
    ```bash
    #在与 tomcat 服务器连通的网卡(eth0)上配置虚拟 IP(192.168.1.250)
    ip addr add 192.168.1.250/32 brd 192.168.1.250 dev eth0
    ip route add 192.168.1.250 dev eth0
    #开启转发
    echo 1 > /proc/sys/net/ipv4/ip_forward
    #安装 ipvsadm
    yum install ipvsadm
    ipvsadm -C
    ipvsadm -A -t 192.168.1.250:80 -s rr
    ipvsadm -a -t 192.168.1.250:80 -r 192.168.1.207:80 -g
    ipvsadm -a -t 192.168.1.250:80 -r 192.168.1.208:80 -g
    ```
- 在两台 tomcat 服务器上都做如下配置
    ```bash
    #禁用 arp 响应
    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
    #添加虚拟 IP
    ip addr add 192.168.1.250/32 brd 192.168.1.250 dev lo
    ip route add 192.168.1.250 dev lo
    #在个人浏览器中访问 http://192.168.1.250/webapp/
    ```

