gpt4 book ai didi

python - kubernetes.client.rest.ApiException:(401)原因:未经授权;创建/删除GKE Pane 的IAM

转载 作者:行者123 更新时间:2023-12-02 12:26:09 27 4
gpt4 key购买 nike

我正在尝试阅读GKE集群中 Activity pods 的列表。我还希望能够使用python客户端以编程方式创建和/或删除Pod。我应该授予我的服务帐户哪些IAM访问权限才能完成此任务?由于我一直在尝试使它工作的几乎所有方法,因此该服务帐户当前具有Kubernetes Engine AdminKubernetes Engine Cluster AdminKubernetes Engine Developer访问权限。
这是一些示例代码,试图读取群集IP的“默认” namespace 中的Pod

import kubernetes
from oauth2client.client import GoogleCredentials


def get_access_config(cluster_ip):
# access the k8s cluster, https://kubernetes.io/docs/tasks/access-application-cluster/access-cluster/
credentials = GoogleCredentials.get_application_default()
access_token = credentials.get_access_token()

# Create a configuration object
access_configuration = kubernetes.client.Configuration()

# Specify the access configuration to access k8s cluster
access_configuration.host = f"https://{cluster_ip}:443"
access_configuration.verify_ssl = False
access_configuration.api_key = {"authorization": "Bearer " + access_token.access_token}
return access_configuration


def get_pod_list(access_configuration):
v1 = kubernetes.client.CoreV1Api(kubernetes.client.ApiClient(access_configuration))
print("Listing pods with their IPs:")
ret = v1.list_namespaced_pod(namespace='default')


cluster_ip = '0.0.0.0' # replace with your cluster_ip
access_configuration = get_access_config(cluster_ip)
get_pod_list(access_configuration)
每当我运行 get_pod_list函数时,都会得到以下回溯:
Traceback (most recent call last):   File "<input>", line 3, in
<module> File
"/Users/<user>/miniconda3/envs/<my_env>/lib/python3.7/site-packages/kubernetes/client/api/core_v1_api.py",
line 12803, in list_namespaced_pod
(data) = self.list_namespaced_pod_with_http_info(namespace, **kwargs) # noqa: E501 File "/Users/<user>/miniconda3/envs/<my_env>/lib/python3.7/site-packages/kubernetes/client/api/core_v1_api.py",
line 12905, in list_namespaced_pod_with_http_info
collection_formats=collection_formats) File "/Users/<user>/miniconda3/envs/<my_env>/lib/python3.7/site-packages/kubernetes/client/api_client.py",
line 345, in call_api
_preload_content, _request_timeout) File "/Users/<user>/miniconda3/envs/<my_env>/lib/python3.7/site-packages/kubernetes/client/api_client.py",
line 176, in __call_api
_request_timeout=_request_timeout) File "/Users/<user>/miniconda3/envs/<my_env>/lib/python3.7/site-packages/kubernetes/client/api_client.py",
line 366, in request
headers=headers) File "/Users/<user>/miniconda3/envs/<my_env>/lib/python3.7/site-packages/kubernetes/client/rest.py",
line 241, in GET
query_params=query_params) File "/Users/<user>/miniconda3/envs/<my_env>/lib/python3.7/site-packages/kubernetes/client/rest.py",
line 231, in request
raise ApiException(http_resp=r) kubernetes.client.rest.ApiException: (401) Reason: Unauthorized HTTP
response headers: HTTPHeaderDict({'Audit-Id':
'22bd2789-30d0-4be8-a211-277a39396de7', 'Content-Type':
'application/json', 'Date': 'Wed, 22 Jul 2020 22:12:17 GMT',
'Content-Length': '129'}) HTTP response body:
{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"Unauthorized","reason":"Unauthorized","code":401} ```

如果我的IAM访问权限不是问题,那么是什么导致此401“未经授权”错误,我该如何解决?
编辑/更新
即使按照当前答案中的说明进行操作,我仍然会遇到相同的错误。这些说明似乎是必需的,但不足以解决问题。
我以为我的 GOOGLE_APPLICATION_CREDENTIALS路径变量可能指向错误的JSON文件,但是我检查了以下代码,并指向了正确的文件:
import os
os.environ('GOOGLE_APPLICATION_CREDENTIALS')
检查之后,我尝试了 https://github.com/kubernetes-client/python的代码的简单版本:
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_namespaced_pod(namespace='default')
该版本适用!!!那么,将 GoogleCredentials加载到 api_client中的 kubernetes.client.CoreV1Api版本有什么问题/差异? GoogleCredentials.get_application_default()行是否以某种方式寻找并找到了错误的凭证?如果是这样,如何将其指向正确的凭据?

最佳答案

您没有提供步骤❓,因此这对我有用,我的服务帐户具有“管理员GKE”访问权限:
创建服务帐户凭据:

gcloud iam service-accounts keys create cred.json --iam-account  mytestserviceaccount@project.iam.gserviceaccount.com
created key [xxxxxxxxxx] of type [json] as [cred.json] for [mytestserviceaccount@project.iam.gserviceaccount.com]``
激活服务帐户:
gcloud auth activate-service-account mytestserviceaccount@project.iam.gserviceaccount.com --key-file=cred.json
您可以通过以下方式进行检查:
gcloud auth list
Credentialed Accounts
ACTIVE ACCOUNT
* mytestserviceaccount@dproject.iam.gserviceaccount.com
name@sample.com
然后:
gcloud container clusters get-credentials gke-cluster-name --zone us-central1-a --project project
确保您允许用户管理员访问您的服务帐户:
image1
另外,该帐户具有Kubernetes管理员角色:
image2
测试的好方法:
  • kubectl get pods应该可以工作。

  • 我复制了上面的代码,它可与我的一个GKE集群一起使用。
    ✌️

    关于python - kubernetes.client.rest.ApiException:(401)原因:未经授权;创建/删除GKE Pane 的IAM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63044154/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com