<p>I am trying to use the python API for kubernetes and I can't seem to be able to execute the request. I think the pipeline is not clear to me.</p>

<p>I am following the steps here: <a href="https://stackoverflow.com/questions/48151388/kubernetes-python-client-authentication-issue">Kubernetes python client: authentication issue</a></p>

<p>On the remote server:</p>

<ul>
<li>I setup my service account and generated the secret as described in the link</li>
<li>I added the token to my code</li>
</ul>

<p>I am getting connection refused. </p>

<ul>
<li>Am I supposed to import any information from the cluster into the local client? </li>
<li>Is the port okay?</li>
</ul>

<pre class="lang-py prettyprint-override"><code>from kubernetes import client, config
def main():
    configuration = client.Configuration() 

    configuration.host = "http://my_ip:8080"
    configuration.api_key_prefix['authorization'] = "Bearer"
    configuration.api_key['authorization'] = "my_token"
    v1 = 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))

if __name__ == '__main__':
    main()
</code></pre>

<p>Output:</p>

<pre class="lang-sh prettyprint-override"><code>   raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='xx.xx.xx.xx', port=8080): Max retries exceeded with url: /api/v1/pods?watch=False (Caused by NewConnectionError('&lt;urllib3.connection.HTTPConnection object at 0x1118e5668&gt;: Failed to establish a new connection: [Errno 61] Connection refused'
</code></pre>

<p>Current local client kubectl config view:</p>

<pre><code>apiVersion: v1
clusters: []
contexts: []
current-context: ""
kind: Config
preferences: {}
users: []
</code></pre>

Ademord
<p>First, check if kubectl is configured correctly, like in the excerpt below from the <a href="https://kubernetes.io/docs/tasks/tools/install-kubectl/#configure-kubectl" rel="nofollow noreferrer">docs here</a>.</p>

<blockquote>
  <h2>Configure kubectl</h2>
  
  <p>In order for kubectl to find and access a Kubernetes cluster, it needs a kubeconfig file, which is created automatically when you create a cluster using <code>kube-up.sh</code> or successfully deploy a Minikube cluster. [...] By default, kubectl configuration is located at <code>~/.kube/config</code>.</p>
  
  <h2>Check the kubectl configuration</h2>
  
  <p>Check that kubectl is properly configured by getting the cluster state:</p>
  
  <p><code>kubectl cluster-info</code></p>
  
  <p>If you see a URL response, kubectl is correctly configured to access your cluster. 
  If you see a message similar to the following, kubectl is not correctly configured or not able to connect to a Kubernetes cluster.</p>
  
  <p><code>The connection to the server &lt;server-name:port&gt; was refused - did you specify the right host or port?</code></p>
</blockquote>

<hr>

<p>Because it looks like it isn't connected to a cluster. If it doesn't give back a URL, you can get the <code>config</code> file from your remote server (from the <code>$HOME/.kube</code> directory). </p>

<p>You can put that file on your local machine, similarly named <code>$HOME/.kube/config</code>.</p>

<p>Then you can load that config file in your Python script with:</p>

<pre><code>def main():
    config.load_kube_config()
</code></pre>

<p>Example can be found <a href="https://medium.com/programming-kubernetes/building-stuff-with-the-kubernetes-api-part-3-using-python-aea5ab16f627" rel="nofollow noreferrer">here</a>.</p>
