Kubernetes API server

6/22/2017

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

I know this is for the local cluster I set up. I want to know how to modify this to make api requests to kubernetes server on http://192.168.237.115:8080

-- vectar
kubernetes
python

4 Answers

6/22/2017

Can you show me the file ~/.kube/config

If you update the API server in it, the python module kubernetes will automatically pick up the new API server you nominated.

- cluster:
    certificate-authority: [Update real ca.crt here]
    server: http://192.168.237.115:8080

There are other changes in ~/.kube/config as well, you'd better get the config from the remote kubernetes server directly.

After successfully config with remote kubernetes API servers, you should be fine to run kubectl and get the deployments, deamons, etc.

Then you should be fine to run with python kubernetes SDK

-- BMW
Source: StackOverflow

6/29/2017

I have two solution for you:

  1. [prefered] Configure your kubectl (i.e. ~/.kube/config) file. After kubectl works with your cluster, python client should automatically work with load_kube_config. See here for configuring kubectl: https://kubernetes.io/docs/tasks/administer-cluster/share-configuration/

  2. You can configure python client directly. For a complete list of configurations, look at: https://github.com/kubernetes-client/python-base/blob/8704ce39c241f3f184d01833dcbaf1d1fb14a2dc/configuration.py#L48

You may need to set some of those configuration for your client to connect to your cluster. For example, if you don't have any certificate or SSL enabled:

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

You may need to set other configurations such as username, api_key, etc. That's why I think if you follow first solution it would be easier.

-- Mehdy
Source: StackOverflow

5/17/2018

You can actually create a simple 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)

Also another neat reference in libcloud. https://github.com/apache/libcloud/blob/trunk/libcloud/container/drivers/kubernetes.py.

Good luck! Hope this helps! :)

-- MartianE
Source: StackOverflow

6/22/2017

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.

See here: https://github.com/kubernetes-incubator/client-python/blob/436351b027df2673869ee00e0ff5589e6b3e2b7d/kubernetes/config/kube_config.py#L283

config.load_kube_config(context='some context')

If you are not familiar with Kubernetes contexts, Kubernetes stores your configuration under ~/.kube/config (default location). In it you will find context definition for every cluster you may have access to. A field called current-context defines your current context.

You can issue the following commands:

kubectl config current-context to see the current context

kubectl config view to view all the configuration

-- Chen Fisher
Source: StackOverflow