Master the core concepts of Kubernetes container orchestration - architecture, components, and how they work together to manage containerized applications at scale.
Kubernetes (K8s) has become the industry standard for container orchestration, enabling organizations to deploy, scale, and manage containerized applications at scale. Originally developed by Google and now maintained by the Cloud Native Computing Foundation (CNCF), Kubernetes provides a robust platform for automating deployment, scaling, and operations of application containers across clusters of hosts.
In this lesson, you'll learn the fundamental concepts of Kubernetes, understand its architecture, and explore how its components work together to provide a resilient, scalable platform for running containerized workloads.
Kubernetes is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications.
Key Capabilities:
Why Kubernetes?
Kubernetes follows a master-worker architecture with a Control Plane that manages the cluster and Worker Nodes that run the workloads.
Control Plane Components:
Worker Node Components:
Add-ons:
Kubernetes uses objects to represent the state of your cluster. Understanding these fundamental objects is key to working with Kubernetes.
Pod
ReplicaSet
Deployment
Service
Namespace
ConfigMap & Secret
Kubernetes supports two approaches to managing resources: declarative and imperative. Understanding when to use each is important.
Imperative Approach
Declarative Approach
Benefits of Declarative:
When to Use Imperative:
Labels, selectors, and annotations are key mechanisms for organizing and selecting Kubernetes resources.
Labels
Selectors
Annotations
Best Practices:
The Kubernetes Scheduler is responsible for assigning Pods to Nodes. Understanding scheduling helps optimize resource utilization and application performance.
Scheduling Process:
Node Selection Factors:
Scheduling Constraints:
Let's explore a Kubernetes cluster to understand its components and how they work together.
1#!/bin/bash
2# Kubernetes Cluster Exploration
3# ==============================
4
5# Prerequisites: kubectl configured to connect to a cluster
6# You can use minikube, kind, or any K8s cluster
7
8# 1. Check cluster info
9echo "=== Cluster Information ==="
10kubectl cluster-info
11kubectl version --short
12
13# 2. View nodes in the cluster
14echo -e "
15=== Cluster Nodes ==="
16kubectl get nodes -o wide
17
18# 3. Describe a node (detailed info)
19echo -e "
20=== Node Details ==="
21kubectl describe node $(kubectl get nodes -o jsonpath='{.items[0].metadata.name}') | head -50
22
23# 4. View system namespaces
24echo -e "
25=== Namespaces ==="
26kubectl get namespaces
27
28# 5. View system pods (control plane components)
29echo -e "
30=== Control Plane Pods ==="
31kubectl get pods -n kube-system
32
33# 6. Check API resources available
34echo -e "
35=== API Resources ==="
36kubectl api-resources | head -20
37
38# 7. Create a test deployment
39echo -e "
40=== Creating Test Deployment ==="
41cat <<EOF | kubectl apply -f -
42apiVersion: apps/v1
43kind: Deployment
44metadata:
45 name: hello-kubernetes
46 labels:
47 app: hello-kubernetes
48spec:
49 replicas: 2
50 selector:
51 matchLabels:
52 app: hello-kubernetes
53 template:
54 metadata:
55 labels:
56 app: hello-kubernetes
57 spec:
58 containers:
59 - name: hello-kubernetes
60 image: paulbouwer/hello-kubernetes:1.10
61 ports:
62 - containerPort: 8080
63 resources:
64 requests:
65 memory: "64Mi"
66 cpu: "100m"
67 limits:
68 memory: "128Mi"
69 cpu: "200m"
70EOF
71
72# 8. Watch pods come up
73echo -e "
74=== Watching Pods ==="
75kubectl get pods -w --timeout=60s &
76sleep 10
77kill %1 2>/dev/null
78
79# 9. Check pod details
80echo -e "
81=== Pod Details ==="
82kubectl get pods -l app=hello-kubernetes -o wide
83
84# 10. View deployment
85echo -e "
86=== Deployment Status ==="
87kubectl get deployment hello-kubernetes
88kubectl rollout status deployment/hello-kubernetes
89
90# 11. Describe the deployment
91echo -e "
92=== Deployment Description ==="
93kubectl describe deployment hello-kubernetes | head -40
94
95# 12. Create a service
96echo -e "
97=== Creating Service ==="
98cat <<EOF | kubectl apply -f -
99apiVersion: v1
100kind: Service
101metadata:
102 name: hello-kubernetes-svc
103spec:
104 selector:
105 app: hello-kubernetes
106 ports:
107 - port: 80
108 targetPort: 8080
109 type: ClusterIP
110EOF
111
112# 13. View the service
113echo -e "
114=== Service Details ==="
115kubectl get service hello-kubernetes-svc
116kubectl describe service hello-kubernetes-svc
117
118# 14. View endpoints (Pods backing the service)
119echo -e "
120=== Service Endpoints ==="
121kubectl get endpoints hello-kubernetes-svc
122
123# 15. View events
124echo -e "
125=== Recent Events ==="
126kubectl get events --sort-by='.lastTimestamp' | tail -20
127
128# 16. Check logs from a pod
129echo -e "
130=== Pod Logs ==="
131POD_NAME=$(kubectl get pods -l app=hello-kubernetes -o jsonpath='{.items[0].metadata.name}')
132kubectl logs $POD_NAME --tail=10
133
134# 17. Execute command in pod
135echo -e "
136=== Execute in Pod ==="
137kubectl exec $POD_NAME -- hostname
138kubectl exec $POD_NAME -- cat /etc/os-release | head -5
139
140# 18. Port forward to access service locally
141echo -e "
142=== Port Forward (run in background) ==="
143echo "kubectl port-forward svc/hello-kubernetes-svc 8080:80"
144
145# 19. Scale the deployment
146echo -e "
147=== Scaling Deployment ==="
148kubectl scale deployment hello-kubernetes --replicas=3
149kubectl get pods -l app=hello-kubernetes
150
151# 20. Cleanup
152echo -e "
153=== Cleanup ==="
154read -p "Delete resources? (y/n) " -n 1 -r
155echo
156if [[ $REPLY =~ ^[Yy]$ ]]; then
157 kubectl delete deployment hello-kubernetes
158 kubectl delete service hello-kubernetes-svc
159 echo "Resources deleted"
160fi
161
162echo -e "
163=== Exploration Complete ==="Your progress is saved in this browser, on this device only.