<p>I wish to access Kubernetes APIs from my local machine. I'm trying to get list of pods using kubernetes Rest APIs.</p>
<p>I've created a kubernetes cluster and some pods on <strong>Google Cloud</strong>.</p>
<p>On my local Windows machine, I've installed gcloud sdk and kubectl component with it.
I connected to my cluster using:</p>
<pre><code>gcloud container clusters get-credentials my-cluster --region us-central1 --project my-project
</code></pre>
<p>I can get the list of pods using <code>kubectl get pods</code></p>
<p>Although, I want to get pods list using kubernetes Rest APIs.</p>
<pre><code>GET https://kubernetes.default/api/v1/namespaces/default/pods
Authorization: Bearer my_access_token
</code></pre>
<p>But I think the request is not going through.</p>
<p>In Postman, I get the error:</p>
<p><code>Error: tunneling socket could not be established, cause=socket hang up</code></p>
<p>Or in Python using requests library (from my local machine), I get the error</p>
<p><code>HTTPSConnectionPool(host='kubernetes.default', port=443): Max retries exceeded with url: /api/v1/namespaces/default/pods (Caused by NewConnectionError('&lt;urllib3.connection.HTTPSConnection object at 0x00000277DCD04D90&gt;: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond'))</code></p>
<p>What am I missing here?</p>

user5155835
<p>The endpoint <code>https://kubernetes.default</code> only works if you want to access Kubernetes REST API from inside the cluster i.e from another pod. For accessing Kubernetes REST API from outside the kubernetes cluster i.e from your local machine you need to use the API server IP or host which is externally accessible i.e the one which is there in kubeconfig file.</p>
<p>For accessing it from outside the kubernetes cruster i.e from your local machine there are three ways referring from the docs <a href="https://kubernetes.io/docs/tasks/administer-cluster/access-cluster-api/#directly-accessing-the-rest-api" rel="nofollow noreferrer">here</a></p>
<ol>
<li><p>Run kubectl in proxy mode (recommended). This method is recommended, since it uses the stored apiserver location and verifies the identity of the API server using a self-signed cert. No man-in-the-middle (MITM) attack is possible using this method.</p>
<p><code>kubectl proxy --port=8080 &amp;</code></p>
<p><code>curl http://localhost:8080/api/v1/namespaces/default/pods</code></p>
</li>
<li><p>It is possible to avoid using kubectl proxy by passing an authentication token directly to the API server, like this:</p>
</li>
</ol>
<p>Check all possible clusters, as your .KUBECONFIG may have multiple contexts:</p>
<pre><code>kubectl config view -o jsonpath='{&quot;Cluster name\tServer\n&quot;}{range .clusters[*]}{.name}{&quot;\t&quot;}{.cluster.server}{&quot;\n&quot;}{end}'
</code></pre>
<p>Select name of cluster you want to interact with from above output:</p>
<pre><code>export CLUSTER_NAME=&quot;some_server_name&quot;
</code></pre>
<p>Point to the API server referring the cluster name</p>
<pre><code>APISERVER=$(kubectl config view -o jsonpath=&quot;{.clusters[?(@.name==\&quot;$CLUSTER_NAME\&quot;)].cluster.server}&quot;)
</code></pre>
<p>Gets the token value</p>
<pre><code>TOKEN=$(kubectl get secrets -o jsonpath=&quot;{.items[?(@.metadata.annotations['kubernetes\.io/service-account\.name']=='default')].data.token}&quot;|base64 --decode)
</code></pre>
<p>Explore the API with TOKEN</p>
<pre><code>curl -X GET $APISERVER/api/v1/namespaces/default/pods --header &quot;Authorization: Bearer $TOKEN&quot; --insecure
</code></pre>
<ol start="3">
<li>Using client library</li>
</ol>
<p>To use Python client, run the following command: <code>pip install kubernetes</code> See <a href="https://github.com/kubernetes-client/python" rel="nofollow noreferrer">Python Client Library page</a> for more installation options.</p>
<p>The Python client can use the same <code>kubeconfig</code> file as the <code>kubectl</code> CLI does to locate and authenticate to the API server. See this example:</p>
<pre><code>from kubernetes import client, config

config.load_kube_config()

v1=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>You can also do it the way you are doing without using kubeconfig file but it's more work and you need to use the kubernetes API Server IP or hostname from the kubeconfig file.</p>
