Grant Namespace Access for Non-admin User
If you are a user with administrator privileges and if you want to provide non-admin permissions to a service account user1 to access a specific namespace ns1, perform the following steps:
-
Create a service account
user1in the namespacens1:apiVersion: v1kind: ServiceAccountmetadata:name: user1namespace: ns1noteIf your Kubernetes version is below v1.22, use
rbac.authorization.k8s.io/v1beta1API version of ClusterRole, ClusterRoleBinding, Role, and RoleBinding. For more information, see RBAC resources. -
Create a
clusterRoleandclusterRoleBindingfor the service account with the required permissions:apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRolemetadata:name: user1-clusterrolerules:- apiGroups: ["*"]resources: ["customresourcedefinitions","virtualmachines","virtualmachineinstances","csidrivers", "services", "namespaces", "clusterrolebindings", "persistentvolumes", "clusterroles", "nodes", "storageclasses"]verbs: ["get", "list", "watch", "create"]- apiGroups: ["stork.libopenstorage.org"]resources: ["schedulepolicies","backuplocations","applicationbackups"]verbs: ["create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"]- apiGroups: ["stork.libopenstorage.org"] # for controller supportresources: ["*"]verbs: ["list", "get", "watch"]- apiGroups: ["snapshot.storage.k8s.io"]resources: ["volumesnapshotclasses"]verbs: ["get", "list"]apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata:name: user1-clusterrolebindingsubjects:- kind: ServiceAccountname: user1namespace: ns1roleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: user1-clusterrole -
Create a
roleandrolebindingfor the namespace with the required permission in the namespacens1:apiVersion: rbac.authorization.k8s.io/v1kind: Rolemetadata:name: ns1-rolenamespace: ns1rules:- apiGroups: ["", "extensions", "apps"]resources: ["*"]verbs: ["*"]- apiGroups: ["batch"]resources:- jobs- cronjobsverbs: ["*"]- apiGroups:- stork.libopenstorage.orgresources: ["*"]verbs:- create- delete- deletecollection- get- list- patch- update- watchapiVersion: rbac.authorization.k8s.io/v1kind: RoleBindingmetadata:name: ns1-rolebindingnamespace: ns1subjects:- kind: ServiceAccountname: user1namespace: ns1roleRef:apiGroup: rbac.authorization.k8s.iokind: Rolename: ns1-role -
Provide the read permissions for the get and list operations for the configmap in the kube-system namespace. You need to link
user1service account created in the namespacens1in the rolebinding. You need not create a new service account in the kube-system.noteRegardless of the namespace, you need these permissions for configmap in the kube-system namespace to read the configmap created by Stork.
apiVersion: rbac.authorization.k8s.io/v1kind: Rolemetadata:name: ks-rolenamespace: kube-systemrules:- apiGroups: [""]resources: ["configmaps"]verbs: ["get", "list"]apiVersion: rbac.authorization.k8s.io/v1kind: RoleBindingmetadata:name: ks-rolebindingnamespace: kube-systemsubjects:- kind: ServiceAccountname: user1namespace: ns1roleRef:apiGroup: rbac.authorization.k8s.iokind: Rolename: ks-role -
Create the secret token for the service account
user1.noteIn current Kubernetes versions, a Secret-based token is not created automatically for a service account. Create it manually as shown below.
apiVersion: v1kind: Secretmetadata:name: user1-tokennamespace: ns1annotations:kubernetes.io/service-account.name: user1type: kubernetes.io/service-account-token -
Fetch the service account token and certificate to update the kube-config:
kubectl get secret user1-token -n ns1 -o "jsonpath={.data.token}" | base64 -dkubectl get secret user1-token -n ns1 -o "jsonpath={.data['ca\\.crt']}" -
Create a kube-config file in the following format, and update the required values along with the certificate and service account token obtained from the previous step.
apiVersion: v1kind: Configclusters:- name: my-clustercluster:server: https://<KUBERNETES_API_SERVER>certificate-authority-data: <BASE64_ENCODED_CA_CERT>users:- name: user1user:token: <SERVICE_ACCOUNT_TOKEN>contexts:- name: user1-contextcontext:cluster: my-clusteruser: user1namespace: ns1current-context: user1-contextSample kube-config for the service account
user1:apiVersion: v1kind: Configclusters:- name: my-clustercluster:server: https://xx.xx.xx.xxx:6443certificate-authority-data: <alpha-numeric-certificate-key>users:- name: user1user:token: <alpha-numeric-certificate-token>contexts:- name: user1-contextcontext:cluster: my-clusteruser: user1namespace: ns1current-context: user1-context