代理和负载均衡的区别
代理负责把连接请求直接转发到后台某个web节点
负载均衡负责把请求使用某种调度算法分散发布给后台所有web节点
---------------------------------------------------------------------------------------------------------------------------------
负载均衡(Load Balance),简写LB
面对高并发web请求,使用各种调度算法(rr,wrr,lc,wlc,ip_hash),分散转发到后台web群集节点,提高数据吞吐量,高容灾
常见的LB:
软件:lvs nginx haproxy
硬件:F5
云LB:阿里云SLB 腾讯云CLB 青云QLB ucloud ULB
四层负载:ip地址 tcp/udp 端口号
七层负载:HTTP https ftp SMTP
---------------------------------------------------------------------------------------------------------------------------------
高可用-增加容错性(HA:High availability)
协议:VRRP(虚拟路由冗余协议) 公有协议 224.0.0.18
HSRP(热备份路由协议) 私有协议,Cisco公司
高可用软件:
keepalived: 使用vrrp实现多台主机高可用群集
高可用角色:master 主服务器
backup 备服务器
---------------------------------------------------------------------------------------------------------------------------------
负载均衡实验步骤
实验规划
lb1 192.168.1.1 centos 7.9
lb2 192.168.1.2 centos 7.9
web1 192.168.1.3 centos 7.9
web2 192.168.1.4 centos 7.9
---------------------------------------------------------------------------------------------------------------------------------
实验前提
将所有服务器关闭防火墙和selinux
systemctl stop firewalld
setenforce 0
---------------------------------------------------------------------------------------------------------------------------------
1.将web1和web2装好nginx服务,并且修改网页内容
yum -y install epel-release
yum -y install nginx
echo "web1" > /usr/share/nginx/html/ (web2就改成web2,别的操作和web1相同)
systemctl start nginx
systemctl enable nginx
---------------------------------------------------------------------------------------------------------------------------------
2.配置nginx负载均衡服务器
yum -y install epel-release
yum -y install nginx
systemctl start nginx
systemctl enable nginx
---------------------------------------------------------------------------------------------------------------------------------
3.扩展:创建优化项文件,网站配置文件直接调用(选做,但推荐)
vim /etc/nginx/nginx_params
添加:
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;
保存退出
---------------------------------------------------------------------------------------------------------------------------------
4.修改lb1的配置文件,添加负载均衡功能
vim /etc/nginx//
修改为:
upstream web_cluster {
server 192.168.1.3:80;
server 192.168.1.4:80;
}
server {
listen 80;
server_name ;
location / {
proxy_pass http://web_cluster;
include nginx_params;
}
}
保存退出
重启nginx:systemctl restart nginx
客户端访问验证,浏览器如果判断不出来,就看web节点上的日志
---------------------------------------------------------------------------------------------------------------------------------
此时,负载均衡服务已实现,但是要做高可用的话,还需要再加一台负载均衡服务器,并且和第一台负载均衡服务器的配置一样。请往下看
---------------------------------------------------------------------------------------------------------------------------------
高可用配置
1.安装第二台负载均衡服务器
yum -y install epel-release
yum -y install nginx
systemctl start nginx
systemctl enable nginx
---------------------------------------------------------------------------------------------------------------------------------
2.将第一台负载均衡服务器的优化项文件和文件都发送到第二台负载均衡服务器上
scp -r /etc/nginx/nginx_params root@192.168.1.2:/etc/nginx/
scp -r /etc/nginx// root@192.168.1.2:/etc/nginx//
---------------------------------------------------------------------------------------------------------------------------------
3.重启第二台负载均衡服务器
systemctl restart nginx
---------------------------------------------------------------------------------------------------------------------------------
4.安装keepalived(两台都装)
yum -y install keepalived
---------------------------------------------------------------------------------------------------------------------------------
5.配置keepalived
主服务器:lb1
vim /etc/keepalived/
修改为:
global_defs {
router_id lb1
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.254
}
}
保存退出
启动服务:systemctl restart keepalived
备服务器:lb2
vim /etc/keepalived/
修改为:
global_defs {
router_id lb2 #路由id号,和主服务器必须不同
}
vrrp_instance VI_1 {
state BACKUP #状态:BACKUP备 MASTER主
interface ens33 #指定网卡名字
virtual_router_id 51
priority 99 #优先级:备比主要小
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.254 #虚拟路由ip,公共ip
}
}
保存退出
启动服务:systemctl restart keepalived
---------------------------------------------------------------------------------------------------------------------------------
6.查看虚拟ip(漂移ip地址)
ip a show dev ens33
---------------------------------------------------------------------------------------------------------------------------------
7.客户端修改hosts文件,访问验证(访问成功,关闭主服务器,再访问)(客户端:随便拿一台测试就行)
vim /etc/hosts
修改为:
192.168.1.254
保存退出
---------------------------------------------------------------------------------------------------------------------------------
到这里呢,我们这个负载均衡以及高可用都完成了,但是这个高可用呢,会存在一些问题,比如脑裂现象.
---------------------------------------------------------------------------------------------------------------------------------
高可用裂脑
高可用节点之间互相失去联系,自认为自己是主服务器,就会出现多主现象,即裂脑现象
裂脑出现的原因:
心跳线松动或网卡故障
服务器硬件故障,崩溃
节点服务器开启防火墙,却没有做vrrp例外
nginx服务死掉,不会出现裂脑现象,但整个集群都无法正常运作
---------------------------------------------------------------------------------------------------------------------------------
面对裂脑现象 相关的一些措施--请往下看
1.检测裂脑脚本(在备用服务器:192.168.1.2运行)
vim split_brain.sh
#!/bin/sh
while true
do
ping -c 2 -W 3 192.168.1.1 &> /dev/null
if [ $? -eq 0 -a `ip add|grep 192.168.1.254|wc -l` -eq 1 ]
then
echo "split brain....."
else
echo "HA is ok"
fi
sleep 5
done
保存退出
chmod +x split_brain.sh
source split_brain.sh
开启防火墙验证:systemctl start firewalld
解决因为防火墙出现的裂脑现象:
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --destination 224.0.0.18 --protocol vrrp -j ACCEPT
firewall-cmd --reload
---------------------------------------------------------------------------------------------------------------------------------
2.解决nginx故障造成群集无法工作
编辑nginx监控脚本
vim /sh/check_nginx_proxy.sh
#!/bin/bash
killall -0 nginx
if [ $? -ne 0 ];then
systemctl stop keepalived
fi
添加脚本追踪模块到keepalived配置文件
vim /etc/keepalived/
global_defs {
router_id lb1
}
vrrp_script check_nginx_proxy {
script “/sh/check_nginx_proxy.sh”
interval 2
weight 5
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.254
}
track_script {
check_nginx_proxy
}
}
保存退出
重启服务:systemctl restart keepalived
---------------------------------------------------------------------------------------------------------------------------------
nginx四层负载的做法
1.配置4层负载均衡,发布内部服务器的ssh和mysql
lb:192.168.1.10
web1: 192.168.1.20
mysql: 192.168.1.30
2.修改配置文件
vim /etc/nginx/
插入数据到http字段上方:
stream {
upstream sshweb1 {
server 192.168.1.20:22;
}
upstream mysql {
server 192.168.1.30:3306;
}
server {
listen 5555;
proxy_pass sshweb1;
proxy_connect_timeout 30;
proxy_timeout 60;
}
server {
listen 7777;
proxy_pass mysql;
proxy_connect_timeout 30;
proxy_timeout 60;
}
}
保存退出
重启服务:systemctl restart nginx
3.客户端访问验证ssh:
xshell: ssh root@192.168.8.129 5555
linux: ssh root@192.168.8.129 -p 5555
4.客户端访问验证mysql:
安装navicat,建立连接192.168.8.129,端口7777