python调用kubernetes api方法

环境概述
kubernetes: 1.13.4
python: 3.6
Kubernetes Python Client: v9.0.1
官方地址:https://github.com/kubernetes-client/python
k8s API地址:https://github.com/kubernetes-client/python/blob/master/kubernetes/README.md
k8s v1.3对应api: https://github.com/kubernetes-client/python/blob/v9.0.1/kubernetes/README.md

安装kubernetes python client

pip intall kubernetes==9.0.1
# 注:选择匹配自己kubernetes集群的python client 版本
示例一: 获取集群内所有ingress域名
方法1. 使用 curl方法获取
curl --insecure  -X GET -H "Authorization: Bearer xxxxx" https://10.39.32.xx:6443/apis/extensions/v1beta1/ingresses
注:1. Authorization 里替换成自己的token地址,tke用户可在个人信息中获取token
2. 需要获取想要的信息 在https://github.com/kubernetes-client/python/blob/master/kubernetes/README.md api里搜索关键字即可,
image.png
3. 如搜索ingress 就可以得到有关ingress的使用方法 单击进去 还可以看到官方使用示例 如下
from __future__ import print_function
import time
import kubernetes.client
from kubernetes.client.rest import ApiException
from pprint import pprint

# Configure API key authorization: BearerToken
configuration = kubernetes.client.Configuration()
configuration.api_key['authorization'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['authorization'] = 'Bearer'
# create an instance of the API class
api_instance = kubernetes.client.ExtensionsV1beta1Api(kubernetes.client.ApiClient(configuration))
_continue = '_continue_example' # str
field_selector = 'field_selector_example' #
include_uninitialized = true # bool 
label_selector = 'label_selector_example' # str
limit = 56 # int 
pretty = 'pretty_example' # str
resource_version = 'resource_version_example' # str 
timeout_seconds = 56 # int
watch = true # bool 
try: 
    api_response = api_instance.list_ingress_for_all_namespaces(_continue=_continue, field_selector=field_selector, include_uninitialized=include_uninitialized, label_selector=label_selector, limit=limit, pretty=pretty, resource_version=resource_version, timeout_seconds=timeout_seconds, watch=watch)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling ExtensionsV1beta1Api->list_ingress_for_all_namespaces: %s\n" % e)
方法2. 编写获取指定空间的ingress 域名脚本
# !/usr/bin/python3
# -*- coding: utf-8 -*-
from pprint import pprint
import kubernetes.client
from kubernetes import client
from kubernetes.client.rest import ApiException


class KubernetesTools(object):
    def __init__(self):
        # 配置BearerToken 认证
        self.configuration = client.Configuration()
        self.configuration.host = 'https://10.39.32.xx:6443'
        self.configuration.verify_ssl = False
        # self.configuration.debug = True
        self.configuration.api_key = {"authorization": "Bearer " + self.get_token()}

    # 获取token: 先预定义token,后计划改写获取token方法
    def get_token(self):
        token = 'xxx'
        return token

    # 因为获取信息不同 使用的k8s api也不同 如 获取ingress 使用ExtensionsV1beta1Api 方法, 获取node节点使用 CoreV1Api 方法
    # def get_api_instance(self,instance):
    #     return 'kubernetes.client.' + instance + '(kubernetes.client.ApiClient(' + str(self.configuration) + '))'

    def get_ns_ingress(self, namespace):
        try:
            api_instance = kubernetes.client.ExtensionsV1beta1Api(kubernetes.client.ApiClient(self.configuration))
            result = api_instance.list_namespaced_ingress(namespace=namespace)
            ingress_list = []
            for ing in result.items:
                for url in ing.spec.rules:
                    ingress_list.append(url.host)
            return ingress_list
        except ApiException as e:
            return "Exception when calling ExtensionsV1beta1Api->list_namespaced_network_policy: %s\n" % e


if __name__ == '__main__':
    ingress = KubernetesTools().get_ns_ingress('xxx')
    print(ingress)

# 结果:['ai.baidu.cn', 'mc.baidu.cn', 'mservice.baidu.cn']



你可能感兴趣的:(python调用kubernetes api方法)