kube-config文件无效。在pod中使用kubernetes客户端python时找不到配置

我正在尝试从 pod 获取 kubernetes api 服务器。

这是我的 pod 配置

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: test
spec:
  schedule: "*/5 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: test
            image: test:v5
            env:
            imagePullPolicy: IfNotPresent
            command: ['python3']
            args: ['test.py']
          restartPolicy: OnFailure

这是我在 test.py 中的 kubernetes-client python 代码

from kubernetes import client, config

# Configs can be set in Configuration class directly or using helper utility
config.load_kube_config()

v1 = client.CoreV1Api()
print("Listing pods with their IPs:")
ret = v1.list_pod_for_all_namespaces(watch=False)
for i in ret.items:
    print("%st%st%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))

但我收到此错误:

kubernetes.config.config_exception.ConfigException: Invalid kube-config file. No configuration found.

回答

在集群中运行时,您需要load_incluster_config()改为。


以上是kube-config文件无效。在pod中使用kubernetes客户端python时找不到配置的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>