Chapter 1: Introduction to Kubernetes Basics
Chapter 1: Introduction to Kubernetes Basics
- Understand the origin and core value of Kubernetes
- Master the overall architecture and core components of Kubernetes
- Complete local Kubernetes environment setup
- Learn to use kubectl for basic operations
What is Kubernetes
The Origin of Kubernetes
Kubernetes (abbreviated as K8s) originated from Google’s internal container orchestration system Borg. It was open-sourced in 2014 and is now maintained by the CNCF (Cloud Native Computing Foundation). The name comes from Greek, meaning “helmsman” or “pilot”.
Why Kubernetes is Needed
When the number of containers grows from a few to hundreds or thousands, manual management becomes impossible. Kubernetes solves the following core problems:
The core value of Kubernetes lies in declarative configuration and automated operations. You simply tell K8s what state you want, and it automatically ensures that state is achieved and maintained.
Kubernetes Architecture
Overall Architecture
Kubernetes adopts a Master-Worker architecture, consisting of a Control Plane and Data Plane:
Control Plane Components
| Component | Role | Description |
|---|---|---|
| API Server | Cluster Gateway | Provides REST API, hub for all component communication |
| etcd | State Storage | Distributed key-value store, saves all cluster state data |
| Scheduler | Pod Scheduling | Decides which node a Pod runs on |
| Controller Manager | Controller Management | Runs various controllers, ensures desired state |
| Cloud Controller | Cloud Platform Integration | Interacts with cloud provider APIs |
Worker Node Components
Core Concepts Overview
Environment Setup
Local Development Environment Options
Minikube is the officially recommended local Kubernetes environment, suitable for learning and development.
# macOS installation
brew install minikube
# Linux installation
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube
# Windows installation (PowerShell admin mode)
choco install minikube
# Start cluster
minikube start
# Start with Docker driver (recommended)
minikube start --driver=docker
# Check status
minikube status
# Stop cluster
minikube stop
# Delete cluster
minikube deleteKind (Kubernetes in Docker) uses Docker containers as nodes, with fast startup speed.
# Install Kind
# macOS
brew install kind
# Linux
curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.20.0/kind-linux-amd64
chmod +x ./kind
sudo mv ./kind /usr/local/bin/kind
# Create cluster
kind create cluster
# Create multi-node cluster
cat <<EOF | kind create cluster --config=-
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
- role: worker
EOF
# List clusters
kind get clusters
# Delete cluster
kind delete clusterDocker Desktop has built-in Kubernetes support, enable with one click.
# Enable steps:
# 1. Open Docker Desktop
# 2. Go to Settings -> Kubernetes
# 3. Check "Enable Kubernetes"
# 4. Click "Apply & Restart"
# Verify installation
kubectl cluster-info
kubectl get nodesK3s is a lightweight Kubernetes distribution with low resource consumption.
# Install K3s (single node)
curl -sfL https://get.k3s.io | sh -
# Check status
sudo systemctl status k3s
# Configure kubectl
mkdir -p ~/.kube
sudo cp /etc/rancher/k3s/k3s.yaml ~/.kube/config
sudo chown $(id -u):$(id -g) ~/.kube/config
# Verify
kubectl get nodesInstall kubectl
kubectl is Kubernetes’ command-line tool for interacting with clusters:
# macOS
brew install kubectl
# Linux
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x kubectl
sudo mv kubectl /usr/local/bin/
# Windows
choco install kubernetes-cli
# Verify installation
kubectl version --client
# Configure command completion (bash)
echo 'source <(kubectl completion bash)' >> ~/.bashrc
source ~/.bashrc
# Configure command completion (zsh)
echo 'source <(kubectl completion zsh)' >> ~/.zshrc
source ~/.zshrc
# Set alias
echo 'alias k=kubectl' >> ~/.bashrc
echo 'complete -o default -F __start_kubectl k' >> ~/.bashrc
Verify Environment
# View cluster info
kubectl cluster-info
# View node status
kubectl get nodes
# View all namespaces
kubectl get namespaces
# View system component status
kubectl get pods -n kube-system
kubectl Basic Operations
Command Structure
The basic format of kubectl commands:
kubectl [command] [TYPE] [NAME] [flags]
Common Commands Quick Reference
# View all Pods
kubectl get pods
# View Pods in all namespaces
kubectl get pods -A
kubectl get pods --all-namespaces
# Detailed output
kubectl get pods -o wide
# YAML format output
kubectl get pod nginx -o yaml
# JSON format output
kubectl get pod nginx -o json
# Custom column output
kubectl get pods -o custom-columns=NAME:.metadata.name,STATUS:.status.phase
# Continuous watch
kubectl get pods -w
# Filter by label
kubectl get pods -l app=nginx
# View multiple resource types
kubectl get pods,services,deployments
# Create from YAML file
kubectl create -f pod.yaml
# Declarative create/update
kubectl apply -f deployment.yaml
# Create from directory
kubectl apply -f ./manifests/
# Create from URL
kubectl apply -f https://example.com/nginx.yaml
# Quick create Deployment
kubectl create deployment nginx --image=nginx
# Quick create Service
kubectl expose deployment nginx --port=80 --type=NodePort
# Create namespace
kubectl create namespace dev
# Edit resource (opens editor)
kubectl edit deployment nginx
# Patch update
kubectl patch deployment nginx -p '{"spec":{"replicas":3}}'
# Set image
kubectl set image deployment/nginx nginx=nginx:1.20
# Scale
kubectl scale deployment nginx --replicas=5
# Add label
kubectl label pod nginx env=prod
# Add annotation
kubectl annotate pod nginx description="web server"
# Delete single resource
kubectl delete pod nginx
# Delete from file
kubectl delete -f pod.yaml
# Delete resources with specified label
kubectl delete pods -l app=nginx
# Delete namespace and all its resources
kubectl delete namespace dev
# Force delete
kubectl delete pod nginx --force --grace-period=0
# Delete all Pods
kubectl delete pods --all
# View detailed information
kubectl describe pod nginx
# View logs
kubectl logs nginx
# Follow logs in real-time
kubectl logs -f nginx
# View previous container's logs
kubectl logs nginx --previous
# Specify container in multi-container Pod
kubectl logs nginx -c sidecar
# Enter container
kubectl exec -it nginx -- /bin/bash
# Execute single command
kubectl exec nginx -- ls /usr/share/nginx/html
# Port forward
kubectl port-forward pod/nginx 8080:80
# Copy files
kubectl cp nginx:/etc/nginx/nginx.conf ./nginx.conf
Resource Type Quick Reference
# View all resource types
kubectl api-resources
# Common resource type abbreviations
# po = pods
# svc = services
# deploy = deployments
# rs = replicasets
# sts = statefulsets
# ds = daemonsets
# ns = namespaces
# cm = configmaps
# pv = persistentvolumes
# pvc = persistentvolumeclaims
# ing = ingresses
# View resource API versions
kubectl api-versions
# View resource field descriptions
kubectl explain pod
kubectl explain pod.spec
kubectl explain pod.spec.containers
Hands-on Practice
Your First Pod
Create a simple Nginx Pod:
# nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
env: demo
spec:
containers:
- name: nginx
image: nginx:1.24
ports:
- containerPort: 80
resources:
requests:
memory: "64Mi"
cpu: "100m"
limits:
memory: "128Mi"
cpu: "200m"
# Create Pod
kubectl apply -f nginx-pod.yaml
# View Pod status
kubectl get pod nginx-pod
# View detailed information
kubectl describe pod nginx-pod
# View Pod logs
kubectl logs nginx-pod
# Enter Pod
kubectl exec -it nginx-pod -- /bin/bash
# View processes inside Pod
ps aux
# Exit Pod
exit
# Port forward to test access
kubectl port-forward pod/nginx-pod 8080:80
# Test in another terminal
curl http://localhost:8080
# Clean up resources
kubectl delete pod nginx-pod
Your First Deployment
Create an Nginx Deployment:
# nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.24
ports:
- containerPort: 80
resources:
requests:
memory: "64Mi"
cpu: "100m"
limits:
memory: "128Mi"
cpu: "200m"
# Create Deployment
kubectl apply -f nginx-deployment.yaml
# View Deployment status
kubectl get deployment nginx-deployment
# View ReplicaSet
kubectl get rs
# View Pods (will have 3)
kubectl get pods -l app=nginx
# Scale to 5 replicas
kubectl scale deployment nginx-deployment --replicas=5
# View scaling results
kubectl get pods -l app=nginx
# Update image version
kubectl set image deployment/nginx-deployment nginx=nginx:1.25
# View rolling update status
kubectl rollout status deployment/nginx-deployment
# View update history
kubectl rollout history deployment/nginx-deployment
# Rollback to previous version
kubectl rollout undo deployment/nginx-deployment
# Clean up
kubectl delete deployment nginx-deployment
Create Service to Expose Application
# nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: NodePort
selector:
app: nginx
ports:
- port: 80
targetPort: 80
nodePort: 30080
# First create Deployment
kubectl apply -f nginx-deployment.yaml
# Create Service
kubectl apply -f nginx-service.yaml
# View Service
kubectl get svc nginx-service
# View Endpoints
kubectl get endpoints nginx-service
# Test access (Minikube)
minikube service nginx-service --url
# Or access using NodePort
curl http://<node-ip>:30080
# Clean up
kubectl delete -f nginx-deployment.yaml
kubectl delete -f nginx-service.yaml
Common Issues and Solutions
Pod Status Anomalies
Troubleshooting Commands
# View Pod details and events
kubectl describe pod <pod-name>
# View Pod logs
kubectl logs <pod-name>
# View previous container's logs (after container restart)
kubectl logs <pod-name> --previous
# Enter container for debugging
kubectl exec -it <pod-name> -- /bin/sh
# View node status
kubectl describe node <node-name>
# View resource usage
kubectl top nodes
kubectl top pods
# View cluster events
kubectl get events --sort-by='.lastTimestamp'
# Check network connectivity
kubectl run tmp-shell --rm -i --tty --image nicolaka/netshoot -- /bin/bash
- CrashLoopBackOff: Container repeatedly crashes, check application logs
- ImagePullBackOff: Cannot pull image, check image name and repository authentication
- Pending: Pod cannot be scheduled, check resource quotas and node selectors
- OOMKilled: Killed due to memory limit exceeded, increase memory limit
Summary
Through this chapter, you should have mastered:
- K8s Origin: Understood Kubernetes’ development history and core value
- Architecture Design: Learned about the component responsibilities of the control plane and data plane
- Environment Setup: Completed local Kubernetes environment installation and configuration
- kubectl Operations: Mastered common kubectl commands and resource management
- Hands-on Experience: Deepened understanding through hands-on practice with Pods, Deployments, and Services
In the next chapter, we will dive deep into core Pod concepts, including Pod design philosophy, lifecycle management, multi-container patterns, and resource configuration.