0


使用nginx-lua配置统一url自动跳转到hadoop-ha集群的active节点

下载安装nginx所用的依赖

yum -yinstall gcc zlib zlib-devel pcre-devel openssl openssl-devel

下载nginx

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

稍后安装nginx

安装lua语言

yum install readline-devel

curl-R-O http://www.lua.org/ftp/lua-5.3.0.tar.gz
tar zxf lua-5.3.0.tar.gz
cd lua-5.3.0
make linux testmakeinstall

安装LuaJIT,安装路径/opt/luaJIT-2.1.0自行设置

wget http://luajit.org/download/LuaJIT-2.1.0-beta2.tar.gz
tar zxf LuaJIT-2.1.0-beta2.tar.gz
cd LuaJIT-2.1.0-beta2
makePREFIX=/opt/luaJIT-2.1.0
makeinstallPREFIX=/opt/luaJIT-2.1.0

设置环境变量
打开:

vi /etc/profile
exportLUAJIT_LIB=/opt/luaJIT-2.1.0/lib 
exportLUAJIT_INC=/opt/luaJIT-2.1.0/include/luajit-2.1

保存:

. /etc/profile

下载ngx_devel_kit(NDK)模块 :https://github.com/simpl/ngx_devel_kit/tags,不需要安装,记住存放位置/opt/luaJIT-2.0.5/package/ngx_devel_kit-0.2.19路径

wget https://github.com/simpl/ngx_devel_kit/archive/v0.2.19.tar.gz
tar-xzvf v0.2.19.tar.gz
cd /opt/luaJIT-2.1.0
mkdir package
mv ngx_devel_kit-0.2.19 /opt/luaJIT-2.0.5/package/

下载最新的lua-nginx-module 模块 :https://github.com/openresty/lua-nginx-module/tags,不需要安装,记住存放位置/opt/luaJIT-2.0.5/package/lua-nginx-module-0.10.13路径
注意:一定要下载0.10.13版本,其他版本可能会报错

wget https://github.com/openresty/lua-nginx-module/archive/v0.10.13.tar.gz
tar-xzvf v0.10.13.tar.gz
mv lua-nginx-module-0.10.13 /opt/luaJIT-2.0.5/package/
# 进入安装包目录cdcd nginx-1.12.2

在nginx中编译,/opt/nginx-1.12.2为安装路径,–add-module后面加上面下载的两个包的位置

./configure --prefix=/opt/nginx-1.12.2 --with-http_stub_status_module --with-http_ssl_module --add-module=/opt/luaJIT-2.0.5/package/lua-nginx-module-0.10.13 --add-module=/opt/luaJIT-2.0.5/package/ngx_devel_kit-0.2.19

修改Makefile文件,防治报错

cd /opt/nginx-1.12.2

找到当前目录下找到objs文件夹,并进入,打开文件Makefile,找到有一下内容的这行:

打开:

vi objs/Makefile

CFLAGS =-pipe-O-W-Wall -Wpointer-arith -Wno-unused-parameter -Werror-g

-Werror: gcc将所有的警告当成错误进行处理把这行内容中的 “-Werror”去掉

CFLAGS =-pipe-O-W-Wall -Wpointer-arith -Wno-unused-parameter -g

保存:Esc,ZZ

回到上一级目录,进行安装

cd /opt/nginx-1.12.2
make&&makeinstall

要在bash中执行

echo $SHELL

需要是/bin/bash如果是其他的,执行

bash

,切换到bash

配置nginx的环境变量
打开:

vi /etc/profile
exportNGINX_HOME=/opt/nginx-1.12.2
exportPATH=$PATH:$NGINX_HOME/sbin

保存:

. /etc/profile

查看结果

nginx -V
nginx version: nginx/1.12.2
built by gcc 8.3.1 20190311(Red Hat 8.3.1-3)(GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/opt/nginx-1.12.2 --with-http_stub_status_module --with-http_ssl_modu
le --add-module=/opt/luaJIT-2.0.5/package/lua-nginx-module-0.10.13 --add-module=/opt/luaJIT-2.0.5/package/ngx_devel_kit-0.2.19

看看有没有断开的链接,如果有的话,自己连一下

ldd $(which /opt/nginx-1.12.2/sbin/nginx)

假如有 libluajit-5.1.so.2 => not found

通过观察,我们知道, libluajit-5.1.so.2 在/opt/luaJIT-2.0.5/lib/中
建立软链接

cd /lib64
ln-s /opt/luaJIT-2.0.5/lib/libluajit-5.1.so.2 libluajit-5.1.so.2

刷新lib库

ldconfig

配置服务

cd /usr/lib/systemd/system/

新建服务nginx.service

vi /usr/lib/systemd/system/nginx.service

输入以下内容,注意路径/opt/nginx-1.12.2为自己的安装nginx的路径

[Unit]Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target

[Service]Type=forking
PIDFile=/opt/nginx-1.12.2/logs/nginx.pid
ExecStartPre=/opt/nginx-1.12.2/sbin/nginx -t-c /opt/nginx-1.12.2/conf/nginx.conf
ExecStart=/opt/nginx-1.12.2/sbin/nginx -c /opt/nginx-1.12.2/conf/nginx.conf
ExecReload=/opt/nginx-1.12.2/sbin/nginx -s reload
ExecStop=/opt/nginx-1.12.2/sbin/nginx -s stop
ExecQuit=/opt/nginx-1.12.2/sbin/nginx -s quit                                                        
PrivateTmp=true

[Install]WantedBy=multi-user.target

在启动服务之前,需要先重载systemctl命令

systemctl daemon-reload

启动和开机自启

#启动服务或者使用systemctl start nginx
systemctl start nginx.service

#运行以下命令设置Nginx服务开机自启动
systemctl enable nginx

获取当前hadoop-ha集群的状态,例如有两台namenode,ip地址分别是192.168.206.215和192.168.206.216,那么如果192.168.206.215是active,那么下面返回active,如果是192.168.206.216是active,那么返回standby

wget -qO- http://192.168.206.215:50070/jmx?qry=Hadoop:service=NameNode,name=NameNodeStatus |sed's
/,/\n/g'|grep'State'|sed's/"State" : "//g'|sed's/[" ]//g'

将上面代码放到一个文件中

cdmkdir run_shell
cd /root/run_shell
vi hadoop-status

i ...

Esc ZZ
chnod +x ./hadoop-status

尝试运行确定无误

./hadoop-status

到nginx的配置文件下,进行配置

cd /opt/nginx-1.12.2/conf/

配置nginx-conf,其中/root/run_shell/hadoop-status为脚本执行的路径,/tmp/hadoop-status.tmp是随便的一个文件,避免文件名冲突,注意是>不是追加>>,access_by_lua_block是lua脚本,需要要安装上面的带有lua脚本的nginx,ngx.log(ngx.ERR, result)是日志信息,调试的时候用,可以删掉,代码可以简化,懒得弄了,user root root;是用户名和组,一定要和hadoop-status脚本权限对应,否则会没有访问执行权限,

proxy_pass http://node15:50070;

是重定向的hadoop-web页面,自行设置,如果报错,执行lua进入命令行,一行一行的执行,看哪里出了问题,退出lua-shell直接Ctrl+C,或者看nginx下面的logs文件中的error报错日志
vi nginx.conf

worker_processes  1;
user root root;

events {
    worker_connections  1024;}

http {

    server {
        listen       80;
        server_name  localhost;

        location /{
            access_by_lua_block {
                os.execute("/bin/bash /root/run_shell/hadoop-status > /tmp/hadoop-status.tmp")
                handle = io.open("/tmp/hadoop-status.tmp","r")
                result = handle:read("*a")handle:close()
                
                sentinel_result = result:match("active") and "active" or "standby"
                ngx.log(ngx.ERR, result)
                ngx.log(ngx.ERR, sentinel_result)
                ngx.log(ngx.ERR,"123")if sentinel_result =="active" then
                    ngx.exec("@node1")else
                    ngx.exec("@node2")
                end
            }}

       
        location @node1 {
            proxy_pass http://node15:50070;}

        location @node2 {
            proxy_pass http://node16:50070;}}}

重启nginx

systemctl restart nginx.service

访问localhost:80即可直接跳转到active的hadoop-ha上
如果是虚拟机,则在主机上访问虚拟机的ip地址即可访问

标签: 大数据 nginx hadoop

本文转载自: https://blog.csdn.net/weixin_44155966/article/details/132599062
版权归原作者 轻夏 所有, 如有侵权,请联系我们删除。

“使用nginx-lua配置统一url自动跳转到hadoop-ha集群的active节点”的评论:

还没有评论