CHAPTER 14
Intermediate
Kubernetes Security Best Practices
Updated: May 15, 2026
25 min read
# CHAPTER 14
Kubernetes Security Best Practices
1. Introduction
Kubernetes is incredibly powerful, which makes it an incredibly dangerous weapon if misconfigured. By default, Kubernetes prioritizes functionality over security. If you grant a junior developer rawkubectl access, they can accidentally delete production databases. If a hacker breaches a single frontend Pod, they can exploit internal cluster networks to access sensitive backend payment systems. In this chapter, we will harden our cluster architecture, focusing on RBAC (Role-Based Access Control), Network Policies, and Pod Security Standards.
2. Learning Objectives
By the end of this chapter, you will be able to:- Define the Principle of Least Privilege in a Kubernetes context.
- Understand RBAC (Roles, RoleBindings, ClusterRoles).
- Understand the function of Service Accounts.
- Implement Network Policies to restrict Pod-to-Pod communication.
- Understand the danger of Root containers and Host mounts.
3. Beginner-Friendly Explanation
Imagine a high-security bank (The Cluster).- RBAC (Employee Badges): A teller (Junior Developer) has a blue badge. It only opens the door to the lobby. The Vault Manager (Senior DevOps) has a red badge. It opens the lobby and the vault. RBAC ensures people can only access the rooms they specifically need.
- Service Accounts (Robot Badges): The bank uses automated robots (Pods) to transport money. The robots also need badges to open doors. Service accounts are badges specifically given to software, not humans.
- Network Policies (Hallway Checkpoints): Just because a robot is in the building doesn't mean it can go anywhere. Network Policies act as security guards in the hallways. They ensure the Frontend Robot is only allowed to walk to the API room, and is mathematically forbidden from walking to the Database room.
4. Role-Based Access Control (RBAC)
RBAC is the absolute foundation of Kubernetes security. It relies on three YAML objects:- 1. The Subject: The human User, or the software ServiceAccount.
- 2. The Role: A rulebook. (e.g., "Allowed to GET and LIST Pods. NOT allowed to DELETE Pods").
- 3. The RoleBinding: The glue. It attaches the Subject to the Role. (e.g., "Give Developer Alice the rulebook").
*Note:* A Role only applies to a single Namespace. If you want to give someone permissions across the entire cluster, you use a ClusterRole and a ClusterRoleBinding.
5. Service Accounts
By default, when you launch a Pod, Kubernetes attaches thedefault Service Account to it. This default account often has permissions to query the Kubernetes API server!
If a hacker breaches your Pod, they can use that default Service Account token to talk to your Master Node and potentially map out your entire cluster.
Best Practice: Create custom, heavily restricted Service Accounts for every application, and disable automountServiceAccountToken: false for Pods that do not absolutely need to talk to the API Server.
6. Network Policies
By default, Kubernetes networks are "Flat." Any Pod in the cluster can communicate with any other Pod in the cluster, regardless of Namespace! A Network Policy is a virtual firewall you apply via YAML. You can enforce rules like: "Thedatabase Pod will only accept incoming traffic from Pods carrying the label app: backend-api. All other traffic is instantly dropped."
7. Mini Project: Configure Secure RBAC Roles
Let's create a restricted role for a new developer.Step-by-Step Tutorial:
- 1. Create a new namespace for the developer:
bash
-
2.
Create a
role.yamlfile. This Role only allows viewing Pods, not creating or deleting them:
yaml
-
3.
Apply the Role:
kubectl apply -f role.yaml
-
4.
Create a
rolebinding.yamlfile to bind this role to a user named "Alice":
yaml
-
5.
Apply the Binding:
kubectl apply -f rolebinding.yaml
-
6.
Now, if User "Alice" authenticates with the cluster, she can look at Pods in the
dev-sandboxnamespace. If she tries to runkubectl delete podor tries to look at Pods in thedefaultnamespace, the API Server will violently reject her request with anHTTP 403 Forbiddenerror!
8. Real-World Scenarios
The Capital One data breach in 2019 was exacerbated by overly permissive IAM roles and Server-Side Request Forgery (SSRF). In a Kubernetes equivalent, if a hacker breaches an Nginx Pod via a vulnerability, they immediately attempt to query the cloud provider's metadata service (e.g.,169.254.169.254). By strictly enforcing Network Policies to block outgoing traffic from the Nginx Pod to the metadata service, DevOps engineers neutralize the hacker's ability to steal AWS credentials, isolating the breach to a single, meaningless Pod.
9. Best Practices
-
Pod Security Standards (PSS): Never allow a container to run as
root(User ID 0). Always enforcesecurityContext: runAsNonRoot: truein your Pod YAML specifications. Furthermore, strictly forbid the mounting of sensitive Host paths (like/etcor/var/run/docker.sock) into a Pod.
10. Common Mistakes
-
Applying ClusterRoles Dangerously: Beginners often get frustrated by RBAC "Forbidden" errors and impatiently grant themselves or their CI/CD pipelines a
cluster-adminClusterRoleBinding. This is the equivalent of handing out the nuclear launch codes to fix a jammed printer. If that CI/CD pipeline is compromised, the entire cluster is lost. Always adhere to Least Privilege.
11. Exercises
-
1.
What is the architectural difference in scope between a
Roleand aClusterRole?
- 2. Explain how the default "flat" network model of Kubernetes poses a massive security risk during a container breach, and how Network Policies mitigate this risk.
12. FAQs
Q: How do I create actual "Users" like Alice in Kubernetes? A: Surprisingly, Kubernetes does not have a "User" object! It relies entirely on external identity providers (like AWS IAM, Google IAM, or Okta/OIDC) to authenticate humans. Kubernetes only handles the authorization (RBAC) *after* the external provider proves who you are.13. Interview Questions
-
Q: Detail the Principle of Least Privilege regarding Kubernetes Service Accounts. Why is it dangerous to rely on the
defaultService Account, and how do you prevent the automatic mounting of API tokens?
- Q: A security audit requires that a Payment API Pod must only receive traffic from the Frontend Web Pod, and must absolutely never communicate with the public internet. Outline the YAML structure of the Network Policy required to enforce this.