🏷️ Resource Organization: Labels and Namespaces
The Big Picture: Organizing Your Toy Box
Imagine you have a huge toy box with hundreds of toys. Without any organization, finding your favorite red car would be impossible!
Kubernetes is like that giant toy box, but instead of toys, it holds pods, services, and other resources. Labels and Namespaces are your stickers and dividers that help you find and organize everything perfectly.
🏷️ Labels and Selectors
What Are Labels?
Labels are like sticky notes you put on your toys.
Simple Example:
- You stick a note saying “color: red” on your red car
- Another note says “type: car”
- Now you can find ALL red things or ALL cars quickly!
metadata:
labels:
color: red
type: car
owner: alex
Real Kubernetes Example
apiVersion: v1
kind: Pod
metadata:
name: my-web-app
labels:
app: website
environment: production
team: frontend
What this means:
app: website→ This pod runs a websiteenvironment: production→ It’s the real, live versionteam: frontend→ The frontend team owns it
Selectors: Finding Your Toys
Selectors are like saying: “Show me all toys that are RED and are CARS”
selector:
matchLabels:
app: website
environment: production
This finds ALL pods with both labels!
Label Rules
graph TD A[📝 Label Rules] --> B[Key: max 63 chars] A --> C[Value: max 63 chars] A --> D[Must start with letter/number] A --> E[Can use: - _ .] style A fill:#4ECDC4,color:#fff style B fill:#FFE66D,color:#333 style C fill:#FFE66D,color:#333 style D fill:#FFE66D,color:#333 style E fill:#FFE66D,color:#333
📝 Annotations
Labels vs Annotations: What’s Different?
Labels = For FINDING things (like tags on a library book) Annotations = For STORING INFO (like notes inside the book)
When to Use Annotations
Think of annotations as a notebook attached to your resource:
metadata:
annotations:
description: "Main website for customers"
build-version: "v2.3.1"
last-updated: "2024-01-15"
contact: "team@company.com"
Key Differences:
| Feature | Labels | Annotations |
|---|---|---|
| Purpose | Find & select | Store info |
| Can search? | ✅ Yes | ❌ No |
| Size limit | 63 chars | 256KB |
| Used for | Grouping | Documentation |
Real-World Annotation Example
apiVersion: v1
kind: Pod
metadata:
name: payment-service
annotations:
kubernetes.io/change-cause: |
Updated to fix security bug
prometheus.io/scrape: "true"
prometheus.io/port: "9090"
📦 Namespaces
The Room Dividers
Imagine your house has different rooms:
- Kitchen for cooking stuff
- Bedroom for sleeping stuff
- Playroom for toys
Namespaces work the same way! They create separate rooms inside Kubernetes.
graph TD K8s[☸️ Kubernetes Cluster] --> NS1[📦 default] K8s --> NS2[📦 production] K8s --> NS3[📦 development] K8s --> NS4[📦 kube-system] NS2 --> P1[🔵 web-pod] NS2 --> P2[🔵 api-pod] NS3 --> P3[🟢 web-pod] NS3 --> P4[🟢 api-pod] style K8s fill:#667eea,color:#fff style NS1 fill:#FF6B6B,color:#fff style NS2 fill:#4ECDC4,color:#fff style NS3 fill:#FFE66D,color:#333 style NS4 fill:#95E1D3,color:#333
Default Namespaces
Kubernetes comes with these rooms already built:
| Namespace | What Lives Here |
|---|---|
default |
Your stuff (if you don’t specify) |
kube-system |
Kubernetes’s own stuff |
kube-public |
Public information |
kube-node-lease |
Node heartbeats |
Creating a Namespace
apiVersion: v1
kind: Namespace
metadata:
name: my-app-production
labels:
environment: production
Using Namespaces
# See all namespaces
kubectl get namespaces
# Create resources in a namespace
kubectl apply -f pod.yaml -n production
# List pods in a namespace
kubectl get pods -n development
🎯 Namespace Strategies
Strategy 1: By Environment
graph LR A[🏢 Company] --> B[🧪 development] A --> C[🔬 staging] A --> D[🚀 production] style A fill:#667eea,color:#fff style B fill:#95E1D3,color:#333 style C fill:#FFE66D,color:#333 style D fill:#FF6B6B,color:#fff
Perfect for: Testing the same app in different stages
Strategy 2: By Team
graph LR A[🏢 Company] --> B[👥 team-alpha] A --> C[👥 team-beta] A --> D[👥 team-gamma] style A fill:#667eea,color:#fff style B fill:#4ECDC4,color:#fff style C fill:#FF6B6B,color:#fff style D fill:#FFE66D,color:#333
Perfect for: Each team manages their own space
Strategy 3: By Application
graph LR A[🏢 Company] --> B[📱 mobile-app] A --> C[🌐 web-app] A --> D[💳 payments] style A fill:#667eea,color:#fff style B fill:#FF6B6B,color:#fff style C fill:#4ECDC4,color:#fff style D fill:#FFE66D,color:#333
Perfect for: Large apps with many components
Choosing the Right Strategy
# Combined approach example
apiVersion: v1
kind: Namespace
metadata:
name: payments-production
labels:
app: payments
env: production
team: fintech
🖥️ Node Labels and Annotations
Why Label Nodes?
Nodes are like different workers in a factory:
- Some are strong (lots of memory)
- Some are fast (many CPUs)
- Some have special tools (GPUs)
Labels help you send the right jobs to the right workers!
Common Node Labels
# Kubernetes adds these automatically
kubernetes.io/arch: amd64
kubernetes.io/os: linux
node.kubernetes.io/instance-type: m5.large
# You can add your own!
disktype: ssd
gpu: nvidia
zone: us-east-1a
Viewing Node Labels
# See all labels on nodes
kubectl get nodes --show-labels
# See one node's details
kubectl describe node my-node
Using Node Selectors
Want your pod to run ONLY on nodes with SSD disks?
apiVersion: v1
kind: Pod
metadata:
name: fast-database
spec:
nodeSelector:
disktype: ssd
containers:
- name: db
image: postgres
Node Annotations
Store extra information about nodes:
# Example node annotations
metadata:
annotations:
node.alpha.kubernetes.io/ttl: "0"
maintenance-window: "Sun 2-4am UTC"
hardware-generation: "gen3"
🎯 Quick Reference: Putting It All Together
graph LR R[🎯 Resource Organization] --> L[🏷️ Labels] R --> A[📝 Annotations] R --> N[📦 Namespaces] R --> NL[🖥️ Node Labels] L --> L1[Find resources] L --> L2[Group resources] L --> L3[Select targets] A --> A1[Store metadata] A --> A2[Tool configs] A --> A3[Documentation] N --> N1[Isolate environments] N --> N2[Separate teams] N --> N3[Organize apps] NL --> NL1[Schedule pods] NL --> NL2[Hardware targeting] NL --> NL3[Zone awareness] style R fill:#667eea,color:#fff style L fill:#FF6B6B,color:#fff style A fill:#4ECDC4,color:#fff style N fill:#FFE66D,color:#333 style NL fill:#95E1D3,color:#333
🚀 You Did It!
You now understand how Kubernetes organizes resources:
✅ Labels = Sticky notes to find and group things ✅ Selectors = Finding things by their labels ✅ Annotations = Notebooks with extra information ✅ Namespaces = Rooms to separate resources ✅ Namespace Strategies = Smart ways to divide your cluster ✅ Node Labels = Directing pods to the right machines
Think of it like organizing the world’s biggest, most amazing toy collection—and now you’re the expert organizer! 🎉