🧠 Concept 18: Security (RBAC + Service Accounts + Production πŸ”)

Image

Image

Image

Image


πŸš€ 1. Core Idea (1-line)

πŸ‘‰ Kubernetes security controls who can do what using RBAC + Service Accounts


🧠 2. Why Security is Needed (VERY IMPORTANT ⚠️)

Without RBAC:

  • Anyone can delete pods ❌

  • Anyone can access secrets ❌

  • Cluster = unsafe πŸ’€


πŸ” 3. Authentication vs Authorization (MUST KNOW πŸ”₯)

πŸ”‘ Authentication β†’ β€œWho are you?”

  • User / Service Account

πŸ›‘οΈ Authorization β†’ β€œWhat can you do?”

  • RBAC decides this

βš™οΈ 4. RBAC (Role-Based Access Control)


🟒 1. Role

πŸ‘‰ Defines permissions inside a namespace

kind: Role
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["get", "list"]

πŸ”΅ 2. RoleBinding

πŸ‘‰ Assigns role to user/service account

kind: RoleBinding
subjects:
- kind: User
  name: yash

🌐 3. ClusterRole

πŸ‘‰ Same as Role but cluster-wide


πŸ”— 4. ClusterRoleBinding

πŸ‘‰ Binds ClusterRole globally


πŸ”₯ 5. Flow (VERY IMPORTANT πŸ”₯)

User β†’ API Server β†’ RBAC check β†’ Allowed / Denied

🧠 6. Service Accounts (VERY IMPORTANT πŸ”₯)

πŸ‘‰ Used by pods to talk to Kubernetes API


Example:

apiVersion: v1
kind: ServiceAccount
metadata:
  name: my-sa

πŸ‘‰ Attach to pod:

spec:
  serviceAccountName: my-sa

πŸ’₯ 7. Real-world Example

Pod wants to:

  • Read secrets

  • Access API

πŸ‘‰ Needs:

  • Service Account

  • Proper Role


⚠️ 8. VERY IMPORTANT Security Rules

  • Never give cluster-admin ❌

  • Follow least privilege principle

  • Rotate secrets


πŸ”’ 9. Secrets Security (Advanced)

Earlier we saw Secrets πŸ‘€

πŸ‘‰ For production:

  • Enable encryption at rest

  • Use:

  • AWS Secrets Manager

  • HashiCorp Vault


πŸ”₯ 10. Real DevOps Insight

In companies:

  • Dev β†’ limited access

  • CI/CD β†’ controlled access

  • Apps β†’ service accounts

πŸ‘‰ Everything controlled via RBAC


⚠️ 11. Common Mistakes

❌ Giving full access
❌ Not using service accounts
❌ Hardcoding credentials


πŸ’Ό 12. Interview Answer

πŸ‘‰ β€œKubernetes security is implemented using RBAC for authorization and Service Accounts for pod-level identity, ensuring controlled and secure access to cluster resources.”


⚑ 13. CKA Commands

kubectl get roles
kubectl get rolebindings
kubectl get serviceaccounts

🧠 14. Memory Trick

πŸ‘‰ RBAC = permissions 🎫
πŸ‘‰ Service Account = identity πŸ†”


πŸ”₯ 15. Pro Insight (Real-world)

πŸ‘‰ Combine:

  • RBAC

  • Network Policies

  • Pod Security

πŸ‘‰ For full cluster security πŸ”


πŸš€ Next Step

Bol:

πŸ‘‰ β€œnext”

Then we go to FINAL:

πŸ”₯ Concept 19: Troubleshooting (Debugging K8s like a PRO πŸ’― β€” MOST IMPORTANT FOR CKA πŸ”₯)