AKS 集群 pods kube 配置位置

AKS cluster pods kube config location

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

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

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

我建议使用服务帐户而不是 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