Kubernetes Basics¶
Overview¶
This guide introduces the fundamental concepts of Kubernetes, its architecture, and basic components for container orchestration.
Prerequisites¶
- Basic understanding of containers and Docker
- Familiarity with command-line operations
- Basic knowledge of YAML
- Understanding of distributed systems concepts
Learning Objectives¶
- Understand Kubernetes architecture
- Learn core Kubernetes concepts
- Master basic kubectl commands
- Implement basic deployments
- Understand pod lifecycle
Table of Contents¶
Core Concepts¶
What is Kubernetes?¶
Kubernetes is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications.
Key Features¶
- Container Orchestration
- Self-healing
- Automatic scaling
- Load balancing
- Rolling updates
- Service discovery
- Configuration management
Architecture¶
Control Plane Components¶
- kube-apiserver: API server that exposes the Kubernetes API
- etcd: Consistent and highly-available key-value store
- kube-scheduler: Assigns nodes to newly created pods
- kube-controller-manager: Runs controller processes
- cloud-controller-manager: Integrates with cloud provider APIs
Node Components¶
- kubelet: Ensures containers are running in a pod
- kube-proxy: Maintains network rules on nodes
- Container Runtime: Software responsible for running containers
Basic Components¶
Pods¶
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
ReplicaSets¶
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx-replicaset
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
Deployments¶
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
kubectl Commands¶
Basic Commands¶
# Get information about cluster components
kubectl get nodes
kubectl get pods
kubectl get services
# Create resources
kubectl create -f deployment.yaml
# Apply configuration
kubectl apply -f config.yaml
# Delete resources
kubectl delete pod nginx-pod
Common Operations¶
# Port forwarding
kubectl port-forward pod/nginx-pod 8080:80
# Get pod logs
kubectl logs nginx-pod
# Execute command in pod
kubectl exec -it nginx-pod -- /bin/bash
# Scale deployment
kubectl scale deployment nginx-deployment --replicas=5
Pod Management¶
Pod Lifecycle¶
- Pending: Pod accepted but containers not running
- Running: Pod bound to node, all containers running
- Succeeded: All containers terminated successfully
- Failed: All containers terminated, at least one failed
- Unknown: Pod state cannot be obtained
Health Checks¶
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.14.2
livenessProbe:
httpGet:
path: /health
port: 80
initialDelaySeconds: 3
periodSeconds: 3
readinessProbe:
httpGet:
path: /ready
port: 80
initialDelaySeconds: 5
periodSeconds: 5
Best Practices¶
- Use declarative configuration
- Implement proper labels and selectors
- Set resource requests and limits
- Use namespaces for organization
- Implement proper health checks
- Use configuration management
- Follow security best practices
Common Pitfalls¶
- Not setting resource limits
- Poor label management
- Missing health checks
- Inadequate monitoring
- Poor security practices
- Insufficient logging
Implementation Examples¶
Complete Pod Configuration¶
apiVersion: v1
kind: Pod
metadata:
name: web-app
labels:
app: web
environment: production
spec:
containers:
- name: web-app
image: nginx:1.14.2
ports:
- containerPort: 80
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
livenessProbe:
httpGet:
path: /health
port: 80
initialDelaySeconds: 3
periodSeconds: 3
readinessProbe:
httpGet:
path: /ready
port: 80
initialDelaySeconds: 5
periodSeconds: 5
env:
- name: ENVIRONMENT
value: "production"
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: app-config
Resources for Further Learning¶
Practice Exercises¶
- Create a basic pod configuration
- Deploy a multi-container pod
- Implement health checks
- Scale a deployment
- Configure pod resources