Bird
Raised Fist0
Kubernetesdevops~10 mins

RoleBindings and ClusterRoleBindings in Kubernetes - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a RoleBinding that assigns the 'view' role to a user named 'alice' in the 'dev' namespace.

Kubernetes
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: view-binding
  namespace: dev
subjects:
- kind: User
  name: alice
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: [1]
  name: view
  apiGroup: rbac.authorization.k8s.io
Drag options to blanks, or click blank then click option'
AClusterRole
BRole
CServiceAccount
DUser
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ClusterRole' as kind in a namespaced RoleBinding without understanding scope.
Confusing 'User' or 'ServiceAccount' as roleRef kind.
2fill in blank
medium

Complete the code to create a ClusterRoleBinding that assigns the 'cluster-admin' ClusterRole to a group named 'admins'.

Kubernetes
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: admin-binding
subjects:
- kind: Group
  name: admins
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: [1]
  name: cluster-admin
  apiGroup: rbac.authorization.k8s.io
Drag options to blanks, or click blank then click option'
AClusterRole
BRole
CUser
DServiceAccount
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Role' as kind in a ClusterRoleBinding.
Confusing subject kinds with roleRef kinds.
3fill in blank
hard

Fix the error in the RoleBinding that tries to assign a ClusterRole in a namespace.

Kubernetes
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: edit-binding
  namespace: test
subjects:
- kind: User
  name: bob
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: [1]
  name: edit
  apiGroup: rbac.authorization.k8s.io
Drag options to blanks, or click blank then click option'
AUser
BClusterRole
CServiceAccount
DRole
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ClusterRole' as kind in a RoleBinding without understanding scope.
Confusing subject kind with roleRef kind.
4fill in blank
hard

Fill both blanks to create a RoleBinding that assigns the 'admin' Role to a ServiceAccount named 'deployer' in the 'prod' namespace.

Kubernetes
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: admin-binding
  namespace: prod
subjects:
- kind: [1]
  name: deployer
  namespace: prod
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: [2]
  name: admin
  apiGroup: rbac.authorization.k8s.io
Drag options to blanks, or click blank then click option'
AServiceAccount
BUser
CRole
DClusterRole
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'User' as subject kind when the subject is a ServiceAccount.
Using 'ClusterRole' as roleRef kind in a RoleBinding.
5fill in blank
hard

Fill all three blanks to create a ClusterRoleBinding that assigns the 'view' ClusterRole to a User named 'charlie' across the cluster.

Kubernetes
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: view-binding
subjects:
- kind: [1]
  name: charlie
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: [2]
  name: [3]
  apiGroup: rbac.authorization.k8s.io
Drag options to blanks, or click blank then click option'
AUser
BClusterRole
Cview
DRole
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Role' instead of 'ClusterRole' in roleRef kind.
Using 'Group' or 'ServiceAccount' instead of 'User' as subject kind.

Practice

(1/5)
1. What is the main difference between a RoleBinding and a ClusterRoleBinding in Kubernetes?
easy
A. RoleBinding and ClusterRoleBinding are exactly the same.
B. RoleBinding grants permissions cluster-wide, while ClusterRoleBinding grants permissions within a single namespace.
C. RoleBinding is used only for system users, ClusterRoleBinding is for regular users.
D. RoleBinding grants permissions within a single namespace, while ClusterRoleBinding grants permissions cluster-wide.

Solution

  1. Step 1: Understand RoleBinding scope

    RoleBinding assigns permissions only inside one namespace.
  2. Step 2: Understand ClusterRoleBinding scope

    ClusterRoleBinding assigns permissions across the entire cluster, not limited to a namespace.
  3. Final Answer:

    RoleBinding is namespace-scoped; ClusterRoleBinding is cluster-scoped. -> Option D
  4. Quick Check:

    Scope difference = RoleBinding grants permissions within a single namespace, while ClusterRoleBinding grants permissions cluster-wide. [OK]
Hint: Remember: RoleBinding = namespace, ClusterRoleBinding = whole cluster [OK]
Common Mistakes:
  • Confusing the scope of RoleBinding and ClusterRoleBinding
  • Thinking both bindings work cluster-wide
  • Assuming RoleBinding is for system users only
2. Which of the following is the correct syntax to create a RoleBinding in Kubernetes YAML?
easy
A. apiVersion: v1 kind: RoleBinding metadata: name: read-pods subjects: - kind: User name: jane roleRef: kind: Role name: pod-reader
B. apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: read-pods subjects: - kind: User name: jane apiGroup: rbac.authorization.k8s.io roleRef: kind: Role name: pod-reader apiGroup: rbac.authorization.k8s.io
C. apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: name: read-pods subjects: - kind: User name: jane roleRef: kind: Role name: pod-reader apiGroup: rbac.authorization.k8s.io
D. apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: read-pods subjects: - kind: User name: jane roleRef: kind: ClusterRole name: pod-reader apiGroup: rbac.authorization.k8s.io

Solution

  1. Step 1: Check apiVersion and kind

    Correct apiVersion for RoleBinding is rbac.authorization.k8s.io/v1 and kind is RoleBinding.
  2. Step 2: Validate subjects and roleRef fields

    Subjects must include kind, name, and apiGroup. roleRef must reference a Role with correct apiGroup.
  3. Final Answer:

    apiVersion: rbac.authorization.k8s.io/v1, kind: RoleBinding, with complete subjects including apiGroup, and roleRef to Role. -> Option B
  4. Quick Check:

    Correct apiVersion and fields = apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: read-pods subjects: - kind: User name: jane apiGroup: rbac.authorization.k8s.io roleRef: kind: Role name: pod-reader apiGroup: rbac.authorization.k8s.io [OK]
Hint: RoleBinding YAML needs apiVersion rbac.authorization.k8s.io/v1 and kind RoleBinding [OK]
Common Mistakes:
  • Using wrong apiVersion or kind
  • Omitting apiGroup in subjects or roleRef
  • Confusing RoleBinding with ClusterRoleBinding syntax
3. Given this YAML snippet for a ClusterRoleBinding:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: admin-binding
subjects:
- kind: User
  name: alice
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: ClusterRole
  name: cluster-admin
  apiGroup: rbac.authorization.k8s.io
What permission scope does user alice get?
medium
A. Permissions cluster-wide with cluster-admin rights
B. No permissions because ClusterRoleBinding requires a ServiceAccount subject
C. Permissions only in the default namespace
D. Permissions only in the namespace where the ClusterRoleBinding is created

Solution

  1. Step 1: Identify the binding type and role

    The YAML defines a ClusterRoleBinding that binds user alice to the cluster-admin ClusterRole.
  2. Step 2: Understand ClusterRoleBinding scope

    ClusterRoleBinding grants permissions cluster-wide, so alice has admin rights across all namespaces.
  3. Final Answer:

    User alice has cluster-wide admin permissions. -> Option A
  4. Quick Check:

    ClusterRoleBinding + cluster-admin = cluster-wide admin [OK]
Hint: ClusterRoleBinding with cluster-admin role = full cluster access [OK]
Common Mistakes:
  • Assuming permissions are limited to one namespace
  • Thinking only ServiceAccounts can be subjects
  • Confusing ClusterRoleBinding with RoleBinding scope
4. You applied this YAML to create a RoleBinding:
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: read-pods
subjects:
- kind: User
  name: bob
roleRef:
  kind: Role
  name: pod-reader
  apiGroup: rbac.authorization.k8s.io
But bob cannot list pods in the namespace. What is the likely problem?
medium
A. The subjects field is missing apiGroup for the user.
B. The roleRef kind should be ClusterRole instead of Role.
C. The RoleBinding must be created in the kube-system namespace.
D. The user bob does not exist in Kubernetes.

Solution

  1. Step 1: Check subjects field completeness

    The subjects entry for user bob lacks the required apiGroup field, which is needed to identify the user correctly.
  2. Step 2: Understand impact of missing apiGroup

    Without apiGroup, Kubernetes cannot match the user to the RoleBinding, so permissions are not granted.
  3. Final Answer:

    Missing apiGroup in subjects causes permission failure. -> Option A
  4. Quick Check:

    Subjects need apiGroup for user binding [OK]
Hint: Always include apiGroup in subjects for users [OK]
Common Mistakes:
  • Omitting apiGroup in subjects
  • Confusing Role and ClusterRole in roleRef
  • Assuming namespace or user existence is the problem
5. You want to grant a service account named deploy-bot in namespace dev permission to create pods across all namespaces. Which is the correct approach?
hard
A. Create a RoleBinding in each namespace binding deploy-bot to a Role with pod creation rights.
B. Create a RoleBinding in the dev namespace binding deploy-bot to a Role with pod creation rights.
C. Create a ClusterRoleBinding binding the deploy-bot service account to a ClusterRole with pod creation rights.
D. Create a ClusterRole with pod creation rights but no binding is needed.

Solution

  1. Step 1: Identify scope needed

    Permission to create pods across all namespaces requires cluster-wide scope.
  2. Step 2: Choose correct binding type

    A ClusterRoleBinding is needed to bind the deploy-bot service account to a ClusterRole with pod creation rights cluster-wide.
  3. Final Answer:

    Create a ClusterRoleBinding for deploy-bot to a ClusterRole with pod creation rights. -> Option C
  4. Quick Check:

    ClusterRoleBinding = cluster-wide permissions [OK]
Hint: ClusterRoleBinding for cluster-wide access to service accounts [OK]
Common Mistakes:
  • Using RoleBinding for cluster-wide permissions
  • Not creating any binding after ClusterRole
  • Creating RoleBinding in only one namespace