← All cheatsheets
$ kubectl auth can-i --listCheck who can do what, create Roles and bindings, manage service account tokens and audit permissions across the cluster.
Check permissions
kubectl auth whoamiShow who you are logged in askubectl auth can-i create deployments -n devCheck one action for yourselfkubectl auth can-i --list -n devList everything you can do in a namespacekubectl auth can-i --list --as=jane -n devList permissions for another userkubectl auth can-i get pods --as=system:serviceaccount:dev:app-sa -n devCheck a service accountCreate roles and bindings
kubectl create role pod-reader --verb=get,list,watch --resource=pods -n devNamespaced read-only role for podskubectl create clusterrole node-reader --verb=get,list,watch --resource=nodesCluster-wide role for nodeskubectl create rolebinding read-pods --role=pod-reader --user=jane -n devBind a role to a userkubectl create rolebinding app-read --role=pod-reader --serviceaccount=dev:app-sa -n devBind a role to a service accountkubectl create clusterrolebinding auditors-view --clusterrole=view --group=auditorsGive a group read access everywherekubectl auth reconcile -f rbac.yamlApply RBAC files safelyService accounts
kubectl create serviceaccount app-sa -n devCreate a service accountkubectl create token app-sa -n dev --duration=1hGet a short-lived tokenkubectl get serviceaccounts -n devList service accountsAudit
kubectl get roles,rolebindings -n devList namespaced RBAC objectskubectl get clusterroles,clusterrolebindingsList cluster RBAC objectskubectl describe clusterrole adminSee what a role allowskubectl get clusterrolebindings -o wide | grep cluster-adminFind who has cluster-adminkubectl get rolebindings -A -o wideList all bindings and subjectsRole and RoleBinding
apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: name: pod-reader namespace: dev rules: - apiGroups: [""] resources: ["pods", "pods/log"] verbs: ["get", "list", "watch"] --- apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: read-pods namespace: dev subjects: - kind: User name: jane apiGroup: rbac.authorization.k8s.io roleRef: kind: Role name: pod-reader apiGroup: rbac.authorization.k8s.io
