七、Nginx的高可用集群

1、什么是 nginx 高可用


2、配置高可用的准备工作
(1)需要两台服务器 192.168.17.129 和 192.168.17.131
(2)在两台服务器安装 nginx
(3)在两台服务器安装 keepalived

3、在两台服务器安装 keepalived
(1)使用 yum 命令进行安装 yum install keepalived –y


(2)安装之后,在 etc 里面生成目录 keepalived,有文件 keepalived.conf

4、完成高可用配置(主从配置)
(1)修改/etc/keepalived/keepalivec.conf 配置文件

global_defs { 
   notification_email { 
     [email protected] 
     [email protected] 
     [email protected] 
   } 
   notification_email_from [email protected] 
   smtp_server 192.168.17.50
   smtp_connect_timeout 30 
   router_id LVS_DEVEL 
} 

vrrp_script chk_http_port { 

   script "/usr/local/src/nginx_check.sh" 

   interval 2      #(检测脚本执行的间隔) 

   weight 2 
}

vrrp_instance VI_1 { 
    state MASTER   # 备份服务器上将 MASTER 改为 BACKUP   
    interface ens33  //网卡 
    virtual_router_id 51   # 主、备机的 virtual_router_id 必须相同 
    priority 100     # 主、备机取不同的优先级,主机值较大,备份机值较小 
    advert_int 1 
    authentication { 
        auth_type PASS 
        auth_pass 1111 
    } 
    virtual_ipaddress { 
        192.168.17.50 // VRRP H 虚拟地址 
    } 
} 

(2)在/usr/local/src 添加检测脚本:nginx_check.sh

#!/bin/bash 
A=`ps -C nginx –no-header |wc -l` 
if [ $A -eq 0 ];then 
    /usr/local/nginx/sbin/nginx 
    sleep 2 
    if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then 
        killall keepalived 
    fi 
fi 

(3)把两台服务器上 nginx 和 keepalived 启动
启动 nginx:./nginx
启动 keepalived:systemctl start keepalived.service

5、最终测试
(1)在浏览器地址栏输入 虚拟 ip 地址 192.168.17.50




(2)把主服务器(192.168.17.129)nginx 和 keepalived 停止,再输入 192.168.17.50



6、Nginx 的原理
(1)mater 和 worker



(2)worker 如何进行工作的



(3)一个 master 和多个 woker 有好处 :
  • 可以使用 nginx –s reload 热部署,利用 nginx 进行热部署操作
  • 每个 woker 是独立的进程,如果有其中的一个 woker 出现问题,其他 woker 独立的, 继续进行争抢,实现请求过程,不会造成服务中断

(4)设置多少个 woker 合适
worker 数和服务器的 cpu 数相等是最为适宜的

(5)连接数 worker_connection

第一个:发送请求,占用了 woker 的几个连接数? 答案:2 或者 4 个
第二个:nginx 有一个 master,有四个 woker,每个 woker 支持最大的连接数 1024,支持的 最大并发数是多少?

  • 普通的静态访问最大并发数是: worker_connections * worker_processes /2,
  • 而如果是 HTTP 作 为反向代理来说,最大并发数量应该是 worker_connections * worker_processes/4。

你可能感兴趣的:(七、Nginx的高可用集群)