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

1/15/2019

I have two pods in a cluster. Lets call them A and B. I've installed kubectl inside pod A and I am trying to run a command inside pod B from pod A using kubectl exec -it podB -- bash. I am getting the following error

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

I've created the following Role and RoleBinding to get access. Role 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"]

RoleBinding 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

Any help is greatly appreciated. Thank you

-- Sampath Surineni
kubectl
kubernetes
rbac

1 Answer

1/15/2019

You would need to give access to the pods/exec subresource in addition to pods like you have there. That said, this is a very weird thing to do and probably think very hard as to if this is the best solution.

-- coderanger
Source: StackOverflow