k8s api
1.go api
https://github.com/kubernetes-client/go
只支持Kubernetes 1.10 和Kubernetes 1.13后面没有再更新支持版本
2.python api
https://github.com/kubernetes-client/python
例如k8s集群为1.20版本
导入k8s包
pip install kubernetes==20.13.0
列出所有的namespace
from kubernetes import client, config config.kube_config.load_kube_config(config_file="kubeconfig.yaml") if __name__ == '__main__': # 获取API的CoreV1Api版本对象 v1 = client.CoreV1Api() # 列出 namespaces for ns in v1.list_namespace().items: print(ns.metadata.name)
2.
在本机获得apiserver地址及token
APISERVER=$(kubectl config view |grep server|cut -f 2- -d ":" | tr -d " ")
TOKEN=$(kubectl describe secrets $(kubectl get secrets -n kube-system |grep admin |cut -f1 -d ' ') -n kube-system |grep -E '^token' |cut -f2 -d':'|tr -d '\t'|tr -d ' ')
curl -H "Authorization: Bearer $TOKEN" $APISERVER/api/v1/namespaces/default/pods --insecure
浙公网安备 33010602011771号