Nextgen Blog
HomeLearnAIQuantumRoboticsSecuritySpaceAbout
Nextgen Blog

By Anubhav โ€” full stack engineer, IIT Mandi CSE. Deep dives into AI, software engineering, and the technologies shaping the next generation of tech.

Navigate

  • Home
  • Learn
  • Blog
  • Archive
  • About

Topics

  • Artificial Intelligence
  • Quantum Computing
  • Robotics
  • Cybersecurity
  • Space Tech

Connect

Built with โค๏ธ & By - Anubhav

ยฉ 2026 Nextgen Blog by Anubhav. All rights reserved.

Privacy PolicyTerms of Service
Phase 04 ยท Operations & SecurityChapter 20 of 33
19.
Phase 04Read time ~16 minDifficulty intermediate

Kubernetes Security ๐Ÿ”

Warrior, now we're entering the Security phase of Kubernetes.

In this chapter
  • 01Kubernetes Security Model
  • 02Authentication vs Authorization
  • 03Authentication
  • 04Authorization
  • 05Simple Real-World Example
  • 06RBAC
  • 07What Is RBAC?
  • 08RBAC Mental Model
  • 09Role
  • 10Understanding rules
  • 11Resources
  • 12Verbs
  • 13API group
  • 14Common Kubernetes Verbs
  • 15RoleBinding
  • 16Role vs RoleBinding
  • 17Role
  • 18RoleBinding
  • 19Complete Example
  • 20Step 1 โ€” Role
  • 21Step 2 โ€” RoleBinding
  • 22Why Not Give Everyone Admin?
  • 23Least Privilege
  • 24Principle of Least Privilege
  • 25ClusterRole
  • 26ClusterRoleBinding
  • 27Role vs ClusterRole
  • 28ServiceAccounts
  • 29What Is a ServiceAccount?
  • 30Why Would a Pod Need Kubernetes API Access?
  • 31ServiceAccount + Role
  • 32Important Security Rule
  • 33Default ServiceAccount
  • 34Disable Unnecessary API Access
  • 35Secrets Are Not Automatically "Secure"
  • 36Base64 Is NOT Encryption
  • 37External Secret Management
  • 38SecurityContext
  • 39Why Avoid Root?
  • 40runAsUser
  • 41readOnlyRootFilesystem
  • 42Drop Linux Capabilities
  • 43Example Secure Container
  • 44allowPrivilegeEscalation
  • 45Privileged Containers
  • 46Pod Security
  • 47Namespace-Level Pod Security
  • 48Security Layers
  • 49Real-World Node.js Application
  • 50Example: Backend Needs to Read ConfigMaps
  • 51Testing Permissions
  • 52Test Another Identity
  • 53Common Security Mistakes
  • 54Mistake 1
  • 55Mistake 2
  • 56Mistake 3
  • 57Mistake 4
  • 58Mistake 5
  • 59Mistake 6
  • 60Mistake 7
  • 61Security Architecture
  • 62Authentication Flow
  • 63The Security Principle You Should Remember
  • 64Interview Questions
  • 65Q1. What is RBAC?
  • 66Q2. Role vs RoleBinding?
  • 67Q3. Role vs ClusterRole?
  • 68Q4. What is a ServiceAccount?
  • 69Q5. User vs ServiceAccount?
  • 70Q6. Authentication vs Authorization?
  • 71Q7. What is least privilege?
  • 72Q8. Why run containers as non-root?
  • 73Q9. Is Kubernetes Secret encrypted?
  • 74Q10. Does a toleration give API permissions?
  • 75RBAC
  • 76Workload identity
  • 77Container security
  • 78Golden rule
  • 79Next โ†’ Chapter 20: Network Policies ๐ŸŒ๐Ÿ”
In this chapter (79 sections)
01Kubernetes Security Model02Authentication vs Authorization03Authentication04Authorization05Simple Real-World Example06RBAC07What Is RBAC?08RBAC Mental Model09Role10Understanding rules11Resources12Verbs13API group14Common Kubernetes Verbs15RoleBinding16Role vs RoleBinding17Role18RoleBinding19Complete Example20Step 1 โ€” Role21Step 2 โ€” RoleBinding22Why Not Give Everyone Admin?23Least Privilege24Principle of Least Privilege25ClusterRole26ClusterRoleBinding27Role vs ClusterRole28ServiceAccounts29What Is a ServiceAccount?30Why Would a Pod Need Kubernetes API Access?31ServiceAccount + Role32Important Security Rule33Default ServiceAccount34Disable Unnecessary API Access35Secrets Are Not Automatically "Secure"36Base64 Is NOT Encryption37External Secret Management38SecurityContext39Why Avoid Root?40runAsUser41readOnlyRootFilesystem42Drop Linux Capabilities43Example Secure Container44allowPrivilegeEscalation45Privileged Containers46Pod Security47Namespace-Level Pod Security48Security Layers49Real-World Node.js Application50Example: Backend Needs to Read ConfigMaps51Testing Permissions52Test Another Identity53Common Security Mistakes54Mistake 155Mistake 256Mistake 357Mistake 458Mistake 559Mistake 660Mistake 761Security Architecture62Authentication Flow63The Security Principle You Should Remember64Interview Questions65Q1. What is RBAC?66Q2. Role vs RoleBinding?67Q3. Role vs ClusterRole?68Q4. What is a ServiceAccount?69Q5. User vs ServiceAccount?70Q6. Authentication vs Authorization?71Q7. What is least privilege?72Q8. Why run containers as non-root?73Q9. Is Kubernetes Secret encrypted?74Q10. Does a toleration give API permissions?75RBAC76Workload identity77Container security78Golden rule79Next โ†’ Chapter 20: Network Policies ๐ŸŒ๐Ÿ”

So far, we've focused on making applications:

  • run
  • communicate
  • scale
  • recover
  • schedule
  • execute background jobs

Now we need to answer:

Who is allowed to do what inside the Kubernetes cluster?

For example:

Developer
   โ†“
Can they delete production Pods?
 
Frontend Pod
   โ†“
Can it access PostgreSQL?
 
Node.js API
   โ†“
Can it read Kubernetes Secrets?
 
Monitoring system
   โ†“
Can it inspect Pods?
 
Admin
   โ†“
Can they modify everything?

Kubernetes security is largely about controlling these permissions and reducing unnecessary access.


1. Kubernetes Security Model#

A useful simplified model is:

                  Kubernetes Security
                         โ”‚
       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
       โ–ผ                 โ–ผ                 โ–ผ
 Authentication     Authorization       Workload Security
       โ”‚                 โ”‚                 โ”‚
       โ–ผ                 โ–ผ                 โ–ผ
    "Who are you?"   "What can you do?"  "How does workload run?"
                         โ”‚
                         โ–ผ
                        RBAC

There are several layers:

Identity
   โ†“
Authentication
   โ†“
Authorization
   โ†“
RBAC
   โ†“
Pod / Container Security
   โ†“
Network Security
   โ†“
Secret Management

We'll study NetworkPolicy separately in Chapter 20.


2. Authentication vs Authorization#

This is one of the most important security concepts.

Authentication#

Authentication asks:

Who are you?

Examples:

User = Anubhav
ServiceAccount = node-api

Authorization#

Authorization asks:

What are you allowed to do?

For example:

Anubhav
   โ†“
Can read Pods? โœ…
Can delete Pods? โŒ

So:

Authentication
= Who are you?
 
Authorization
= What can you do?

3. Simple Real-World Example#

Imagine your production cluster.

Developer:

developer@company.com

They should perhaps be able to:

get pods
get logs
describe deployments

But maybe not:

delete namespace
delete production database
modify cluster-wide configuration

So Kubernetes needs an authorization mechanism.

That's where:

RBAC#

comes in.


4. What Is RBAC?#

RBAC stands for:

Role-Based Access Control

Instead of giving permissions individually to every person, you define roles.

Example:

Developer Role
    โ”‚
    โ”œโ”€โ”€ get Pods
    โ”œโ”€โ”€ list Pods
    โ”œโ”€โ”€ get Logs
    โ””โ”€โ”€ describe Deployments

Then assign the role to users or ServiceAccounts.


5. RBAC Mental Model#

Think:

WHO
 โ”‚
 โ–ผ
RoleBinding
 โ”‚
 โ–ผ
Role
 โ”‚
 โ–ผ
Permissions

More accurately:

User / Group / ServiceAccount
            โ”‚
            โ–ผ
       RoleBinding
            โ”‚
            โ–ผ
           Role
            โ”‚
            โ–ผ
     API permissions

For cluster-wide permissions:

User / Group / ServiceAccount
            โ”‚
            โ–ผ
     ClusterRoleBinding
            โ”‚
            โ–ผ
       ClusterRole

6. Role#

A Kubernetes Role defines permissions within a namespace.

Example:

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
 
metadata:
  name: pod-reader
  namespace: development
 
rules:
  - apiGroups: [""]
    resources:
      - pods
    verbs:
      - get
      - list
      - watch

This means:

In the development namespace, this Role allows reading Pods.

It does not automatically grant access to every namespace.


7. Understanding rules#

This section:

rules:
  - apiGroups: [""]
    resources:
      - pods
    verbs:
      - get
      - list
      - watch

contains three important pieces.

Resources#

pods
deployments
services
secrets
configmaps

Verbs#

get
list
watch
create
update
patch
delete

API group#

Pods belong to the core API group:

apiGroups: [""]

Deployments belong to:

apps

8. Common Kubernetes Verbs#

Verb Meaning
get Read one resource
list List resources
watch Watch changes
create Create resource
update Replace/update resource
patch Partially modify resource
delete Delete resource

Example:

get pods

means:

Read a specific Pod.

list pods

means:

List Pods.


9. RoleBinding#

Creating a Role isn't enough.

You need to connect that Role to an identity.

That's what:

RoleBinding

does.

Example:

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
 
metadata:
  name: pod-reader-binding
  namespace: development
 
subjects:
  - kind: User
    name: developer@example.com
 
roleRef:
  kind: Role
  name: pod-reader
  apiGroup: rbac.authorization.k8s.io

Architecture:

developer@example.com
        โ”‚
        โ–ผ
RoleBinding
        โ”‚
        โ–ผ
pod-reader Role
        โ”‚
        โ–ผ
get/list/watch Pods

10. Role vs RoleBinding#

Don't confuse these.

Role#

Defines:

What permissions exist?

RoleBinding#

Defines:

Who gets those permissions?

Memory trick:

Role
= Rules
 
RoleBinding
= Assignment

11. Complete Example#

Let's say:

Namespace = development

We want:

developer@example.com

to read Pods.

Step 1 โ€” Role#

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
 
metadata:
  name: pod-reader
  namespace: development
 
rules:
  - apiGroups: [""]
    resources:
      - pods
    verbs:
      - get
      - list
      - watch

Step 2 โ€” RoleBinding#

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
 
metadata:
  name: pod-reader-binding
  namespace: development
 
subjects:
  - kind: User
    name: developer@example.com
 
roleRef:
  kind: Role
  name: pod-reader
  apiGroup: rbac.authorization.k8s.io

Now:

developer
   โ†“
RoleBinding
   โ†“
pod-reader
   โ†“
Pods
   โ”œโ”€โ”€ get โœ…
   โ”œโ”€โ”€ list โœ…
   โ”œโ”€โ”€ watch โœ…
   โ””โ”€โ”€ delete โŒ

12. Why Not Give Everyone Admin?#

You could give:

cluster-admin

to everyone.

But that's dangerous.

Imagine:

Developer
   โ†“
kubectl delete namespace production

If they have unrestricted permissions:

๐Ÿ’ฅ Production destroyed

Instead:

Developer
   โ†“
Only required permissions

This is called:

Least Privilege#


13. Principle of Least Privilege#

The rule is:

Give an identity only the permissions it actually needs.

For example:

A monitoring service might need:

get pods
list pods
watch pods

It probably doesn't need:

delete pods
delete namespaces
create clusterroles

So:

Required permissions
       โ†“
       only

not:

Everything
       โ†“
just in case

14. ClusterRole#

A Role is namespace-scoped.

A:

ClusterRole

can define permissions usable at cluster scope and can also be bound into namespaces depending on how it's referenced.

Example:

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
 
metadata:
  name: node-reader
 
rules:
  - apiGroups: [""]
    resources:
      - nodes
    verbs:
      - get
      - list
      - watch

Nodes aren't namespace-scoped resources.

So cluster-level access is appropriate here.


15. ClusterRoleBinding#

To give that ClusterRole to a user:

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
 
metadata:
  name: node-reader-binding
 
subjects:
  - kind: User
    name: developer@example.com
 
roleRef:
  kind: ClusterRole
  name: node-reader
  apiGroup: rbac.authorization.k8s.io

Now:

Developer
    โ†“
ClusterRoleBinding
    โ†“
ClusterRole
    โ†“
Nodes

16. Role vs ClusterRole#

Feature Role ClusterRole
Namespace-scoped definition โœ… Can be used
Cluster-scoped resources โŒ โœ…
Nodes โŒ โœ…
Namespace-local Pods โœ… โœ…
Typical use App/team permissions Cluster-wide permissions

A ClusterRole can be bound with a RoleBinding to grant its rules within a particular namespace, which is a useful pattern for reusable permission sets.


17. ServiceAccounts#

So far we've talked about humans.

But applications also need identities.

Suppose:

Node.js API Pod

needs to communicate with the Kubernetes API.

What identity does it use?

A:

ServiceAccount


18. What Is a ServiceAccount?#

A ServiceAccount provides an identity for workloads running inside Kubernetes.

Think:

Human
 โ†“
User identity
 
Pod
 โ†“
ServiceAccount identity

Example:

apiVersion: v1
kind: ServiceAccount
 
metadata:
  name: node-api
  namespace: production

Then:

spec:
  serviceAccountName: node-api

Now the Pod runs using that ServiceAccount identity.


19. Why Would a Pod Need Kubernetes API Access?#

Imagine an internal operator or controller.

It may need to:

List Pods
Watch Deployments
Read ConfigMaps

Instead of giving it an administrator identity:

Pod
 โ†“
ServiceAccount
 โ†“
Minimal Role

20. ServiceAccount + Role#

Example:

apiVersion: v1
kind: Role
 
metadata:
  name: pod-reader
  namespace: production
 
rules:
  - apiGroups: [""]
    resources:
      - pods
    verbs:
      - get
      - list
      - watch

ServiceAccount:

apiVersion: v1
kind: ServiceAccount
 
metadata:
  name: monitoring-app
  namespace: production

RoleBinding:

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
 
metadata:
  name: monitoring-binding
  namespace: production
 
subjects:
  - kind: ServiceAccount
    name: monitoring-app
    namespace: production
 
roleRef:
  kind: Role
  name: pod-reader
  apiGroup: rbac.authorization.k8s.io

Architecture:

Monitoring Pod
      โ”‚
      โ–ผ
ServiceAccount
      โ”‚
      โ–ผ
RoleBinding
      โ”‚
      โ–ผ
Role
      โ”‚
      โ–ผ
get/list/watch Pods

21. Important Security Rule#

Don't give every Pod:

cluster-admin

Instead:

Pod
 โ†“
Dedicated ServiceAccount
 โ†“
Minimal RBAC permissions

This dramatically reduces blast radius.


22. Default ServiceAccount#

Pods can use a namespace's default ServiceAccount if you don't specify one.

For example:

kubectl get serviceaccounts

You may see:

NAME      SECRETS
default   ...

However, production workloads should generally use an explicitly chosen ServiceAccount when they need permissions.


23. Disable Unnecessary API Access#

If your application doesn't need to communicate with the Kubernetes API, consider:

automountServiceAccountToken: false

Example:

apiVersion: apps/v1
kind: Deployment
 
metadata:
  name: frontend
 
spec:
  replicas: 3
 
  template:
    metadata:
      labels:
        app: frontend
 
    spec:
      automountServiceAccountToken: false
 
      containers:
        - name: frontend
          image: my-frontend:1.0

This reduces unnecessary credentials exposure to the application.


24. Secrets Are Not Automatically "Secure"#

We covered Secrets in Chapter 9.

A Kubernetes Secret is useful for sensitive configuration, but don't assume:

Secret
=
Automatically encrypted everywhere

Important considerations include:

  • RBAC permissions
  • encryption at rest
  • API access
  • secret exposure in logs
  • secret exposure in environment variables
  • cluster configuration
  • external secret managers

25. Base64 Is NOT Encryption#

Remember:

data:
  password: bXlQYXNzd29yZA==

That is Base64 encoding.

Someone can decode it.

Conceptually:

Base64
   โ†“
Encoding
   โŒ
Encryption

Never treat Base64 as a security mechanism.


26. External Secret Management#

Production environments often integrate with secret-management systems such as:

AWS Secrets Manager
Google Secret Manager
Azure Key Vault
HashiCorp Vault

Architecture:

External Secret Manager
         โ”‚
         โ–ผ
 Kubernetes integration
         โ”‚
         โ–ผ
      Secret
         โ”‚
         โ–ผ
        Pod

This can reduce the need to store long-lived plaintext credentials directly in application manifests.


27. SecurityContext#

Now let's move from API authorization to container security.

Kubernetes supports:

securityContext

It controls security-related settings for Pods and containers.

For example:

securityContext:
  runAsNonRoot: true

This means:

The container should not run as root.


28. Why Avoid Root?#

Suppose your application is compromised.

If it runs as root:

Attacker
   โ†“
Compromised application
   โ†“
Root privileges
   โ†“
Potentially greater impact

If it runs as a non-root user:

Attacker
   โ†“
Compromised application
   โ†“
Limited privileges

This is an example of reducing blast radius.


29. runAsUser#

You can specify a user ID:

securityContext:
  runAsUser: 10001
  runAsNonRoot: true

Now the container process should run as UID:

10001

Your Docker image must be compatible with this user.


30. readOnlyRootFilesystem#

You can make the container's root filesystem read-only:

securityContext:
  readOnlyRootFilesystem: true

Then:

Application
   โ†“
Cannot normally write to root filesystem

If the application needs temporary writable storage, you can mount a writable volume such as emptyDir at a specific path.


31. Drop Linux Capabilities#

Linux capabilities provide subsets of privileged operations.

You can drop capabilities:

securityContext:
  capabilities:
    drop:
      - ALL

Then selectively add only what is genuinely required.

This follows the same principle:

Least privilege.


32. Example Secure Container#

apiVersion: v1
kind: Pod
 
metadata:
  name: secure-app
 
spec:
  securityContext:
    runAsNonRoot: true
 
  containers:
    - name: app
      image: my-node-api:1.0
 
      securityContext:
        allowPrivilegeEscalation: false
        readOnlyRootFilesystem: true
 
        capabilities:
          drop:
            - ALL

This is a good starting pattern, but your application must actually be compatible with these restrictions.


33. allowPrivilegeEscalation#

Consider:

allowPrivilegeEscalation: false

This helps prevent a process from gaining more privileges than its parent process allows through supported Linux privilege mechanisms.

Again:

Security settings should be tested against your application rather than copied blindly.


34. Privileged Containers#

Avoid:

securityContext:
  privileged: true

unless there is a well-understood infrastructure requirement.

A privileged container has substantially elevated access to the host.

For ordinary:

Node.js
Python
Next.js
Nginx

applications, privileged mode is generally unnecessary.


35. Pod Security#

Kubernetes provides Pod Security Standards with profiles commonly described as:

Privileged
Baseline
Restricted

Conceptually:

Privileged
    โ†“
More permissions
 
Baseline
    โ†“
Common restrictions
 
Restricted
    โ†“
Stronger hardening

For production workloads, stronger restrictions are generally desirable when the application supports them.


36. Namespace-Level Pod Security#

Pod security controls can be applied through namespace labels.

For example, a namespace can be configured to enforce a Pod Security Standard level.

Conceptually:

Namespace: production
       โ”‚
       โ–ผ
Pod Security policy level
       โ”‚
       โ–ผ
Pods must satisfy restrictions

This helps prevent developers from accidentally deploying overly privileged workloads.


37. Security Layers#

Now let's combine everything:

                   Kubernetes Security
                           โ”‚
          โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
          โ–ผ                โ–ผ                 โ–ผ
      Identity          RBAC            Workload
          โ”‚                โ”‚              Security
          โ–ผ                โ–ผ                 โ–ผ
   User/ServiceAccount   Role          SecurityContext
                         โ”‚
                    RoleBinding
                         โ”‚
                         โ–ผ
                   Permissions

Then networking:

                 NetworkPolicy
                      โ”‚
                      โ–ผ
              Pod-to-Pod traffic

We'll study this in Chapter 20.


38. Real-World Node.js Application#

Imagine:

production
โ”‚
โ”œโ”€โ”€ frontend
โ”œโ”€โ”€ backend
โ”œโ”€โ”€ redis
โ””โ”€โ”€ worker

You might design identities like:

frontend-sa
backend-sa
worker-sa

And permissions:

frontend-sa
โ†’ almost no Kubernetes API permissions
 
backend-sa
โ†’ only required API permissions
 
worker-sa
โ†’ only required API permissions

Architecture:

Frontend Pod
    โ”‚
    โ–ผ
frontend-sa
    โ”‚
    โ–ผ
Minimal permissions
 
Backend Pod
    โ”‚
    โ–ผ
backend-sa
    โ”‚
    โ–ผ
Required permissions only
 
Worker Pod
    โ”‚
    โ–ผ
worker-sa
    โ”‚
    โ–ผ
Required permissions only

39. Example: Backend Needs to Read ConfigMaps#

Suppose your Node.js backend needs to watch a ConfigMap.

Create:

apiVersion: v1
kind: ServiceAccount
 
metadata:
  name: backend-sa
  namespace: production

Role:

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
 
metadata:
  name: backend-config-reader
  namespace: production
 
rules:
  - apiGroups: [""]
    resources:
      - configmaps
    verbs:
      - get
      - list
      - watch

RoleBinding:

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
 
metadata:
  name: backend-config-reader-binding
  namespace: production
 
subjects:
  - kind: ServiceAccount
    name: backend-sa
    namespace: production
 
roleRef:
  kind: Role
  name: backend-config-reader
  apiGroup: rbac.authorization.k8s.io

Deployment:

spec:
  template:
    spec:
      serviceAccountName: backend-sa

Now:

Backend
  โ†“
backend-sa
  โ†“
RoleBinding
  โ†“
Role
  โ†“
ConfigMap

40. Testing Permissions#

One extremely useful command:

kubectl auth can-i get pods

This asks:

Can the current identity get Pods?

You can test:

kubectl auth can-i delete pods

Maybe:

no

Excellent.


41. Test Another Identity#

You can also check as a specific user or ServiceAccount:

kubectl auth can-i get pods \
  --as=system:serviceaccount:production:backend-sa

And:

kubectl auth can-i delete pods \
  --as=system:serviceaccount:production:backend-sa

This is very useful for debugging RBAC.


42. Common Security Mistakes#

Mistake 1#

Giving every application:

cluster-admin

โŒ Extremely broad permissions.


Mistake 2#

Using one ServiceAccount for everything.

All Pods
 โ†“
same identity

โŒ Large blast radius.


Mistake 3#

Storing secrets in Git.

DATABASE_PASSWORD=myPassword123

โŒ Dangerous.


Mistake 4#

Thinking Base64 is encryption.

Base64 โ‰  encryption

Mistake 5#

Running everything as root.

root

โŒ Unnecessary privilege.


Mistake 6#

Using:

privileged: true

without a strong reason.


Mistake 7#

Giving applications Kubernetes API permissions they don't need.


43. Security Architecture#

A production-oriented application could look like:

                         Kubernetes Cluster
                                โ”‚
              โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
              โ”‚                 โ”‚                 โ”‚
              โ–ผ                 โ–ผ                 โ–ผ
          Frontend            Backend           Worker
              โ”‚                 โ”‚                 โ”‚
              โ–ผ                 โ–ผ                 โ–ผ
       frontend-sa       backend-sa          worker-sa
              โ”‚                 โ”‚                 โ”‚
              โ–ผ                 โ–ผ                 โ–ผ
         Minimal RBAC      Required RBAC      Required RBAC
                                โ”‚
                                โ–ผ
                           Kubernetes API
 
         Secrets
            โ”‚
            โ–ผ
   External Secret Manager
            โ”‚
            โ–ผ
        Kubernetes
         Secrets
 
       NetworkPolicy
            โ”‚
            โ–ผ
     Pod-to-Pod restrictions

44. Authentication Flow#

For a human:

Developer
    โ”‚
    โ–ผ
Identity Provider / Cluster Auth
    โ”‚
    โ–ผ
Authenticated identity
    โ”‚
    โ–ผ
RBAC
    โ”‚
    โ–ผ
Kubernetes API

For a Pod:

Pod
 โ”‚
 โ–ผ
ServiceAccount
 โ”‚
 โ–ผ
RBAC
 โ”‚
 โ–ผ
Kubernetes API

45. The Security Principle You Should Remember#

Almost every Kubernetes security decision can be simplified to:

Give the smallest amount of access required for the shortest reasonable scope.

That means:

Namespace scope
   over
Cluster scope

when possible.

Specific resource
   over
All resources
Read-only
   over
Write
Non-root
   over
Root
Dedicated ServiceAccount
   over
Shared powerful identity

46. Interview Questions#

Q1. What is RBAC?#

Role-Based Access Control is Kubernetes' authorization mechanism for controlling what users, groups, and ServiceAccounts can do with Kubernetes resources.

Q2. Role vs RoleBinding?#

Role
โ†’ permissions
 
RoleBinding
โ†’ assigns permissions

Q3. Role vs ClusterRole?#

Role
โ†’ namespace-oriented
 
ClusterRole
โ†’ cluster-wide resource permissions and reusable permission definitions

Q4. What is a ServiceAccount?#

An identity used by workloads running inside Kubernetes.

Q5. User vs ServiceAccount?#

User
โ†’ typically human/external identity
 
ServiceAccount
โ†’ workload identity

Q6. Authentication vs Authorization?#

Authentication
โ†’ Who are you?
 
Authorization
โ†’ What can you do?

Q7. What is least privilege?#

Grant only the permissions required to perform the intended task.

Q8. Why run containers as non-root?#

To reduce the potential impact if the application or container is compromised.

Q9. Is Kubernetes Secret encrypted?#

Not automatically merely because it is a Secret object. Security depends on configuration such as encryption at rest, access controls, and how secrets are handled.

Q10. Does a toleration give API permissions?#

No.

Tolerations are about Pod scheduling onto tainted nodes, not RBAC.


๐Ÿง  Chapter 19 Cheat Sheet#

Authentication
      โ†“
"Who are you?"
 
Authorization
      โ†“
"What can you do?"
 
RBAC
      โ†“
Permissions

RBAC#

Role
   โ†“
Permissions
 
RoleBinding
   โ†“
Who gets Role?
 
ClusterRole
   โ†“
Cluster-level/reusable permissions
 
ClusterRoleBinding
   โ†“
Who gets ClusterRole?

Workload identity#

Pod
 โ†“
ServiceAccount
 โ†“
RBAC

Container security#

securityContext
 โ”œโ”€โ”€ runAsNonRoot
 โ”œโ”€โ”€ runAsUser
 โ”œโ”€โ”€ readOnlyRootFilesystem
 โ”œโ”€โ”€ allowPrivilegeEscalation
 โ””โ”€โ”€ capabilities

Golden rule#

Least privilege everywhere.


๐ŸŽฏ Your Production Security Mental Model#

For the Node.js/Next.js platform we've been building:

                       USER
                        โ”‚
                        โ–ผ
                     Ingress
                        โ”‚
                        โ–ผ
                     Service
                        โ”‚
                        โ–ผ
                 โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                 โ”‚ Node.js API  โ”‚
                 โ”‚     Pod      โ”‚
                 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                        โ”‚
                  ServiceAccount
                        โ”‚
                        โ–ผ
                       RBAC
                        โ”‚
                 Only required
                 Kubernetes API
                 permissions
 
          โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
          โ”‚                          โ”‚
          โ–ผ                          โ–ผ
       Secrets                  SecurityContext
          โ”‚                          โ”‚
          โ–ผ                          โ–ผ
 External Secret Manager        Non-root
                                Read-only FS
                                No privilege escalation
 
                       โ”‚
                       โ–ผ
                 NetworkPolicy
                       โ”‚
                       โ–ผ
              Allowed Pod traffic

You now have the foundation for securing a production Kubernetes application.


Next โ†’ Chapter 20: Network Policies ๐ŸŒ๐Ÿ”#

This is the natural next step.

We'll answer:

"If every Pod can potentially communicate over the cluster network, how do I control who can talk to whom?"

We'll build rules like:

Internet
   โ”‚
   โ–ผ
Ingress
   โ”‚
   โ–ผ
Frontend
   โ”‚
   โ–ผ
Backend โœ…
   โ”‚
   โ”œโ”€โ”€ PostgreSQL โœ…
   โ”œโ”€โ”€ Redis โœ…
   โ””โ”€โ”€ Other Pods โŒ

We'll cover Ingress/Egress policies, default-deny, labels, namespaces, DNS access, PostgreSQL/Redis isolation, and real production NetworkPolicy YAML.

Finished this chapter?

Mark it as complete to track your mastery.

Previous ChapterChapter 18 โ€” Jobs & CronJobsNext ChapterChapter 20 โ€” Kubernetes Network Policies ๐ŸŒ๐Ÿ”
PrevNext