Zookeeper+Hadoop+Hbase+Hive+Kylin+Nginx集群搭建十一(Nginx负载均衡篇)

Zookeeper+Hadoop+Hbase+Hive+Kylin+Nginx集群搭建十一(Nginx负载均衡篇)

全文请看:https://blog.csdn.net/tktttt/article/details/108578804
大数据集群搭建安装包:https://download.csdn.net/download/tktttt/12879318
大数据集群搭建安装包2:https://download.csdn.net/download/tktttt/12879355

十三、Nginx负载均衡

1.Nginx安装环境

  1. gcc
    安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc:
yum install gcc-c++ 
  1. PCRE
    PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库
yum install -y pcre pcre-devel

注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。

  1. zlib
    zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库
yum install -y zlib zlib-devel
  1. openssl
    OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。
    nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。
yum install -y openssl openssl-devel

2.编译

  1. 在线安装以上4个环境
  2. 上传nginx安装包到/usr/local/software/nginx
  3. 解压 tar -zxvf 安装包名:
    tar -zxvf nginx-1.17.2.tar.gz
  4. 在/usr/local下创建文件夹nginx (下面会指定引用)
cd /usr/local
mkdir nginx
  1. 在/var下创建/temp/nginx文件夹 (下面会指定引用,注意是建了两个文件夹)
cd /var
mkdir temp
cd temp
mkdir nginx
  1. 在nginx的解压目录执行配置 (在目录下直接粘贴这段代码)
#进入目录
cd /usr/local/software/nginx/nginx-1.17.2
#执行配置
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi \
--with-http_dav_module \
--with-http_stub_status_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_flv_module  \
--with-http_mp4_module
  1. 等上面命令运行结束,继续同目录下运行:
make && make install

3.启动测试

启动:

cd /usr/local/nginx/sbin/
./nginx

为了方便最好配置Nginx环境变量:

#打开配置文件
vi /etc/profile
#加入以下内容
export PATH=$PATH:$NGINX_HOME/sbin
export NGINX_HOME=/usr/local/nginx
#保存后,使之生效
source /etc/profile

这样就不用进入安装目录运行命令

#停止命令
nginx -s stop
#重启命令
nginx -s reload
#查看nginx配置文件是否正确
nginx -t
#查看nginx状态
ps aux| grep nginx

验证:
在浏览器输入自己的IP查看:(即nginx安装所在机器IP):http://192.168.88.129
会出现welcome to nginx的页面
说明启动成功

以上参考自此处,侵删

4.nginx实现kylin集群的负载均衡

nginx若启动着,则停止;
进入nginx的编译目录/usr/local/nginx/conf,修改nginx.conf文件内容:


##user  nobody;
#worker_processes  1;
user root;
worker_processes  auto;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
error_log  /var/log/nginx/error.log;
error_log  /var/log/nginx/error.log  notice;
error_log  /var/log/nginx/error.log  info;


#pid        logs/nginx.pid;
pid        /var/run/nginx/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    log_format  main  escape=json  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;
    access_log  /var/log/nginx/access.log;

    sendfile        on;
    #tcp_nopush     on;
    tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;
        charset utf-8;

        #access_log  logs/host.access.log  main;
        access_log  /var/log/nginx/host.access.log main;

        location / {
            #root   html;
            #index  index.html index.htm;
            proxy_pass http://kylin;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    upstream kylin {
        ip_hash;
        server 192.168.88.129:7070;server 192.168.88.130:7070;server 192.168.88.131:7070;
    }
}

为了维持session会话持久性,避免频繁刷新页面出现kylin登陆页面进行登陆,需要在nginx.conf文件内配置ip_hash
重新启动nginx,命令:nginx
将Nginx服务以及所有节点的kylin服务启动,我们可以在浏览器中输入:http://192.168.88.129,来访问我们的Kylin集群。
若要验证是否负载均衡,查看日志打印

5.问题

若是通过命令:

curl 192.168.88.129/中文

查看日志,发现中文的日志输出打印非中文,而是如:"GET /\xE4\xB8\xAD\xE6\x96\x87 HTTP/1.1"
则是未在配置文件的日志属性log_format中加入:escape=json
然后为access_log /var/log/nginx/host.access.log main;中访问日志的最后面添加main,使之生效;
重新通过命令进行:

curl 192.168.88.129/中文

查看日志发现正常:"GET /中文 HTTP/1.1"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值