Kubernetes
Chapter 60: Kubernetes Basics
Section titled “Chapter 60: Kubernetes Basics”Overview
Section titled “Overview”This chapter covers Kubernetes container orchestration basics.
Why This Matters in DevOps/SRE
Section titled “Why This Matters in DevOps/SRE”Kubernetes is the industry-standard for container orchestration - essential for managing containerized applications at scale. Understanding K8s is critical for cloud-native development and DevOps roles.
┌─────────────────────────────────────────────────────────────────────────────┐│ KUBERNETES IN DEVOPS │├─────────────────────────────────────────────────────────────────────────────┤│ ││ ┌─────────────────────────────────────────────────────────────────────┐ ││ │ WHY KUBERNETES │ ││ │ │ ││ │ • Container orchestration at scale │ ││ │ • Self-healing and automated rollouts │ ││ │ • Service discovery and load balancing │ ││ │ • Declarative configuration (GitOps ready) │ ││ │ • Runs on any infrastructure (cloud, on-prem, edge) │ ││ │ │ ││ └─────────────────────────────────────────────────────────────────────┘ ││ ││ ┌─────────────────────────────────────────────────────────────────────┐ ││ │ KEY CONCEPTS │ ││ │ │ ││ │ • Pod: Smallest deployable unit │ ││ │ • Deployment: Manages replica sets and updates │ ││ │ • Service: Network abstraction for pods │ ││ │ • ReplicaSet: Ensures desired number of pods │ ││ │ • Namespace: Resource isolation │ ││ │ │ ││ └─────────────────────────────────────────────────────────────────────┘ ││ │└─────────────────────────────────────────────────────────────────────────────┘60.1 Kubernetes Architecture
Section titled “60.1 Kubernetes Architecture”Components
Section titled “Components” Kubernetes Architecture+------------------------------------------------------------------+| || +------------------+ || | Control Plane | || | +--------------+ | || | | kube-apiserver| || | | etcd | || | | kube-scheduler| || | | kube-controller| || | +--------------+ | || +------------------+ || | || | || +------------------+ +------------------+ || | Worker Nodes | | Worker Nodes | || | +--------------+ | | +--------------+ | || | | kubelet | | | | kubelet | | || | | kube-proxy | | | | kube-proxy | | || | | containerd | | | | containerd | | || | +--------------+ | | +--------------+ | || +------------------+ +------------------+ || |+------------------------------------------------------------------+60.2 kubectl
Section titled “60.2 kubectl”Basic Commands
Section titled “Basic Commands”# Cluster infokubectl cluster-infokubectl get nodes
# Podskubectl get podskubectl get pods -o widekubectl describe pod pod_namekubectl logs pod_namekubectl exec -it pod_name -- /bin/sh
# Deploymentskubectl get deploymentskubectl create deployment nginx --image=nginxkubectl scale deployment nginx --replicas=3kubectl rollout status deployment nginx
# Serviceskubectl get serviceskubectl expose deployment nginx --port=8060.3 Pods
Section titled “60.3 Pods”Pod Definition
Section titled “Pod Definition”apiVersion: v1kind: Podmetadata: name: nginx labels: app: nginxspec: containers: - name: nginx image: nginx:1.24 ports: - containerPort: 80 resources: limits: memory: "128Mi" cpu: "500m" requests: memory: "64Mi" cpu: "250m"60.4 Deployments
Section titled “60.4 Deployments”Deployment Example
Section titled “Deployment Example”apiVersion: apps/v1kind: Deploymentmetadata: name: nginxspec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.24 ports: - containerPort: 8060.5 Services
Section titled “60.5 Services”Service Types
Section titled “Service Types”apiVersion: v1kind: Servicemetadata: name: nginxspec: type: ClusterIP # or NodePort, LoadBalancer selector: app: nginx ports: - port: 80 targetPort: 80# NodePort examplekubectl expose deployment nginx --type=NodePort
# LoadBalancerkubectl expose deployment nginx --type=LoadBalancer60.6 ConfigMaps and Secrets
Section titled “60.6 ConfigMaps and Secrets”ConfigMap
Section titled “ConfigMap”apiVersion: v1kind: ConfigMapmetadata: name: app-configdata: database_url: "postgres://db:5432/app" cache_enabled: "true"Secret
Section titled “Secret”apiVersion: v1kind: Secretmetadata: name: db-secrettype: OpaquestringData: username: admin password: changeme60.7 Namespaces
Section titled “60.7 Namespaces”Namespace Commands
Section titled “Namespace Commands”# List namespaceskubectl get namespaces
# Create namespacekubectl create namespace dev
# Use namespacekubectl config set-context --current --namespace=dev
# Get in namespacekubectl get pods -n devCommon Mistakes & Anti-Patterns
Section titled “Common Mistakes & Anti-Patterns”1. Running Pods Without Resource Limits
Section titled “1. Running Pods Without Resource Limits”WRONG:
spec: containers: - name: app image: myapp# No resources defined - pod can consume all node resourcesCORRECT:
spec: containers: - name: app image: myapp resources: requests: memory: "128Mi" cpu: "250m" limits: memory: "256Mi" cpu: "500m"Why: Without limits, one pod can starve others; also enables scheduling decisions.
2. Not Using Namespaces
Section titled “2. Not Using Namespaces”WRONG:
# All resources in default namespacekubectl get pods# Mixed dev, staging, productionCORRECT:
# Separate namespaces for environmentskubectl create namespace devkubectl create namespace stagingkubectl create namespace production
# Use RBAC to restrict accessWhy: Namespaces provide isolation and prevent accidental deletion.
3. Storing Secrets in ConfigMaps
Section titled “3. Storing Secrets in ConfigMaps”WRONG:
apiVersion: v1kind: ConfigMapmetadata: name: app-configdata: database-password: "secretpassword" # WRONG!CORRECT:
apiVersion: v1kind: Secretmetadata: name: app-secretstype: OpaquestringData: database-password: "secretpassword"Why: Secrets are base64 encoded, not encrypted - but better than ConfigMaps.
4. Not Using Liveness/Readiness Probes
Section titled “4. Not Using Liveness/Readiness Probes”WRONG:
spec: containers: - name: app image: myapp# No health checks - unhealthy pods stay in serviceCORRECT:
spec: containers: - name: app image: myapp livenessProbe: httpGet: path: /healthz port: 8080 initialDelaySeconds: 30 periodSeconds: 10 readinessProbe: httpGet: path: /ready port: 8080 periodSeconds: 5Why: Probes enable self-healing and proper traffic routing.
5. Using Latest Image Tag
Section titled “5. Using Latest Image Tag”WRONG:
spec: containers: - name: app image: myapp:latest # Changes over time!CORRECT:
spec: containers: - name: app image: myapp:v1.2.3 # Pin to specific versionWhy: latest changes - non-reproducible deployments.
Summary
Section titled “Summary”In this chapter, you learned:
- ✅ Kubernetes architecture
- ✅ kubectl commands
- ✅ Pods
- ✅ Deployments
- ✅ Services
- ✅ ConfigMaps and Secrets
- ✅ Namespaces
Next Chapter
Section titled “Next Chapter”Chapter 60: Container Security
Last Updated: February 2026