JAVA和Nginx 教程大全

网站首页 > 精选教程 正文

0364-如何使用Nginx实现HiveServer2负载均衡

wys521 2024-10-03 04:22:10 精选教程 23 ℃ 0 评论

温馨提示:如果使用电脑查看图片不清晰,可以使用手机打开文章单击文中的图片放大查看高清原图。

Fayson的github:

https://github.com/fayson/cdhproject

提示:代码块部分可以左右滑动查看噢

1.文档编写目的


在使用Hive JDBC连接HiveServer2服务时,默认是不带负载均衡的,但一个HiveServer2很可能会产生单点的问题,这里我们就需要考虑HiveServer2的负载均衡。

但我们常常会碰到企业已经在用Nginx,Nginx毕竟在http和反向代理这块是最优秀的,这个时候我们就需要考虑复用Nginx,在前面的文章Fayson也介绍了《

如何使用Nginx实现Impala负载均衡

》。所以Fayson在这篇文章就介绍一下如何使用Nginx实现HiveServer2服务的负载均衡。

  • 内容概述

1.Nginx安装及启停

2.配置HiveServer2负载均衡策略

3.JDBC测试及配置HiveServer2 Balancer

4.常见问题

  • 测试环境

1.CM和CDH版本为5.15.0

2.采用root用户操作

3.Nginx1.12.2

4.集群未启用Kerberos

2.Nginx服务安装及启停


1.下载Nginx安装包,下载地址如下

选择集群中任意一台服务器用来安装Nginx服务或者选用一台独立的服务器用来部署Nginx,需要确保Nginx所在的服务器与集群中所有的HiveServer2节点网络是通的(包括端口号10000)

http://nginx.org/download/nginx-1.12.2.tar.gz

(可左右滑动)

2.解压nginx-1.21.2.tar.gz压缩包,并进行编译安装

[root@cdh05 ~]# tar -zxf nginx-1.12.2.tar.gz 
[root@cdh05 ~]# cd nginx-1.12.2
[root@cdh05 nginx-1.12.2]# ./configure --with-stream
[root@cdh05 nginx-1.12.2]# make && make install

(可左右滑动)

注意:必须编译stream模块

默认Nginx的安装目录为/usr/local/nginx

3.将nginx服务加如系统自启动服务,在/etc/init.d目录下创建nginx脚本,内容如下

#!/bin/sh
#chkconfig: - 85 15
#description: Http server daemon
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/nginx
set -e
[ -x "$DAEMON" ] || exit 0
do_start() {
$DAEMON || echo -n "nginx is already running";
}
do_stop() {
$DAEMON -s stop || echo -n "nginx stop failed"
}
do_reload() {
$DAEMON -s reload || echo -n "nginx reload failed"
}
case "$1" in
start)
do_start
;;
stop)
do_stop
;;
reload|graceful)
do_reload
;;
restart)
do_stop
sleep 2
do_start
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac
exit 0

(可左右滑动)

注意:上图标注部分为Nginx的home目录,根据自己安装的目录进行调整。

4.设置自启动脚本执行权限

[root@cdh05 ~]# chmod +x /etc/init.d/nginx
[root@cdh05 ~]# ll /etc/init.d/nginx

(可左右滑动)

5.将nginx服务加入系统自启动并设置开机启动

[root@cdh05 ~]# chkconfig --add nginx
[root@cdh05 ~]# chkconfig nginx on

(可左右滑动)

6.启动与停止Nginx,这里Fayson将Nginx的端口修改为8089

[root@cdh05 conf]# service nginx start
[root@cdh05 conf]# netstat -an |grep 80

(可左右滑动)

[root@cdh05 conf]# service nginx stop

(可左右滑动)

7.测试Nginx是否正常访问,在浏览器输入http://hostname

如上图显示则表示Nginx已成功安装并启动。

3.配置HiveServer2负载均衡策略


1.修改/usr/local/nginx/conf/nginx.conf文件,在文件末尾增加如下配置

stream{
 log_format basic '$remote_addr [$time_local] ' '$protocol $status $bytes_sent $bytes_received' '$session_time';
 upstream hiveserver2 {
 least_conn; #路由策略:least_conn:最少连接
 server cdh01.fayson.com:10000;
 server cdh01.fayson.com:10000; 
 }
 server{ #hiveserver2 jdbc 负载均衡
 listen 10010;
 proxy_pass hiveserver2;
 }
}

(可左右滑动)

2.重启Nginx服务

[root@cdh05 ~]# service nginx restart

(可左右滑动)

4.测试HiveServer2负载均衡


1.使用beeline通过JDBC访问负载均衡地址cdh05.fayson.com:10010

[root@cdh05 conf]# beeline 
beeline> !connect jdbc:hive2://cdh05.fayson.com:10010 hive hive
0: jdbc:hive2://cdh05.fayson.com:10010> select count(*) from hosts;

(可左右滑动)

执行成功

5.配置HiveServer2 Load Balancer


1.登录CM进入hive服务界面,搜索“load balancer”,配置为HiveServer2服务负载均衡地址

保存配置后,回到CM主页根据提示重启相应服务。

6.常见问题


1.Nginx编译时报错./configure: error: C compiler cc is not found

[root@ip-172-31-6-148 nginx-1.12.2]# ./configure 
checking for OS
 + Linux 2.6.32-431.el6.x86_64 x86_64
checking for C compiler ... not found
./configure: error: C compiler cc is not found
[root@ip-172-31-6-148 nginx-1.12.2]# 

(可左右滑动)

解决方法:

yum -y install gcc gcc-c++

(可左右滑动)

2.Nginx编译报错./configure: error: the HTTP rewritemodule requires the PCRE library

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

(可左右滑动)

解决方法:

yum -y install pcre-devel

(可左右滑动)

提示:代码块部分可以左右滑动查看噢

为天地立心,为生民立命,为往圣继绝学,为万世开太平。

温馨提示:如果使用电脑查看图片不清晰,可以使用手机打开文章单击文中的图片放大查看高清原图。

推荐关注Hadoop实操,第一时间,分享更多Hadoop干货,欢迎转发和分享。

原创文章,欢迎转载,转载请注明:转载自微信公众号Hadoop实操

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表