RBAC Authorization
RBAC Authorization - one of the ways to assign roles to users in a Kubernetes cluster.
Allocate user roles
-
Connect the Service Account Token, as without it you will need to re-download kubeconfig after any role change.
-
Assign roles.
An example manifest that creates two namespaces and two users, each of whom will only be able to manage pods in their namespace:
apiVersion: v1kind: Namespacemetadata:name: test-one---apiVersion: v1kind: Namespacemetadata:name: test-two---apiVersion: rbac.authorization.k8s.io/v1kind: RoleBindingmetadata:name: read-pods-onenamespace: test-onesubjects:- kind: ServiceAccountname: test-sa-oneapiGroup: ""roleRef:kind: Rolename: pod-reader-oneapiGroup: rbac.authorization.k8s.io---apiVersion: rbac.authorization.k8s.io/v1kind: RoleBindingmetadata:name: read-pods-twonamespace: test-twosubjects:- kind: ServiceAccountname: test-sa-twoapiGroup: ""roleRef:kind: Rolename: pod-reader-twoapiGroup: rbac.authorization.k8s.io---apiVersion: rbac.authorization.k8s.io/v1kind: Rolemetadata:namespace: test-onename: pod-reader-onerules:- apiGroups: [""] # "" indicates the core API groupresources: ["pods"]verbs: ["get", "watch", "list"]---apiVersion: rbac.authorization.k8s.io/v1kind: Rolemetadata:namespace: test-twoname: pod-reader-tworules:- apiGroups: [""] # "" indicates the core API groupresources: ["pods"]verbs: ["get", "watch", "list"]---apiVersion: v1kind: ServiceAccountmetadata:namespace: test-onename: test-sa-one---apiVersion: v1kind: ServiceAccountmetadata:namespace: test-twoname: test-sa-two -
Run the manifest.
-
Create tokens:
kubectl get secret $(kubectl get serviceaccount test-sa-one -o jsonpath='{.secrets[0].name}' --namespace test-one) -o jsonpath='{.data.token}' --namespace test-one | base64 -d<long and secure token for test-sa-one>kubectl get secret $(kubectl get serviceaccount test-sa-two -o jsonpath='{.secrets[0].name}' --namespace test-two) -o jsonpath='{.data.token}' --namespace test-two | base64 -d<long and secure token for test-sa-two> -
Manually add tokens to users in the
kubeconfig.yamlfile for passwordless authorization:users:...- name: test-sa-oneuser:token: long and secure token test-sa-one- name: test-sa-twouser:token: long and secure token test-sa-two... -
Check the operation of the role assignment:
kubectl config set-context --current --user=test-sa-twoContext "admin@kubernetes" modified.kubectl get pods --namespace test-twoNo resources found in test-two namespace.kubectl get pods --namespace test-oneError from server (Forbidden): pods is forbidden: User "system:serviceaccount:test-two:test-sa-two" cannot list resource "pods" in API group "" in the namespace "test-one"________kubectl config set-context --current --user=test-sa-oneContext "admin@kubernetes" modified.kubectl get pods --namespace test-twoError from server (Forbidden): pods is forbidden: User "system:serviceaccount:test-one:test-sa-one" cannot list resource "pods" in API group "" in the namespace "test-two"kubectl get pods --namespace test-oneNo resources found in test-one namespace.The test-sa-two user now has access to pods in the test-two namespace and no access in the test-one namespace. The test-sa-one user is the opposite.