zoukankan      html  css  js  c++  java
  • 集群中nginx服务的健康检查及负载均衡模板更新

    课前回顾

    编译的时候大长串是指定相关目录,指定启动用户
    源码安装完成之后还能再添加模块,重新编译就好
    
    卸载nginx的时候要先备份相关的.conf文件
    
    

    tZEN5Q.md.png

    nginx负载均衡的健康检查

    nginx_upstream_check_module

    该模块可以为Nginx提供主动式后端服务器健康检查的功能, 以图形化的方式实时检查后端主机池主机的状态,为集群的批量管理打下基础,

    通过负载均衡,检查主机池里的主机nginx是否正常的运行

    lb01源码安装nginx和第三方软件

    0.卸载yum安装的nginx
    yum remove -y nginx
    
    1.安装依赖包
    [root@lb02 ~]# yum install -y gcc glibc gcc-c++ pcre-devel openssl-devel patch
    
    2.下载nginx源码包以及nginx_upstream_check模块第三方模块,由于第三方模块不支持nginx-1.18,所以选择下载nginx-1.16
    [root@lb02 ~]# wget http://nginx.org/download/nginx-1.16.1.tar.gz
    [root@lb02 ~]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip
    
    3.解压nginx源码包以及第三方模块
    [root@lb02 ~]# tar xf nginx-1.16.1.tar.gz
    [root@lb02 ~]# unzip master.zip
    
    4.进入nginx目录,打补丁(nginx的版本是1.14补丁就选择1.14的,p1代表在nginx目录,p0是不在nginx目录),#nginx版本要和check版本一致。
    
    [root@lb02 ~]# cd nginx-1.16.1/
    [root@lb02 nginx-1.16.1]# patch -p1 <../nginx_upstream_check_module-master/check_1.16.1+.patch 
    ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=www --group=www --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --add-module=/root/nginx_upstream_check_module-master --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
    [root@lb02 nginx-1.14.2]# make && make install
    
    5.修改nginx.conf,创建相关目录
    [root@lb01 ~]# mkdir /etc/nginx/conf.d
    [root@lb01 ~]# vim /etc/nginx.conf
    user  www;
    ...
    include /etc/nginx/conf.d/*.conf;
    [root@lb01 ~]# groupadd www -g666 && useradd www -u 666 -g 666
    
    6.在已有的负载均衡上增加健康检查的功能
    [root@lb01 conf.d]# vim /etc/nginx/conf.d/jc.conf
    upstream web {
        server 172.16.1.7:80 max_fails=2 fail_timeout=10s;
        server 172.16.1.8:80 max_fails=2 fail_timeout=10s;
        server 172.16.1.9:80 max_fails=2 fail_timeout=10s;
        
        check interval=3000 rise=2 fall=3 timeout=1000 type=tcp;
        #interval  检测间隔时间,单位为毫秒
        #rise      表示请求2次正常,标记此后端的状态为up
        #fall      表示请求3次失败,标记此后端的状态为down
        #type      类型为tcp(健康检查包的类型)
        #timeout   超时时间,单位为毫秒
    }
    
    server {
        listen 80;
        server_name cs.jc.com;
        location / {
            proxy_pass http://web;
            include proxy_params;
        }
    
        location /uc {
            check_status;
        }
    
         location = /zt {
            stub_status;
    		}
    }
    
    7.nginx启动程序加入环境变量
    [root@lb01 /etc/nginx]# vim /etc/profile.d/export.sh
    export PATH="/usr/sbin/nginx:$PATH"
    [root@lb01 /etc/nginx]# source /etc/profile.d/export.sh 
    
    8.编辑/etc/nginx/proxy_params文件
    	# 客户端的请求头部信息,带着域名来找我,我也带着域名去找下一级(代理机或者代理服务器)
    	proxy_set_header Host $host;
    	# 显示客户端的真实ip(和代理的所有IP)
    	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    	
    	#nginx代理与后端服务器连接超时时间(代理连接超时)
    	proxy_connect_timeout 60s;
    	#nginx代理等待后端服务器的响应时间
    	proxy_read_timeout 60s;
    	#后端服务器数据回传给nginx代理超时时间
    	proxy_send_timeout 60s;
    	
    	#nignx会把后端返回的内容先放到缓冲区当中,然后再返回给客户端,边收边传, 不是全部接收完再传给客户端
    	proxy_buffering on;
    	#设置nginx代理保存用户头信息的缓冲区大小
    	proxy_buffer_size 4k;
    	#proxy_buffer_size 8k;
    	#proxy_buffers 缓冲区
    	proxy_buffers 8 4k;
    	#proxy_buffers 8 8k;
    	#使用http 1.1协议版本
    	proxy_http_version 1.1;
    	
    	#解决集群单点故障的反馈页面影响用户体验,自动跳转到下一个负载均衡主机
    	proxy_next_upstream error timeout http_500 http_502 http_503 http_504 http_404;
    7.启动nginx
    [root@lb01 /etc/nginx]# mkdir -p /var/cache/nginx/client_temp
    nginx
    nginx -s reload
    
    8.域名解析
    10.0.0.5 cs.jc.com
    

    tZUspR.md.png


    负载均衡模板

    vim /etc/nginx/conf.d/wp.conf
    server {
    	listen 80;
    	server_name cs.zh.com;
    
        location / {
            proxy_pass http://backend;
            include proxy_params;
        }
    }
    #include后面的相对路径指定就是/etc/nginx/ 下
    
    vim /etc/nginx/nginx.conf
    ...
    include /etc/nginx/upstream;
    include /etc/nginx/conf.d/*.conf;
    
    vim /etc/nginx/upstream
    upstream backend {
        #server backend1.example.com       weight=5;
        #server backend2.example.com:8080;
        #server unix:/tmp/backend3;
        #server backup1.example.com:8080   backup;
        #server 10.0.0.7:80 down;
        #server 10.0.0.9:80 backup;
        
        server 10.0.0.7;
        server 10.0.0.8;
        server 10.0.0.9:80 max_fails=1 fail_timeout=10s;
        
        check interval=3000 rise=2 fall=3 timeout=1000 type=tcp;
        #interval  检测间隔时间,单位为毫秒
        #rise      表示请求2次正常,标记此后端的状态为up
        #fall      表示请求3次失败,标记此后端的状态为down
        #type      类型为tcp
        #timeout   超时时间,单位为毫秒
    }
    
    vim /etc/nginx/proxy_params
    	# 客户端的请求头部信息,带着域名来找我,我也带着域名去找下一级(代理机或者代理服务器)
    	proxy_set_header Host $host;
    	# 显示客户端的真实ip(和代理的所有IP)
    	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    	
    	#nginx代理与后端服务器连接超时时间(代理连接超时)
    	proxy_connect_timeout 60s;
    	#nginx代理等待后端服务器的响应时间
    	proxy_read_timeout 60s;
    	#后端服务器数据回传给nginx代理超时时间
    	proxy_send_timeout 60s;
    	
    	#nignx会把后端返回的内容先放到缓冲区当中,然后再返回给客户端,边收边传, 不是全部接收完再传给客户端
    	proxy_buffering on;
    	#设置nginx代理保存用户头信息的缓冲区大小
    	proxy_buffer_size 4k;
    	#proxy_buffer_size 8k;
    	#proxy_buffers 缓冲区
    	proxy_buffers 8 4k;
    	#proxy_buffers 8 8k;
    	#使用http 1.1协议版本
    	proxy_http_version 1.1;
    	
    	#解决集群单点故障的反馈页面影响用户体验,自动跳转到下一个负载均衡主机
    	#proxy_next_upstream error timeout http_500 http_502 http_503 http_504 http_404;
    
  • 相关阅读:
    计算字符串中al_num,spance_num,digit_num,other_num的个数
    for与if、else的使用
    len字符串的长度
    Measuring Signal Similarities
    离散信号MATLAB频谱分析程序
    给新人程序员的八点建议 转载
    翻译网站
    jquery常用选择器
    java处理数据库的CRUD
    JDBC连接数据库工具类
  • 原文地址:https://www.cnblogs.com/syy1757528181/p/12982773.html
Copyright © 2011-2022 走看看