環(huán)境概述
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對應(yīng)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 版本
示例一: 獲取集群內(nèi)所有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用戶可在個(gè)人信息中獲取token
2. 需要獲取想要的信息 在https://github.com/kubernetes-client/python/blob/master/kubernetes/README.md api里搜索關(guān)鍵字即可,
image.png
3. 如搜索ingress 就可以得到有關(guān)ingress的使用方法 單擊進(jìn)去 還可以看到官方使用示例 如下
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 認(rèn)證
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: 先預(yù)定義token别垮,后計(jì)劃改寫獲取token方法
def get_token(self):
token = 'xxx'
return token
# 因?yàn)楂@取信息不同 使用的k8s api也不同 如 獲取ingress 使用ExtensionsV1beta1Api 方法, 獲取node節(jié)點(diǎn)使用 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)
# 結(jié)果:['ai.baidu.cn', 'mc.baidu.cn', 'mservice.baidu.cn']