All exams← All Certified Kubernetes Administrator (CKA) notes

Security & RBAC: Certified Kubernetes Administrator (CKA) revision notes

Revision notes

## Security & RBAC in Kubernetes

Security in Kubernetes is multifaceted, covering authentication, authorization, network controls, and workload isolation. Role-Based Access Control (RBAC) is the primary authorization mechanism, defining what authenticated users and processes can do.

## Authentication and Authorization

Authentication verifies the identity of a user or process (e.g., client certificates, service account tokens, OIDC). Once authenticated, Authorization determines if the identity is permitted to perform a requested action on a resource. The Kubernetes API server handles both.

## Role-Based Access Control (RBAC)

RBAC uses four key objects:

  • Role: Defines a set of permissions *within a specific namespace*. Permissions are defined by verbs (e.g., `get`, `list`, `create`, `delete`) on resources (e.g., `pods`, `deployments`) within API groups.
  • ClusterRole: Similar to a Role, but defines permissions that are *cluster-scoped* (e.g., `nodes`) or apply to *non-namespaced resources*, or grant access across *all namespaces*.
  • RoleBinding: Grants the permissions defined in a Role to a user, group, or Service Account *within a specific namespace*.
  • ClusterRoleBinding: Grants the permissions defined in a ClusterRole to a user, group, or Service Account *cluster-wide*.

Use `kubectl auth can-i <verb> <resource> --namespace <namespace>` to check permissions.

## Service Accounts

Service Accounts provide an identity for processes running in pods. Each namespace has a default service account. Pods are automatically assigned the `default` service account of their namespace unless specified otherwise. A token for the service account is mounted into the pod at `/var/run/secrets/kubernetes.io/serviceaccount/token`. You can disable this with `automountServiceAccountToken: false` in the pod or service account specification.

## Secrets

Secrets are used to store sensitive information like passwords, OAuth tokens, and SSH keys. They are Base64 encoded, not encrypted by default at rest (unless configured with an encryption provider). Secrets can be mounted as data volumes or exposed as environment variables within pods. Common types include `Opaque`, `kubernetes.io/dockerconfigjson`, and `kubernetes.io/tls`.

## Network Policies

Network Policies specify how groups of pods are allowed to communicate with each other and with external network endpoints. They define ingress (incoming) and egress (outgoing) rules based on pod selectors and namespace selectors. Network Policies are enforced by a Network Policy Controller, which is typically part of the Container Network Interface (CNI) plugin (e.g., Calico, Cilium).

## Pod Security Standards (PSS)

Pod Security Standards (PSS) replaced Pod Security Policies (PSPs) to enforce security best practices for pods. PSS defines three levels:

  • Privileged: Unrestricted, allowing known escalations.
  • Baseline: Minimally restrictive, preventing known escalations.
  • Restricted: Heavily restricted, following current hardening best practices.

PSS are enforced by the PodSecurity admission controller, which can be configured to `enforce`, `audit`, or `warn` based on security levels for specific namespaces using labels like `pod-security.kubernetes.io/enforce: restricted`.

Key facts

Flashcards

What is the primary authorization mechanism in Kubernetes?
RBAC (Role-Based Access Control).
tap to reveal
Which Kubernetes object grants permissions defined in a Role to a user within a specific namespace?
RoleBinding.
tap to reveal
What is the purpose of a Service Account?
To provide an identity for processes running in a Pod.
tap to reveal
How are Kubernetes Secrets stored by default?
Base64 encoded (not encrypted at rest unless explicitly configured).
tap to reveal
Which Kubernetes object controls network traffic between pods?
NetworkPolicy.
tap to reveal
What replaced Pod Security Policies (PSPs) for enforcing pod security standards?
Pod Security Standards (PSS), enforced by the PodSecurity admission controller.
tap to reveal
Which `kubectl` command helps check if a user or service account can perform a specific action?
`kubectl auth can-i`.
tap to reveal
What is the difference between a Role and a ClusterRole?
A Role defines permissions within a specific namespace, while a ClusterRole defines cluster-wide permissions or for non-namespaced resources.
tap to reveal
Practice questions

Security & RBAC: 5 practice questions

Q1. You need to grant a user permission to view Pods, but only within the 'dev' namespace. Which RBAC object is most appropriate for defining these specific, namespaced permissions?
  • A.  ClusterRoleBinding
  • B.  RoleBinding
  • C.  Role
  • D.  ClusterRole
Show answer & explanation
✓ Answer: C. Role
A 'Role' is a namespaced resource used to define permissions on other resources within that same namespace, which is exactly what is required here.
Q2. An administrator creates a `Role` with the verb 'get' for the 'pods' resource. What action does this permission allow a user bound to this role to perform?
  • A.  Watch for changes to all pods in the namespace.
  • B.  List all pods in the namespace.
  • C.  Create a new pod in the namespace.
  • D.  Retrieve the details of a single, specific pod by name.
Show answer & explanation
✓ Answer: D. Retrieve the details of a single, specific pod by name.
The 'get' verb in RBAC allows retrieving a single, specific resource by its name, whereas 'list' is used for retrieving multiple resources of a type.
Q3. You have a `ServiceAccount` named 'deploy-bot' in the 'ci-cd' namespace and a `Role` named 'pod-manager' in the same namespace. How do you grant the 'deploy-bot' the permissions defined in 'pod-manager'?
  • A.  Create a `RoleBinding` in the 'ci-cd' namespace.
  • B.  Create a new `Role` that references the `ServiceAccount` as its subject.
  • C.  Add the `roleName: pod-manager` field to the ServiceAccount's YAML manifest.
  • D.  Create a `ClusterRoleBinding` linking the `ServiceAccount` to the `Role`.
Show answer & explanation
✓ Answer: A. Create a `RoleBinding` in the 'ci-cd' namespace.
A `RoleBinding` is used to grant the permissions defined in a `Role` to a subject (like a User, Group, or ServiceAccount) within a specific namespace.
Q4. In a Pod specification, what is the primary purpose of setting `securityContext.fsGroup`?
  • A.  It defines the user ID that the container process will run as.
  • B.  It specifies a supplemental group ID that is applied to all containers, making files in mounted volumes owned by this group.
  • C.  It prevents the container from escalating privileges to root.
  • D.  It sets the primary group ID for all processes running in the container.
Show answer & explanation
✓ Answer: B. It specifies a supplemental group ID that is applied to all containers, making files in mounted volumes owned by this group.
The `fsGroup` in a Pod's Security Context is a special supplemental group that Kubernetes applies to mounted volumes so that files are owned and writable by that group ID.
Q5. If a `NetworkPolicy` is applied to a Pod, but it has no `ingress` or `egress` rules defined (an empty policy), what is the effect on the Pod's traffic?
  • A.  All ingress and egress traffic is allowed, as no specific rules are defined.
  • B.  The policy is invalid and will be rejected by the API server.
  • C.  Only egress traffic is allowed; all ingress is denied.
  • D.  The Pod is completely isolated; all ingress and egress traffic is denied.
Show answer & explanation
✓ Answer: D. The Pod is completely isolated; all ingress and egress traffic is denied.
Once a Pod is selected by any NetworkPolicy, it becomes isolated. An empty policy with no rules will result in denying all ingress and egress traffic to/from that Pod.
Test yourself
See the Certified Kubernetes Administrator (CKA) page

More Certified Kubernetes Administrator (CKA) topics