-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathkubernetes.html
More file actions
785 lines (684 loc) · 33.1 KB
/
kubernetes.html
File metadata and controls
785 lines (684 loc) · 33.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Kubernetes -- Container Orchestration From Zero - Better Dev</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
<link rel="stylesheet" href="style.css">
</head>
<body>
<header class="topbar">
<button class="sidebar-toggle" aria-label="Open navigation" aria-expanded="false">
<span class="hamburger-icon"></span>
</button>
<a href="index.html" class="logo">Better Dev</a>
</header>
<div class="sidebar-backdrop" aria-hidden="true"></div>
<aside class="sidebar" aria-label="Site navigation">
<div class="sidebar-header">
<span class="sidebar-title">Navigation</span>
<button class="sidebar-close" aria-label="Close navigation">×</button>
</div>
<div class="sidebar-search">
<input type="text" class="sidebar-search-input" placeholder="Search topics..." aria-label="Search topics">
<div class="sidebar-search-results"></div>
</div>
<nav class="sidebar-nav">
<div class="sidebar-group">
<a href="index.html">Home</a>
</div>
<div class="sidebar-group">
<div class="sidebar-group-label">Mathematics</div>
<a href="pre-algebra.html">Pre-Algebra</a>
<a href="algebra.html">Algebra</a>
<a href="sequences-series.html">Sequences & Series</a>
<a href="geometry.html">Geometry</a>
<a href="calculus.html">Calculus</a>
<a href="discrete-math.html">Discrete Math</a>
<a href="linear-algebra.html">Linear Algebra</a>
<a href="probability.html">Probability & Statistics</a>
<a href="binary-systems.html">Binary & Number Systems</a>
<a href="number-theory.html">Number Theory for CP</a>
<a href="computational-geometry.html">Computational Geometry</a>
<a href="game-theory.html">Game Theory</a>
</div>
<div class="sidebar-group">
<div class="sidebar-group-label">Data Structures & Algorithms</div>
<a href="dsa-foundations.html">DSA Foundations</a>
<a href="arrays.html">Arrays & Strings</a>
<a href="stacks-queues.html">Stacks & Queues</a>
<a href="hashmaps.html">Hash Maps & Sets</a>
<a href="linked-lists.html">Linked Lists</a>
<a href="trees.html">Trees & BST</a>
<a href="graphs.html">Graphs</a>
<a href="sorting.html">Sorting & Searching</a>
<a href="patterns.html">LeetCode Patterns</a>
<a href="dp.html">Dynamic Programming</a>
<a href="advanced.html">Advanced Topics</a>
<a href="string-algorithms.html">String Algorithms</a>
<a href="advanced-graphs.html">Advanced Graphs</a>
<a href="advanced-dp.html">Advanced DP</a>
<a href="advanced-ds.html">Advanced Data Structures</a>
<a href="leetcode-650.html">The 650 Problems</a>
<a href="competitive-programming.html">CP Roadmap</a>
</div>
<div class="sidebar-group">
<div class="sidebar-group-label">Languages & Systems</div>
<a href="cpp.html">C++</a>
<a href="golang.html">Go</a>
<a href="javascript.html">JavaScript Deep Dive</a>
<a href="typescript.html">TypeScript</a>
<a href="nodejs.html">Node.js Internals</a>
<a href="os.html">Operating Systems</a>
<a href="linux.html">Linux</a>
<a href="git.html">Git</a>
<a href="backend.html">Backend</a>
<a href="system-design.html">System Design</a>
<a href="networking.html">Networking</a>
<a href="cloud.html">Cloud & Infrastructure</a>
<a href="docker.html">Docker & Compose</a>
<a href="kubernetes.html">Kubernetes</a>
<a href="message-queues.html">Queues & Pub/Sub</a>
<a href="selfhosting.html">VPS & Self-Hosting</a>
<a href="databases.html">PostgreSQL & MySQL</a>
<a href="stripe.html">Stripe & Payments</a>
<a href="distributed-systems.html">Distributed Systems</a>
<a href="backend-engineering.html">Backend Engineering</a>
</div>
<div class="sidebar-group">
<div class="sidebar-group-label">JS/TS Ecosystem</div>
<a href="js-tooling.html">Tooling & Bundlers</a>
<a href="js-testing.html">Testing</a>
<a href="ts-projects.html">Building with TS</a>
</div>
<div class="sidebar-group">
<div class="sidebar-group-label">More</div>
<a href="seans-brain.html">Sean's Brain</a>
</div>
</nav>
</aside>
<div class="container">
<!-- ===== PAGE HEADER ===== -->
<div class="page-header">
<div class="breadcrumb"><a href="index.html">Home</a> / Kubernetes</div>
<h1>Kubernetes (K8s)</h1>
<p>Docker runs containers. Kubernetes manages thousands of them. Learn how K8s orchestrates your containers across machines -- scaling, healing, and deploying your apps automatically. Read the <a href="docker.html">Docker page</a> first if you haven't.</p>
</div>
<!-- ===== TABLE OF CONTENTS ===== -->
<div class="toc">
<h4>Table of Contents</h4>
<a href="#why-k8s">1. Why Kubernetes Exists</a>
<a href="#architecture">2. K8s Architecture (Control Plane & Nodes)</a>
<a href="#core-objects">3. Core Objects: Pods, Deployments, Services</a>
<a href="#yaml">4. Writing K8s YAML</a>
<a href="#networking">5. Networking & Service Discovery</a>
<a href="#storage">6. Storage & ConfigMaps</a>
<a href="#scaling">7. Scaling & Self-Healing</a>
<a href="#kubectl">8. Essential kubectl Commands</a>
<a href="#local-dev">9. Running K8s Locally (Minikube)</a>
<a href="#vs-compose">10. Kubernetes vs Docker Compose</a>
</div>
<!-- ============================================================ -->
<!-- SECTION 1: WHY K8S -->
<!-- ============================================================ -->
<section id="why-k8s">
<h2>1. Why Kubernetes Exists</h2>
<p>You know Docker. You can run containers. But what happens when your app gets popular?</p>
<ul>
<li>You need to run <strong>50 copies</strong> of your API across 10 servers to handle the traffic</li>
<li>If one container crashes, something needs to <strong>automatically restart</strong> it</li>
<li>When you deploy a new version, you want <strong>zero downtime</strong> -- no one should notice the update</li>
<li>You need to <strong>distribute traffic evenly</strong> across all those containers</li>
<li>Some containers need more CPU, others need more memory -- you need smart <strong>scheduling</strong></li>
</ul>
<p>Doing all of this manually with <code>docker run</code> commands would be a nightmare. <strong>Kubernetes automates all of it.</strong></p>
<div class="example-box">
<div class="label">The Analogy</div>
<p>Docker is like being able to <strong>cook a dish</strong>. Kubernetes is like being the <strong>manager of a restaurant chain</strong>. You don't cook -- you tell the system "I need 10 chefs making pasta, 5 making pizza, and if anyone calls in sick, hire a replacement immediately". Kubernetes handles the logistics.</p>
<p>You declare <em>what you want</em> ("run 3 copies of my API"), and Kubernetes figures out <em>how to make it happen</em> -- which machine to put each container on, how to restart failures, how to route traffic.</p>
</div>
<div class="warning-box">
<div class="label">Do You Actually Need Kubernetes?</div>
<p>Kubernetes is powerful but complex. If you're running a side project or a small app with a few containers, <strong>Docker Compose is probably enough</strong>. K8s shines when you have multiple services, need auto-scaling, or are deploying to production across multiple servers. Learn it because it's everywhere in industry, but don't feel pressured to use it for everything.</p>
</div>
</section>
<!-- ============================================================ -->
<!-- SECTION 2: ARCHITECTURE -->
<!-- ============================================================ -->
<section id="architecture">
<h2>2. K8s Architecture</h2>
<p>A Kubernetes <strong>cluster</strong> is a group of machines (physical or virtual) that run your containers together. There are two types of machines in a cluster:</p>
<h3>Control Plane (The Brain)</h3>
<p>This is the management layer. It makes all the decisions -- where to run containers, when to restart them, how to scale. You never run your app on the control plane. It has these components:</p>
<table>
<tr>
<th>Component</th>
<th>What It Does</th>
</tr>
<tr>
<td><strong>API Server</strong></td>
<td>The front door. Every command you run (<code>kubectl</code>) goes through here. It validates and processes all requests.</td>
</tr>
<tr>
<td><strong>etcd</strong></td>
<td>The database. Stores the entire state of the cluster -- what's running, what's desired, configurations. It's a key-value store.</td>
</tr>
<tr>
<td><strong>Scheduler</strong></td>
<td>Decides <em>which node</em> to place a new container on, based on available resources, constraints, and affinity rules.</td>
</tr>
<tr>
<td><strong>Controller Manager</strong></td>
<td>Watches the current state and makes it match the <em>desired</em> state. If you want 3 replicas and only 2 are running, it starts a third.</td>
</tr>
</table>
<h3>Worker Nodes (The Muscle)</h3>
<p>These are the machines that actually <strong>run your containers</strong>. Each node has:</p>
<table>
<tr>
<th>Component</th>
<th>What It Does</th>
</tr>
<tr>
<td><strong>kubelet</strong></td>
<td>Agent on each node. Talks to the control plane, ensures containers are running as expected.</td>
</tr>
<tr>
<td><strong>Container Runtime</strong></td>
<td>Actually runs containers (usually containerd, which is what Docker uses under the hood).</td>
</tr>
<tr>
<td><strong>kube-proxy</strong></td>
<td>Handles networking -- routes traffic to the right containers.</td>
</tr>
</table>
<div class="formula-box">
<strong>How It All Flows:</strong><br><br>
You (kubectl) → API Server → Scheduler picks a node → kubelet on that node → Container Runtime starts container<br><br>
Controller Manager constantly watches: "Are things running as declared?" → If not, fix it.
</div>
</section>
<!-- ============================================================ -->
<!-- SECTION 3: CORE OBJECTS -->
<!-- ============================================================ -->
<section id="core-objects">
<h2>3. Core Objects: Pods, Deployments, Services</h2>
<p>Everything in Kubernetes is an <strong>object</strong> defined in YAML. There are three you absolutely need to understand:</p>
<h3>Pod -- The Smallest Unit</h3>
<p>A Pod is the smallest thing you can deploy in Kubernetes. It's a wrapper around one or more containers that share the same network and storage.</p>
<div class="example-box">
<div class="label">Think of It This Way</div>
<p>A Pod is like a <strong>tiny server</strong> that runs your container. Usually it's one container per Pod. Sometimes you put two containers in the same Pod if they're tightly coupled (like your app + a logging sidecar), but that's less common.</p>
<p>You almost never create Pods directly. Instead, you create a <strong>Deployment</strong> that creates Pods for you.</p>
</div>
<h3>Deployment -- Managing Pods</h3>
<p>A Deployment tells Kubernetes: "I want N copies of this Pod running at all times." It handles:</p>
<ul>
<li><strong>Replication</strong> -- Run 3 copies of my API</li>
<li><strong>Rolling updates</strong> -- Deploy a new version without downtime</li>
<li><strong>Rollback</strong> -- If the new version is broken, go back to the old one</li>
<li><strong>Self-healing</strong> -- If a Pod crashes, start a new one</li>
</ul>
<h3>Service -- Exposing Pods to Traffic</h3>
<p>Pods get random IP addresses that change every time they restart. You can't hard-code those IPs. A <strong>Service</strong> gives your Pods a <strong>stable address</strong> and load-balances traffic across them.</p>
<div class="example-box">
<div class="label">Service Types</div>
<p><strong>ClusterIP</strong> (default) -- Only accessible inside the cluster. Other services can reach it, but the outside world can't. Use this for internal communication (e.g., API talking to database).</p>
<p><strong>NodePort</strong> -- Exposes the service on a port on every node. Useful for development. Traffic to <code>NodeIP:30000</code> gets routed to your Pods.</p>
<p><strong>LoadBalancer</strong> -- Creates a cloud load balancer (on AWS, GCP, etc.) that routes external traffic to your Pods. This is how you expose apps to the internet in production.</p>
</div>
<div class="formula-box">
<strong>The Relationship:</strong><br><br>
Deployment manages → ReplicaSet manages → Pods (your containers)<br>
Service → routes traffic to → Pods (by label selector)
</div>
</section>
<!-- ============================================================ -->
<!-- SECTION 4: YAML -->
<!-- ============================================================ -->
<section id="yaml">
<h2>4. Writing K8s YAML</h2>
<p>Everything in K8s is declared in YAML files. You tell Kubernetes what you want, and it makes it happen. Here are the essential configs:</p>
<h3>Deployment YAML</h3>
<pre><code># deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-api # Name of this deployment
spec:
replicas: 3 # Run 3 copies of this Pod
selector:
matchLabels:
app: my-api # Find Pods with this label
template: # Pod template
metadata:
labels:
app: my-api # Label for this Pod
spec:
containers:
- name: api
image: myapp:1.0 # Docker image to run
ports:
- containerPort: 3000
env:
- name: DATABASE_URL
value: "postgres://user:pass@db-service:5432/mydb"
resources:
requests: # Minimum resources needed
memory: "128Mi"
cpu: "100m" # 100 millicores = 0.1 CPU
limits: # Maximum resources allowed
memory: "256Mi"
cpu: "500m"</code></pre>
<div class="example-box">
<div class="label">Breaking This Down</div>
<p><strong><code>apiVersion</code> + <code>kind</code></strong> -- What type of object this is. Deployments use <code>apps/v1</code>.</p>
<p><strong><code>metadata.name</code></strong> -- The name you give this deployment. Used to reference it in commands.</p>
<p><strong><code>spec.replicas: 3</code></strong> -- "I want 3 Pods running at all times." If one dies, K8s starts a replacement.</p>
<p><strong><code>selector.matchLabels</code></strong> -- How the Deployment finds its Pods. It looks for Pods with <code>app: my-api</code> label.</p>
<p><strong><code>template</code></strong> -- The Pod blueprint. Every Pod created by this Deployment will look like this.</p>
<p><strong><code>resources.requests</code></strong> -- The minimum resources this Pod needs. The scheduler uses this to decide which node has enough room.</p>
<p><strong><code>resources.limits</code></strong> -- The maximum. If the Pod tries to use more, K8s throttles or kills it.</p>
</div>
<h3>Service YAML</h3>
<pre><code># service.yaml
apiVersion: v1
kind: Service
metadata:
name: my-api-service
spec:
type: ClusterIP # Internal only (default)
selector:
app: my-api # Route to Pods with this label
ports:
- port: 80 # Port other services use
targetPort: 3000 # Port on the container</code></pre>
<p>Now other services in the cluster can reach your API at <code>http://my-api-service:80</code>. K8s handles load balancing across all 3 Pods.</p>
<h3>Applying YAML</h3>
<pre><code># Create or update resources from a file
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
# Apply all YAML files in a directory
kubectl apply -f ./k8s/
# Delete resources
kubectl delete -f deployment.yaml</code></pre>
</section>
<!-- ============================================================ -->
<!-- SECTION 5: NETWORKING -->
<!-- ============================================================ -->
<section id="networking">
<h2>5. Networking & Service Discovery</h2>
<p>K8s networking follows simple rules:</p>
<ul>
<li>Every Pod gets its own IP address</li>
<li>Pods can communicate with any other Pod without NAT</li>
<li>Services provide stable DNS names</li>
</ul>
<h3>How Services Find Pods</h3>
<p>Services use <strong>label selectors</strong>. When you create a Service with <code>selector: { app: my-api }</code>, it automatically finds all Pods with that label and routes traffic to them. If a Pod dies and a new one starts with the same label, the Service picks it up automatically.</p>
<h3>DNS Inside the Cluster</h3>
<pre><code># Service "my-api-service" in namespace "default" is accessible at:
my-api-service # Short name (same namespace)
my-api-service.default # With namespace
my-api-service.default.svc.cluster.local # Fully qualified</code></pre>
<div class="formula-box">
<strong>Kubernetes DNS Naming Rule:</strong><br><br>
<code><service-name>.<namespace>.svc.cluster.local</code><br><br>
• Within same namespace: just use <code><service-name></code><br>
• Cross-namespace: use <code><service-name>.<namespace></code><br>
• Full FQDN: <code><service-name>.<namespace>.svc.cluster.local</code>
</div>
<h3>Ingress -- Exposing to the Internet</h3>
<p>An <strong>Ingress</strong> is a set of rules for routing external HTTP traffic to your Services. Think of it as a <strong>reverse proxy</strong> (like Nginx) that K8s manages for you.</p>
<pre><code># ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-ingress
spec:
rules:
- host: api.myapp.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-api-service
port:
number: 80</code></pre>
<div class="tip-box">
<div class="label">The Traffic Flow</div>
<p>Internet → Ingress Controller (Nginx/Traefik) → Service → Pods</p>
<p>The Ingress Controller is the actual reverse proxy. The Ingress YAML just configures its rules.</p>
</div>
</section>
<!-- ============================================================ -->
<!-- SECTION 6: STORAGE -->
<!-- ============================================================ -->
<section id="storage">
<h2>6. Storage & ConfigMaps</h2>
<h3>ConfigMaps -- Non-Secret Configuration</h3>
<p>Store configuration data separately from your container image. This lets you change config without rebuilding.</p>
<pre><code># configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
APP_ENV: "production"
LOG_LEVEL: "info"
MAX_CONNECTIONS: "100"</code></pre>
<h3>Secrets -- Sensitive Data</h3>
<pre><code># secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: db-credentials
type: Opaque
stringData:
DB_PASSWORD: "supersecret123"
API_KEY: "sk-abc123"</code></pre>
<h3>Using ConfigMaps and Secrets in Pods</h3>
<pre><code>spec:
containers:
- name: api
image: myapp:1.0
envFrom: # Load all keys as env vars
- configMapRef:
name: app-config
- secretRef:
name: db-credentials
# OR load individual keys:
env:
- name: DATABASE_PASSWORD
valueFrom:
secretKeyRef:
name: db-credentials
key: DB_PASSWORD</code></pre>
<h3>Persistent Volumes -- Durable Storage</h3>
<p>Just like Docker volumes, Pods need persistent storage for databases. K8s uses <strong>PersistentVolumeClaims (PVC)</strong> to request storage:</p>
<pre><code># pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: db-storage
spec:
accessModes:
- ReadWriteOnce # One node can write at a time
resources:
requests:
storage: 10Gi # 10 GB of storage</code></pre>
<pre><code># Use it in a Pod
spec:
containers:
- name: postgres
image: postgres:16
volumeMounts:
- name: db-data
mountPath: /var/lib/postgresql/data
volumes:
- name: db-data
persistentVolumeClaim:
claimName: db-storage</code></pre>
</section>
<!-- ============================================================ -->
<!-- SECTION 7: SCALING -->
<!-- ============================================================ -->
<section id="scaling">
<h2>7. Scaling & Self-Healing</h2>
<h3>Manual Scaling</h3>
<pre><code># Scale to 5 replicas
kubectl scale deployment my-api --replicas=5
# Scale to 0 (stops all pods, saves resources)
kubectl scale deployment my-api --replicas=0</code></pre>
<h3>Horizontal Pod Autoscaler (HPA)</h3>
<p>Automatically scale based on CPU or memory usage:</p>
<pre><code># hpa.yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: my-api-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: my-api
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70 # Scale up when CPU > 70%</code></pre>
<div class="example-box">
<div class="label">How Auto-Scaling Works</div>
<p>K8s constantly monitors your Pods' CPU usage. If the average goes above 70%, it adds more Pods (up to 10). If usage drops, it removes Pods (down to 2). This means you handle traffic spikes automatically without manual intervention or paying for servers you don't need during quiet hours.</p>
</div>
<h3>Self-Healing</h3>
<p>Kubernetes constantly watches your Pods. If one crashes, it immediately starts a replacement. This happens because the <strong>Controller Manager</strong> compares the <em>desired state</em> (3 replicas) with the <em>actual state</em> (2 running) and reconciles the difference.</p>
<p>You can also define <strong>health checks</strong> so K8s restarts unhealthy containers:</p>
<pre><code>spec:
containers:
- name: api
image: myapp:1.0
livenessProbe: # Is the container alive?
httpGet:
path: /health
port: 3000
initialDelaySeconds: 10
periodSeconds: 15
readinessProbe: # Is it ready to receive traffic?
httpGet:
path: /ready
port: 3000
initialDelaySeconds: 5
periodSeconds: 5</code></pre>
<div class="tip-box">
<div class="label">Liveness vs Readiness</div>
<p><strong>Liveness</strong> -- "Is this container still working?" If it fails, K8s kills and restarts the container. Use this to detect deadlocks or stuck processes.</p>
<p><strong>Readiness</strong> -- "Can this container handle traffic right now?" If it fails, K8s stops sending traffic to it but doesn't restart it. Use this for startup time (e.g., waiting for a database connection).</p>
</div>
</section>
<!-- ============================================================ -->
<!-- SECTION 8: KUBECTL -->
<!-- ============================================================ -->
<section id="kubectl">
<h2>8. Essential kubectl Commands</h2>
<pre><code># ===== VIEWING RESOURCES =====
kubectl get pods # List all pods
kubectl get pods -o wide # With more detail (node, IP)
kubectl get deployments # List deployments
kubectl get services # List services
kubectl get all # List everything
# ===== DETAILED INFO =====
kubectl describe pod my-pod # Detailed pod info + events
kubectl describe deployment my-api # Deployment details
# ===== LOGS =====
kubectl logs my-pod # View pod logs
kubectl logs -f my-pod # Follow logs (real-time)
kubectl logs my-pod -c my-container # Specific container in pod
# ===== DEBUGGING =====
kubectl exec -it my-pod -- bash # Shell into a pod
kubectl port-forward my-pod 3000:3000 # Forward local port to pod
# ===== APPLYING & DELETING =====
kubectl apply -f deployment.yaml # Create/update from file
kubectl delete -f deployment.yaml # Delete from file
kubectl delete pod my-pod # Delete a specific pod
# ===== SCALING =====
kubectl scale deployment my-api --replicas=5
# ===== ROLLOUTS =====
kubectl rollout status deployment my-api # Watch deployment progress
kubectl rollout history deployment my-api # See revision history
kubectl rollout undo deployment my-api # Rollback to previous version
# ===== CONTEXT =====
kubectl config get-contexts # See available clusters
kubectl config use-context my-cluster # Switch clusters
kubectl get namespaces # List namespaces
kubectl -n production get pods # Pods in a specific namespace</code></pre>
</section>
<!-- ============================================================ -->
<!-- SECTION 9: LOCAL DEV -->
<!-- ============================================================ -->
<section id="local-dev">
<h2>9. Running K8s Locally (Minikube)</h2>
<p>You don't need a cloud account to learn K8s. <strong>Minikube</strong> runs a single-node K8s cluster on your laptop.</p>
<pre><code># Install Minikube (on Linux with curl)
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube
# Start a cluster
minikube start
# Check it's running
kubectl get nodes
# Build images inside Minikube (so it can find them)
eval $(minikube docker-env)
docker build -t myapp:1.0 .
# Access a service
minikube service my-api-service
# Dashboard (web UI)
minikube dashboard
# Stop the cluster
minikube stop
# Delete the cluster
minikube delete</code></pre>
<div class="tip-box">
<div class="label">Other Local Options</div>
<p><strong>kind</strong> (Kubernetes IN Docker) -- Runs K8s nodes as Docker containers. Lighter than Minikube.</p>
<p><strong>k3s</strong> -- Lightweight K8s distribution. Great for Raspberry Pi or resource-constrained environments.</p>
<p><strong>Docker Desktop</strong> -- Has a built-in K8s cluster you can enable in settings.</p>
</div>
</section>
<!-- ============================================================ -->
<!-- SECTION 10: VS COMPOSE -->
<!-- ============================================================ -->
<section id="vs-compose">
<h2>10. Kubernetes vs Docker Compose</h2>
<table>
<tr>
<th>Feature</th>
<th>Docker Compose</th>
<th>Kubernetes</th>
</tr>
<tr>
<td>Purpose</td>
<td>Local development, simple deployments</td>
<td>Production orchestration at scale</td>
</tr>
<tr>
<td>Complexity</td>
<td>Simple -- one YAML file</td>
<td>Complex -- multiple YAML files, many concepts</td>
</tr>
<tr>
<td>Scaling</td>
<td>Manual (<code>--scale</code>)</td>
<td>Automatic (HPA)</td>
</tr>
<tr>
<td>Self-healing</td>
<td>Basic (<code>restart: always</code>)</td>
<td>Advanced (health checks, auto-replacement)</td>
</tr>
<tr>
<td>Multi-machine</td>
<td>Single machine only</td>
<td>Distributed across many machines</td>
</tr>
<tr>
<td>Rolling updates</td>
<td>Recreate only</td>
<td>Zero-downtime rolling updates</td>
</tr>
<tr>
<td>Load balancing</td>
<td>None built-in</td>
<td>Built-in across Pods</td>
</tr>
<tr>
<td>When to use</td>
<td>Dev environments, small apps</td>
<td>Production, microservices, scale</td>
</tr>
</table>
<div class="warning-box">
<div class="label">The Practical Advice</div>
<p>Use <strong>Docker Compose for development</strong> -- it's simpler and faster to iterate. Use <strong>Kubernetes for production</strong> when you need scaling, reliability, and zero-downtime deployments. Many teams use both: Compose locally, K8s in the cloud.</p>
</div>
</section>
</div>
<footer>
<p>Better Dev -- built for self-learners. Keep going, you've got this.</p>
<p style="margin-top: 1rem; font-size: 0.85rem;"><a href="https://github.com/pythonwithsean/BetterDev/blob/main/kubernetes.html" target="_blank" rel="noopener noreferrer" class="edit-link">Edit this page on GitHub <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="vertical-align: middle; margin-left: 2px;"><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"></path><polyline points="15 3 21 3 21 9"></polyline><line x1="10" y1="14" x2="21" y2="3"></line></svg></a></p>
</footer>
<script>
(function(){
var sidebar=document.querySelector('.sidebar');
var backdrop=document.querySelector('.sidebar-backdrop');
var toggleBtn=document.querySelector('.sidebar-toggle');
var closeBtn=document.querySelector('.sidebar-close');
function openSidebar(){
sidebar.classList.add('open');
backdrop.classList.add('visible');
document.body.classList.add('sidebar-open');
toggleBtn.setAttribute('aria-expanded','true');
var si=sidebar.querySelector('.sidebar-search-input');
if(si)si.focus();
}
function closeSidebar(){
sidebar.classList.remove('open');
backdrop.classList.remove('visible');
document.body.classList.remove('sidebar-open');
toggleBtn.setAttribute('aria-expanded','false');
}
if(toggleBtn)toggleBtn.addEventListener('click',function(){
sidebar.classList.contains('open')?closeSidebar():openSidebar();
});
if(closeBtn)closeBtn.addEventListener('click',closeSidebar);
if(backdrop)backdrop.addEventListener('click',closeSidebar);
document.addEventListener('keydown',function(e){
if(e.key==='Escape'&&sidebar.classList.contains('open'))closeSidebar();
});
// Active page detection
var currentPage=window.location.pathname.split('/').pop()||'index.html';
var navLinks=document.querySelectorAll('.sidebar-nav a');
for(var i=0;i<navLinks.length;i++){
if(navLinks[i].getAttribute('href')===currentPage)navLinks[i].classList.add('active');
}
// Search
var input=document.querySelector('.sidebar-search-input');
var box=document.querySelector('.sidebar-search-results');
if(!input||!box)return;
var links=[].slice.call(document.querySelectorAll('.sidebar-nav a'));
var topics=links.map(function(a){return{text:a.textContent.trim(),href:a.getAttribute('href')};});
var idx=-1;
function render(q){
if(!q){box.classList.remove('visible');box.innerHTML='';idx=-1;return;}
var lc=q.toLowerCase();
var matches=topics.filter(function(t){return t.text.toLowerCase().indexOf(lc)!==-1;});
if(matches.length===0){box.innerHTML='<div class="no-results">No topics found</div>';box.classList.add('visible');idx=-1;return;}
box.innerHTML=matches.map(function(m){return'<a href="'+m.href+'">'+m.text+'</a>';}).join('');
box.classList.add('visible');
idx=-1;
}
function highlight(items){
for(var i=0;i<items.length;i++){items[i].classList.toggle('highlighted',i===idx);}
}
input.addEventListener('input',function(){render(this.value);});
input.addEventListener('keydown',function(e){
var items=box.querySelectorAll('a');
if(!items.length)return;
if(e.key==='ArrowDown'){e.preventDefault();idx=Math.min(idx+1,items.length-1);highlight(items);items[idx].scrollIntoView({block:'nearest'});}
else if(e.key==='ArrowUp'){e.preventDefault();idx=Math.max(idx-1,0);highlight(items);items[idx].scrollIntoView({block:'nearest'});}
else if(e.key==='Enter'&&idx>=0){e.preventDefault();items[idx].click();}
});
input.addEventListener('blur',function(){setTimeout(function(){box.classList.remove('visible');idx=-1;},200);});
input.addEventListener('focus',function(){if(this.value)render(this.value);});
})();
</script>
</body>
</html>