手机与PC端分离访问及负载均衡高可用

手机与PC访问分离

环境

主机 ip
Proxy 10.0.0.6
web01 10.0.0.7
web02 10.0.0.8

配置web01访问页面为iphone页面

[root@web01 conf.d]# cat sj.conf 
server {
    listen 80;
    server_name sj.oldboy.com;

    location / {
        root /sj;
        index index.html;
    }
}
[root@web01 conf.d]# mkdir /sj
[root@web01 conf.d]# echo "Iphone....." >/sj/index.html
[root@web01 conf.d]# systemctl restart nginx

配置web02为android访问页面

[root@web02 conf.d]# cat sj.conf 
server {
    listen 80;
    server_name sj.oldboy.com;

    location / {
        root /sj;
        index index.html;
    }
}
[root@web02 conf.d]# mkdir /sj
[root@web02 conf.d]# echo "Android...." > /sj/index.html
[root@web02 conf.d]# systemctl restart nginx

通过负载均衡分流

[root@lb01 ~]# cd /etc/nginx/conf.d/
[root@lb01 conf.d]# cat sj_proxy.conf 
upstream iphone {
    server 172.16.1.7:80;
}

upstream android {
    server 172.16.1.8:80;
}

server {
    listen 80;
    server_name sj.oldboy.com;
    location / {
        if ($http_user_agent ~* "iphone"){
            proxy_pass http://iphone;
        }
        if ($http_user_agent ~* "android"){
            proxy_pass http://android;
        }
    }
}
[root@lb01 conf.d]# systemctl restart nginx

访问测试

nginx+keepalive实现负载均衡高可用

环境

服务器系统 角色 外网IP 内网IP
CentOS7.5 keepalived-master eth0:10.0.0.5 eth1:172.16.1.5
CentOS7.5 keepalived-slave eth0:10.0.0.6 eth1:172.16.1.6

在lb01和lb02分别安装keepalived

[root@lb01 ~]# yum install keepalived -y  
[root@lb02 ~]# yum install keepalived -y

配置lb01-keepalive-master

[root@lb01 ~]# cat /etc/keepalived/keepalived.conf
global_defs {     
    router_id lb01   
}  
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 50
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
}
    virtual_ipaddress {
        10.0.0.3
    }
}

配置lb02-keepalive-backup

[root@lb02 ~]# cat /etc/keepalived/keepalived.conf
global_defs {     
    router_id lb02   
}  
vrrp_instance VI_1 {
    state BAKCUP
    interface eth0
    virtual_router_id 50
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
}
    virtual_ipaddress {
        10.0.0.3
    }
}

keepalived配置文件含义

image

列脑故障排查思路

当keepalive-backup服务器无法联系主服务器时会响应虚拟ip,此时两台服务器同时响应虚拟ip,导致ip无法连接的现象为列脑。

常见列脑故障原因

服务器网线松动等网络故障
服务器硬件故障发生损坏现象而崩溃
主备都开启firewalld防火墙
Nginx服务死掉等

解决思路

在备上编写检测脚本, 测试如果能ping通主并且备节点还有VIP的话则认为产生了列脑
1.编写脚本

[root@lb02 ~]# cat check_split_brain.sh
#!/bin/sh
lb01_vip=10.0.0.3
lb01_ip=10.0.0.5
while true;do
    ping -c 2 -W 3 $lb01_ip &>/dev/null
    if [ $? -eq 0 -a `ip add|grep "$lb01_vip"|wc -l` -eq 1 ];then
        echo "ha is split brain.warning."
    else
        echo "ha is ok"
    fi
sleep 5
done

2.执行脚本
通过screen命令在后台执行该脚本即可
如果Nginx宕机, 会导致用户请求失败, 但Keepalived并不会进行切换, 所以需要编写一个脚本检测Nginx的存活状态, 如果不存活则kill nginx和keepalived

[root@lb01 ~]# mkdir /server/scripts
[root@lb01 ~]# vim /server/scripts/check_web.sh
#!/bin/sh
#使用while死循环
while true;do
nginxpid=$(ps -C nginx --no-header|wc -l)
#1.判断Nginx是否存活,如果不存活则尝试启动Nginx
if [ $nginxpid -eq 0 ];then
    systemctl start nginx
    sleep 5
    #2.5秒后再次获取一次Nginx状态
    nginxpid=$(ps -C nginx --no-header|wc -l) 
    #3.再次进行判断, 如Nginx还不存活则停止Keepalived,让地址进行漂移,并退出脚本  
    if [ $nginxpid -eq 0 ];then
        systemctl stop keepalived
        exit 1
   fi
fi
     sleep 5
done

[root@lb01 ~]# chmod +x /server/scripts/check_web.sh

在keepalived配置文件中调用此脚本,lb01与lb02都需操作(并将脚本中的循环取消掉)

[root@lb02 ~]# cat /etc/keepalived/keepalived.conf
global_defs {     
    router_id lb02   
}  
vrrp_script check_web {
   script "/server/scripts/check_web.sh"
   interval 2
   weight 50
}
vrrp_instance VI_1 {
    state BAKCUP
    interface eth0
    virtual_router_id 50
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
}
    virtual_ipaddress {
        10.0.0.3
    }
}

你可能感兴趣的:(手机与PC端分离访问及负载均衡高可用)