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.
    config.load_kube_config()

    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()

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 : http://192.168.237.115:8080 How do I do that?

When I do kubectl config view , I get this :

apiVersion: v1
clusters:
- cluster:
    certificate-authority: /home/piyush/.minikube/ca.crt
    server: https://192.168.99.100:8443
  name: minikube
contexts:
- context:
    cluster: minikube
    user: minikube
  name: minikube
current-context: minikube
kind: Config
preferences: {}
users:
- name: minikube
  user:
    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
133 views

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

Example

from kubernetes import client, configuration
def main():
    configuration.host = "http://192.168.237.115:8080"
    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

    @property
    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

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


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

kube_two = K8s(configuration_yaml='~/.kube/config2')
kube_two.client.list_pod_for_all_namespaces(watch=False)
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=0.0.0.0 option to the line https://github.com/kubernetes/kubernetes/blob/v1.2.0/docs/getting-started-guides/coreos/azure/cloud_config_templates/kubernetes-cluster-main-nodes-template.yml#L218  Created ...READ MORE

answered Aug 30, 2018 in Kubernetes by Kalgi
• 35,750 points
30 views
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
87 views
+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
49 views
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
144 views
0 votes
2 answers

Access Kubernetes api from within a pod container

wget version: KUBE_TOKEN=$(</var/run/secrets/kubernetes.io/serviceaccount/token) wget -vO- ...READ MORE

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

© 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.