Python3 优雅上下线YARN NM节点

Python3 优雅上下线YARN NM节点

# !/usr/bin/env python
# -*-coding:utf-8 -*-

"""
@ 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))
    # 将ip写入到黑名单
    cmd1 = "echo {} > {}".format(ip, blacklist_path)
    logging.info("执行命令 {}".format(cmd1))
    out1 = execute_cmd(cmd1)
    # print(out1)
    # 刷新节点
    cmd2 = "source /etc/profile; yarn rmadmin -refreshNodes"
    logging.info("执行命令 {}".format(cmd2))
    out2 = execute_cmd(cmd2)
    # 循环检查nm进程
    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)
    # print(out4)
    # 刷新节点
    cmd2 = "source /etc/profile; yarn rmadmin -refreshNodes"
    logging.info("执行命令 {}".format(cmd2))
    out2 = execute_cmd(cmd2)
    # print(out2)
    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()
    # ip = '10.83.195.8'
    if err is None:
        for ip in nm_list:
            offline_nm(ip)
            online_nm(ip)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值