美文网首页
搭建LVS负载均衡集群

搭建LVS负载均衡集群

作者: 右耳菌 | 来源:发表于2022-08-06 00:49 被阅读0次

1. 负载均衡集群介绍

  • 主流开源软件LVS、keepalived、haproxy、nginx等。
  • LVS属于4层负载均衡,Nginx属于7层负载均衡,Haproxy 既可以认为是4层,也可以做7层使用。
  • LVS这种4层的负载均衡是可以分发除80外的其他端口通信的,比如MySQL的,而Nginx仅仅支持HTTP、HTTPS、MAIL; Haproxy也支持MySQL这种。
  • 相比较来说,LVS这种4层的更稳定,能承受更多的请求,而Nginx这种7层的更加灵活,能实现更多个性化需求。

2. LVS简介

  • LVS是由国人章文嵩开发。
  • LVS是基于TCP/IP做的路由和转发,稳定性和效率很高。
  • LVS是工作于Linux内核中的。
  • LVS有三种常见的模式:NAT、DR、IP Tunnelo
  • LVS架构中有一个核心角色叫做分发器(Load Balancer),它用来分发用户的请求。

一个LVS集群往往包含以下角色:

  1. DS: Director Server。指的是前端负载均衡器节点。
  2. RS: Real Server。后端真实的工作服务器。
  3. VIP:向外部直接面向用户请求,作为用户请求的目标的IP地址。
  4. DIP: Director Server lP,主要用于和内部主机通讯的IP地址。
  5. RIP: Real ServerlP,后端服务器的IP地址。
  6. CIP: Client lP,访问客户端的IP地址。

3. LVS NAT模式 - 改IP实现

  • 这种模式借助iptables的nat表来实现。
  • 用户的请求到分发器后,通过预设的iptables规则,把请的数据包转发到后端的RS上去。
  • RS需要设定网关为分发器的内网IP。
  • 用户请求的数据包和返回给用户的数据包全部经过分发器,所以分发器成为瓶颈。
  • 在nat模式中,只需要分发器有公网IP即可,所以比较节省公网IP资源。

4. LVS IP Tunnel 模式 - 封装报文实现

  • 这种模式需要有一个公共的IP配置在分发器和所有的RS上我们把它叫做VIP。
  • 客户端请求的目标lP为VIP,分发器接收到请求数据包后,会对数据包做一个加工,会把目标IP改为RS的IP,这样数据包
    就到了RS上。
  • RS接收数据包后,会还原原始数据包,这样目标IP为VIP,因为所有RS上配置了这个VIP,所以它会认为是它自己。

5. LVS DR模式 - 改MAC地址实现

  • 这种模式,也需要有一个公共的IP配置在分发器和所有RS上,也就是VIP。
  • 和IP Tunnel不同的是,它会把数据包的MAC地址修改为RS的MAC地址。
  • RS接收数据包后,会还原原始数据包,这样目标IP为VIP,因为所有RS上配置了这个VIP,所以它会认为是它自己。



6. LVS调度算法

针对不同的网络服务需求和服务器配置,IPVS调度器实现了如下八种负载调度算法:

  • 轮叫(Round Robin)
  • 加权轮叫(Weighted Round Robin)
  • 最少链接(Least Connections)
  • 加权最少链接(Weighted Least Connections)
  • 基于局部性的最少链接(Locality-Based Least Connections)
  • 带复制的基于局部性最少链接((Locality-Based Least Connections with Replication)
  • 目标地址散列(Destination Hashing)
  • 源地址散列(Source Hashing)

7. LVS 集群搭建

环境准备:

  • 虚拟机:Oracle VM VirtualBox 5.2.22
  • 虚拟主机:(example)
    • Host1-vbox: Ubuntu 18.04、192.168.3.101
    • Host2-vobx: Ubuntu 18.04、192.168.3.102
    • Host3-vbox: Ubuntu 18.04、192.168.3.103
    • Host4-vbox: Ubuntu 18.04、192.168.3.104
  • 系统服务:LVS,Keepalived
  • Web服务:Nginx
  • 集群搭建:LVS DR模式
7.1 安装ipvs 和 keepalived

根据需要可能需要 sudo

yum -y install ipvsadm 
yum -y install keepalived

or

apt install ipvsadm 
apt install keepalived
7.2 查看本机内核是否支持ipvs
sudo lsmod | grep ip_vs
7.3 创建keepalived.conf
vim /etc/keepalived/keepalived.conf
# Global Configuration
global_defs{
  lvs_id director1
}

vrrp_instance LVS {
  state MASTER
  interface eth0   #对应虚拟机的网卡,使用ifconfig查看
  virtual_router_id 100    #这个数值 master和slave必须统一
  priority 151     #这个数值决定哪台服务器是master 
  advert_int 1
  authentication {
        auth_type PASS 
        auth_pass 123456
  }
  virtual_ipaddress {
        192.168.3.110
  }
}

#Virtual Server Configuration - for www server
virtual_server 192.168.3.110 80 {
  delay_loop 6
  lb_algo rr  # 负载均衡逻辑策略 rr 表示轮叫(轮询)
  lb_kind DR  # 负载均衡模式DR

  # 同一个IP地址,同一个子集,在同一段时间返回的Real Server IP一样 
  # 0 表示每次返回都不相同,60表示一分钟
  # persistence_timeout 50
  protocol TCP # 协议TCP

  # Real Server 1  configuration
  real_server 192.168.146.139 80 {
    weight 1
    TCP_CHECK {
      connect_timeout 3
      nb_get_retry 3
      delay_before_retry 3
    }
  }
  # Real Server 2  configuration
  real_server 192.168.146.140 80 {
    weight 1
    TCP_CHECK {
      connect_timeout 3
      nb_get_retry 3
      delay_before_retry 3
    }
  }
}

host2 配置基本一致

# Global Configuration
global_defs{
  lvs_id director2
}

vrrp_instance LVS {
  state MASTER
  interface eth0   #对应虚拟机的网卡,使用ifconfig查看
  virtual_router_id 100    #这个数值 master和slave必须统一
  priority 150     #这个数值决定哪台服务器是master 
  advert_int 1
  authentication {
        auth_type PASS 
        auth_pass 123456
  }
  virtual_ipaddress {
        192.168.3.110
  }
}

#Virtual Server Configuration - for www server
virtual_server 192.168.3.110 80 {
  delay_loop 6
  lb_algo rr  # 负载均衡逻辑策略 rr 表示轮叫(轮询)
  lb_kind DR  # 负载均衡模式DR

  # 同一个IP地址,同一个子集,在同一段时间返回的Real Server IP一样 
  # 0 表示每次返回都不相同,60表示一分钟
  # persistence_timeout 50
  protocol TCP # 协议TCP

  # Real Server 1  configuration
  real_server 192.168.146.139 80 {
    weight 1
    TCP_CHECK {
      connect_timeout 3
      nb_get_retry 3
      delay_before_retry 3
    }
  }
  # Real Server 2  configuration
  real_server 192.168.146.140 80 {
    weight 1
    TCP_CHECK {
      connect_timeout 3
      nb_get_retry 3
      delay_before_retry 3
    }
  }
}

7.4 修改Nginx配置
  • host3
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
  worker_connections 768;
  # multi_accept on;
}

http {
  server {  
    listen 80; # 监听80端口
    
    location / {
      default_type text/html;
      return 200 "Hello, Nginx! Server host3@192.168.3.103";
    }
  }
}
  • host4
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
  worker_connections 768;
  # multi_accept on;
}

http {
  server {  
    listen 80; # 监听80端口
    
    location / {
      default_type text/html;
      return 200 "Hello, Nginx! Server host4@192.168.3.104";
    }
  }
}

可以使用curl -i 192.168.3.103/104 测试是否访问正常

7.5 创建lvrs文件 host3 和 host4 都需要
vim emacs /etc/init.d/lvsrs
# 如果要关闭则全部写 echo "0"
#!/bin/bash  
ifconfig lo:192.168.3.110 netmask 255.255.255.255 broadcast 192.168.3.110 up route add -host 192.168.3.110 dev lo:0
echo "0" > /proc/sys/net/ipv4/ip_forward
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

exit 0

将以上文件设置成可执行的

sudo chmod +x  /etc/init.d/lvsrs

然后执脚本即可

sudo /etc/init.d/lvsrs
7.6 启动host1 和 host2 的keepalived即可
sudo service keepalived start

查看

sudo ipvsadm -ln

如果觉得有收获,欢迎点赞和评论,更多知识,请点击关注查看我的主页信息哦~

相关文章

网友评论

      本文标题:搭建LVS负载均衡集群

      本文链接:https://www.haomeiwen.com/subject/figvwrtx.html