I would like to grant a Kubernetes service account privileges for executing kubectl --token $token get pod --all-namespaces
. I'm familiar with doing this for a single namespace but don't know how to do it for all (including new ones that may be created in the future and without granting the service account full admin privileges).
Currently I receive this error message:
Error from server (Forbidden): pods is forbidden: User "system:serviceaccount:kube-system:test" cannot list resource "pods" in API group "" at the cluster scope
What (cluster) roles and role bindings are required?
UPDATE Assigning role view
to the service with the following ClusterRoleBinding
works and is a step forward. However, I'd like to confine the service account's privileges further to the minimum required.
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: test
subjects:
- kind: ServiceAccount
name: test
namespace: kube-system
roleRef:
kind: ClusterRole
name: view
apiGroup: rbac.authorization.k8s.io
The service account's token can be extracted as follows:
secret=$(kubectl get serviceaccount test -n kube-system -o=jsonpath='{.secrets[0].name}')
token=$(kubectl get secret $secret -n kube-system -o=jsonpath='{.data.token}' | base64 --decode -)
ClustRole
&ClusterRoleBinding
are correct when you need all namespaces, just shrink down the permissions:Then all pods in a namespace
your-ns
will get a k8s token automatically mounted. You can use bare kubectl or k8s sdk inside a pod without passing any secrets. Note that you don't need to pass--token
, just run the command in a pod within the namespace where you created that ServiceAccount.Here's a good article explaining the concepts https://medium.com/@ishagirdhar/rbac-in-kubernetes-demystified-72424901fcb3
deploy test pod from the below sample