Keppalive负载均衡


简介

Keepalived的作用是检测服务器的状态,如果有一台web服务器宕机,或工作出现故障,Keepalived将检测到,并将有故障的服务器从系统中剔除,同时使用其他服务器代替该服务器的工作,当服务器工作正常后Keepalived自动将服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的服务器。

工作原理

keepalived是以VRRP协议为实现基础的,VRRP全称Virtual Router Redundancy Protocol,即虚拟路由冗余协议。

虚拟路由冗余协议,可以认为是实现路由器高可用的协议,即将Ñ台提供相同功能的路由器组成一个路由器组,这个组里面有一个主和多个备份,主上面有一个对外提供服务的VIP(该路由器所在局域网内其他机器的默认路由为该VIP),主会发组播,当备份收不到VRRP包时就认为主宕掉了,这时就需要根据VRRP优先的级来选举一个备份当master。这样的话就可以保证路由器的高可用了。

KEEPALIVED主要有三个模块,分别是核心,检查和vrrp.core模块为KEEPALIVED的核心,负责主进程的启动,维护以及全局配置文件的加载和解析。检查负责健康检查,包括常见的各种检查方式。 VRRP模块是来实现VRRP协议的。

Keppalive基本配置

安装

yum -y install keepalived*     #安装

修改主配置文件

vi /etc/keepalived/keepalived.conf

global_defs {
    router_id LVS_TEST # 服务器名字
}

vrrp_instance VI_1 {
    state MASTER # 配置主备,备用机此配置项为BACKUP
    interface ens33 # 指定接口
    virtual_router_id 51 # 指定路由ID,主备必须一样
    priority 99 # 设置优先级,主略高于备份
    advert_int 1 # 设置检查时间
    authentication {
        auth_type PASS # 设置验证加密方式
        auth_pass 1111 # 设置验证密码
    }
    virtual_ipaddress {
        172.20.10.10
    }
}

关闭防火墙

systemctl stop firewalld
setenforce 0

启动keepalived服务

systemctl enable keepalived
systemctl start keepalived

LVS(DR模式)+Keepalive双击热备份

准备工作

server1 192.168.2.168
server2 192.168.2.169
主Keepalave 192.168.2.170
备Keepalive 192.168.2.171

配置server1

yum -y install epel-release
yum -y install nginx

systemctl start nginx
systemctl enable nginx

mv /usr/share/nginx/html/index.html /usr/share/nginx/html/1111
echo "server 1111" > /usr/share/nginx/html/index.html

systemctl stop firewalld
setenforce 0

yum -y install net-tools*
ifconfig lo:1 192.168.2.100 broadcast 192.168.2.100 netmask 255.255.255.255 up
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

配置Server2

yum -y install epel-release
yum -y install nginx

systemctl start nginx
systemctl enable nginx

mv /usr/share/nginx/html/index.html /usr/share/nginx/html/2222
echo "server 22222" > /usr/share/nginx/html/index.html

systemctl stop firewalld
setenforce 0

yum -y install net-tools*
ifconfig lo:1 192.168.2.100 broadcast 192.168.2.100 netmask 255.255.255.255 up
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

主Keepalive服务器

安装Keepalive

yum -y install keepalive*

修改配置文件

mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak    #备份主配置文件

vi /etc/keepalived/keepalived.conf
global_defs {
    router_id test 	#服务器名字
}

vrrp_instance VI_1 {
    state MASTER 	#配置主备,备用机此配置项为BACKUP
    interface ens33 	#指定接口
    virtual_router_id 51 	#指定路由ID,主备必须一样
    priority 101 	#设置优先级,主略高于备份
    advert_int 1 	#设置检查时间
    authentication {
        auth_type PASS 	#设置验证加密方式
        auth_pass 1234 	#设置验证密码
    }
    virtual_ipaddress {
        192.168.2.100
    }
}

virtual_server 192.168.2.100 80 {
    delay_loop 15 	#健康检查时间
    lb_algo rr 	#LVS调度算法
    lb_kind DR 	#LVS工作模式
    !persistence 60 	#是否保持连接,!不保持
    protocol TCP 	#服务采用TCP协议
    real_server 192.168.2.168 80 {
        weight 1 	#权重
        TCP_CHECK { 	#TCP检查
            connect_port 80 	#检查端口80
            connect_timeout 3 	#超时时间3秒
            nb_get_retry 3 	#重试次数3次
            delay_before_retry 4 	#重试间隔4秒
        }
    }
    real_server 192.168.2.169 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 4
        }
    }
}

启动服务

systemctl start keepalived
systemctl enable keepalived

yum -y install ipvsadm
modprobe ip_vs

关闭防火墙

systemctl stop firewalld
setenforce 0

备keepalive服务器

安装Keepalive

yum -y install keepalive*

修改主配置文件

mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak    #备份主配置文件

vi /etc/keepalived/keepalived.conf
global_defs {
    router_id test 	#服务器名字
}

vrrp_instance VI_1 {
    state MASTER 	#配置主备,备用机此配置项为BACKUP
    interface ens33 	#指定接口
    virtual_router_id 51 	#指定路由ID,主备必须一样
    priority 99	#设置优先级,主略高于备份
    advert_int 1 	#设置检查时间
    authentication {
        auth_type PASS 	#设置验证加密方式
        auth_pass 1234 	#设置验证密码
    }
    virtual_ipaddress {
        192.168.2.100
    }
}

virtual_server 192.168.2.100 80 {
    delay_loop 15 	#健康检查时间
    lb_algo rr 	#LVS调度算法
    lb_kind DR 	#LVS工作模式
    !persistence 60 	#是否保持连接,!不保持
    protocol TCP 	#服务采用TCP协议
    real_server 192.168.2.168 80 {
        weight 1 	#权重
        TCP_CHECK { 	#TCP检查
            connect_port 80 	#检查端口80
            connect_timeout 3 	#超时时间3秒
            nb_get_retry 3 	#重试次数3次
            delay_before_retry 4 	#重试间隔4秒
        }
    }
    real_server 192.168.2.169 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 4
        }
    }
}

启动服务

systemctl start keepalived
systemctl enable keepalived

yum -y install ipvsadm
modprobe ip_vs

关闭防火墙

systemctl stop firewalld
setenforce 0

测试

curl 192.168.2.100

ipvsadm -ln

或者测试keepalive

systemctl stop keepalved     #关闭主Keepalive服务器

ip a                  #在备份Keepalive服务器上查看IP地址和网站数量
ipvsadm -ln

LVS(NAT模式)+keepalive双击热备份

准备工作

server1 192.168.2.177
server2 192.168.2.169
主keepalive 192.168.2.179(添加外网网卡 192.168.43.162)
从keepalive 192.168.2.175(添加外网网卡 192.168.43.180)

配置server1

yum -y install epel-release
yum -y install nginx

systemctl start nginx
systemctl enable nginx

mv /usr/share/nginx/html/index.html /usr/share/nginx/html/1111
echo "server 1111111" > /usr/share/nginx/html/index.html

systemctl stop firewalld
setenforce 0

vi /etc/sysconfig/network-scripts/ifcfg-ens33
BOOTPROTO=static
IPADDR=192.168.2.177
NETMASK=255.255.255.0
GATEWAY=192.168.2.100

配置server2

yum -y install epel-release
yum -y install nginx

systemctl start nginx
systemctl enable nginx

mv /usr/share/nginx/html/index.html /usr/share/nginx/html/22222
echo "server 2222222" > /usr/share/nginx/html/index.html

systemctl stop firewalld
setenforce 0

vi /etc/sysconfig/network-scripts/ifcfg-ens33
BOOTPROTO=static
IPADDR=192.168.2.169
NETMASK=255.255.255.0
GATEWAY=192.168.2.100

主keepalive

添加网卡

添加外部网卡(192.168.43.162)

安装keepalive服务

yum -y install keepalived

修改主配置文件

mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
vi /etc/keepalived/keepalived.conf
global_defs {
    router_id test
}

vrrp_instance VI_1 {
    state MASTER          #主keepalived服务
    interface ens33		  #第二块网卡名
    virtual_router_id 51
    priority 101		  #主keepalived优先级
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1234
    }
    virtual_ipaddress {
        192.168.2.100
    }
}

vrrp_instance VI_2 {
    state MASTER		#主keepalived服务
    interface ens37		#第二块网卡名
    virtual_router_id 51
    priority 101		#主keepalived优先级
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1234
    }
    virtual_ipaddress {
        192.168.43.100
    }
}

virtual_server 192.168.43.100 80 {
    delay_loop 15
    lb_algo rr
    lb_kind NAT
    !persistence 60
    protocol TCP
    real_server 192.168.2.177 80 {
        weight 1
        TCP_CHECK {
        connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 4
        }
    }
    real_server 192.168.2.169 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 4
        }
    }
}

启动服务

systemctl start keepalived
systemctl enablekeepalived

systemctl stop firewalld
setenforce 0

配置ipvsadm

yum -y install ipvsadm
modprobe ip_vs
echo "1" > /proc/sys/net/ipv4/ip_forward    #NAT模式要开启路由转发功能

ipvsadm -ln

从keepalive

添加网卡

添加外部网卡(192.168.43.180)

安装keepalived服务

yum -y install keepalived

修改主配置文件

mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
vi /etc/keepalived/keepalived.conf
global_defs {
    router_id test
}

vrrp_instance VI_1 {
    state BACKUP		#从keepalived服务
    interface ens33		#第二块网卡名
    virtual_router_id 51
    priority 99			#从keepalived优先级,小于主优先级
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1234
    }
    virtual_ipaddress {
        192.168.2.100
    }
}

vrrp_instance VI_2 {
    state BACKUP		#从keepalived服务
    interface ens37		#第二块网卡名
    virtual_router_id 51
    priority 99			#从keepalived优先级,小于主优先级
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1234
    }
    virtual_ipaddress {
        192.168.43.100
    }
}

virtual_server 192.168.43.100 80 {
    delay_loop 15
    lb_algo rr
    lb_kind NAT
    !persistence 60
    protocol TCP
    real_server 192.168.2.177 80 {
        weight 1
        TCP_CHECK {
        connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 4
        }
    }
    real_server 192.168.2.169 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 4
        }
    }
}

启动服务

systemctl start keepalived
systemctl enablekeepalived

systemctl stop firewalld
setenforce 0

配置ipvsadm

yum -y install ipvsadm
modprobe ip_vs
echo "1" > /proc/sys/net/ipv4/ip_forward    #NAT模式要开启路由转发功能

ipvsadm -ln

测试

curl 192.168.43.100       #这需要外网测试

http://192.168.43.100
03-01 23:31