在非默认命名空间中部署时,集群配置无法获取 pods

In Cluster Config is unable to get pods when deployed in a non-default namespace

当我将 golang 服务部署到 default 命名空间以外的任何命名空间时,该服务无法在任何命名空间上检索 pods。使用 golang client-go api.

部署在 default 命名空间上的相同服务完美运行

这是安全问题吗?

谢谢。

这个问题是权限问题。由于您正在使用 rest.InClusterConfig(config) 创建客户端。这意味着它使用 pod 的服务帐户作为凭证。因此检查该服务帐户是否具有在任何命名空间中获取 pods 的权限。

if service account in the pod is not defined, then it will use default service account.

如果您的集群中启用了 RBAC,则检查该命名空间中的角色绑定,以确定您的服务帐户是否具有权限。

# to see the list of role bindings in 'default' namespace
kubectl get rolebindings --namespace default

查看具体角色绑定

kubectl get rolebindings ROLE-BINDING-NAME --namespace default -o yaml

您还可以创建角色和角色绑定来授予权限。要了解 RBAC 角色和角色绑定,请参阅此处:https://kubernetes.io/docs/reference/access-authn-authz/rbac/

以下是我在 minikube 集群上使用的方法,使默认服务帐户能够访问公共资源上的 crud 操作。明显的警告是您需要在真实集群上小心。

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: crud-role
  namespace: default
rules:
- apiGroups: ["", "apps", "batch"]
  resources: [ "deployments", "jobs", pods", "replicasets", services" ]
  verbs: [ "create", "get", "list", "delete"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: crud-role-binding
  namespace: default
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: crud-role
subjects:
  - kind: ServiceAccount
    name: default
    namespace: default