LVS集群负载均衡

安装工具

yum -y install  tallipvsadm    #提供ipvsadm 命令工具

ipvsadm  -A -t 192.168.8.5:80  -s wrr    #创建LVS虚拟集群服务器,算法为极权轮询wrr

ipvsadm -Ln        #查看LVS规则表


-A 添加虚拟服务器    -E 修改虚拟服务器    -D 删除虚拟服务器    -C 清空所有
-a 添加真是服务器    -e 修改真实服务器    -d 删除真实服务器    -L 查看LVS规则表
-s [rr|wrr|lc|wlc|sh]   设定集群算法【轮询 | 加权轮询 | 最少连接 | 加权最少连接 | 源地址散列 】

-t tcp协议    -u udp协议

ipvsadm -e -t|u 192.168.5.4:80 -r 192.168.2.100 [-g|i|m]  [-w 权重]

-g DR直连路由模式    -i 隧道模式    -m NAT转发模式



echo 1 > /proc/sys/net/ipv4/ip_foward
#开启调度器路由转发功能

echo "net.ipv4.ip_forward = 1 " >> /etc/sysctl.conf
#修改配置文件,设置永久规则

ipvsadm -A -t 192.168.4.5:80 -s wrr  #创建集群服务器

ipvsadm -a -t 192.168.4.5:80 -r 192.168.2.100 -w 1 -m??????????

ipvsadm -Ln  #查看LVS规则

ipvsadm-save -n > /etc/sysconfig/ipvsadm??????????

LVS-DR 直连模式配置

cd /etc/sysconfig/network-scripts/
cp ifcfg-eth0{,:0}
vim ifcfg-eht0
TYPE=Ethernet
BOOTPROTO=none
NAME=eth0
SEVICE=eth0
ONBOOT=yes
IPADDR=192.168.4.5
RPEFIX=24

vim ifcfg-eth0:0
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
NAME=eth0:0
DEVICE=eth0:0
ONBOOT=yes
IPADDR=192.168.4.15
PREFIX=24

systemctl stop NetworkManager    
systemctl restart network
cd /etc/sysconfig/network-scripts/
cp ifcfg-lo{,:0}
vim ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.4.15
NETMASK=255.255.255.255
NETWORK=192.168.4.15
BROADCAST=192.168.4.15
NOBOOT=yes
NAME=lo:0
vim /etc/sysctl.conf

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

#目的是为了访问192.168.4.15的数据包,只有调度器会响应,其他主机都不做任何响应,防止地址冲突的问题

sysctl -p  #重新加载配置文件/刷新

高可用集群Keepalived

yum -y install  keepalived

vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {            #设置报警收件人邮箱
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc    #设置发件人
   smtp_server 192.168.200.1            #定义邮箱服务器
   smtp_connect_timeout 30
   router_id LVS_DEVEL                   #设置路由ID
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {
    state MASTER                        #主服务器为MASTER(被服务器修改为BACKUP)
    interface eth0                      #定义网络接口
    virtual_router_id 51                #主备服务器VRID号必须一致
    priority 100                        #服务器优先级,优先级高 优先获得VIP
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111                  #主备服务器密码必须一致
    }
    virtual_ipaddress {                 #谁是主服务器谁获得该VIP
        192.168.200.16
        192.168.200.17
        192.168.200.18
    }
}
virtual_server 192.168.200.100 443 {
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    persistence_timeout 50
    protocol TCP

    real_server 192.168.201.100 443 {
        weight 1
        SSL_GET {
            url {
              path /
              digest ff20ad2481f97b1754ef3e12ecd3a9cc
            }
            url {
              path /mrtg/
              digest 9b3a0c85a887a256d6939da88aabd8cd
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

virtual_server 10.10.10.2 1358 {        #设置ipvsadm的VIP规则
    delay_loop 6
    lb_algo rr                            #设置LVS调度算法为轮询
    lb_kind NAT                            #设置LVS的模式为DR
    persistence_timeout 50
    protocol TCP

    sorry_server 192.168.200.200 1358

    real_server 192.168.200.2 1358 {            #设置后端web服务器真是IP
        weight 1                            #设置权重
        HTTP_GET {
            url {
              path /testurl/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url {
              path /testurl2/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url {
              path /testurl3/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

 real_server 192.168.200.3 1358 {
        weight 1
        HTTP_GET {
            url {
              path /testurl/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334c
            }
            url {
              path /testurl2/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334c
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

virtual_server 10.10.10.3 1358 {
    delay_loop 3
    lb_algo rr
    lb_kind NAT
    persistence_timeout 50
    protocol TCP

    real_server 192.168.200.4 1358 {
        weight 1
        HTTP_GET {
            url {
              path /testurl/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
 }
            url {
              path /testurl2/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url {
              path /testurl3/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 192.168.200.5 1358 {
        weight 1
        HTTP_GET {
            url {
              path /testurl/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url {
              path /testurl2/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url {
              path /testurl3/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
             connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}


systemctl  start keepalived  

iptables -F
setenforce 0

Keepalived + lvs 

nmcli connetion modify eth0 ipv4.method manual ipv4.addresses 192.168.4.100/24 connetion.autoconnet yes

nmcli connetion up eth0

vim /etc/sysconfig/network-scripts/ifcfg-lo:0    #配置web1本地回环子接口

vim /etc/sysctl.conf        #配置

HAproxy

yum -y install haproxy

vim /etc/haproxy/haproxy.cfg

#---------------------------------------------------------------------
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    log         127.0.0.1 local2            #err warning info debug

    chroot      /var/lib/haproxy            
    pidfile     /var/run/haproxy.pid
    maxconn     4000                        #最大连接数,默认4000
    user        haproxy
    group       haproxy
    daemon                                #创建1个进程进入deamon模式运行

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode                    http            #默认的模式mode{tcp|http|health}
    log                     global
    option                  httplog            #日志类被http日志格式
    option                  dontlognull        #不记录健康检查的日志信息
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch        #
    retries                 3                    #3次连接失败就认为服务不可用
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000            #最大连接数
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  main *:5000
    acl url_static       path_beg       -i /static /images /javascript /stylesheets
    acl url_static       path_end       -i .jpg .gif .png .css .js

    use_backend static          if url_static
    default_backend             app

#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
    balance     roundrobin
    server      static 127.0.0.1:4331 check

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
    balance     roundrobin
    server  app1 127.0.0.1:5001 check
    server  app2 127.0.0.1:5002 check
    server  app3 127.0.0.1:5003 check
    server  app4 127.0.0.1:5004 check


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值