RBAC Authorization
RBAC Authorization — один из способов распределения ролей между пользователями в кластере Kubernetes.
Распределить роли пользователей
-
Подключите Service Account Token, так как без его использования после любого изменения ролей потребуется заново скачивать kubeconfig.
-
Распределите роли.
Пример манифеста, который создает два пространства имен и двух пользователей, каждый из которых сможет управлять подами только в своем пространстве имен:
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 -
Запустите манифест.
-
Создайте токены:
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> -
Вручную добавьте токены пользователям в файле
kubeconfig.yamlдля авторизации без пароля: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... -
Проверьте работу распределения ролей:
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.Теперь пользователь test-sa-two имеет доступ к подам в пространстве имен test-two и не имеет доступа в пространстве имен test-one. Пользователь test-sa-one — наоборот.