{"id":1115337,"date":"2025-01-08T18:05:48","date_gmt":"2025-01-08T10:05:48","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1115337.html"},"modified":"2025-01-08T18:05:50","modified_gmt":"2025-01-08T10:05:50","slug":"python%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96k8s%e6%95%b0%e6%8d%ae%e4%bf%a1%e6%81%af","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1115337.html","title":{"rendered":"python\u5982\u4f55\u83b7\u53d6k8s\u6570\u636e\u4fe1\u606f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25080133\/850a2f36-36c3-4b4d-a09f-60c4391b4ea4.webp\" alt=\"python\u5982\u4f55\u83b7\u53d6k8s\u6570\u636e\u4fe1\u606f\" \/><\/p>\n<p><p> <strong>Python\u83b7\u53d6K8s\u6570\u636e\u4fe1\u606f\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\uff1a\u4f7f\u7528Kubernetes Python\u5ba2\u6237\u7aef\u5e93\u3001\u8c03\u7528Kubernetes API\u3001\u5229\u7528\u7b2c\u4e09\u65b9\u5e93kubeconfig\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u4f7f\u7528Kubernetes Python\u5ba2\u6237\u7aef\u5e93<\/strong> \u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u4e0eKubernetes API\u4ea4\u4e92\u7684\u4fbf\u6377\u63a5\u53e3\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Kubernetes Python\u5ba2\u6237\u7aef\u5e93\u6765\u83b7\u53d6K8s\u6570\u636e\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001KUBERNETES PYTHON\u5ba2\u6237\u7aef\u5e93<\/h3>\n<\/p>\n<p><h4>1. \u5b89\u88c5Kubernetes Python\u5ba2\u6237\u7aef\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86Kubernetes Python\u5ba2\u6237\u7aef\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install kubernetes<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u914d\u7f6eKubernetes\u5ba2\u6237\u7aef<\/h4>\n<\/p>\n<p><p>\u914d\u7f6eKubernetes\u5ba2\u6237\u7aef\u7684\u6700\u7b80\u5355\u65b9\u6cd5\u662f\u4f7f\u7528\u9ed8\u8ba4\u7684Kubeconfig\u6587\u4ef6\uff0c\u8be5\u6587\u4ef6\u901a\u5e38\u4f4d\u4e8e<code>~\/.kube\/config<\/code>\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\u6765\u914d\u7f6e\u5ba2\u6237\u7aef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from kubernetes import client, config<\/p>\n<h2><strong>\u52a0\u8f7dKubeconfig\u6587\u4ef6<\/strong><\/h2>\n<p>config.load_kube_config()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u83b7\u53d6K8s\u6570\u636e\u4fe1\u606f<\/h4>\n<\/p>\n<p><p>\u4e00\u65e6\u5ba2\u6237\u7aef\u914d\u7f6e\u5b8c\u6210\uff0c\u5c31\u53ef\u4ee5\u5f00\u59cb\u83b7\u53d6K8s\u6570\u636e\u4fe1\u606f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><p><strong>\u83b7\u53d6\u6240\u6709Pod\u4fe1\u606f\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">v1 = client.CoreV1Api()<\/p>\n<p>pods = v1.list_pod_for_all_namespaces(watch=False)<\/p>\n<p>for pod in pods.items:<\/p>\n<p>    print(f&quot;{pod.metadata.namespace} {pod.metadata.name}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u83b7\u53d6\u6240\u6709\u8282\u70b9\u4fe1\u606f\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">v1 = client.CoreV1Api()<\/p>\n<p>nodes = v1.list_node()<\/p>\n<p>for node in nodes.items:<\/p>\n<p>    print(node.metadata.name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u83b7\u53d6\u6240\u6709\u547d\u540d\u7a7a\u95f4\u4fe1\u606f\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">v1 = client.CoreV1Api()<\/p>\n<p>namespaces = v1.list_namespace()<\/p>\n<p>for ns in namespaces.items:<\/p>\n<p>    print(ns.metadata.name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u8c03\u7528Kubernetes API<\/h3>\n<\/p>\n<p><h4>1. \u4f7f\u7528requests\u5e93\u76f4\u63a5\u8c03\u7528API<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528Kubernetes Python\u5ba2\u6237\u7aef\u5e93\uff0c\u8fd8\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528Kubernetes API\u6765\u83b7\u53d6\u6570\u636e\u4fe1\u606f\u3002\u9996\u5148\uff0c\u9700\u8981\u83b7\u53d6API\u670d\u52a1\u5668\u7684URL\u548c\u8bbf\u95ee\u4ee4\u724c\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528requests\u5e93\u8fdb\u884cAPI\u8c03\u7528\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>api_server = &quot;https:\/\/&lt;api-server-url&gt;&quot;<\/p>\n<p>token = &quot;&lt;your-access-token&gt;&quot;<\/p>\n<p>headers = {<\/p>\n<p>    &quot;Authorization&quot;: f&quot;Bearer {token}&quot;,<\/p>\n<p>    &quot;Accept&quot;: &quot;application\/json&quot;<\/p>\n<p>}<\/p>\n<h2><strong>\u83b7\u53d6\u6240\u6709Pod\u4fe1\u606f<\/strong><\/h2>\n<p>response = requests.get(f&quot;{api_server}\/api\/v1\/pods&quot;, headers=headers, verify=False)<\/p>\n<p>pods = response.json()<\/p>\n<p>for pod in pods[&#39;items&#39;]:<\/p>\n<p>    print(pod[&#39;metadata&#39;][&#39;name&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5904\u7406API\u54cd\u5e94<\/h4>\n<\/p>\n<p><p>API\u8c03\u7528\u8fd4\u56de\u7684\u6570\u636e\u901a\u5e38\u662fJSON\u683c\u5f0f\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684json\u5e93\u8fdb\u884c\u89e3\u6790\u548c\u5904\u7406\u3002\u786e\u4fdd\u9002\u5f53\u5904\u7406API\u54cd\u5e94\u548c\u9519\u8bef\uff0c\u4ee5\u786e\u4fdd\u4ee3\u7801\u7684\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5229\u7528\u7b2c\u4e09\u65b9\u5e93kubeconfig<\/h3>\n<\/p>\n<p><p><strong>\u5b89\u88c5kubeconfig\u5e93<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install kubeconfig<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import kubeconfig<\/p>\n<h2><strong>\u52a0\u8f7dKubeconfig\u6587\u4ef6<\/strong><\/h2>\n<p>config = kubeconfig.Kubeconfig()<\/p>\n<p>config.load_kube_config()<\/p>\n<h2><strong>\u83b7\u53d6\u96c6\u7fa4\u4fe1\u606f<\/strong><\/h2>\n<p>clusters = config.get_clusters()<\/p>\n<p>for cluster in clusters:<\/p>\n<p>    print(cluster.name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython\u83b7\u53d6K8s\u6570\u636e\u4fe1\u606f\u7684\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Kubernetes Python\u5ba2\u6237\u7aef\u5e93\u3001\u8c03\u7528Kubernetes API\u548c\u5229\u7528\u7b2c\u4e09\u65b9\u5e93kubeconfig\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528Kubernetes Python\u5ba2\u6237\u7aef\u5e93<\/strong> \u662f\u6700\u5e38\u89c1\u548c\u65b9\u4fbf\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u83b7\u53d6K8s\u96c6\u7fa4\u4e2d\u7684\u5404\u79cd\u8d44\u6e90\u4fe1\u606f\uff0c\u5982Pod\u3001\u8282\u70b9\u548c\u547d\u540d\u7a7a\u95f4\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u91c7\u7528\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u786e\u4fdd\u6709\u9002\u5f53\u7684\u6743\u9650\u8bbf\u95eeK8s API\uff0c\u5e76\u4e14\u5904\u7406API\u54cd\u5e94\u65f6\u8981\u8003\u8651\u5230\u53ef\u80fd\u7684\u9519\u8bef\u548c\u5f02\u5e38\u60c5\u51b5\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Python\u83b7\u53d6K8s\u6570\u636e\u4fe1\u606f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8fde\u63a5\u5230Kubernetes\u96c6\u7fa4\uff1f<\/strong><br \/>\u8981\u8fde\u63a5\u5230Kubernetes\u96c6\u7fa4\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>kubernetes<\/code> Python\u5ba2\u6237\u7aef\u5e93\u3002\u9996\u5148\uff0c\u786e\u4fdd\u60a8\u5df2\u5b89\u88c5\u6b64\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7<code>pip install kubernetes<\/code>\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\u3002\u8fde\u63a5\u65f6\uff0c\u60a8\u9700\u8981\u63d0\u4f9b\u96c6\u7fa4\u7684\u914d\u7f6e\u6587\u4ef6\u8def\u5f84\uff0c\u901a\u5e38\u4e3a<code>~\/.kube\/config<\/code>\u3002\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\u53ef\u4ee5\u5efa\u7acb\u8fde\u63a5\uff1a<\/p>\n<pre><code class=\"language-python\">from kubernetes import client, config\n\nconfig.load_kube_config()  # \u52a0\u8f7d\u672c\u5730kubeconfig\u6587\u4ef6\nv1 = client.CoreV1Api()  # \u521b\u5efaAPI\u5b9e\u4f8b\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u83b7\u53d6Kubernetes\u4e2d\u7684Pod\u4fe1\u606f\uff1f<\/strong><br \/>\u83b7\u53d6Pod\u4fe1\u606f\u975e\u5e38\u7b80\u5355\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528CoreV1Api\u5bf9\u8c61\u7684<code>list_namespaced_pod<\/code>\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u83b7\u53d6\u6307\u5b9a\u547d\u540d\u7a7a\u95f4\u4e2d\u6240\u6709Pod\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">namespace = &#39;default&#39;  # \u6307\u5b9a\u547d\u540d\u7a7a\u95f4\npods = v1.list_namespaced_pod(namespace)\nfor pod in pods.items:\n    print(f&quot;Pod Name: {pod.metadata.name}, Status: {pod.status.phase}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u904d\u5386\u6307\u5b9a\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6240\u6709Pod\uff0c\u5e76\u6253\u5370\u5b83\u4eec\u7684\u540d\u79f0\u548c\u72b6\u6001\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406Kubernetes API\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u4e0eKubernetes API\u4ea4\u4e92\u65f6\uff0c\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\u662f\u5f88\u91cd\u8981\u7684\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u5757\u6765\u6355\u6349\u5f02\u5e38\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">from kubernetes.client.rest import ApiException\n\ntry:\n    pods = v1.list_namespaced_pod(namespace)\nexcept ApiException as e:\n    print(f&quot;Exception when calling CoreV1Api-&gt;list_namespaced_pod: {e}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u60a8\u8bc6\u522b\u548c\u5904\u7406API\u8c03\u7528\u4e2d\u7684\u95ee\u9898\uff0c\u786e\u4fdd\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u66f4\u5177\u9c81\u68d2\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u83b7\u53d6K8s\u6570\u636e\u4fe1\u606f\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\uff1a\u4f7f\u7528Kubernetes Python\u5ba2\u6237\u7aef\u5e93\u3001\u8c03\u7528Kub [&hellip;]","protected":false},"author":3,"featured_media":1115346,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1115337"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1115337"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1115337\/revisions"}],"predecessor-version":[{"id":1115348,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1115337\/revisions\/1115348"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1115346"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1115337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1115337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1115337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}