Sub topic 5
Granting namespaced permissions with a Role
This chapter assumes you can write and apply YAML manifests with kubectl. It also assumes you know what pods, namespaces, and secrets are, and that the Kubernetes API is a REST API with resources and verbs. A Role is where RBAC permissions start, so we build one rule at a time.
What a Role is
A Role is a namespaced object that holds a list of rules. Each rule says which verbs are allowed on which resources. A Role only grants permissions inside the namespace where it lives. It does not say who gets the permissions. That is the job of a RoleBinding, covered in the next chapter.
Because RBAC is additive and deny-by-default, a Role can only add permissions. You never write a "deny" rule.
A first Role
This Role lives in the default namespace and lets a subject read pods.
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: default
name: pod-reader
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "watch", "list"]Apply it with kubectl apply -f pod-reader.yaml. Nothing changes for any user yet, because no one is bound to the Role.
Reading the rule
Each entry under rules has three main fields.
| Field | Meaning | In this example |
|---|---|---|
| `apiGroups` | The API group that owns the resource | `""` is the core group |
| `resources` | The resource types the rule covers | `pods` |
| `verbs` | The actions allowed on those resources | `get`, `watch`, `list` |
The empty string in apiGroups means the core API group. Pods, services, secrets, and configmaps live there. Deployments belong to the apps group, so a rule for them needs apiGroups: ["apps"].
The verbs map to API requests. get reads one object. list reads many. watch streams changes. Others include create, update, patch, and delete.
Choosing the namespace
The metadata.namespace field decides where the permissions apply. This Role grants access to pods in default only. A pod in staging stays off limits. If you omit the namespace, kubectl uses your current namespace, so set it explicitly in manifests you plan to share.
flowchart TD
A[Role pod-reader in default] --> B[Rule: get, list, watch pods]
B --> C[Applies to pods in default]
B -. no access .-> D[Pods in other namespaces]The block above is only a sketch. The diagram below uses the supported fence format.
Common mistakes
- Wrong API group. A rule for
deploymentswithapiGroups: [""]matches nothing. Deployments are inapps. - Plural, lowercase resource names. Write
pods, notPod. - Missing verbs.
kubectl get pods -wneedswatchas well aslist. - Expecting a Role alone to work. A Role with no binding grants nothing.
Restricting to named objects
A rule can also list resourceNames to limit access to specific objects. This rule lets a subject read only one configmap.
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: default
name: app-config-reader
rules:
- apiGroups: [""]
resources: ["configmaps"]
resourceNames: ["app-config"]
verbs: ["get"]Note that list and watch cannot be limited by name in a useful way, and create cannot be limited at all, because the object name is not known when the request is authorized. Use get, update, patch, and delete with resourceNames.
Exercise
You write a Role in the default namespace with apiGroups: [""], resources: ["deployments"], and verbs: ["get", "list"]. A user bound to it still cannot list deployments. What is wrong, and how do you fix it?
Show answer
Deployments are in the apps API group, not the core group, so the rule matches nothing. Change apiGroups to ["apps"].