简体   繁体   English

无法连接到kubernetes python api - 找不到.kube / config文件

[英]Unable to connect to kubernetes python api - .kube/config file not found

I'm having trouble connecting to the kubernetes python client even though I'm following the examples here in the api. 即使我在api中遵循这里的示例,我也无法连接到kubernetes python客户端。

Basically this line can't connect to the kubernetes client: 基本上这行不能连接到kubernetes客户端:

config.load_kube_config()

What I'm doing: 我在做什么:

I have a Dockerfile file like this that I'm building my image with. 我有一个这样的Dockerfile文件,我正在构建我的图像。 This is just a simple python/flask app. 这只是一个简单的python / flask应用程序。

FROM python:2

RUN mkdir -p /usr/src/app

WORKDIR /usr/src/app

COPY requirements.txt /usr/src/app/

RUN pip install --no-cache-dir -r requirements.txt

COPY . /usr/src/app

EXPOSE 5000

CMD [ "python", "./app.py" ]

This is my requirements.txt: 这是我的要求.txt:

Flask==1.0.2
gunicorn==19.8.1
kubernetes==6.0.0
requests # Apache-2.0

After building the Dockerfile it outputs: 构建Dockerfile后,它输出:

    Successfully built a2590bae9fd9
    Successfully tagged testapp:latest

but when I do docker run a2590bae9fd9 I receive an error: 但当我做docker run a2590bae9fd9我收到一个错误:

Traceback (most recent call last):
  File "./app.py", line 10, in <module>
    config.load_kube_config()
  File "/usr/local/lib/python2.7/site-     packages/kubernetes/config/kube_config.py", line 470, in load_kube_config
    config_persister=config_persister)
   File "/usr/local/lib/python2.7/site-   packages/kubernetes/config/kube_config.py", line 427, in     _get_kube_config_loader_for_yaml_file
    with open(filename) as f:
 IOError: [Errno 2] No such file or directory: '/root/.kube/config'

I thought it might've been my python directory but I checked and its running in /usr/local/bin/python. 我以为它可能是我的python目录,但我检查并在/ usr / local / bin / python中运行。

I'm really stumped - any suggestions/tips? 我真的很难过 - 任何建议/提示? thank you. 谢谢。

You don't want config.load_kube_config() , you want config.load_incluster_config() 你不想要config.load_kube_config() ,你想要config.load_incluster_config()

If you need to distinguish between your setup and when it's running in a Pod , one mechanism is if os.getenv('KUBERNETES_SERVICE_HOST'): config.load_incluster_config() since that for sure will be in the environment while in a Pod , and is unlikely to be in your local environment. 如果你需要区分你的设置和它在Pod运行的时间,一种机制是if os.getenv('KUBERNETES_SERVICE_HOST'): config.load_incluster_config()因为肯定会在Pod中的环境中,并且不太可能在你当地的环境中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 gcp python 函数 - 删除 kubernetes 命名空间,并获取 .kube/config 文件 - gcp python function - delete kubernetes namespaces, and get .kube/config file 使用 Kubernetes Python 客户端加载 kubect 配置文件返回错误:无效的 kube-config 文件 - Load kubect config file using Kubernetes Python client returns error: Invalid kube-config file 如何在Python SDK中没有配置文件的情况下连接到Kubernetes集群? - How to connect to kubernetes cluster without config file in Python SDK? 如何修复&#39;没有这样的文件或目录:&#39;/ home / jenkins / .kube / config&#39;&#39;在python openshift休息客户端使用load_incluster_config时 - How to fix 'No such file or directory: '/home/jenkins/.kube/config'' when using load_incluster_config in python openshift rest client FileNotFoundError:[错误2]没有这样的文件或目录:&#39;/root/.kube/config&#39;但文件存在 - FileNotFoundError: [Errno 2] No such file or directory: '/root/.kube/config' but file exists 如何通过 Python Kubernetes 客户端连接 microk8s API? - How to connect to microk8s API through Python Kubernetes client? Python 无法在配置文件夹中找到 mplstyle 文件 - Python unable to locate mplstyle file in config folder Python:在 API 中放置配置文件的位置 - Python: Where to place the config file in an API 无法运行 pyinstaller 创建的 exe 文件,显示错误“google-api-python-client was not found” - Unable to run exe file created by pyinstaller, showing error "google-api-python-client was not found" 尝试读取配置文件以连接到 twitter API - Trying to read a config file in order to connect to twitter API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM