Metrics-Python操作Metrics Server API 获取node、pod资源率

时间:2021-05-24 22:44:55   收藏:0   阅读:45

一、原理说明

二、API 访问 metrics-server


# kubectl proxy --port=8181 --address=0.0.0.0
# nodes -> curl localhost:8181/apis/metrics.k8s.io/v1beta1/nodes
# pods -> curl localhost:8181/apis/metrics.k8s.io/v1beta1/namespaces/default/pods/redis-master-dasdhjasd
          curl localhost:8181/apis/metrics.k8s.io/v1beta1/namespaces/test/pods
查看node资源指标
        # kubectl get --raw "/apis/metrics.k8s.io/v1beta1/nodes" | jq  | less
查看pods资源指标
        # kubectl get --raw "/apis/metrics.k8s.io/v1beta1/pods" | jq  | less

三、requests封装API

# 仅为示例,代码不完整!!!
import requests
import re

def eventer_alarm_cutpods(string):
    string = re.sub(r‘\\n‘, ‘\n‘, string)
    pods = string.split(‘\n‘)[2].split()[1]
    print(pods)
    return pods

def metric_get_pods(host, pods):
    """
    :param hosts -> 127.0.0.1:8181
    """
    if pods:
        url = ‘http://‘ + host  + ‘/apis/metrics.k8s.io/v1beta1/namespaces/test/pods‘
        response = requests.get(url)
        with response:
            print(response.json())

    else:
        url = ‘http://‘ + host + ‘/apis/metrics.k8s.io/v1beta1/pods‘

def metric_get_nodes(host, node):
    """
    :param hosts -> 127.0.0.1:8181
    :return:
    """
    if node:
        pass
    else:
        url = ‘http://‘ + host + ‘/apis/metrics.k8s.io/v1beta1/nodes‘
        response = requests.get(url)
        with response:
            print(response.json())





if __name__ == ‘__main__‘:
    # metric_get_nodes(‘127.0.0.1:8181‘)
    string = ‘EventType: Warning\nEventKind: Pod\nEventObject: test-nginx-7d97ffc85d-nrnfn\nEventReason: Failed\nEventTime: 2021-05-23 17:48:12 +0800 CST\nEventMessage: Error: ErrImagePull‘
    eventer_alarm_cutpods(string)

原文:https://www.cnblogs.com/dai-zhe/p/14805691.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!