CentOS 7 LVS+Keepalived实现Nginx的高可用以及负载均衡

在Linux下有很多服务可以实现负载均衡,比如Nginx、HAProxy、LVS等服务,但是对于高可用服务,我们在Linux下最常见也是应用最多的是Keepalived,对于这些综合服务对于一个管理员来说是必须要会的,所以今天我们主要介绍的是CentOS7+Nginx+Keepalived实现WEB服务的高可用负载均衡。

一、准备工作:关闭防火墙,selinux以免对实验结果造成影响,准备虚机,设置IP地址、主机名

hostname:Nginx01
IP:192.168.1.87
Role:Nginx Server
hostname:Nginx02
IP: 192.168.1.88
Role:Nginx Server
hostname:LVS01
IP: 192.168.1.89
Role:LVS+Keepalived
hostname:LVS02
IP: 192.168.1.90
Role:LVS+Keepalived
VIP:192.168.1.253

二、安装相关软件、以及配置文件修改:LVS、keepalived、nginx 该文档软件安装全部使用yum
1、nginx01 以及nginx02 都执行
yum install -y nginx
 
#安装以后启动nginx
 
systemctl start nginx.service

修改测试页面内容便于区分,根据主机名修改index.html文件

vim /usr/share/nginx/html/index.html

CentOS 7 LVS+Keepalived实现Nginx的高可用以及负载均衡


修改完访问页面如下:

CentOS 7 LVS+Keepalived实现Nginx的高可用以及负载均衡

nginx两台服务器准备完成,在LVS01、LVS02上安装LVS以及keepalived
yum install -y ipvsadm
#安装keepalived依赖的软件
yum install -y gcc openssl openssl-devel
yum install -y keepalived

安装完成以后,LVS01上先清空keepalived的配置文件
:>/etc/keepalived/keepalived.conf
复制下面内如到 /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lvs_clu_1
}
virrp_sync_group Prox {
group {
mail
}
}
vrrp_instance mail {
state MASTER
interface eno16777736
lvs_sync_daemon_interface eno16777736
virtual_router_id 50
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.253
}
}
virtual_server 192.168.1.253 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 0
protocol TCP
nat_mask 255.255.255.0 
real_server 192.168.1.87 80 {
weight 1 
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.1.88 80 {
weight 1  
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}

在LVS02上也先清空keepalived的配置文件
:>/etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lvs_clu_1
}
virrp_sync_group Prox {
group {
mail
}
}
vrrp_instance mail {
state BACKUP
interface ens33
lvs_sync_daemon_interface ens33
virtual_router_id 50
priority 60
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.253
}
}
virtual_server 192.168.1.253 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 0
protocol TCP
real_server 192.168.1.87 80 {
weight 1 
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.1.88 80 {
weight 1 
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}

配置完成以后在两台机器上启动 keepalived
注意 interface 需要修改为自己电脑的网卡

systemctl start keepalived

三、测试结果
1、正常情况下

CentOS 7 LVS+Keepalived实现Nginx的高可用以及负载均衡

2、当nginx01宕机以后(在nginx01上执行命令),结果如图:

systemctl stop nginx.service

CentOS 7 LVS+Keepalived实现Nginx的高可用以及负载均衡

nginx01启动以后,在nginx02上停止nginx,效果雷同,不在贴图。
3、当LVS01上keepalived死掉以后,LVS02上的keepalived会启动配置VIP到自己机器上
systemctl stop keepalived

CentOS 7 LVS+Keepalived实现Nginx的高可用以及负载均衡

CentOS 7 LVS+Keepalived实现Nginx的高可用以及负载均衡

CentOS 7 LVS+Keepalived实现Nginx的高可用以及负载均衡

结果显示正常,LVS01上keepalived启动以后会把VIP配置到自己的网卡。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/e182671de36ec00e25fd009298491c7a.html