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,220 points
1,427 views

3 answers to this question.

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
• 52,350 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
• 7,060 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
• 18,520 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
• 52,350 points
1,015 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
• 52,350 points
921 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
2,771 views
0 votes
5 answers

Kubernetes ingress kepps returning 502 server error

I have added "/" endpoints in each ...READ MORE

answered May 6, 2019 in Kubernetes by Oishi
12,817 views
+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
4,036 views
+2 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
• 18,520 points
1,889 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
• 7,060 points
2,756 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP