nginx代理和并发和Keepalived高可用案例

这里写自定义目录标题

负载均衡,这里有个地方没有写=5,注意下图

nginx代理和并发和Keepalived高可用案例

nginx代理和并发和Keepalived高可用案例

安装 telnet
nginx代理和并发和Keepalived高可用案例
重启ng,并配置ip
nginx代理和并发和Keepalived高可用案例

nginx代理和并发和Keepalived高可用案例
nginx代理和并发和Keepalived高可用案例
地址hash
nginx代理和并发和Keepalived高可用案例
nginx代理和并发和Keepalived高可用案例
最大链接数
nginx代理和并发和Keepalived高可用案例
携带header
nginx代理和并发和Keepalived高可用案例
nginx代理和并发和Keepalived高可用案例
带主机名和ip
nginx代理和并发和Keepalived高可用案例
域名的拆分
nginx代理和并发和Keepalived高可用案例
nginx代理和并发和Keepalived高可用案例
nginx代理和并发和Keepalived高可用案例
include包含文件
nginx代理和并发和Keepalived高可用案例
proxy.conf代理设置
nginx代理和并发和Keepalived高可用案例
nginx代理和并发和Keepalived高可用案例
动静分离
nginx代理和并发和Keepalived高可用案例
通过扩展名实现动静分离,也阔以通过if判断
nginx代理和并发和Keepalived高可用案例
nginx代理和并发和Keepalived高可用案例
通过useragent进行判断
nginx代理和并发和Keepalived高可用案例

nginx代理和并发和Keepalived高可用案例
健康检查
nginx代理和并发和Keepalived高可用案例

高可用 Keepalived

高可用:两台业务系统启动着相同的服务,如果有一台故障,另一台自动接管,我们将将这个称之为高可用;
nginx代理和并发和Keepalived高可用案例

Keepalived 会生成一个虚拟IP,此虚拟IP与多台真实的服务器的IP绑定,当用户发来请求到虚拟IP时,请求先路由到主服务器,当主服务器宕机后,请求会路由到备用服务器,以达到高可用的目的。
nginx代理和并发和Keepalived高可用案例
Keepalived工作方式:抢占式、非抢占式
@1. yum安装:

yum install keepalived  -y

日志存放位置:/var/log/messages

@2. 官网
可以到Keepalived官网(https://www.keepalived.org)下载

tar -zxvf keepalived-2.2.4.tar.gz

@1. 安装依赖包
yum -y install libnl libnl-devel

yum install -y openssl openssl-devel

@2. 执行配置命令,进入目录执行
./configure --prefix=/usr/local/keepalived --sysconf=/etc

@3. 执行编译并安装命令
make && make install

@4. 安装成功后,在 /etc/keepalived 目录下,会看到 keepalived.conf 文件。

二. 方式1的配置

进入 /etc/keepalived 目录下,修改 keepalived.conf 文件。
A服务器配置如下:

! Configuration File for keepalived

global_defs {
   
   # 全局唯一的主机标识
   router_id server_a
   
}

vrrp_instance VI_1 {

    # 标识是主节点还是备用节点,值为 MASTER 或 BACKUP
    state MASTER
    # 绑定的网卡
    interface ens33
    # 虚拟路由id,保证主备节点是一致的
    virtual_router_id 51
    # 权重
    priority 100
    # 同步检查时间,间隔默认1秒
    advert_int 1
    # 认证授权的密码,所有主备需要一样
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    # 虚拟IP
    virtual_ipaddress {
        192.168.1.88
    }
}
B服务器配置如下:


! Configuration File for keepalived

global_defs {
   
   router_id server_b
   
}

vrrp_instance VI_1 {
    
    # 设置为备机
    state BACKUP
    interface ens33
    virtual_router_id 51
    # 权重设置的要比主机低
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    # 虚拟IP需要主备设置成一样
    virtual_ipaddress {
        192.168.1.88
    }
}
  1. 给出虚拟ip,并建立shell脚本,一定要给权限,一定要给权限nginx代理和并发和Keepalived高可用案例
    nginx代理和并发和Keepalived高可用案例

5、启动Keepalived

进入 /usr/local/keepalived/sbin 目录
./keepalived
或者
用脚本启动nginx   以及keepalived:

systemctl start keepalived.service

nginx代理和并发和Keepalived高可用案例
查看日志:
nginx代理和并发和Keepalived高可用案例
启动脚本, 已经帮我把nginx启动了。查看网卡生成的虚拟ip地址:192.168.116.15:
nginx代理和并发和Keepalived高可用案例
验证服务
nginx代理和并发和Keepalived高可用案例
nginx代理和并发和Keepalived高可用案例

6、停止A服务器,看虚拟IP是否路由到B服务器
经试验可知,A服务器关闭后,虚拟IP便路由到了B服务器。
nginx代理和并发和Keepalived高可用案例
最后修改nginx的代理指向这个虚拟IP即可

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;                           
    upstream server_pools {
      server 10.0.0.7:80;
      server 10.0.0.8:80;
    }
    server {
        listen 192.168.1.15:80;
        server_name www.etiantian.org;
        location / {
            proxy_pass http://server_pools;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $remote_addr;
        }
    } 

二. 方式2的配置

1、keepaliaved 抢占式配置

master配置

nginx代理和并发和Keepalived高可用案例

backup配置

nginx代理和并发和Keepalived高可用案例

2、Keepalived非抢占式配置:

非抢占式不再有主从之分,全部都为BACKUP,并且配置文件中添加nopreempt,用来标识为非抢占式;
第一台配置文件设置
nginx代理和并发和Keepalived高可用案例
第二台配置文件设置
nginx代理和并发和Keepalived高可用案例

3、nginx+keepalived

实现思路:将keepalived 中的vip作为nginx负载均衡的监听地址,并且域名绑定的也是vip的地址。说明:Nginx 负载均衡实现高可用,需要借助Keepalived地址漂移功能。
nginx代理和并发和Keepalived高可用案例
两台负载均衡配置:
nginx代理和并发和Keepalived高可用案例
nginx代理和并发和Keepalived高可用案例

4、keepalived脑裂现象

由于某些原因,导致两台keepalived高可用服务器在指定时间内,无法检测到对方存活心跳信息,从而导致互相抢占对方的资源和服务所有权,然而此时两台高可用服务器有都还存活。

可能出现的原因:

  • 1、服务器网线松动等网络故障;
  • 2、服务器硬件故障发生损坏现象而崩溃;
  • 3、主备都开启了firewalld 防火墙。
  • 4、在Keepalived+nginx 架构中,当Nginx宕机,会导致用户请求失败,但是keepalived不会进行切换,

! 所以需要编写一个检测nginx的存活状态的脚本,如果nginx不存活,则kill掉宕掉的nginx主机上面的keepalived。(所有的keepalived都要配置)
nginx代理和并发和Keepalived高可用案例
脚本如下:
nginx代理和并发和Keepalived高可用案例
配置文件如下:
nginx代理和并发和Keepalived高可用案例

多实例的配置

nginx代理和并发和Keepalived高可用案例

编写监控脚本,check_www.sh

vim check_www.sh



#!/bin/bash

while true
do
if [ `ps -ef |grep nginx |grep -v grep |wc -l` -lt 2 ]
then
   /etc/init.d/keepalived stop
   exit
fi
done


chomod +x check_www.sh

2.7.1 lb01的keepalived配置文件

[root@lb01 scripts]# cat  /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   router_id lb01
}

vrrp_script check {
   script "/server/scripts/check_www.sh"
   interval 2 
   weight 2
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.0.0.3/24 dev eth0 label eth0:1
    }
    track_script {
       check
    }
}
vrrp_instance VI_2 {
    state BACKUP
    interface eth0
    virtual_router_id 52
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.0.0.4/24 dev eth0 label eth0:2
    }
}

2.7.2 修改lb02的keepalived配置文件

[root@lb02 conf]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   router_id lb02
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
     10.0.0.3 dev eth0 label eth0:1
    }
}
vrrp_instance VI_2 {
    state MASTER
    interface eth0
    virtual_router_id 52
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
     10.0.0.4 dev eth0 label eth0:2
    }
}

修改nginx配置文件,让bbs 与www分别监听不同的ip地址

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;                           
    upstream server_pools {
      server 10.0.0.7:80;
      server 10.0.0.8:80;
      server 10.0.0.9:80;
    }
    server {
        listen 10.0.0.3:80;
        server_name www.etiantian.org;
        location / {
            proxy_pass http://server_pools;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $remote_addr;
        }
    } 
    server {
        listen 10.0.0.4:80;
        server_name bbs.etiantian.org;
        location / {
            proxy_pass http://server_pools;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $remote_addr;
        }
    } 
}

nginx代理和并发和Keepalived高可用案例



上一篇:凸包 和 旋转卡壳(模板)


下一篇:dependency check工具的使用