您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關如何在python3項目中使用kubernetes api,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
pip install kubernetes
首先引入SDK支持庫。然后將 ~/.kube 的config文件的內容復制到本地目錄,保存為文件kubeconfig.yaml,然后運行下面的python代碼。
[root@k8s-m ~]# cp .kube/config kubeconfig.yaml #使用 from kubernetes import client, config config.kube_config.load_kube_config(config_file="/root/kubeconfig.yaml")
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)
github:https://github.com/kubernetes-client/python/tree/master/examples
創建資源(提前寫好yaml資源清單)
#創建deploy [root@k8s-m ~]# 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() [root@k8s-m ~]# kubectl get pod NAME READY STATUS RESTARTS AGE mydeploy-6946c867dc-bgcld 1/1 Running 0 40s mydeploy-6946c867dc-rdnvj 1/1 Running 0 40s [root@k8s-m ~]# kubectl get deploy NAME READY UP-TO-DATE AVAILABLE AGE mydeploy 2/2 2 2 44s #創建pod例子(其它資源得自己查源碼自己找對應的API) [root@k8s-m ~]# 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-m ~]# 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
參考地址:/usr/local/python3/lib/python3.6/site-packages/kubernetes/client/
[root@k8s-m ~]# 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
#查看(read) [root@k8s-m ~]# 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-m ~]# 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(2019, 8, 30, 9, 13, 49, tzinfo=tzutc())}} nginx
[root@k8s-m ~]# 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-m ~]# python3 pp.py nginx:alpine
上述就是小編為大家分享的如何在python3項目中使用kubernetes api了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。