如何使用位于同一 k8s 集群中的 kubectl exec 从一个 pod 执行另一个 pod 中的命令

How to execute command from one pod inside another pod using kubectl exec which are inside a same k8s cluster

我在一个集群中有两个 pods。让我们称它们为 A 和 B。我已经在 pod A 中安装了 kubectl,我正在尝试使用 kubectl exec -it podB -- bash 从 pod A 中 运行 pod B 中的命令。 我收到以下错误

Error from server (Forbidden): pods "B" is forbidden: User "system:serviceaccount:default:default" cannot create pods/exec in the namespace "default"

我创建了以下 Role 和 RoleBinding 以获得访问权限。 角色 yaml

kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: api-role
  namespace: default
  labels:
    app: tools-rbac
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]

角色绑定 yaml

kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: global-rolebinding
  namespace: default
  labels:
    app: tools-rbac
subjects:
- kind: Group
  name: system:serviceaccounts
  apiGroup: rbac.authorization.k8s.io

非常感谢任何帮助。谢谢

除了 pods 之外,您还需要授予对 pods/exec 子资源的访问权限,就像您在那里拥有的那样。也就是说,这是一件非常奇怪的事情,可能会非常认真地思考这是否是最佳解决方案。