Kubernetes Basics for Developers: Getting Started Guide
Learn Kubernetes fundamentals. Pods, deployments, services, and how to deploy containerized applications at scale.
What is Kubernetes?
Kubernetes (K8s) is an open-source container orchestration platform that automates deployment, scaling, and management of containerized applications.
Why Use Kubernetes?
- Automatic Scaling: Scale apps up/down based on demand
- Self-Healing: Restarts failed containers automatically
- Load Balancing: Distributes traffic across containers
- Rolling Updates: Zero-downtime deployments
- Service Discovery: Automatic DNS for services
Core Concepts
1. Pods
The smallest deployable unit in Kubernetes. A pod contains one or more containers.
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
2. Deployments
Manages replica sets and rolling updates.
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.21
ports:
- containerPort: 80
3. Services
Exposes pods to network traffic.
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
Essential kubectl Commands
# Get cluster info
kubectl cluster-info
# List pods
kubectl get pods
# Create deployment
kubectl create deployment nginx --image=nginx
# Scale deployment
kubectl scale deployment nginx --replicas=5
# Expose service
kubectl expose deployment nginx --port=80 --type=LoadBalancer
# View logs
kubectl logs pod-name
# Execute command in pod
kubectl exec -it pod-name -- /bin/bash
# Delete resources
kubectl delete deployment nginx
Kubernetes Architecture
- Control Plane: Manages cluster (API server, scheduler, controller manager)
- Nodes: Worker machines running pods
- kubelet: Agent running on each node
- etcd: Distributed key-value store for cluster data
Getting Started
1. Install kubectl
# 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/
2. Local Development
- Minikube: Local Kubernetes cluster
- Docker Desktop: Built-in Kubernetes
- kind: Kubernetes in Docker
Best Practices
- ✅ Use namespaces to organize resources
- ✅ Set resource limits (CPU, memory)
- ✅ Use liveness and readiness probes
- ✅ Store secrets in Kubernetes Secrets
- ✅ Use ConfigMaps for configuration
- ✅ Enable monitoring (Prometheus, Grafana)