Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Kubernetes security policy expertise from a comprehensive 146-skill, 112-agent multi-agent orchestration system.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/rbac-patterns.md
1# RBAC Patterns and Best Practices23## Common RBAC Patterns45### Pattern 1: Read-Only Access67```yaml8apiVersion: rbac.authorization.k8s.io/v19kind: ClusterRole10metadata:11name: read-only12rules:13- apiGroups: ["", "apps", "batch"]14resources: ["*"]15verbs: ["get", "list", "watch"]16```1718### Pattern 2: Namespace Admin1920```yaml21apiVersion: rbac.authorization.k8s.io/v122kind: Role23metadata:24name: namespace-admin25namespace: production26rules:27- apiGroups: ["", "apps", "batch", "extensions"]28resources: ["*"]29verbs: ["*"]30```3132### Pattern 3: Deployment Manager3334```yaml35apiVersion: rbac.authorization.k8s.io/v136kind: Role37metadata:38name: deployment-manager39namespace: production40rules:41- apiGroups: ["apps"]42resources: ["deployments"]43verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]44- apiGroups: [""]45resources: ["pods"]46verbs: ["get", "list", "watch"]47```4849### Pattern 4: Secret Reader (ServiceAccount)5051```yaml52apiVersion: rbac.authorization.k8s.io/v153kind: Role54metadata:55name: secret-reader56namespace: production57rules:58- apiGroups: [""]59resources: ["secrets"]60verbs: ["get"]61resourceNames: ["app-secrets"] # Specific secret only62---63apiVersion: rbac.authorization.k8s.io/v164kind: RoleBinding65metadata:66name: app-secret-reader67namespace: production68subjects:69- kind: ServiceAccount70name: my-app71namespace: production72roleRef:73kind: Role74name: secret-reader75apiGroup: rbac.authorization.k8s.io76```7778### Pattern 5: CI/CD Pipeline Access7980```yaml81apiVersion: rbac.authorization.k8s.io/v182kind: ClusterRole83metadata:84name: cicd-deployer85rules:86- apiGroups: ["apps"]87resources: ["deployments", "replicasets"]88verbs: ["get", "list", "create", "update", "patch"]89- apiGroups: [""]90resources: ["services", "configmaps"]91verbs: ["get", "list", "create", "update", "patch"]92- apiGroups: [""]93resources: ["pods"]94verbs: ["get", "list"]95```9697## ServiceAccount Best Practices9899### Create Dedicated ServiceAccounts100101```yaml102apiVersion: v1103kind: ServiceAccount104metadata:105name: my-app106namespace: production107---108apiVersion: apps/v1109kind: Deployment110metadata:111name: my-app112spec:113template:114spec:115serviceAccountName: my-app116automountServiceAccountToken: false # Disable if not needed117```118119### Least-Privilege ServiceAccount120121```yaml122apiVersion: rbac.authorization.k8s.io/v1123kind: Role124metadata:125name: my-app-role126namespace: production127rules:128- apiGroups: [""]129resources: ["configmaps"]130verbs: ["get"]131resourceNames: ["my-app-config"]132```133134## Security Best Practices1351361. **Use Roles over ClusterRoles** when possible1372. **Specify resourceNames** for fine-grained access1383. **Avoid wildcard permissions** (`*`) in production1394. **Create dedicated ServiceAccounts** for each app1405. **Disable token auto-mounting** if not needed1416. **Regular RBAC audits** to remove unused permissions1427. **Use groups** for user management1438. **Implement namespace isolation**1449. **Monitor RBAC usage** with audit logs14510. **Document role purposes** in metadata146147## Troubleshooting RBAC148149### Check User Permissions150151```bash152kubectl auth can-i list pods --as [email protected]153kubectl auth can-i '*' '*' --as system:serviceaccount:default:my-app154```155156### View Effective Permissions157158```bash159kubectl describe clusterrole cluster-admin160kubectl describe rolebinding -n production161```162163### Debug Access Issues164165```bash166kubectl get rolebindings,clusterrolebindings --all-namespaces -o wide | grep my-user167```168169## Common RBAC Verbs170171- `get` - Read a specific resource172- `list` - List all resources of a type173- `watch` - Watch for resource changes174- `create` - Create new resources175- `update` - Update existing resources176- `patch` - Partially update resources177- `delete` - Delete resources178- `deletecollection` - Delete multiple resources179- `*` - All verbs (avoid in production)180181## Resource Scope182183### Cluster-Scoped Resources184185- Nodes186- PersistentVolumes187- ClusterRoles188- ClusterRoleBindings189- Namespaces190191### Namespace-Scoped Resources192193- Pods194- Services195- Deployments196- ConfigMaps197- Secrets198- Roles199- RoleBindings200