1、安装python3
github:https://github.com/kubernetes-client/pythonnode
或者参考以下链接快速安装:
https://blog.51cto.com/xushaojie/2479753python
2、安装kubernetes sdknginx
python3 -m pip install kubernetes -i https://pypi.doubanio.com/simple
3、认证
一、kubeconfig文件认证
首先引入SDK支持库。而后将 ~/.kube/config文件的内容复制到本地目录,保存为文件/root/kubeconfig.yaml,而后运行下面的python代码。git
[root@k8s-master python-k8s]#cp /root/.kube/config /root/kubeconfig.yaml
4、api使用
一、列出资源信息github
from kubernetes import client, config config.kube_config.load_kube_config(config_file="/root/kubeconfig.yaml") #获取API的CoreV1Api版本对象 v1 = client.CoreV1Api() #列出 namespaces for ns in v1.list_namespace().items: print(ns.metadata.name) #列出全部的services 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 )) #列出全部的pod 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)) #列出全部deploy ret = v1.list_deployments_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)) ##列出其余资源和以上相似,具体能够查看(kubectl api-resources)
二、建立k8s资源对象json
github:https://github.com/kubernetes-client/python/tree/master/examples 建立资源(提早写好yaml资源清单) #建立deploy [root@k8s-master python-k8s]# cat create_deploy.py from os import path import yaml from kubernetes import client, config def main(): config.load_kube_config() with open(path.join(path.dirname(__file__), "/root/deploy.yaml")) as f: dep = yaml.safe_load(f) k8s_apps_v1 = client.AppsV1Api() resp = k8s_apps_v1.create_namespaced_deployment( body=dep, namespace="default") print("Deployment created. status='%s'" % resp.metadata.name) main()
#建立pod例子(其它资源得查源码找对应的API)api
[root@k8s-master python-k8s] cat create_pod.py from os import path import yaml from kubernetes import client, config def main(): config.load_kube_config() with open(path.join(path.dirname(__file__), "/root/pod.yaml")) as f: dep = yaml.safe_load(f) k8s_core_v1 = client.CoreV1Api() resp = k8s_core_v1.create_namespaced_pod( body=dep, namespace="default") print("Pod created. status='%s'" % resp.metadata.name) if __name__ == '__main__': main()
[root@k8s-master python-k8s]# python3 create_pod.py Pod created. status='nginx-pod' [root@k8s-m ~]# kubectl get pod nginx-pod NAME READY STATUS RESTARTS AGE nginx-pod 1/1 Running 0 8s
三、删除资源(我这里展现pod例子,其它资源删除差很少)
参考地址:/usr/local/python3/lib/python3.6/site-packages/kubernetes/client/app
[root@k8s-master python-k8s]# cat dp.py from os import path import yaml from kubernetes import client, config def main(): config.load_kube_config() k8s_core_v1 = client.CoreV1Api() resp = k8s_core_v1.delete_namespaced_pod(namespace="default",name='nginx-pod') print("delete Pod ")
[root@k8s-m ~]# python3 dp.py delete Pod
四、查看资源(相似kubectl get pod xxx -o json)ide
[root@k8s-master python-k8s]# cat rp.py from os import path import yaml from kubernetes import client, config def main(): config.load_kube_config() k8s_core_v1 = client.CoreV1Api() resp = k8s_core_v1.read_namespaced_pod(namespace="default",name='nginx-pod') print("read Pod ") #详细信息 print(resp) #指定信息 print(resp.spec.containers[0].image) if __name__ == '__main__': main() [root@k8s-master python-k8s]# python3 rp.py |tail 'host_ip': '172.31.250.229', 'init_container_statuses': None, 'message': None, 'nominated_node_name': None, 'phase': 'Running', 'pod_ip': '10.244.167.134', 'qos_class': 'BestEffort', 'reason': None, 'start_time': datetime.datetime(2020, 7, 16, 2, 20, 15, tzinfo=tzutc())}} nginx
五、修改spa
[root@k8s-master python-k8s]# cat pp.py from os import path import yaml from kubernetes import client, config def main(): config.load_kube_config() k8s_core_v1 = client.CoreV1Api() old_resp = k8s_core_v1.read_namespaced_pod(namespace="default",name='nginx-pod') old_resp.spec.containers[0].image = "nginx:alpine" #修改镜像 new_resp = k8s_core_v1.patch_namespaced_pod(namespace="default",name='nginx-pod',body=old_resp) print(new_resp.spec.containers[0].image) if __name__ == '__main__': main() [root@k8s-master python-k8s]# python3 pp.py nginx:alpine