Python3 优雅上下线YARN NM节点
"""
@ File : graceful_offline_online_nm.py
@ Time : 2024-01-19 10:00
@ Author : Shylin Zhang
@ version : python 3.6
@ Description: YARN 优雅上下线NM节点
"""
import logging
import socket
import subprocess
import time
import requests
logging.basicConfig(level=logging.INFO,
format='%(message)s',
filemode='a',
filename='./res.log'
)
blacklist_path = "/opt/apache/hadoop/etc/hadoop/yarn-hosts.excludes"
rm1 = '10.83.192.112'
rm2 = '10.83.195.4'
port = 8088
def execute_cmd(cmd):
p = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, encoding='utf-8')
out = p.stdout
return out
def get_nm_ips():
res = None
err = None
res_list = list()
try:
url = "http://{}:{}/ws/v1/cluster/nodes?states=RUNNING".format(rm1, port)
res = requests.get(url, timeout=5).json().get("nodes").get("node")
except Exception as e:
logging.error("请求RM失败 {}".format(e))
url = "http://{}:{}/ws/v1/cluster/nodes?states=RUNNING".format(rm2, port)
res = requests.get(url, timeout=5).json().get("nodes").get("node")
if res:
for n in res:
host = n.get("nodeHostName")
try:
ip = socket.gethostbyname(n.get("nodeHostName"))
res_list.append(ip)
except Exception as e:
err = e
logging.error("根据hostname获取ip失败 {}".format(e))
return err, res_list
def offline_nm(ip):
"""
优雅下线NM
:param ip:
:return:
"""
logging.info("------开始优雅下线NM {} ------".format(ip))
cmd1 = "echo {} > {}".format(ip, blacklist_path)
logging.info("执行命令 {}".format(cmd1))
out1 = execute_cmd(cmd1)
cmd2 = "source /etc/profile; yarn rmadmin -refreshNodes"
logging.info("执行命令 {}".format(cmd2))
out2 = execute_cmd(cmd2)
cmd3 = """ssh admin@{} "jps | grep NodeManager" """.format(ip)
logging.info("执行命令 {}".format(cmd3))
while True:
out3 = execute_cmd(cmd3)
time.sleep(20)
if out3 == '':
break
cmd4 = "echo '' > {}".format(blacklist_path)
logging.info("执行命令 {}".format(cmd4))
out4 = execute_cmd(cmd4)
cmd2 = "source /etc/profile; yarn rmadmin -refreshNodes"
logging.info("执行命令 {}".format(cmd2))
out2 = execute_cmd(cmd2)
logging.info("------优雅下线NM {}完成------".format(ip))
def online_nm(ip):
"""
上线NM
:param ip:
:return:
"""
logging.info("------开始上线NM {}------".format(ip))
cmd1 = """ssh admin@{} "source /etc/profile; setsid yarn --daemon start nodemanager" """.format(ip)
logging.info("执行命令 {}".format(cmd1))
out1 = execute_cmd(cmd1)
time.sleep(20)
cmd2 = """ssh admin@{} "jps | grep NodeManager" """.format(ip)
logging.info("执行命令 {}".format(cmd2))
out2 = execute_cmd(cmd2)
if out2:
logging.info("------上线NM {}完成------".format(ip))
else:
logging.error("------上线NM {}失败------".format(ip))
if __name__ == '__main__':
err, nm_list = get_nm_ips()
if err is None:
for ip in nm_list:
offline_nm(ip)
online_nm(ip)