Deployment / ReplicaSet
Useful Links
Architecture
Detailed Description
Kubernetes Deployments
areserve designedas toa manageblueprint statelessfor servicesrunning your application in a cluster. Building on ReplicaSets
, they ensure your clusterapplication andremains are designed to definein the desired state by maintaining the defined number of yourinstances.
ReplicaSets Thetake Desiredcare Stateof describesthe how your application should look in Kubernetes. You tell Kubernetes:following:
How many Pods should be running?Which container image should be used?What configurations (e.g., environment variables, ports) should be applied?
A Deployment ensures that the actual state in the cluster matchesEnsuring the desired state.number Forof example,Pods ifare aalways Podrunning
crashes,
KeyOn Featurestop of that, a Deployment adds powerful features, such as:
- Automatically
Rollbacks:rollingRevertout new versions of your application - Rolling back to a previous version
duringifansomethingupdategoesissue using kubectl rollout undo.wrong Rolling Updates:Smooth transitions between versions, supported by:maxSurge:Temporary increase in Pods duringManaging updates(e.g.,with25%).strategies - like
maxUnavailable:Allowable percentage of Pods down duringrolling updates(e.g.,or25%).recreating Pods
Scaling:Adjust replica counts with kubectl scale.Monitoring Updates:Use kubectl rollout status to track rollout progress.
Command Reference Guide
Remeber to use dry-run and tee to check the configuration of each command first.
--dry-run=client -o yaml | tee nginx-deployment.yaml
Create a ReplicaSet using a YAML file (declarative method)
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: nginxdemos/hello
ports:
- containerPort: 80
# Apply replicaset
kubectl apply -f nginx-replicaset.yaml
# Get ReplicaSet informatoin
kubectl get replicaset nginx-replicaset -o wide
# Get detailed ReplicaSet information
kubectl describe replicaset/nginx-replicaset
# Check the current Pods running
kubectl get pods
# Delete a Pod of the ReplicaSet
kubectl delete pod nginx-replicaset-7x6gl
# Recheck running Pods
kubectl get pods
# Change image in yaml to nginxdemos/hello:v0.2 and apply replicaset again
kubectl apply -f nginx-replicaset.yaml
# You will encounter that the replicaset was updated - but the pods are still using the old image
kubectl describe replicaset/nginx-replicaset
kubectl describe pod/<podname>
# You have to kill and recreate the pods, so the new ones will be created with the new image. (see Hint section)
kubectl scale rs nginx-replicaset --replicas=0
kubectl scale rs nginx-replicaset --replicas=3
kubectl describe pod/<podname>
Create a Deplyoment (imperative method)
# Create nginx deployment with the default of one replica
kubectl create deployment nginxnginx-deplyoment --image=nginxdemos/hello --port=80
# Create nginx deployment with three replicas
kubectl create deployment nginxnginx-deplyoment --image=nginxdemos/hello --port=80 --replicas=3
# Expose application as ClusterIP with port 8080 (ClusterIP is the default if not defined)
kubectl expose deployment nginx --type=ClusterIP --port=8080 --target-port=80
# --port=8080: The port exposed by the service (used internally to access the deployment)
# --target-port=80: The port on the pods where the application is running
# Get services
kubectl get service nginx -o wide
# Get full resource description using describe
kubectl describe service/nginx
# Get created endpoints
kubectl get endpoints
# curl by default service DNS entry
# Each curl request gets a different hostname due to Kubernetes' Kube-Proxy load balancing
curl nginx.default.svc.cluster.local
# Delete service
kubectl delete service/nginx
Hints
Deployments manage ReplicaSets, primarily due to historical reasons. There is no practical need to manually create ReplicaSets anymore,(or previously, ReplicationControllers), as DeploymentsDeployments, providebuilt on top of ReplicaSets, offer a more user-friendly and feature-rich abstraction for managing the application lifecycle, including replication, updates, and rollbacks.
ReplicaSets do not support auto updates. As long as required number of pods exist matching the selector labels, replicaset's jobs is done.
Open questions
./.
My personal summary
./.