Skip to main content
Kubernetes Introduction
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 raw kubectl 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. 1. The Subject: The human User, or the software ServiceAccount.
  1. 2. The Role: A rulebook. (e.g., "Allowed to GET and LIST Pods. NOT allowed to DELETE Pods").
  1. 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 the default 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: "The database 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. 1. Create a new namespace for the developer:

bash
1
kubectl create namespace dev-sandbox
  1. 2. Create a role.yaml file. This Role only allows viewing Pods, not creating or deleting them:
yaml
123456789
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: dev-sandbox
  name: pod-reader
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["get", "watch", "list"]
  1. 3. Apply the Role: kubectl apply -f role.yaml
  1. 4. Create a rolebinding.yaml file to bind this role to a user named "Alice":
yaml
12345678910111213
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: read-pods-binding
  namespace: dev-sandbox
subjects:
- kind: User
  name: alice
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: Role
  name: pod-reader
  apiGroup: rbac.authorization.k8s.io
  1. 5. Apply the Binding: kubectl apply -f rolebinding.yaml
  1. 6. Now, if User "Alice" authenticates with the cluster, she can look at Pods in the dev-sandbox namespace. If she tries to run kubectl delete pod or tries to look at Pods in the default namespace, the API Server will violently reject her request with an HTTP 403 Forbidden error!

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 enforce securityContext: runAsNonRoot: true in your Pod YAML specifications. Furthermore, strictly forbid the mounting of sensitive Host paths (like /etc or /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-admin ClusterRoleBinding. 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. 1. What is the architectural difference in scope between a Role and a ClusterRole?
  1. 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 default Service 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.

14. Summary

In Chapter 14, we secured the perimeter and the interior of our cluster. We dismantled the permissive default settings of Kubernetes, enforcing the Principle of Least Privilege through rigorous RBAC RoleBindings. We recognized that software requires strictly limited permissions via custom Service Accounts, and we conceptualized Network Policies as mandatory virtual firewalls, ensuring that a compromise in one microservice does not cascade into a catastrophic cluster-wide breach.

15. Next Chapter Recommendation

Our cluster is highly secure. We are ready to deploy sensitive, permanent data. Proceed to Chapter 15: Deploying Databases in Kubernetes.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·