Docker Kubernetes Introduction
An introduction to Kubernetes as a container orchestration platform.
Basic Kubernetes Concepts
Pod
# A Pod is the smallest unit in Kubernetes
apiVersion: v1
kind: Pod
metadata:
name: api-pod
spec:
containers:
- name: api
image: myapp:1.0
ports:
- containerPort: 3000
Deployment
# A Deployment manages replicaset
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-deployment
spec:
replicas: 3
selector:
matchLabels:
app: api
template:
metadata:
labels:
app: api
spec:
containers:
- name: api
image: myapp:1.0
ports:
- containerPort: 3000
Service
# A Service provides access to pods
apiVersion: v1
kind: Service
metadata:
name: api-service
spec:
selector:
app: api
ports:
- port: 80
targetPort: 3000
type: LoadBalancer
Use Case: Application on K8s
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-api
spec:
replicas: 3
selector:
matchLabels:
app: myapp-api
template:
metadata:
labels:
app: myapp-api
spec:
containers:
- name: api
image: myapp-api:1.0
ports:
- containerPort: 3000
env:
- name: DB_HOST
value: "database-service"
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: db-secret
key: password
# Deploy to Kubernetes
kubectl apply -f deployment.yaml
# View pods
kubectl get pods
# View deployments
kubectl get deployments
# View services
kubectl get services
Docker Swarm vs Kubernetes
| Feature | Docker Swarm | Kubernetes |
|---|---|---|
| Complexity | Simple | Complex |
| Scaling | Easy | More flexible |
| Ecosystem | Limited | Very extensive |
| Learning curve | Low | High |
Everyday Use Case
Case: A developer needs to deploy an application to Kubernetes.
# Build the image
docker build -t myapp-api:1.0 .
# Push to the registry
docker push registry.myapp.local/myapp-api:1.0
# Deploy to K8s
kubectl apply -f k8s/
# Check the status
kubectl get pods
kubectl get services
Best Practices
- Use Helm charts: For packaging
- ConfigMaps and Secrets: For configuration
- Resource limits: Set CPU and memory
- Health checks: Liveness and readiness probes
- Rolling updates: For zero-downtime deployments
Conclusion
Kubernetes is a powerful orchestration platform. For complex applications, Kubernetes provides greater flexibility and scalability.