<p>I'm trying to replicate the <code>kubectl get pods</code> command in Python3 using the <a href="https://github.com/kubernetes-client/python" rel="nofollow noreferrer">kubernetes python library</a>. Except, I'm working with a remote kubernetes cluster, NOT my localhost. The configuration host is a particular web address.</p>
<p>Here's what I tried:</p>
<pre><code>    v1 = kubernetes.client.CoreV1Api()
    print(&quot;Listing pods with their IPs:&quot;)
    ret = v1.list_pod_for_all_namespaces(watch=False)
    for i in ret.items:
        print(&quot;%s\t%s\t%s&quot; % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))
</code></pre>
<p>As recommended in the <a href="https://github.com/kubernetes-client/python" rel="nofollow noreferrer">documentation</a>. This however defaults to searching my localhost instead of the specific web address. I know I have access to this web address because the following runs totally 100% as expected:</p>
<pre><code>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 is optional and default to http://localhost
configuration.host = &quot;THE WEB HOST I'M USING&quot;

# Enter a context with an instance of the API kubernetes.client
with kubernetes.client.ApiClient(configuration) as api_client:
    # Create an instance of the API class
    api_instance = kubernetes.client.AdmissionregistrationApi(api_client)
    
    try:
        api_response = api_instance.get_api_group()
        pprint(api_response)
    except ApiException as e:
        print(&quot;Exception when calling AdmissionregistrationApi-&gt;get_api_group: %s\n&quot; % e)
</code></pre>
<p>What do you all think? How do I force it to check the pods of that host getting around the <code>localhost</code> default?</p>

<p>I know two solutions that may help in your case.
I will describe both of them and you may choose which one suits you best.</p>
<h3>Using kubeconfig file</h3>
<p>I recommend setting up a <code>kubeconfig</code> file which allows you to connect to a remote cluster.
You can find more information on how to configure it in the documentation: <a href="https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/" rel="nofollow noreferrer">Organizing Cluster Access Using kubeconfig Files</a></p>
<p>If you have a <code>kubeconfig</code> file configured, you can use the <a href="https://github.com/kubernetes-client/python-base/blob/master/config/kube_config.py#L782" rel="nofollow noreferrer">load_kube_config()</a> function to load authentication and cluster information from your <code>kubeconfig</code> file.</p>
<p>I've created a simple <code>list_pods_1.py</code> script to illustrate how it may work:</p>
<pre><code>$ cat list_pods_1.py
#!/usr/bin/python3.7
# Script name: list_pods_1.py
import kubernetes.client
from kubernetes import client, config

config.load_kube_config(&quot;/root/config&quot;)   # I'm using file named &quot;config&quot; in the &quot;/root&quot; directory

v1 = kubernetes.client.CoreV1Api()
print(&quot;Listing pods with their IPs:&quot;)
ret = v1.list_pod_for_all_namespaces(watch=False)
for i in ret.items:
    print(&quot;%s\t%s\t%s&quot; % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))
    

$ ./list_pods_1.py 
Listing pods with their IPs:
10.32.0.2       kube-system     coredns-74ff55c5b-5k28b
10.32.0.3       kube-system     coredns-74ff55c5b-pfppk
10.156.15.210   kube-system     etcd-kmaster
10.156.15.210   kube-system     kube-apiserver-kmaster
10.156.15.210   kube-system     kube-controller-manager-kmaster
10.156.15.210   kube-system     kube-proxy-gvxhq
10.156.15.211   kube-system     kube-proxy-tjxch
10.156.15.210   kube-system     kube-scheduler-kmaster
10.156.15.210   kube-system     weave-net-6xqlq
10.156.15.211   kube-system     weave-net-vjm7j
</code></pre>
<h3>Using Bearer token</h3>
<p>As described in this example - <a href="https://github.com/kubernetes-client/python/blob/6d4587e18064288d031ed9bbf5ab5b8245460b3c/examples/remote_cluster.py" rel="nofollow noreferrer">remote_cluster.py</a>:</p>
<blockquote>
<p>Is it possible to communicate with a remote Kubernetes cluster from a server outside of the cluster without kube client installed on it.The communication is secured with the use of <strong>Bearer token</strong>.</p>
</blockquote>
<p>You can see how to create and use the token in the <a href="https://kubernetes.io/docs/tasks/access-application-cluster/access-cluster/" rel="nofollow noreferrer">Accessing Clusters</a> documentation.</p>
<p>I've created simple <code>list_pods_2.py</code> script (
based on the <a href="https://github.com/kubernetes-client/python/blob/6d4587e18064288d031ed9bbf5ab5b8245460b3c/examples/remote_cluster.py" rel="nofollow noreferrer">remote_cluster.py</a> script) to illustrate how it may work:</p>
<pre><code>$ cat list_pods_2.py 
#!/usr/bin/python3.7
import kubernetes.client
from kubernetes import client, config
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

 # Define the barer token we are going to use to authenticate.
    # See here to create the token:
    # https://kubernetes.io/docs/tasks/access-application-cluster/access-cluster/
aToken = &quot;&lt;MY_TOKEN&gt;&quot;

    # Create a configuration object
aConfiguration = client.Configuration()

    # Specify the endpoint of your Kube cluster
aConfiguration.host = &quot;https://&lt;ENDPOINT_OF_MY_K8S_CLUSTER&gt;&quot;

    # Security part.
    # In this simple example we are not going to verify the SSL certificate of
    # the remote cluster (for simplicity reason)
aConfiguration.verify_ssl = False
    # Nevertheless if you want to do it you can with these 2 parameters
    # configuration.verify_ssl=True
    # ssl_ca_cert is the filepath to the file that contains the certificate.
    # configuration.ssl_ca_cert=&quot;certificate&quot;

aConfiguration.api_key = {&quot;authorization&quot;: &quot;Bearer &quot; + aToken}

    # Create a ApiClient with our config
aApiClient = client.ApiClient(aConfiguration)

    # Do calls
v1 = client.CoreV1Api(aApiClient)
print(&quot;Listing pods with their IPs:&quot;)
ret = v1.list_pod_for_all_namespaces(watch=False)
for i in ret.items:
    print(&quot;%s\t%s\t%s&quot; %
            (i.status.pod_ip, i.metadata.namespace, i.metadata.name))



$ ./list_pods_2.py 
Listing pods with their IPs:
10.32.0.2       kube-system     coredns-74ff55c5b-5k28b
10.32.0.3       kube-system     coredns-74ff55c5b-pfppk
10.156.15.210   kube-system     etcd-kmaster
10.156.15.210   kube-system     kube-apiserver-kmaster
10.156.15.210   kube-system     kube-controller-manager-kmaster
10.156.15.210   kube-system     kube-proxy-gvxhq
10.156.15.211   kube-system     kube-proxy-tjxch
10.156.15.210   kube-system     kube-scheduler-kmaster
10.156.15.210   kube-system     weave-net-6xqlq
10.156.15.211   kube-system     weave-net-vjm7j
</code></pre>
<p><strong>NOTE:</strong> As an example, I am using a token for the default service account (you will probably want to use a different <code>ServiceAcccount</code>), but to work it properly this <code>ServiceAccount</code> needs appropriate permissions.<br />
For example, you may add a <code>view</code> role to your <code>ServiceAccount</code> like this:</p>
<pre><code>$ kubectl create clusterrolebinding --serviceaccount=default:default --clusterrole=view default-sa-view-access
clusterrolebinding.rbac.authorization.k8s.io/default-sa-view-access created
</code></pre>
