What's the difference between Role and ClusterRole, and when do we use RoleBinding vs. ClusterRoleBinding?
The Role defines permissions limited to just one Namespace (such as reading pods in dev space). The ClusterRole defines permissions at the entire cluster level (such as PVs and Namespaces) or permissions for all namespaces together. When using RoleBinding with a ClusterRole, those permissions are restricted to apply only within the namespace in which the RoleBinding was created.
Detailed Technical Explanation & PracticeExpand DetailsCollapse Details
The complete matrix of RBAC relationships:
- Role: Permissions within a specific namespace.
- ClusterRole: Global rights for the entire cluster or objects not restricted to Namespace (such as Nodes and PersistentVolumes).
- RoleBinding: Grants rights within its own namespace only.
- ClusterRoleBinding: Grants cluster-wide permissions to all namespaces and global resources.
Professional application example:
If you want to grant a developer view access to only the staging space but not the entire cluster:
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name:developer-view
namespace: staging
subjects:
- kind: User
Name: ali@example.com
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: ClusterRole # Use generic out-of-the-box ClusterRole
name: view
apiGroup: rbac.authorization.k8s.io- Granting the ServiceAccount the ClusterRoleBinding privilege with the cluster-admin privilege as a quick solution to avoid permission errors, which is a serious security breach in production environments.
- The belief is that a ClusterRole always requires a ClusterRoleBinding; using RoleBinding to bind a standard ClusterRole (such as edit or view) is a best practice for restricting validity to a specific Namespace without duplicating the creation of Roles.
- What are the basic fields in the RBAC base (apiGroups, resources, verbs)?
- How do you check if a user or account has a certain privilege using the kubectl auth can-i command?
What are the mandatory settings in SecurityContext for container hardening against Container Escape vulnerabilities?
Basic settings include: runAsNonRoot: true to prevent booting as root, readOnlyRootFilesystem: true to prevent an attacker from modifying system executable files or downloading malware, allowPrivilegeEscalation: false to prevent gaining additional privileges via suid binaries, and dropping all Linux capabilities via capabilities.drop: ['ALL'].
Detailed Technical Explanation & PracticeExpand DetailsCollapse Details
SecurityContext model hardened for production according to the Restricted Pod Security Standard:
spec:
securityContext:
runAsNonRoot: true
runAsUser: 10001
runAsGroup: 10001
fsGroup: 10001
seccompProfile:
type: RuntimeDefault
containers:
-name: secure-app
image:my-app:v1.0.0
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop:
-ALL
volumeMounts:
-name: tmp-dir
mountPath: /tmp
volumes:
-name: tmp-dir
emptyDir: {}This configuration prevents:
- Modify any file in the file system.
- Exploiting SUID programs to obtain additional powers.
- Use any Linux kernel capabilities such as
CAP_NET_RAWorCAP_SYS_ADMIN.
- Enable privileged: true within production environments, giving the container direct access to all host server hardware and processes and making container escape almost certain when the application is compromised.
- Enable readOnlyRootFilesystem without providing emptyDir volumes for temporary folders that the application requires for writing (such as /tmp or /var/run/app).
- What is the difference between Pod-level SecurityContext (pod.spec.securityContext) and Container-level SecurityContext (pod.spec.containers[].securityContext)?
- What are the three Pod Security Standards (Privileged, Baseline, Restricted)?