Request Kubernetes API server

0 votes

So I have just started using Kubernetes API server and I tried this example :

from kubernetes import client, config
def main():
    # Configs can be set in Configuration class directly or using helper
    # utility. If no argument provided, the config will be loaded from
    # default location.

    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,
if __name__ == '__main__':

This worked but it returned the pods that are on my local minikube, I want to get the pods that are at the kubernetes server here : How do I do that?

When I do kubectl config view , I get this :

apiVersion: v1
- cluster:
    certificate-authority: /home/piyush/.minikube/ca.crt
  name: minikube
- context:
    cluster: minikube
    user: minikube
  name: minikube
current-context: minikube
kind: Config
preferences: {}
- name: minikube
    client-certificate: /home/piyush/.minikube/apiserver.crt
    client-key: /home/piyush/.minikube/apiserver.key

How do i request kubernetes api server 

Aug 31, 2018 in Kubernetes by lina
• 8,100 points

3 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

config.load_kube_config() takes context as a parameter. If passed None (the default) then the current context will be used. Your current context is probably your minikube.

answered Aug 31, 2018 by Kalgi
• 35,750 points
0 votes

Try one of these:

1) Configure your kubectl
2) Configure python client directly


from kubernetes import client, configuration
def main(): = ""
    configuration.api_key_prefix['authorization'] = "Bearer"
    configuration..api_key['authorization'] = "YOUR_TOKEN"
    v1 = client.CoreV1Api()
answered Aug 31, 2018 by Nilesh
• 6,900 points
0 votes

Create a api wrapper. This way you can pass through different yaml configuration files, that I imagine may have different hosts

import yaml
from kubernetes import client
from kubernetes.client import Configuration
from kubernetes.config import kube_config

class K8s(object):
    def __init__(self, configuration_yaml):
        self.configuration_yaml = configuration_yaml
        self._configuration_yaml = None

    def config(self):
        with open(self.configuration_yaml, 'r') as f:
            if self._configuration_yaml is None:
                self._configuration_yaml = yaml.load(f)
        return self._configuration_yaml

    def client(self):
        k8_loader = kube_config.KubeConfigLoader(self.config)
        call_config = type.__call__(Configuration)
        return client.CoreV1Api()

# Instantiate your kubernetes class and pass in config
kube_one = K8s(configuration_yaml='~/.kube/config1')

kube_two = K8s(configuration_yaml='~/.kube/config2')
answered Aug 31, 2018 by Hannah
• 14,080 points

Related Questions In Kubernetes

0 votes
1 answer

Not able to access kubernetes api from a pod in azure

Follow these steps Add --bind-address= option to the line  Created ...READ MORE

answered Aug 30, 2018 in Kubernetes by Kalgi
• 35,750 points
0 votes
1 answer

Kubernetes API authentication

Best way to retrieve the token is: kubectl ...READ MORE

answered Sep 3, 2018 in Kubernetes by Kalgi
• 35,750 points
+1 vote
4 answers

Kubernetes- HTTPS API return `Unauthorized`

One way to do this is by ...READ MORE

answered Sep 5, 2018 in Kubernetes by u_told_me_to
0 votes
5 answers
+13 votes
2 answers
0 votes
1 answer
0 votes
2 answers

Access Kubernetes API using minKube

Try these commands: kubectl proxy --port=8080 You can then ...READ MORE

answered Aug 28, 2018 in Kubernetes by Hannah
• 14,080 points
0 votes
2 answers

Access Kubernetes api from within a pod container

wget version: KUBE_TOKEN=$(</var/run/secrets/ wget -vO- ...READ MORE

answered Aug 29, 2018 in Kubernetes by Nilesh
• 6,900 points

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.