<p>I wrote a service to retrieve some information from the Kubernetes cluster. Below is a snippet from the <code>kubernetes_service.py</code> file that works perfectly when I run it on my local machine.</p>
<pre class="lang-py prettyprint-override"><code>from kubernetes.client.rest import ApiException
from kubernetes import client, config
from exceptions.logs_not_found_exceptions import LogsNotFound
import logging

log = logging.getLogger(&quot;services/kubernetes_service.py&quot;)


class KubernetesService:
    def __init__(self):
        super().__init__()
        config.load_kube_config()
        self.api_instance = client.CoreV1Api()

    def get_pods(self, body):
        try:
            api_response = self.api_instance.list_namespaced_pod(namespace=body['namespace'])
            dict_response = api_response.to_dict()
            pods = []
            for item in dict_response['items']:
                pods.append(item['metadata']['name'])

            log.info(f&quot;Retrieved the pods: {pods}&quot;)
            return pods
        except ApiException as e:
            raise ApiException(e)

    def get_logs(self, body):
        try:
            api_response = self.api_instance.read_namespaced_pod_log(name=body['pod_name'], namespace=body['namespace'])
            tail_logs = api_response[len(api_response)-16000:]

            log.info(f&quot;Retrieved the logs: {tail_logs}&quot;)
            return tail_logs
        except ApiException:
            raise LogsNotFound(body['namespace'], body['pod_name'])
</code></pre>
<p>When creating the docker image using Dockerfile, it also installed kubectl. Below is my Dockerfile.</p>
<pre><code>FROM python:3.8-alpine
RUN mkdir /app
WORKDIR /app

COPY requirements.txt .
RUN pip install -r requirements.txt &amp;&amp; rm requirements.txt

RUN apk add curl openssl bash --no-cache
RUN curl -LO &quot;https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl&quot; \
    &amp;&amp; chmod +x ./kubectl \
    &amp;&amp; mv ./kubectl /usr/local/bin/kubectl

COPY . .
EXPOSE 8087
ENTRYPOINT [ &quot;python&quot;, &quot;bot.py&quot;]
</code></pre>
<p>To grant the container permissions to run the command <code>kubectl get pods</code> I added the role in the deployment.yml file:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Service
metadata:
  name: pyhelper
spec:
  selector:
    app: pyhelper
  ports:
    - protocol: &quot;TCP&quot;
      port: 8087
      targetPort: 8087
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: pyhelper
spec:
  selector:
    matchLabels:
      app: pyhelper
  replicas: 1
  template:
    metadata:
      labels:
        app: pyhelper
    spec:
      serviceAccountName: k8s-101-role
      containers:
        - name: pyhelper
          image: **********
          imagePullPolicy: Always
          ports:
            - containerPort: 8087
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: k8s-101-role
subjects:
  - kind: ServiceAccount
    name: k8s-101-role
    namespace: ind-iv
roleRef:
  kind: ClusterRole
  name: cluster-admin
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: k8s-101-role
</code></pre>
<p>At the start up of the container it returns the error <code>kubernetes.config.config_exception.ConfigException: Invalid kube-config file. No configuration found</code> at the line <code>config.load_kube_config()</code> in the <code>kubernetes_service.py</code> file. I checked the config file by running the command <code>kubectl config view</code> and the file is indeed empty. What am I doing wrong here?
Empty config file:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
clusters: null
contexts: null
current-context: &quot;&quot;
kind: Config
preferences: {}
users: null
</code></pre>
<p>Also tried to run the command <code>kubectl get pods</code> in the shell of the container and it successfully returned the pods.</p>

<p>I believe you'll want <a href="https://github.com/kubernetes-client/python-base/blob/3aa8b4c94282707a20482f71e86624f3b39a2cc6/config/__init__.py#L24" rel="nofollow noreferrer"><code>kubernetes.config.load_config</code></a> which differs from the <code>load_kube_config</code> you're currently using in that the package-level one looks for any <code>$HOME/.kube/config</code> as you expected, but <em>then</em> falls back to the in-cluster config as the <code>ServiceAccount</code> usage expects</p>
<pre class="lang-py prettyprint-override"><code>from kubernetes.config import load_config

class KubernetesService:
    def __init__(self):
        super().__init__()
        load_config()
</code></pre>
