Explain Kubernetes RBAC and how you would give a service account read-only access to pods.
RBAC (Role-Based Access Control) is the authorization mechanism in Kubernetes. It uses three objects:
- Role/ClusterRole: Defines what actions are allowed on which resources.
- ServiceAccount: An identity for pods or external tools.
- RoleBinding/ClusterRoleBinding: Links a ServiceAccount to a Role.
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: pod-reader
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: read-pods
subjects:
- kind: ServiceAccount
name: my-service-account
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io