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安装环境
- gcc
安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc:
yum install gcc-c++
- PCRE
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库
yum install -y pcre pcre-devel
注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。
- zlib
zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库
yum install -y zlib zlib-devel
- openssl
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。
nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。
yum install -y openssl openssl-devel
2.编译
- 在线安装以上4个环境
- 上传nginx安装包到
/usr/local/software/nginx
- 解压 tar -zxvf 安装包名:
tar -zxvf nginx-1.17.2.tar.gz
- 在/usr/local下创建文件夹nginx (下面会指定引用)
cd /usr/local
mkdir nginx
- 在/var下创建/temp/nginx文件夹 (下面会指定引用,注意是建了两个文件夹)
cd /var
mkdir temp
cd temp
mkdir nginx
- 在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
- 等上面命令运行结束,继续同目录下运行:
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"