Kubernetes Python API 快速入门
Kubernetes提供了多种语言的客户端SDK, 其中Python API可以使用python语言来操纵集群,实现管理的自动化,非常方便、功能强大。
- 官方网站,https://github.com/kubernetes-client/python
- 注意:这里介绍的方法可以在kubernetes里面的JupyterHub中运行,pod里没有~/.kube目录,直接使用官网教程的load_kubeconfig方法会失败,这里改用从文件中载入配置参数,可以从网上操作集群。
1、安装
pip install git+https://github.com/kubernetes-client/python.git
或者:
pip install kubernetes
2、初始化
首先引入SDK支持库。然后将 ~/.kube 的config文件的内容复制到本地目录,保存为文件kubeconfig.yaml,然后运行下面的python代码:
from kubernetes import client, config
config.kube_config.load_kube_config(config_file="kubeconfig.yaml")
获得API的CoreV1Api版本对象:
v1 = client.CoreV1Api()
3、列出 namespaces
for ns in v1.list_namespace().items:
print(ns.metadata.name)
结果如下:
blockchain
default
ingress
istio-system
jupyter
ks-dev
kube-public
kube-system
kubeflow-dev
kubeflow013
monitor
rook-ceph-system
4、列出所有的services
print("Listing All services with their info:\n")
ret = v1.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 ))
5、列出所有的pods
print("Listing pods with their IPs:")
ret = v1.list_pod_for_all_namespaces(watch=False)
for i in ret.items:
print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))
结果如下:
Listing pods with their IPs:
10.244.0.155 default busybox-577868d55b-47pkx
10.244.0.164 default busybox-deployment-6c975f66c6-ndlt2
10.244.0.154 default nfs-server-nfs-server-provisioner-0
10.244.0.117 default nginx-65899c769f-tgthn
10.244.0.145 istio-system istio-citadel-7bdc7775c7-p4ww8
10.244.0.125 istio-system istio-cleanup-old-ca-dkvcx
10.244.0.144 istio-system istio-egressgateway-795fc9b47-plrjf
10.244.0.148 istio-system istio-ingress-84659cf44c-ztgl7
10.244.0.143 istio-system istio-ingressgateway-7d89dbf85f-mgm6h
10.244.0.126 istio-system istio-mixer-post-install-rfgrh
10.244.0.137 istio-system istio-pilot-66f4dd866c-qcntx
10.244.0.146 istio-system istio-policy-76c8896799-88t7b
10.244.0.139 istio-system istio-sidecar-injector-645c89bc64-d8mxc
10.244.0.159 istio-system istio-statsd-prom-bridge-949999c4c-rcpgt
10.244.0.138 istio-system istio-telemetry-6554768879-vqw86
10.244.0.158 istio-system prometheus-86cb6dd77c-dgp7c
10.244.0.141 jupyter hook-image-puller-1530505782-9lp9b
10.244.0.162 jupyter hub-7b46b56bb8-9l7qh
10.244.0.167 jupyter jupyter-supermap
10.244.0.129 jupyter proxy-757b9676bf-wngcd
192.168.199.249 kube-system etcd-supermap
10.244.0.140 kube-system flux-helm-operator-68f9f586f4-f8p2v
192.168.199.249 kube-system kube-apiserver-supermap
192.168.199.249 kube-system kube-controller-manager-supermap
10.244.0.156 kube-system kube-dns-58dd58c6c4-6hx59
192.168.199.249 kube-system kube-flannel-ds-sg9p6
192.168.199.249 kube-system kube-proxy-79l6g
192.168.199.249 kube-system kube-scheduler-supermap
10.244.0.119 kube-system kubernetes-dashboard-7d5dcdb6d9-xknlr
10.244.0.136 kube-system nginx-ingress-controller-67b9bf4c56-64lpc
10.244.0.134 kube-system nginx-ingress-default-backend-d676cbb5f-8q6t8
10.244.0.147 kube-system nvidia-device-plugin-daemonset-gzhvn
10.244.0.128 kube-system tiller-deploy-f44fbbb48-ff6qq
10.244.0.127 kubeflow-dev ambassador-d74c99b6f-28xnf
10.244.0.115 kubeflow-dev ambassador-d74c99b6f-92mkv
10.244.0.132 kubeflow-dev ambassador-d74c99b6f-mvb79
10.244.0.124 kubeflow-dev spartakus-volunteer-7976c7dd5c-rdd6q
10.244.0.130 kubeflow-dev tf-hub-0
10.244.0.135 kubeflow-dev tf-job-dashboard-7b57c549c8-h2sv2
10.244.0.122 kubeflow-dev tf-job-operator-594d8c7ddd-b75jw
10.244.0.116 kubeflow013 ambassador-5bfc88cc87-4njpb
10.244.0.123 kubeflow013 ambassador-5bfc88cc87-4s7jf
10.244.0.161 kubeflow013 ambassador-5bfc88cc87-x7bvz
10.244.0.118 kubeflow013 spartakus-volunteer-7976c7dd5c-btwtw
10.244.0.126 kubeflow013 tf-hub-0
10.244.0.121 kubeflow013 tf-job-dashboard-7b57c549c8-zwq92
10.244.0.125 kubeflow013 tf-job-operator-594d8c7ddd-ddmqn
10.244.0.120 monitor grafana-8698b89767-hd7sv
10.244.0.163 monitor prometheus-alertmanager-6df98765f4-gkhrh
10.244.0.131 monitor prometheus-kube-state-metrics-6584885ccf-2tlh4
192.168.199.249 monitor prometheus-node-exporter-tcqlx
10.244.0.160 monitor prometheus-pushgateway-5495f55cdf-qbf4v
10.244.0.165 monitor prometheus-server-5959898967-snztn
192.168.199.249 rook-ceph-system rook-ceph-agent-s5tjx
10.244.0.142 rook-ceph-system rook-ceph-mgr0-77d9fb65c-skljh
10.244.0.151 rook-ceph-system rook-ceph-mon3-sgqs8
10.244.0.157 rook-ceph-system rook-ceph-mon5-4rftd
10.244.0.153 rook-ceph-system rook-ceph-mon6-vx9ch
10.244.0.149 rook-ceph-system rook-ceph-operator-6c7687c646-l6r2r
10.244.0.133 rook-ceph-system rook-ceph-osd-frf6h
10.244.0.152 rook-ceph-system rook-discover-ptfwp