Cloud Notes
Master Kubernetes Deployments for application lifecycle management and Services for networking, covering rolling updates, scaling, and service types.
Deployments and Services are the two most important Kubernetes resources. Deployments manage how your application runs (replicas, updates, rollbacks), while Services manage how your application is accessed (networking, load balancing, discovery).
Deployments
Deployment Architecture
Complete Deployment YAML
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app
namespace: production
labels:
app: web-app
version: v2.0
spec:
replicas: 5
selector:
matchLabels:
app: web-app
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 2 # Max pods above desired during update
maxUnavailable: 1 # Max pods unavailable during update
template:
metadata:
labels:
app: web-app
version: v2.0
spec:
containers:
- name: web
image: myregistry/web-app:v2.0
ports:
- containerPort: 8080
env:
- name: DATABASE_URL
valueFrom:
secretKeyRef:
name: db-credentials
key: url
resources:
requests:
memory: "256Mi"
cpu: "250m"
limits:
memory: "512Mi"
cpu: "500m"
readinessProbe:
httpGet:
path: /ready
port: 8080
initialDelaySeconds: 5
periodSeconds: 10
livenessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 15
periodSeconds: 20
imagePullSecrets:
- name: registry-credentialsDeployment Operations
# Create deployment
kubectl apply -f deployment.yaml
# Check status
kubectl rollout status deployment/web-app
# Scale
kubectl scale deployment web-app --replicas=10
# Update image (triggers rolling update)
kubectl set image deployment/web-app web=myregistry/web-app:v3.0
# View rollout history
kubectl rollout history deployment/web-app
# Rollback to previous version
kubectl rollout undo deployment/web-app
# Rollback to specific revision
kubectl rollout undo deployment/web-app --to-revision=2
# Pause/Resume rollout
kubectl rollout pause deployment/web-app
kubectl rollout resume deployment/web-appServices
Service Types
| Service IP: 10.96.0.1:80 | ||
|---|---|---|
| Only accessible within the cluster | ||
| <NodeIP>:30080 → Service → Pods | ||
| Range: 30000-32767 | ||
| External LB IP → NodePort → Service → Pods | ||
| Creates cloud load balancer automatically | ||
| Maps service name to external DNS |
Service YAML Examples
# ClusterIP (default - internal communication)
apiVersion: v1
kind: Service
metadata:
name: api-service
spec:
type: ClusterIP
selector:
app: api
ports:
- port: 80
targetPort: 3000
# Ingress (HTTP routing)
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: app-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: app.example.com
http:
paths:
- path: /api
pathType: Prefix
backend:
service:
name: api-service
port:
number: 80
- path: /
pathType: Prefix
backend:
service:
name: web-service
port:
number: 80Rolling Update Process
| Step 1 | Current State (v1.0, 3 replicas) |
| Step 2 | Create new pod (maxSurge=1) |
| Step 3 | Terminate old pod (maxUnavailable=1) |
| Step 4 | Continue rolling |
| Step 5 | Complete |
Interview Questions
- What is a Kubernetes Deployment and why use it over bare Pods?
A Deployment manages a set of identical pods, providing declarative updates, rolling rollouts, automatic rollbacks, and scaling. Bare pods don't self-heal (if they crash, they're gone). Deployments ensure the desired number of healthy pods always runs.
- Explain the four Kubernetes Service types.
ClusterIP (default, internal-only), NodePort (exposes on each node's IP at a static port), LoadBalancer (provisions external cloud load balancer), ExternalName (DNS CNAME alias to external service). Each builds on the previous.
- How does a rolling update work in Kubernetes?
K8s creates new pods with the updated image while gradually terminating old pods. maxSurge controls how many extra pods can exist during update; maxUnavailable controls how many can be down. Readiness probes ensure new pods are healthy before old ones are removed.
- What is the difference between readiness and liveness probes?
Liveness probe: checks if the container is alive (restart if failing). Readiness probe: checks if the container is ready to serve traffic (remove from service endpoints if failing). A pod can be alive but not ready (warming up, loading data).
- How do Services discover and route to the correct Pods?
Services use label selectors to identify target pods. kube-proxy maintains network rules routing service ClusterIP traffic to pod IPs. When pods are created/destroyed, endpoints are updated automatically. Pods find services via DNS (service-name.namespace.svc.cluster.local).
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Kubernetes Deployments and Services.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Cloud Computing topic.
Search Terms
cloud-computing, cloud computing, cloud, computing, containers, and, orchestration, deployments
Related Cloud Computing Topics