【问题标题】:Kubernetes client python API error 401 - UnauthorizedKubernetes 客户端 python API 错误 401 - 未经授权
【发布时间】:2021-01-07 21:41:37
【问题描述】:

我正在尝试创建一个 Python 客户端来连接并在 AKS 集群上的 pod 中执行命令,但是当尝试连接时,我从客户端收到消息错误 401 Unauthorized。有没有人在 API 中遇到过这个问题?

API 异常消息:

kubernetes.client.rest.ApiException: (401)
Reason: Unauthorized
HTTP response headers: HTTPHeaderDict({'Audit-Id': 'ba23c2b3-d65b-4200-b802-161300119860', 'Cache-Control': 'no-cache, private', 'Content-Type': 'application/json', 'Date': 'Mon, 21 Sep 2020 18:21:59 GMT', 'Content-Length': '129'})
HTTP response body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"Unauthorized","reason":"Unauthorized","code":401}

Python 客户端 API Kubernetes

    from __future__ import print_function
import time
import kubernetes.client
import os
from kubernetes.stream import stream
from kubernetes.client.rest import ApiException
from pprint import pprint


name = input("Insira o POD name cadastrado")
namespace = input("namespace do POD cadastrado")
NomeAtuador = input("Insira o nome do atuador a ser gerado o arquivo de configuração")


configuration = kubernetes.client.Configuration()
#configuration.verify_ssl=False
#configuration.assert_hostname = False
configuration.api_key_prefix['authorization'] = 'Bearer'
configuration.api_key['authorization'] = 'MYTOKEN'
configuration.ssl_ca_cert= 'PATH TO CA.CRT'
configuration.host = "HOST_IP:443"

api_instance = kubernetes.client.CoreV1Api(
    kubernetes.client.ApiClient(configuration))


exec_command = [
            '/etc/openvpn/setup/newClientCert.sh',
            (NomeAtuador), 
            'xxxxxxx']

resp = stream(api_instance.connect_post_namespaced_pod_exec(
    (name), (namespace), command=exec_command,
              stderr=True, stdin=True,
              stdout=True, tty=True))
print("Response: " + resp)

我正在使用 Python 3.8.2Kubernetes 1.16.13

【问题讨论】:

  • 您是否添加了正确的令牌?您可以使用config.load_kube_config() 检索它,然后使用kubernetes.client.configuration.Configuration._default.api_key
  • 嗨@MariuszK。!抱歉耽搁了。令牌是正确的,可能某些 RBAC 规则正在丢弃请求。为了解决这个问题,我需要将以下配置添加到集群配置中。 kubectl create clusterrolebinding serviceaccounts-cluster-admin \ --clusterrole=cluster-admin \ --group=system:serviceaccounts
  • 请考虑发布您的解决方案作为答案 - 它可能对遇到类似问题的其他人有所帮助。

标签: python azure kubernetes azure-aks


【解决方案1】:

为了解决我的问题,我将以下配置添加到集群配置中。

kubectl create clusterrolebinding serviceaccounts-cluster-admin \   --clusterrole=cluster-admin \   --group=system:serviceaccounts

【讨论】:

    猜你喜欢
    • 2018-07-07
    • 2022-11-11
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2013-09-13
    • 1970-01-01
    • 2021-01-09
    • 2019-05-06
    相关资源
    最近更新 更多