四、Nginx代理Hadoop HA

(3) Nginx代理Hadoop HA
虽然安装了lua-upstream-nginx-module模块,但是仍需要使用OpenResty的healthcheck.lua模块才能完成upstream的健康检查功能。
(1) 下载最新版本的OpenResty代码。执行如下命令:
make && make install ls /usr/local/openresty/lualib/resty/upstream/healthcheck.lua
其中healthcheck.lua脚本就是我们需要的健康检查模块。
(2) 配置nginx.conf:
# upstream upstream resourcemanagers { server 192.168.0.1:8084; server 192.168.0.2:8084; keepalive 2000; } upstream namenodes { server 192.168.0.1:50070; server 192.168.0.2:50070; keepalive 2000; } # health check lua_package_path "/usr/local/openresty/lualib/?.lua;;"; lua_shared_dict healthcheck 1m; lua_socket_log_errors off; init_worker_by_lua_block { local hc = require "resty.upstream.healthcheck" local ok, err = hc.spawn_checker { shm = "healthcheck", upstream = "resourcemanagers ", type = "http", http_req = "GET / HTTP/1.0\r\n\r\n", interval = 2000, timeout = 5000, fall = 3, rise = 2, valid_statuses = {302}, concurrency = 1, } if not ok then ngx.log(ngx.ERR, "=======> failed to spawn RM health checker: ", err) return end local ok, err = hc.spawn_checker { shm = "healthcheck", upstream = "namenodes ", type = "http", http_req = "GET /webhdfs/v1/?op=LISTSTATUS HTTP/1.0\r\n\r\n", interval = 2000, timeout = 5000, fall = 3, rise = 2, valid_statuses = {200}, concurrency = 1, } if not ok then ngx.log(ngx.ERR, "=======> failed to spawn NameNode health checker: ", err) return end } # proxy location /yarn/ { proxy_pass http://resourcemanagers/; # some sub_filter, rewrite config } location /hdfs/ { proxy_pass http://namenodes/; # some sub_filter, rewrite config }
更新Nginx配置:
$ nginx -s reload
访问http://127.0.0.1:8080/hdfs或http://127.0.0.1:8080/yarn即可看到HDFS或YARN的Web页面。
五、总结
综上,使用Lua扩展Nginx的功能十分强大,且十分容易定制,这也是OpenResty的功能如此强大的原因。虽然OpenResty已经提供了lua-resty-upstream-healthcheck模块完成upstream的健康检查功能,不过我们仍在社区版的Nginx上亲自扩展了该功能。希望这篇文章能帮助大家快速的配置Nginx+Lua的环境,并方便地开发自己的Nginx扩展功能。
更多Hadoop相关信息见Hadoop 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=13
本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-07/133120.htm
