使用Nginx+Lua代理Hadoop HA,nginxhadoop

來源:互聯網
上載者:User

使用Nginx+Lua代理Hadoop HA,nginxhadoop

一、Hadoop HAWeb頁面訪問

 

Hadoop開啟HA後,會同時存在兩個Master組件提供服務,其中正在使用的組件稱為Active,另一個作為備份稱為Standby,例如HDFS的NameNode、YARN 的ResourceManager。HDFS的web頁面只有通過Active的NameNode才能正常訪問,同樣地,YARN的web頁面也只有通過Active的ResouceManager才能正常訪問。

 

 

(1) HDFS HA的Web訪問

 

正常使用Nginx的proxy_pass代理單一的Web服務地址時非常簡單(參考博文最簡反向 Proxy配置),而面對Hadoop HA這樣的多Web服務地址時就會有點麻煩。

 

 

(2) HDFS HA的Web代理

 

雖然Nginx的upstream支援配置多個Web地址,預設會隨機將Web請求隨機轉寄到任意一個Web地址,只有某個web地址被認為不可達後,才會被Nginx列入黑名單。而Hadoop HA的Active和Standby節點都是一直服務的,只是同一個時刻,最多隻有一個節點的Web訪問是有效,這就要求Nginx對upstream中的Web地址更細緻地檢查,而非粗略地判斷是否可達。

 

二、Nginxupstream健全狀態檢查

 

對upstream的地址有效性檢查稱為健全狀態檢查。通過週期性調用檢查邏輯,對upstream配置的Web地址進行標記,不健康的Web地址會被臨時列入黑名單內,直到該地址被標記為健康狀態時,才會有新的Web請求轉寄到該地址上。

(1)Nginx本身對upstream的健全狀態檢查支援並不強大,做不到對檢查邏輯的自由定製。

 

(2)開源項目nginx_upstream_check_module以Nginx補丁的方式擴充了Nginx的upstream文法,支援自訂HTTP請求的方式檢查Web服務的健康狀態。但在實際使用過程中,遇到一個很不方便的地方。

upstream resourcemanagers {    server 192.168.0.1:8084;    server 192.168.0.2:8084;    check interval=30000 rise=1 fall=3 timeout=5000 type=http;    check_http_send "HEAD / HTTP/1.0\r\n\r\n";    check_http_expect_alive http_3xx;    keepalive 2000;}

nginx_upstream_check_module使用check命令定義健全狀態檢查的基本屬性,使用check_http_send自訂HTTP請求,check_http_expect_alive定義期望的健康狀態HTTP code。這裡使用http_3xx是該模組定義的內建匹配文法,表示以3開頭的HTTP code。想必大家已經想到,這種定義方式是無法精確區分301、302、307報文的。當然正常情況下,3xx的報文應該是同類型的報文,不需要如此精確的區分,但是不巧的是Hadoop2.7.2版本的Active ResourceManager和Standby ResourceManager分別返回的是302和307報文!

 

(3)以上兩種方案並不是解決Nginx upstream健全狀態檢查的完美方案,真正完美的方案是OpenResty的lua-resty-upstream-healthcheck。OpenResty內建了大量的Lua庫,可以自由擴充、定製Nginx的功能。其中healthcheck.lua模組用於upstream的健全狀態檢查。

不過我希望在Nginx的基礎上只擴充upstream健全狀態檢查的功能,而非用OpenResty代替Nginx,因此需要使用Nginx的lua-upstream-nginx-module模組。

 

三、編譯安裝擴充Nginx

 

(1)由於lua-upstream-nginx-module是使用Lua指令碼對Nginx進行擴充,因此必須安裝Lua解譯器。LuaJIT是Lua語言的即時編譯器,效率更高。

$ wget http://luajit.org/download/LuaJIT-2.0.4.tar.gz$ tar zxvf LuaJIT-2.0.4.tar.gz$ cd LuaJIT-2.0.4$ make$ make install$ export LUAJIT_LIB=/usr/local/lib$ export LUAJIT_INC=/usr/local/include/luajit-2.0$ rm /usr/local/lib/libluajit-5.1.so*$ cd ..

匯出環境變數LUAJIT_LIB和LUAJIT_INC是為了後續編譯lua-nginx-module模組使用。刪除libluajit的所有動態連結程式庫是為了保證後續編譯時間是靜態連結,否則預設為動態連結。

 

(2)準備好Lua環境後,接下來下載Nginx的Lua模組lua-nginx-module、Nginx開發包ngx_devel_kit、Nginx upstreamLua模組lua-upstream-nginx-module、pcre庫和openssl庫、Nginx源碼。解壓後的檔案清單如下:

./lua-nginx-module-0.10.5./lua-upstream-nginx-module-0.05./nginx-1.10.1./ngx_devel_kit-0.3.0./openssl-OpenSSL_1_0_1t./pcre-8.38

 

執行命令編譯Nginx:

$ cd nginx-1.10.1$ ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=root --group=root --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-threads --with-stream --with-stream_ssl_module --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_v2_module --with-http_v2_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' --with-pcre=../pcre-8.38 --with-openssl=../openssl-OpenSSL_1_0_1t --add-module=../ngx_devel_kit-0.3.0 --add-module=../lua-nginx-module-0.10.5 --add-module=../lua-upstream-nginx-module-0.05$ make && make install

 

(3) 安裝完畢後,Nginx的設定檔為/etc/nginx/nginx.conf,可執行檔為/usr/sbin/nginx。執行Nginx啟動命令:

$ nginx

訪問http://127.0.0.1:8080即可看到Nginx首頁。

 

(4) 添加Lua測試連結,測試Lua模組是否正常工作。

location /lua {    set $test "hello, world.";    content_by_lua '        ngx.header.content_type = "text/plain";        ngx.say(ngx.var.test);    ';}

 

更新Nginx配置:

$ nginx -s reload

訪問http://127.0.0.1:8080/lua即可看到”hello,world.”。

 

四、Nginx代理Hadoop HA

 

 

(3) Nginx代理Hadoop HA

 

雖然安裝了lua-upstream-nginx-module模組,但是仍需要使用OpenResty的healthcheck.lua模組才能完成upstream的健全狀態檢查功能。

 

(1) 下載最新版本的OpenResty代碼。執行如下命令:

make && make installls /usr/local/openresty/lualib/resty/upstream/healthcheck.lua

其中healthcheck.lua指令碼就是我們需要的健全狀態檢查模組。

 

(2) 配置nginx.conf:

# upstreamupstream 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 checklua_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} # proxylocation /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擴充功能。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.