【问题标题】:AKS cluster pods kube config locationAKS 集群 pods kube 配置位置
【发布时间】:2020-12-08 21:21:45
【问题描述】:

我正在尝试使用使用 kubernetes client library 的 C# 辅助服务在我的 AKS 群集上执行一些操作。目前我的服务正在集群中的单个 pod 上运行。当我尝试执行CreateSecret 操作时,我得到一个403 异常

我尝试获取一个不记名令牌并用它来设置 KubeConfig 的 AccessToken,但这也不起作用。

我想知道是否有一种方法可以从我的 pod 访问 kubeconfig(我猜它只在主节点上可用?)或者我可以指向不同的配置位置?

【问题讨论】:

  • HTTP 错误 403 被禁止。通常意味着您没有对数据的访问权限或您的身份验证错误(令牌可能错误)。通常我建议在 c# 应用程序开发之前先使用供应商工具。如果您让供应商工具正常工作,则使用像 wireshark 或 fiddler 这样的嗅探器,并将工作工具中的标头与 c# 标头进行比较。使 c# 看起来像供应商标头。 c# 中的默认标头与其他应用程序不同。 TLS 通常用于身份验证,6 月份 TLS 1.0/1,1 已过时,您必须使用 1.2。您必须使用支持 1.2 的 API。

标签: c# kubernetes kubectl azure-aks


【解决方案1】:

我建议使用服务帐户而不是 kubeconfg,因为您在集群内将应用程序作为 pod 运行。

var config = KubernetesClientConfiguration.InClusterConfig()

以上代码将使用部署 pod 的命名空间中的 default 服务帐户。您将收到 Forbidden 错误并解决您需要定义 RBAC 以向服务帐户提供授权的问题。下面是 RoleRoleBinding 假设您使用 default 命名空间来部署 pod。

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: default
  name: secret-creator
rules:
- apiGroups: [""]
  resources: ["secrets"]
  verbs: ["create"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: role-binding
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: secret-creator
subjects:
- kind: ServiceAccount
  name: default
  namespace: default

【讨论】:

  • 我尝试了上面的 Role 和 RoleBinding,但我仍然可以从我的 KubeClient 看到这个错误:Error in KubeClient :{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"secrets \"test\" is forbidden: User \"system:serviceaccount:default:default\" cannot get resource \"secrets\" in API group \"\" in the namespace \"istio-system\"","reason":"Forbidden","details":{"name":"test","kind":"secrets"},"code":403} 另外,我必须在 Istio-System 命名空间和我的服务中创建秘密正在使用默认命名空间的 pod 上运行。
  • 在阅读有关 ClusterRole 的文章后,我将上面的 yaml 修改为:apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: secret-creator rules: - apiGroups: [""] resources: ["secrets"] verbs: ["create"] --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: name: role-binding roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: secret-creator subjects: - kind: ServiceAccount name: default namespace: default 但这也没有用。
  • 将其标记为 ClusterRole 有效,只是在我添加的动词中,列出和删除也是如此,因为我也在使用它们,但它们仍在抛出 403。谢谢@Arghya 的帮助!
猜你喜欢
  • 2018-11-09
  • 2021-12-24
  • 1970-01-01
  • 2019-11-07
  • 1970-01-01
  • 2021-09-06
  • 1970-01-01
  • 2013-05-16
  • 2016-05-22
相关资源
最近更新 更多