CentOS7 搭建LVS

LVS是linux virtual server的简写linux虚拟服务器,是一个虚拟的服务器集群系统,可以再unix/linux平台下实现负载均衡集群功能。该项目在1998年5月由章文嵩博士组织成立。LVS是4层负载均衡。
官网文档:
http://www.linuxvirtualserver.org/zh/lvs1.html
http://www.linuxvirtualserver.org/zh/lvs2.html
http://www.linuxvirtualserver.org/zh/lvs3.html
http://www.linuxvirtualserver.org/zh/lvs4.html

一、实验环境

服务器 操作系统 IP
keepalived CentOS7 10.10.10.30
lvs1 CentOS7 10.10.10.140
lvs2 CentOS7 10.10.10.150

二、keepalived安装

yum install ipvsadm -y
yum install -y gcc openssl openssl-devel  -y
wget http://www.keepalived.org/software/keepalived-1.2.19.tar.gz  
tar -zxvf keepalived-1.2.19.tar.gz 
cd keepalived-1.2.19
./configure --sysconf=/etc --prefix=/usr/local/keepalived
make
make install
ln -s /usr/local/keepalived/etc/keepalived/ /etc/
ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/  

开启路由转发

cat /proc/sys/net/ipv4/ip_forward
echo 1 > /proc/sys/net/ipv4/ip_forward
cat /proc/sys/net/ipv4/ip_forward

获取健康检查hash

/usr/local/keepalived/bin/genhash -s 10.10.10.140 -p 80 -u /keep.html
/usr/local/keepalived/bin/genhash -s 10.10.10.150 -p 80 -u /keep.html

配置

1.配置keepalived

# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
  router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER
    interface eno33554984
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.10.10.250
    }
}

virtual_server 10.10.10.250 80 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    nat_mask 255.255.255.0
    persistence_timeout 50
    protocol TCP

    real_server 10.10.10.140 80 {
        weight 1
        HTTP_GET {
            url {
              path /keep.html
              digest eff5bc1ef8ec9d03e640fc4370f5eacd
            }
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }

    real_server 10.10.10.150 80 {
        weight 1
        HTTP_GET {
            url {
              path /keep.html
              digest eff5bc1ef8ec9d03e640fc4370f5eacd
            }
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
}

测试

lsmod |grep ip_vs                    ##查看内核中是否已加载ip_vs模块
ip_vs                 136798  0 
nf_conntrack          105702  7 ip_vs,nf_nat,nf_nat_ipv4,nf_nat_ipv6,xt_conntrack,nf_conntrack_ipv4,nf_conntrack_ipv6
libcrc32c              12644  2 xfs,ip_vs
systemctl start keepalived      #/etc/init.d/keepalived restart

在keepalived上使用ipvsadm命令

yum install -y ipvsadm
ipvsadm -Ln

说明
配置lvs的时候,不能直接配置内核中的ipvs,需要使用ipvs的管理工具ipvsadm进行管理。

术语

名称 缩写 全拼 说明
VIP 虚拟IP virtual ip address VIP为Director用于向客户端计算机提供服务的IP地址,如:www.test.com域名就要解析到vip上提供服务
RIP 真实IP地址 Real Server Ip Address 在集群下面节点上使用的IP地址,物理IP地址
DIP Director Director IP Address 用于连接内外网络的IP地址,物理网卡上的IP地址,是负载均衡器上的IP
CIP 客户端主机地址 Client IP address 客户端用户计算机请求集群服务器的IP地址,该地址用作发送给集群的请求的源IP地址

LVS转发原理
LVS负载均衡器接受所有入站请求,并根据调度算法决定哪个realserver处理该请求。

LVS调度算法
- 轮询 rr
- 加权轮询 wrr
- 最小连接数 lc
- 加权最小连接数 wlc

LVS转发模式
- NAT network Address Translation
- DR Direct Routing
- TUN

参考:
http://www.cnblogs.com/zhaijunming5/p/6019885.html

你可能感兴趣的:(运维-CentOS/RHEL)