天天看点

负载均衡集群

利用 LVS + KeepAlived 架构,构建 Load Balancer

LVS 官方文档:http://www.linuxvirtualserver.org/zh/lvs1.html

LVS 集群技术:http://zh.linuxvirtualserver.org/node/95

keepalived 官网:http://www.keepalived.org/index.html

环境准备

内容
Virtual IP (VIP) 172.16.0.245
Load Balancer 172.16.0.238
Real Server1 172.16.0.244
Real Server2 172.16.0.246

系统必要配置

systemctl stop firewalld

systemctl disable firewalld

yum install -y net-tools

yum install -y vim bash-completion

  • 关闭集群内每台节点的防火墙,禁用 SELinux
  • 安装 net-tools 软件包
  • 安装 vim 及 bash-completion 软件包(可选)

集群软件部署

LVS 由两部分组成,IPVS 模块及IPVS 管理工具

lsmod | grep ip_vs

yum install -y ipvsadm

ipvsadm 具体使用说明可通过 ipvsadm --help 命令查看

yum install -y keepalived

修改 virtual_ipaddress 部分,将 eth0 修改成你具体的网卡接口

vrrp_instance VI_1 {
    ...
    interface eth0
    ...
    virtual_ipaddress {
        172.16.0.245 dev eth0
    }
}      

配置 virtual_server 及 real_server 部分:

virtual_server 172.16.0.245 80 {
    delay_loop 6
    lb_algo rr     // 算法采用 (Round Robin, rr) 轮循算法进行 load balance
    lb_kind DR     // NAT, DR, TUNNEL 模式中的 DR (直接路由) 模式
    nat_mask 255.255.255.0
    # persistence_timeout 50     // 注释掉该行,意思是保持单台 real_server 的长连接时间
    protocol TCP

    real_server 172.16.0.244 80 {
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 172.16.0.246 80 {
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}      

systemctl restart keepalived

systemctl enable keepalived

ipvsadm -l -n

初始状态

IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.16.0.245:80 rr
  -> 172.16.0.244:80              Route   1      0          0         
  -> 172.16.0.246:80              Route   1      0          0      

此时说明 lvs 已经配置好了,提供 172.16.0.245 VIP 访问,后端两台服务器 172.16.0.244 及 172.16.0.246 提供实际服务,使用的是 rr 轮循算法进行请求分配。

  1. LVS 已经被编译进内核,应用时自动加载,不需要手动加载
  2. 在 Load Balancer 机器上安装 ipvs 管理工具 ipvsadm
  3. 在 Load Balancer 机器上安装 keepalived 软件
  4. 配置 

    /etc/keepalived/keepalived.conf

     文件
  5. 启动 keepalived 服务
  6. 检查 lvs 状态

配置 Real Server

根据文档,两台 real server 需要进行内核参数调整,达到 ARP hidden,以下操作需要在每台 real server 上执行

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

内核参数生效

sysctl -p

修改 

/etc/sysconfig/network-scripts/ifcfg-lo

 配置文件,添加 VIP 信息:

IPADDR0=172.16.0.245
NETMASK0=255.255.255.255      

重启网络

systemctl restart network

  1. 修改 

    /etc/sysctl.conf

     文档,添加如下内容:
  2. 将 lo 回环口配成 VIP

客户端测试

在网络内其他机器执行:

curl http://172.16.0.245

转载于:https://my.oschina.net/91devel/blog/2878273

继续阅读