利用CDH提供的API進行Hbase服務的狀態檢測及自動重啟


背景

由於公司CDH集群資源有限,在使用Hbase對數據廠商上報數據文件進行解析寫入hbase過程中,一旦遇到數據上報峰值(如歷史數據批量上報,異常數據批量重傳),都會導致Hbase可用機器資源不足,導致Hbase服務異常終止。嘗試調試解析數據文件的並發線程數及Hbase服務的可分配內存資源均不能有效解決該問題,每次都需要手動重啟解決(后續考慮集群擴容以支撐數據讀寫壓力,當前姑息解決)。為了及時發現Hbase服務異常終止現象,有嘗試通過監控CDH各節點部署的Hbase服務進程存活情況進行監控。但是由於內外網不通的原因,即使在凌晨及時發現了Hbase服務異常終止,也無法立即進行恢復操作。嘗試通過定位各服務進程啟動命令,定位到服務啟動、重啟的腳本,但是由於用戶、權限等問題,通過CDH腳本自動重啟Hbase異常服務的嘗試,始終沒有奏效。好在找到了CM的開放API。

解決方案

1.獲取到集群名稱

curl -u CM用戶名:密碼 'http://localhost:7180/api/v1/clusters'

2.獲取hbase服務名稱

curl -u CM用戶名:密碼 'http://localhost:7180/api/v1/clusters/Cluster%201/services'  #注意,默認的集群名稱是有空格的,需要轉義為%20

3.獲取hbase服務狀態

curl -u CM用戶名:密碼 'http://localhost:7180/api/vi/clusters/Cluster%201/services/{servicename}   #  注意,{servicename}為2中獲取的hbase服務的名稱,一般為hbase

該接口返回的json數據中,包含了每個master、regionserver、restserver、thriftserver的服務名稱、是否啟動、健康狀態等,其中服務名稱比較重要,后續重啟哪些服務,均要指定服務名稱列表。

4.重啟異常的hbase服務

curl -X POST -H "Content-Type:application/json" -u CM用戶名:密碼 \
  -d '{ "items": ["regionserver-xxxxxxx","master-xxxx"] }' \
  'http://localhost:7180/api/v1/clusters/Cluster%201/services/hbase/roleCommands/restart'

其他CDH服務的狀態監控、啟停同理。

附:

import os
import json
import time
 
stime = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
os.popen("echo '%s check...' >> /home/dqrcsc/check/hbase.logs" % (stime))
os.popen("curl -u username:password 'http://ip:7180/api/v1/clusters/Cluster%201/services/hbase/roles' > /home/csc/check/servers.txt")
 
f = open("/home/csc/check/servers.txt","r")
 
s = json.load(f)
for item in s['items']:
    if item['healthSummary'] == 'BAD':
        print(item['name'])
        print(item['type'])
        print(item['roleState'])
        print(item['healthSummary'])
        os.popen('curl -X POST -H "Content-Type:application/json" -u username:password -d \'{"items":["'+item['name']+'"]}\' "http://ip:7180/api/v1/clusters/Cluster%201/services/hbase/roleCommands/restart"');
        os.popen("echo '%s %s %s restart' >> /home/csc/check/hbase.logs" % (stime, item['type'], item['name']))
 
f.close()
  • 官網關於CDH相關服務啟動管理的描述
In a non-Cloudera Manager managed cluster, you most likely start a role instance using an init script, for example, service hadoop-hdfs-datanode start.
Cloudera Manager does not use init scripts for the daemons it manages; in a Cloudera Manager managed cluster, starting and stopping services using init scripts will not work.
In a Cloudera Manager managed cluster you can only start or stop services via Cloudera Manager. Cloudera Manager uses an open source process management tool called supervisord, 
which takes care of redirecting log files, notifying of process failure, setting the effective user ID of the calling process to the right user, and so on. Cloudera Manager supports automatically 
restarting a crashed process. It will also flag a role instance with a bad state if it crashes repeatedly right after start up.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM