All technological notes.
Deployment
ReplicaSetReplicaSet.
ReplicaSet and roll back to the previous version.mainly used for stateless workloads
pods created with deployment are fungibleMajor roles:
Progressing;
Deployment creates a new ReplicaSet.Deployment is scaling up its newest ReplicaSet.Deployment is scaling down its older ReplicaSet(s).Pods become ready or available (ready for at least MinReadySeconds).complete:
replicas have been updated to the latest versionreplicas are available.replicas for the Deployment are running.Failed
Readiness probe failuresDeployment failure:
kubectl describe:
kubectl get -o yaml
pod-template-hash Label
rs and pod
RS namePod template gets changed, a new ReplicaSet is created.ReplicaSet: Ensures a fixed number of Pods are running.Deployment: Manages ReplicaSets, and adds features like rolling updates and rollbacks.
Deployment controls the ReplicaSet, which in turn controls the individual Pods.a ReplicaSet object that’s automatically created when creating the Deployment object.
Deployment object doesn‘t directly manage the Pod objects, but manages them through a ReplicaSet objectUpdating Pod template
ReplicaSet: update only the new created podDeploymen: update pods immediatelyReplicaSet: controlled by RSDeploymen: not controlled by Deploy, because additional label pod-template-hash will be added.when you delete a Deployment object, the underlying ReplicaSet and Pods are also deleted.
Preserving the ReplicaSet and Pods
kubectl delete deploy NAME --cascade=orphanAdopting an existing ReplicaSet and Pods
pod-template-hash is calculated based on the pod template.| Command | Description |
|---|---|
kubectl explain deploy |
show deployment documentation |
kubectl get deploy |
List all Deployments in the current namespace. |
kubectl describe deploy deploy_name |
Show detailed information about a specific Deployment. |
kubectl create deploy deploy_name --image=img_name |
Create a deployment using image |
kubectl create deploy nginx --image=nginx --dry-run=client --replicas=4 -o yaml |
Show the deployemnt in yaml file |
kubectl set image deploy_name nginx=nginx:1.25 |
Update the container image in a deployment |
kubectl delete deploy deploy_name |
Delete a Deployment by name. |
kubectl scale deploy deploy_name --replicas=count-num |
Scale the number of replicas for a Deployment. |
kubectl get deploy
NAME:
READY:
UP-TO-DATE
AVAILABLE
spec.minReadySecondsAGE
spec.replicas:
spec.selector:
matchLabels: a map of lablesmatchExpressions: a list of label selector requirementsspec.template:
spec.strategy:
Pods are replaced when updating the Pod template.When scaling a Deployment, the Deployment controller does nothing but scale the underlying ReplicaSet
Deployment controller updates the desired number of the replicas in the ReplicaSetReplicaSet controller to do the rest.If scale the underlying RS, the RS controller scales the number of pod imdiately.
deploy controller detects the difference between the desired replicas in the deploy and the underlying rs, and update the replicas in the rsrs controller reconcile the pod number.If the underlying rs object get deleted, the deploy controller will recreate the rs
replicas fieldPractical but problematical scenario:
replicas=3 in manifestkubectl scalereplicas field and apply
To avoid accidentally scaling a Deployment each time you reapply its manifest file
replicas field, which creates only 1 replica when creatingkubectl scale to update the replica number
if the replica already apply:
kubectl apply edit-last-applied deploy NAME: to remove the replicas field in the snapshotreplicas field# demo-deploy-with-replica.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo-deploy-with-replica
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: nginx
name: nginx
kubectl apply -f demo-deploy-with-replica.yaml
# deployment.apps/demo-deploy-with-replica created
kubectl get deploy
# NAME READY UP-TO-DATE AVAILABLE AGE
# demo-deploy-with-replica 3/3 3 3 25s
kubectl scale deploy demo-deploy-with-replica --replicas=5
# deployment.apps/demo-deploy-with-replica scaled
# confirm
kubectl get deploy
# NAME READY UP-TO-DATE AVAILABLE AGE
# demo-deploy-with-replica 5/5 5 5 2m56s
kubectl apply -f demo-deploy-with-replica.yaml
# deployment.apps/demo-deploy-with-replica configured
# confirm: replica back to 3
kubectl get deploy
# NAME READY UP-TO-DATE AVAILABLE AGE
# demo-deploy-with-replica 3/3 3 3 3m53s
the replica number change back to 3
# demo-deploy-without-replica.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo-deploy-without-replica
spec:
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: nginx
name: nginx
kubectl apply -f demo-deploy-without-replica.yaml
# deployment.apps/demo-deploy-without-replica created
kubectl get deploy
# NAME READY UP-TO-DATE AVAILABLE AGE
# demo-deploy-without-replica 1/1 1 1 22s
kubectl scale deploy demo-deploy-without-replica --replicas=5
# deployment.apps/demo-deploy-without-replica scaled
kubectl get deploy
# NAME READY UP-TO-DATE AVAILABLE AGE
# demo-deploy-without-replica 5/5 5 5 2m33s
kubectl apply -f demo-deploy-without-replica.yaml
# deployment.apps/demo-deploy-without-replica configured
kubectl get deploy demo-deploy-without-replica
# NAME READY UP-TO-DATE AVAILABLE AGE
# demo-deploy-without-replica 5/5 5 5 5m29s
the replica remain 5.
# demo-deploy-default-replica.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo-deploy-default-replica
spec:
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
type: front-end
spec:
containers:
- name: nginx
image: nginx
# create deployment
kubectl create -f demo-deploy-default-replica.yaml
# deployment.apps/demo-deploy-default-replica created
kubectl get deploy
# NAME READY UP-TO-DATE AVAILABLE AGE
# demo-deploy-default-replica 1/1 1 1 96s
# also create rs
kubectl get rs
# NAME DESIRED CURRENT READY AGE
# demo-deploy-default-replica-65cf588b89 1 1 1 106s
kubectl get pods
# NAME READY STATUS RESTARTS AGE
# demo-deploy-default-replica-65cf588b89-v4kpd 1/1 Running 0 2m20s
# confirm deploy:
# default replica=1; default StrategyType=RollingUpdate; RollingUpdateStrategy:25% max unavailable, 25% max surge
kubectl describe deploy demo-deploy-default-replica
# Selector: app=nginx
# Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable
# StrategyType: RollingUpdate
# RollingUpdateStrategy: 25% max unavailable, 25% max surge
# confirm rs:
# Controlled by: deloy
# additional label added: pod-template-hash + selector labels
kubectl describe rs demo-deploy-default-replica
# Labels: app=nginx
# pod-template-hash=65cf588b89
# type=front-end
# Controlled By: Deployment/demo-deploy-default-replica
# Replicas: 1 current / 1 desired
# confirm pod:
# Controlled by rs
# additional label added: pod-template-hash + selector labels
kubectl describe pod demo-deploy-default-replica
# Controlled By: ReplicaSet/demo-deploy-default-replica-65cf588b89
# Labels: app=nginx
# pod-template-hash=65cf588b89
# type=front-end