码迷,mamicode.com
首页 > 其他好文 > 详细

LVS+Keepalived实现高可用负载均衡

时间:2021-06-02 17:50:07      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:keepaliv   ali   interface   ifconf   ble   ifconfig   try   remote   ret   

测试环境

系统 IP 软件
Centos8 192.168.248.80 lvs+keepalived
Centos8 192.168.248.81 lvs+keepalived
Centos8 192.168.248.100 Web server
Centos8 192.168.248.101 Web server

vip暂定192.168.248.254

配置keepalived

master配置

[root@master ~]# yum install ipvsadm keepalived -y

[root@master ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id LVS_01 
}


vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 90   #虚拟路由 ID(0-255),在一个 VRRP 实例中主备服务器 ID 必须一样
    mcast_src_ip 192.168.248.80
    priority 100           # 优先级值设定:MASTER 要比 BACKUP 的值大
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        192.168.248.254
    }
}

virtual_server 192.168.248.254 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.248.100 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 192.168.248.101 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
[root@master ~]# systemctl start keepalived

 

 

backup配置

[root@backup ~]# yum install ipvsadm keepalived -y

[root@backup ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id LVS_02
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 90
    mcast_src_ip 192.168.248.81
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        192.168.248.254
    }
}

virtual_server 192.168.248.254 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.248.100 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 192.168.248.101 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

[root@backup ~]# systemctl start keepalived

 

配置 RS

在 RS 的lo网卡上配置  VIP

配置脚本,两台RS都需要

[root@RS1 ~]# cat /scripts/lvs_rs.sh 
#!/bin/bash
VIP=192.168.248.254
case "$1" in
start)
       ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
       /sbin/route add -host $VIP dev lo:0
       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
       sysctl -p >/dev/null 2>&1
       echo "RealServer Start OK"
       ;;
stop)
       ifconfig lo:0 down
       route del $VIP >/dev/null 2>&1
       echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
       echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
       echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
       echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
       echo "RealServer Stoped"
       ;;
*)
       echo "Usage: $0 {start|stop}"
       exit 1
esac
exit 0

#安装nginx测试
[root@RS1 ~]# yum -y install nginx
[root@RS1 ~]#  echo ‘192.168.248.100‘ /usr/share/nginx/html/index.html
[root@RS1 ~]# systemctl  start nginx

 

 检查测试

主节点查看ip,可以看到 VIP 目前已经正确配置在网卡上。

技术图片

 

 

输入ipvsadm -Ln 或watch ipvsadm -Ln --stats可以看到负责均衡状态

 [root@master ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.248.254:80 rr persistent 50
  -> 192.168.248.100:80           Route   1      0          0         
  -> 192.168.248.101:80           Route   1      0          0  

 

测试访问VIP

技术图片

 

 

手动停止一个 RS,再次访问 VIP,LVS 会自动剔除无法访问的服务,重启后,服务会被自动添加

[root@RS1 ~]# systemctl  stop nginx

技术图片

 

 

手动停止master上的 keepalived,模拟 master节点挂了,VIP 会自动转移到backup上。

技术图片

 

 在backup上查看技术图片

 

 

重启master后,由于master的优先级高于backup,此时vip会转移到master,从而实现 HA。

技术图片

 

LVS+Keepalived实现高可用负载均衡

标签:keepaliv   ali   interface   ifconf   ble   ifconfig   try   remote   ret   

原文地址:https://www.cnblogs.com/diqiyao/p/14828797.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!