Kubernetes Security Best Practices π
The Castle Guard Story
Imagine your Kubernetes cluster is a magical castle with treasures inside. Youβre the chief guard. Your job? Keep the bad guys OUT while letting the good people do their work safely.
Today we learn three super-important guard rules:
- Security Best Practices - The master rulebook
- Least Privilege Principle - Give only whatβs needed
- Image Security Scanning - Check before you let them in
1. Security Best Practices π°
What Are They?
Security best practices are like the rulebook for guards. They tell you:
- What to check
- What to lock
- What to watch
The 5 Golden Rules
Think of these as the five guard duties:
| Rule | What It Means | Castle Example |
|---|---|---|
| Lock the gates | Secure network access | Only open needed doors |
| Check ID cards | Authentication | Know who enters |
| Set permissions | Authorization | Who can go where |
| Watch everything | Monitoring | Eyes on all rooms |
| Update locks | Keep things patched | Fix broken locks fast |
Real Kubernetes Example
# Good: Secure Pod Configuration
apiVersion: v1
kind: Pod
metadata:
name: secure-app
spec:
securityContext:
runAsNonRoot: true
runAsUser: 1000
containers:
- name: app
image: myapp:v1.2.3
securityContext:
readOnlyRootFilesystem: true
allowPrivilegeEscalation: false
What this does:
runAsNonRoot: trueβ Donβt run as king (root)readOnlyRootFilesystem: trueβ Canβt write on castle wallsallowPrivilegeEscalation: falseβ Canβt promote yourself to king
Quick Security Checklist
β Enable RBAC (Role-Based Access Control) β Use Network Policies β Enable Audit Logging β Encrypt Secrets β Keep Kubernetes Updated
2. Least Privilege Principle π
The Story
Imagine you hire a gardener for your castle.
Bad approach: Give them keys to EVERY room
- Kitchen? β
- Treasury? β
- Bedrooms? β
- Dungeon? β
Good approach: Give them keys to ONLY what they need
- Garden shed? β
- Garden gate? β
- Everything else? β
This is Least Privilege. Give the minimum access needed to do the job.
Why It Matters
graph TD A["Gardener gets ALL keys"] --> B["Gardener account hacked"] B --> C["Hacker accesses EVERYTHING"] C --> D["π Castle destroyed"] E["Gardener gets ONLY garden keys"] --> F["Gardener account hacked"] F --> G["Hacker accesses garden only"] G --> H["β Castle safe!"]
Kubernetes Example: RBAC
Bad - Too Much Power:
# DON'T DO THIS
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: bad-binding
subjects:
- kind: ServiceAccount
name: my-app
namespace: default
roleRef:
kind: ClusterRole
name: cluster-admin # ALL THE KEYS!
apiGroup: rbac.authorization.k8s.io
Good - Just Enough:
# DO THIS INSTEAD
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: pod-reader
namespace: my-namespace
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list"] # Only read pods
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: read-pods
namespace: my-namespace
subjects:
- kind: ServiceAccount
name: my-app
namespace: my-namespace
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io
The Three Questions
Before giving access, always ask:
- What do they need to access? β Only those resources
- Where do they need it? β Only those namespaces
- How do they need to use it? β Only those actions (get, list, create, etc.)
Real Life Examples
| Who | What They Need | What They Get |
|---|---|---|
| Monitoring tool | Read metrics | get, list on pods |
| Deployment tool | Deploy apps | create, update on deployments |
| Log viewer | See logs | get on pods/log |
| Admin | Everything | Full access (rare!) |
3. Image Security Scanning π
The Story
Before letting someone into your castle, you check their bags.
Why? They might carry:
- π‘οΈ Hidden weapons (malware)
- π Bugs (vulnerabilities)
- π Fake documents (outdated packages)
Image scanning = Checking bags before entry!
What Gets Scanned?
graph TD A["Container Image"] --> B["Scanner"] B --> C{Check for} C --> D["Known Vulnerabilities"] C --> E["Malware"] C --> F["Misconfigurations"] C --> G["Secrets/Passwords"] D --> H["Safe to deploy?"] E --> H F --> H G --> H
How It Works
Step 1: You build an image
FROM node:18
COPY . /app
RUN npm install
CMD ["node", "app.js"]
Step 2: Scanner checks it
# Using Trivy (popular scanner)
trivy image myapp:latest
Step 3: See the report
myapp:latest (debian 11.6)
==========================
Total: 42 (CRITICAL: 2, HIGH: 8, MEDIUM: 20, LOW: 12)
ββββββββββββββββ¬ββββββββββββ¬βββββββββββ
β Library β Severity β Status β
ββββββββββββββββΌββββββββββββΌβββββββββββ€
β openssl β CRITICAL β Fix: 3.0 β
β curl β HIGH β Fix: 8.1 β
ββββββββββββββββ΄ββββββββββββ΄βββββββββββ
Popular Scanning Tools
| Tool | Type | Best For |
|---|---|---|
| Trivy | Open source | Quick local scans |
| Snyk | Commercial | Developer-friendly |
| Anchore | Enterprise | Policy enforcement |
| Clair | Open source | Registry integration |
Kubernetes Integration
Block bad images automatically:
# Admission Controller Policy
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingWebhookConfiguration
metadata:
name: image-scanner
webhooks:
- name: scan.images.k8s.io
rules:
- apiGroups: [""]
resources: ["pods"]
operations: ["CREATE"]
# Blocks pods with vulnerable images
Best Practices for Image Scanning
β Scan in CI/CD - Check before images reach production β Use trusted base images - Start with official images β Scan regularly - New vulnerabilities appear daily β Set severity thresholds - Block CRITICAL, warn on HIGH β Keep images updated - Old images = more vulnerabilities
Simple Workflow
graph LR A["Developer pushes code"] --> B["CI builds image"] B --> C["Scanner checks image"] C --> D{Vulnerabilities?} D -->|CRITICAL found| E["β Block deploy"] D -->|Clean or LOW| F["β Deploy to K8s"]
Bringing It All Together π―
These three practices work together like a security team:
| Practice | Guard Role | What It Stops |
|---|---|---|
| Best Practices | Chief Guard | Overall bad behavior |
| Least Privilege | Key Master | Unauthorized access |
| Image Scanning | Bag Checker | Hidden threats |
Your Security Checklist
Daily Habits:
- [ ] Run image scans on new deployments
- [ ] Review access permissions monthly
- [ ] Check for Kubernetes security updates
One-Time Setup:
- [ ] Enable RBAC
- [ ] Configure Network Policies
- [ ] Set up admission controllers
- [ ] Integrate scanner in CI/CD
Remember! π§
Security is not a feature. Itβs a habit.
Start with these three practices:
- Follow the rulebook (Security Best Practices)
- Give only whatβs needed (Least Privilege)
- Check before trusting (Image Scanning)
Your Kubernetes castle will be safe! π°β¨
