Kubernetes python client: authentication issue

前端 未结 3 1948
太阳男子
太阳男子 2020-12-09 13:35

We are using the kubernetes python client (4.0.0) in combination with google\'s kubernetes engine (master + nodepools run k8s 1.8.4) to periodically schedule workloads on ku

相关标签:
3条回答
  • 2020-12-09 13:46

    In order to authenticate to your API server, you can use Role Based Access Control (RBAC), which can define a series of roles to manage authentication and access to your API.

    This is based on granting roles and cluster roles to different users or service accounts through the use of bindings. These roles include some rules that represent a set of permissions, and can be defined to act on a namespace (roles) or an entire cluster (cluster roles).

    The first step to enable RBAC is to start your API server with the following option:

    --authorization-mode=RBAC
    

    You can define specific roles with the kubectl command. For example, if you want to grant an admin cluster role to a user (e.g. Bob) on a namespace (e.g. acme), you can use this command:

    kubectl create rolebinding bob-admin-binding --clusterrole=admin --user=bob --namespace=acme
    

    You can also define a cluster role for a user (e.g. root) to have admin privileges across an entire cluster:

    kubectl create clusterrolebinding root-cluster-admin-binding --clusterrole=cluster-admin --user=root
    

    If you want to use service accounts instead, you can use a command like this to grant roles to a service account:

    kubectl create rolebinding my-sa-view  --clusterrole=view --serviceaccount=my-namespace:my-sa --namespace=my-namespace
    

    You can check here for more information about RBAC, including all the possible roles and cluster roles you can grant to your user or service account.

    0 讨论(0)
  • 2020-12-09 13:57

    I have a python container using the Kubernetes client, and was looking for a way to have it use a service account when executing in cluster, but load a mounted kube config when executing locally. It took me a while to find load_incluster_config(), which will automatically configure based on the service account of the container when executing in cluster. I now switch on an env var when running locally. This might be helpful for you :

    https://github.com/kubernetes-client/python/blob/master/examples/in_cluster_config.py

    0 讨论(0)
  • 2020-12-09 13:59

    In the end we have solved this by using bearer token authentication, instead of relying on the default gcloud authentication method.

    Here are the steps that I did to achieve this.

    First create a service account in the desired namespace, by creating a file with the following content.

    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: <name_of_service_account>
    

    Then use this file to create the service account

    kubectl create -f <path_to_file> --namespace=<namespace_name>
    

    Each service account has a bearer token linked to it, which can be used for authentication. This bearer token is automatically mounted as a secret into the namespace. To find out what this token is, first find the name of the secret (is of the form <service_account_name>-token-<random_string>) and then use that name to get to content.

    # To search for out service account's token name
    kubectl get secrets --namespace=<namespace_name>
    
    # To find the token name
    kubectl describe secret/<secret_name>
    

    After this you should find out the ip address of the API server, and the Cluster CA certificate of the kubernetes cluster. This can be done by going to the kubernetes engine detail page on google cloud console. Copy the content of the certificate into a local file.

    You can now use the bearer token to authenticate via the kubernetes python client, as follows:

    from kubernetes import client
    
    configuration = client.Configuration()
    configuration.api_key["authorization"] = '<bearer_token>'
    configuration.api_key_prefix['authorization'] = 'Bearer'
    configuration.host = 'https://<ip_of_api_server>'
    configuration.ssl_ca_cert = '<path_to_cluster_ca_certificate>'
    
    v1 = client.CoreV1Api(client.ApiClient(configuration))
    
    0 讨论(0)
提交回复
热议问题