午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術(shù)文章
文章詳情頁

Python使用Kubernetes API訪問集群

瀏覽:171日期:2022-06-18 09:09:43

通過將身份認證令牌直接傳給 API 服務(wù)器,可以避免使用 kubectl 代理,像這樣:使用 grep/cut 方式:

# 查看所有的集群,因為你的 .kubeconfig 文件中可能包含多個上下文kubectl config view -o jsonpath=’{'Cluster nametServern'}{range .clusters[*]}{.name}{'t'}{.cluster.server}{'n'}{end}’# 從上述命令輸出中選擇你要與之交互的集群的名稱export CLUSTER_NAME='some_server_name'# 指向引用該集群名稱的 API 服務(wù)器APISERVER=$(kubectl config view -o jsonpath='{.clusters[?(@.name=='$CLUSTER_NAME')].cluster.server}')# 獲得令牌TOKEN=$(kubectl get secrets -o jsonpath='{.items[?(@.metadata.annotations[’kubernetes.io/service-account.name’]==’default’)].data.token}'|base64 -d)# 使用令牌玩轉(zhuǎn) APIcurl -X GET $APISERVER/api --header 'Authorization: Bearer $TOKEN' --insecure

客戶端庫:https://kubernetes.io/zh/docs/reference/using-api/client-libraries/

python舉例:

目錄結(jié)構(gòu)

Python使用Kubernetes API訪問集群

配置文件兩種方式

1、將集群中的~/.kube/config,重命名為kubeconfig.yaml代碼:

from kubernetes import client,configfrom kubernetes.stream import streamimport yamlconfig_file = r'D:UsersJackHePycharmProjectsJJk8sauthkubeconfig.yaml'config.kube_config.load_kube_config(config_file=config_file)Api_Instance = client.CoreV1Api()Api_Batch = client.BatchV1Api()#列出所有的namesapcefor ns in Api_Instance.list_namespace().items: print(ns.metadata.name)#列出所有的nodesdef list_node(): api_response = Api_Instance.list_node() data = {} for i in api_response.items:data[i.metadata.name] = {'name': i.metadata.name,'status': i.status.conditions[-1].type if i.status.conditions[-1].status == 'True' else 'NotReady','ip': i.status.addresses[0].address,'kubelet_version': i.status.node_info.kubelet_version,'os_image': i.status.node_info.os_image, } return datanodes = list_node()print(nodes)

2、使用token形式,獲取命令上文所示。代碼:

# -*- coding: utf-8 -*-from kubernetes.client import api_clientfrom kubernetes.client.apis import core_v1_apifrom kubernetes import client,configclass KubernetesTools(object): def __init__(self):self.k8s_url = ’https://192.168.1.56:6443’ def get_token(self):'''獲取token:return:'''with open(r’D:UsersJackHePycharmProjectsJJk8sauthtoken’, ’r’) as file: Token = file.read().strip(’n’) return Token def get_api(self):'''獲取API的CoreV1Api版本對象:return:'''configuration = client.Configuration()configuration.host = self.k8s_urlconfiguration.verify_ssl = Falseconfiguration.api_key = {'authorization': 'Bearer ' + self.get_token()}client1 = api_client.ApiClient(configuration=configuration)api = core_v1_api.CoreV1Api(client1)return api def get_namespace_list(self):'''獲取命名空間列表:return:'''api = self.get_api()namespace_list = []for ns in api.list_namespace().items: # print(ns.metadata.name) namespace_list.append(ns.metadata.name)return namespace_list def get_pod_list(self): api = self.get_api() print('Listing pods with their IPs:') ret = api.list_pod_for_all_namespaces(watch=False) for i in ret.items: print('%st%st%s' % (i.status.pod_ip, i.metadata.namespace, i.metadata.name)) def get_service_list(self):api = self.get_api()ret = api.list_service_for_all_namespaces(watch=False)for i in ret.items: print('%s t%s t%s t%s t%s n' %(i.kind,i.metadata.namespace,i.metadata.name,i.spec.cluster_ip,i.spec.ports))if __name__ == ’__main__’: namespace_list = KubernetesTools().get_namespace_list() pod_list = KubernetesTools().get_pod_list() service = KubernetesTools().get_service_list() print(namespace_list) print(pod_list) print(service)

到此這篇關(guān)于Python使用Kubernetes API訪問集群的文章就介紹到這了,更多相關(guān)Python Kubernetes API訪問集群內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 色屁屁ts人妖系列二区 | 色综综 | 岛国中文字幕 | 99久久综合 | 自拍偷拍精品 | 三级不卡视频 | 色婷婷亚洲综合 | 一区二区网 | 小萝莉末成年一区二区 | 在线艹 | 日本黄色大片在线观看 | 精品久久中文 | 一级中国毛片 | 99riav1国产精品视频 | 欧美成人免费观看视频 | 国产日韩一区二区 | 亚洲影视一区 | 亚洲自拍色图 | 天天cao| 中文字幕日产乱码中 | 国产91免费看 | 成人毛片在线观看 | 国产免费91| 亚洲欧美乱综合图片区小说区 | 在线视频中文字幕 | 91丨porny丨刺激 | 国产一区二区三区免费视频 | 极品闺蜜苏姨小说阅读 | 久久久国产一级片 | 黄色av网址在线观看 | 欧美亚洲天堂网 | 亚洲欧美国产精品专区久久 | 国产乱码一区二区三区 | 免费精品视频 | 欧美一区二区公司 | 久久精彩免费视频 | 可以在线看的av | 国产日韩三级 | 亚洲久久天堂 | 欧美区亚洲区 | 欧美视频免费在线 |