Chuyển đến nội dung chính

Bài 3: RBAC & Authorization

RBAC in-depth cho CKA. Tạo Roles, ClusterRoles, RoleBindings. ServiceAccounts. Kiểm tra quyền với kubectl auth can-i. Certificate-based authentication.

RBAC Hands-on — ServiceAccount, RoleBinding, kubectl auth can-i

1. RBAC Concepts (CKA Depth)

CKA yêu cầu hands-on: tạo RBAC resources bằng kubectl, verify permissions, và debug access issues.

RBAC objects:
  Role          → namespaced permissions
  ClusterRole   → cluster-wide permissions
  RoleBinding   → bind Role or ClusterRole to Subject (in a namespace)
  ClusterRoleBinding → bind ClusterRole to Subject (cluster-wide)

Subject types:
  - User (string, không có K8s object)
  - Group (string)
  - ServiceAccount (K8s object: namespace/name)

2. Tạo RBAC Imperatively

# Tạo Role (namespaced)
kubectl create role pod-reader \
  --verb=get,list,watch \
  --resource=pods \
  --namespace=default

# Tạo RoleBinding
kubectl create rolebinding read-pods \
  --role=pod-reader \
  --user=jane \
  --namespace=default

# Tạo ClusterRole
kubectl create clusterrole secret-reader \
  --verb=get,list \
  --resource=secrets

# Tạo ClusterRoleBinding
kubectl create clusterrolebinding read-secrets \
  --clusterrole=secret-reader \
  --user=jane

# Bind ClusterRole trong 1 namespace (dùng RoleBinding!)
kubectl create rolebinding read-secrets-dev \
  --clusterrole=secret-reader \
  --user=jane \
  --namespace=dev

Exam tip: Dùng --dry-run=client -o yaml để generate YAML rồi edit. Nhanh hơn viết tay. Ví dụ: kubectl create role myrole --verb=get --resource=pods --dry-run=client -o yaml > role.yaml

3. ServiceAccounts trong CKA

# Tạo ServiceAccount
kubectl create serviceaccount monitoring-sa -n default

# Bind permissions
kubectl create clusterrole metrics-reader \
  --verb=get,list,watch \
  --resource=pods,nodes

kubectl create clusterrolebinding monitoring-binding \
  --clusterrole=metrics-reader \
  --serviceaccount=default:monitoring-sa

# Sử dụng SA trong Pod spec
spec:
  serviceAccountName: monitoring-sa

4. Verify Permissions — kubectl auth can-i

# Kiểm tra quyền của user hiện tại
kubectl auth can-i get pods
kubectl auth can-i delete pods --namespace=production
kubectl auth can-i '*' '*'  # Check all

# Kiểm tra qua user khác (--as)
kubectl auth can-i get pods --as=jane
kubectl auth can-i get pods --as=jane --namespace=dev
kubectl auth can-i get secrets --as=system:serviceaccount:default:monitoring-sa

5. Certificate-Based Authentication

Create user with client certificate:
  1. Generate key: openssl genrsa -out alice.key 2048
  2. Create CSR: openssl req -new -key alice.key -out alice.csr -subj "/CN=alice/O=developers"
  3. Sign with K8s CA:
     # Create CertificateSigningRequest object
     kubectl apply -f alice-csr.yaml
     kubectl certificate approve alice
  4. Get signed cert: kubectl get csr alice -o jsonpath='{.status.certificate}' | base64 -d > alice.crt
  5. Add to kubeconfig

Exam tip: CKA thường cho task "create user với certificate và bind RBAC". Nhớ quy trình: generate key → CSR → approve CSR → extract cert → kubeconfig. Dùng kubectl auth can-i để verify.

6. Cheat Sheet

TaskCommand
Check what user can dokubectl auth can-i --list --as=user
Check specific permissionkubectl auth can-i get secrets --as=user -n ns
Create serviceaccountkubectl create sa SA-NAME -n NAMESPACE
Role binding to SA--serviceaccount=ns:sa-name
Approve cert requestkubectl certificate approve NAME

7. Practice Questions

Q1: A developer needs read-only access to all Pods and Services across the entire cluster. Which RBAC approach is most appropriate?

  • A) Create a Role with get/list in the default namespace
  • B) Create a ClusterRole with get/list on pods and services, then ClusterRoleBinding ✓
  • C) Create a Role in each namespace
  • D) Grant the developer cluster-admin access

Explanation: For cluster-wide access, use ClusterRole (defines permissions) + ClusterRoleBinding (grants cluster-wide). Creating Roles in each namespace is tedious and error-prone. cluster-admin is too broad.

Q2: After creating a ServiceAccount and RoleBinding for a monitoring application, you need to verify the SA can list pods in the "monitoring" namespace. Which command does this?

  • A) kubectl get rolebinding -n monitoring
  • B) kubectl describe serviceaccount monitoring-sa
  • C) kubectl auth can-i list pods --as=system:serviceaccount:monitoring:monitoring-sa -n monitoring ✓
  • D) kubectl auth check serviceaccount monitoring-sa

Explanation: kubectl auth can-i with --as=system:serviceaccount:NAMESPACE:NAME impersonates the ServiceAccount. This verifies the exact access path (SA → binding → role) rather than just inspecting the objects.

Q3: A ClusterRole named "pod-manager" exists. You want user "alice" to use this ClusterRole but ONLY within the "staging" namespace. What should you create?

  • A) ClusterRoleBinding for alice → pod-manager
  • B) RoleBinding in staging namespace for alice → pod-manager ✓
  • C) A new Role in staging with the same permissions as pod-manager
  • D) A new ClusterRole scoped to the staging namespace

Explanation: RoleBinding can reference a ClusterRole but constrains it to the binding's namespace. This reuses the ClusterRole definition without granting cluster-wide access. ClusterRoleBinding would grant access to all namespaces.