How to connect to kubernetes cluster without config file in Python SDK?

6/26/2019

I am making an app that will use K8S (EKS) to orchestrate the deployments on Cluster. Currently, I am developing using Minikube so its going as expected, but it uses $HOME/.kube/config file to load configuration. What are the other ways to connect to EKS?

Sample Code that I am using for Proof of Work:

from kubernetes import client, config

# Configs can be set in Configuration class directly or using helper utility
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))
-- Pur Ducaz
aws-eks
kubernetes
python

1 Answer

6/27/2019

As per documentation for "load_authentication":

This function goes through various authentication methods in user section of kube-config and stops if it finds a valid authentication method. The order of authentication methods is:

  • auth-provider (gcp, azure, oidc)
  • token field (point to a token file)
  • exec provided plugin
  • username/password

_load_authentication_ - Read authentication from kube-config user section if exists. Here is an example how to use client.Configuration() with barer token to authenticate.

Please take a look also for Authenticate with the Azure Management Libraries for Python

### update ###

For AWS specific env you can use AWS SDK for Python (Boto 3) and Create a kubeconfig for Amazon EKS

There is an community example - how to setup credentials for aws into kubeconfig or build the configuration using boto3

Hope this help.

-- Hanx
Source: StackOverflow