我正在尝试使用kubernetes python 库kubectl get pods在 Python3 中复制该命令。除此之外,我正在使用远程 kubernetes 集群,而不是我的本地主机。配置主机是一个特定的网址。
这是我尝试过的:
v1 = kubernetes.client.CoreV1Api()
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))
Run Code Online (Sandbox Code Playgroud)
正如文档中建议的那样。然而,这默认搜索我的本地主机而不是特定的网址。我知道我可以访问该网址,因为以下内容完全 100% 按预期运行:
import time
import kubernetes.client
from kubernetes.client.rest import ApiException
from pprint import pprint
configuration = kubernetes.client.Configuration()
# Configure API key authorization: BearerToken
configuration.api_key['authorization'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
configuration.api_key_prefix['authorization'] = 'Bearer'
# Defining host …Run Code Online (Sandbox Code Playgroud)