Kubernetes RBAC YAML example: ServiceAccount, Role and RoleBinding
Kubernetes permissions take three resources. A ServiceAccount is the identity a Pod runs as. A Role lists what may be done in one namespace. A RoleBinding grants that Role to that ServiceAccount.
This manifest shows the full triangle and the Deployment that uses it through serviceAccountName. It also includes a NetworkPolicy, which limits network traffic and is separate from RBAC, although the two are often confused.
What this manifest does
This manifest defines 5 resources in the prod namespace: the main thing it runs is 1 Deployment. Let’s walk through it.
Deployment api runs 2 Pods, running myorg/api:v1.0.0.
Pods run as the ServiceAccount api-sa, which is granted the Role api-reader permissions.
Network access is restricted by NetworkPolicy api-deny-egress. Only Pods that match its rules can talk to the protected workloads.
Things to know before using it
No livenessProbeDeployment api
Container "api" has no livenessProbe. Kubernetes can't detect and restart a hung container. Fix: Add a livenessProbe with httpGet, tcpSocket or exec.
Container may run as rootDeployment api
Container "api" doesn't enforce runAsNonRoot. A compromise gives the attacker root inside the container. Fix: Set securityContext.runAsNonRoot: true and runAsUser to a non-zero UID.
allowPrivilegeEscalation not setDeployment api
Container "api" doesn't explicitly disable privilege escalation. Defaults to true on older kubelets. Fix: Set securityContext.allowPrivilegeEscalation: false.
Writable root filesystemDeployment api
Container "api" doesn't set readOnlyRootFilesystem. Attackers can write tools into / at runtime. Fix: Set securityContext.readOnlyRootFilesystem: true and use volumeMounts for paths that need write access (/tmp, /var/run, ...).
Linux capabilities not droppedDeployment api
Container "api" doesn't drop ALL capabilities. By default it runs with ~14 caps including NET_RAW, MKNOD. Fix: Set securityContext.capabilities.drop: ["ALL"] and add only what you need under capabilities.add.
ServiceAccount token auto-mountedDeployment api
Pod auto-mounts the SA token. If the workload doesn't talk to the K8s API, this is unnecessary attack surface. Fix: Set automountServiceAccountToken: false unless you call the K8s API.
No topology spread or anti-affinityDeployment api
Deployment "api" has 2 replicas but no spread rules. All Pods could land on one node, a single failure domain. Fix: Add topologySpreadConstraints or podAntiAffinity to spread across zones/nodes.
The full manifest
# RBAC triangle (ServiceAccount + Role + RoleBinding) plus a NetworkPolicy.
apiVersion: v1
kind: ServiceAccount
metadata:
name: api-sa
namespace: prod
annotations:
eks.amazonaws.com/role-arn: arn:aws:iam::123456789012:role/api-prod-reader
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: api-reader
namespace: prod
rules:
- apiGroups: [""]
resources: ["configmaps", "secrets"]
verbs: ["get", "list", "watch"]
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: api-reader-binding
namespace: prod
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: api-reader
subjects:
- kind: ServiceAccount
name: api-sa
namespace: prod
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: api-deny-egress
namespace: prod
spec:
podSelector:
matchLabels:
app: api
policyTypes: ["Ingress", "Egress"]
ingress:
- from:
- podSelector:
matchLabels:
role: frontend
ports:
- port: 8080
protocol: TCP
egress:
- to:
- namespaceSelector:
matchLabels:
name: kube-system
ports:
- port: 53
protocol: UDP
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: api
namespace: prod
spec:
replicas: 2
selector:
matchLabels:
app: api
template:
metadata:
labels:
app: api
spec:
serviceAccountName: api-sa
containers:
- name: api
image: myorg/api:v1.0.0
ports:
- containerPort: 8080
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 500m
memory: 256Mi
readinessProbe:
httpGet:
path: /ready
port: 8080
Related examples
Stuck on a Kubernetes concept?
Ask anything and get a narrated visual lesson, pitched at your level. Free.
Try an example