1.基本架设示意图
2.真实节点的安装与配置
在192.168.157.163、192.168.157.164和192.168.157.165 三个节点上安装nginx,并且配置节点的本地回环接口,因为我们采用的是LVS的DR模式。
以192.168.157.163节点为例
cd /etc/sysconfig/network-scripts
复制该目录的ifcfg-lo文件,命名为ifcfg-lo:1
cp ifcfg-lo ifcfg-lo:1
编辑ifcfg-lo:1文件
DEVICE=lo:1
# 虚拟ip地址
IPADDR=192.168.157.169
NETMASK=255.255.255.255
NETWORK=127.0.0.0
# If you're having problems with gated making 127.0.0.0/8 a martian,
# you can change this to something else (255.255.255.255, for example)
BROADCAST=127.255.255.255
ONBOOT=yes
NAME=loopback
重启network服务
service network restart
查看配置结果
ip addr
说明配置成功,其他两个节点如上配置。
3.LVS+Keepalived安装与配置
keepalived下载地址:https://www.keepalived.org/download.html
本文以keepalived-2.0.20.tar.gz为例。
将keepalived-2.0.20.tar.gz安装到192.168.157.161和192.168.157162节点上,并且注册为系统服务。具体操作请查看博主上篇博文,这里就不做多的描述了。
主节点(192.168.157.161)配置keepalived
cd /etc/keepalived
编辑keepalived.conf文件
vim keepalived.conf
配置如下:
! Configuration File for keepalived
global_defs {
# LVS_161名字自定义,保证同一网络下唯一
router_id LVS_161
}
vrrp_instance VI_1 {
# 主节点
state MASTER
# 是自己节点的网卡的名称,通过 ip addr查看
interface ens33
# 主备节点不相同且在0-255之间就行
virtual_router_id 161
# 权重
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
# 虚拟ip
192.168.157.169
}
}
# 配置集群地址访问的IP+端口,端口和nginx保持一致,都是80
virtual_server 192.168.157.169 80 {
# 健康检查的时间,单位:秒
delay_loop 6
# 配置负载均衡的算法,默认是轮询
lb_algo rr
# LVS模式为DR
lb_kind DR
# 设置会话持久化的时间
persistence_timeout 10
# 协议 -t
protocol TCP
# 负载均衡的真实服务器,也就是nginx节点的具体的真实ip地址
real_server 192.168.157.163 80 {
# 轮询的默认权重配比设置为1
weight 1
# 健康检查
TCP_CHECK {
# 检查的端口
connect_port 80
# 超时时间 2s
connect_timeout 2
# 重试次数 2次
nb_get_retry 2
#间隔时间 3s
delay_before_retry 3
}
}
real_server 192.168.157.164 80 {
weight 1
TCP_CHECK {
# 检查的端口
connect_port 80
# 超时时间 2s
connect_timeout 2
# 重试次数 2次
nb_get_retry 2
#间隔时间 3s
delay_before_retry 3
}
}
real_server 192.168.157.165 80 {
weight 1
TCP_CHECK {
# 检查的端口
connect_port 80
# 超时时间 2s
connect_timeout 2
# 重试次数 2次
nb_get_retry 2
#间隔时间 3s
delay_before_retry 3
}
}
}
备节点(192.168.157.162)配置keepalived
cd /etc/keepalived
编辑keepalived.conf文件
vim keepalived.conf
配置如下:
! Configuration File for keepalived
global_defs {
# LVS_162名字自定义,保证同一网络下唯一
router_id LVS_162
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 162
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.157.169
}
}
# 配置集群地址访问的IP+端口,端口和nginx保持一致,都是80
virtual_server 192.168.157.169 80 {
# 健康检查的时间,单位:秒
delay_loop 6
# 配置负载均衡的算法,默认是轮询
lb_algo rr
# LVS模式为DR
lb_kind DR
# 设置会话持久化的时间
persistence_timeout 10
# 协议 -t
protocol TCP
# 负载均衡的真实服务器,也就是nginx节点的具体的真实ip地址
real_server 192.168.157.163 80 {
# 轮询的默认权重配比设置为1
weight 1
# 健康检查
TCP_CHECK {
# 检查的端口
connect_port 80
# 超时时间 2s
connect_timeout 2
# 重试次数 2次
nb_get_retry 2
#间隔时间 3s
delay_before_retry 3
}
}
real_server 192.168.157.164 80 {
weight 1
TCP_CHECK {
# 检查的端口
connect_port 80
# 超时时间 2s
connect_timeout 2
# 重试次数 2次
nb_get_retry 2
#间隔时间 3s
delay_before_retry 3
}
}
real_server 192.168.157.165 80 {
weight 1
TCP_CHECK {
# 检查的端口
connect_port 80
# 超时时间 2s
connect_timeout 2
# 重试次数 2次
nb_get_retry 2
#间隔时间 3s
delay_before_retry 3
}
}
}
安装LVS
yum install -y ipvsadm
在真实节点上启动nginx,在主节点或者备节点上查看配置是否OK
ipvsadm -Ln
这样LVS+Keepalived+nginx高可用集群负载均衡就搭建完成了。
本文地址:https://blog.csdn.net/qq_34399639/article/details/107333457
您可能感兴趣的文章:
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!
网友评论