Recreate Strategy - Delete and then recreate
Rolling Update - Default Strategy. This takes down on pod and replaces it one at at time.
When making a change to a deployment, say, change the image from nginx:1.7.1 to nginx:1.8.0, update the yaml and then apply the changes with
kubectl apply -f <filename>
This will begin a rolling update, updating a single pod at a time.
For the test, you can also use the "set image" command
kubectl set image deployment/myapp-deployment nginx=nginx:1.8.1
Notice in the yaml, there are 2 spec sections. One for the deployment and one for the container.
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deployment
labels:
app: myapp
type: front-end
spec:
replicas: 3
selector:
matchLabels:
type: front-end
template:
metadata:
labels:
app: myapp
type: front-end
spec:
containers:
- name: nginx-container
image: nginx:1.7.1 #update this to 1.8
This uses the strategy
section to specify the type of update and how to handle the update. Choices for updates are:
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 0
maxSurge: 1
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: tester-dep
name: tester-dep
spec:
replicas: 1
selector:
matchLabels:
app: tester-dep
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 0
maxSurge: 1
template:
metadata:
creationTimestamp: null
labels:
app: tester-dep
spec:
containers:
- command:
- /bin/sh
- -c
- while true; do echo $NAME; sleep 10; done
image: busybox
name: tester-pod
envFrom:
- configMapRef:
name: myconfig
resources: {}
When a change is made to a deployment, Kubernetes will create a new repliacset and start deploying pods to the new replicaset. As a new pod is created I the new replicaset, it will also take down a pod in the old repliaset.
If you need to rollback the update and go back to the old repliaset, use
#kubectl rollout undo deployment/<deployment_name>
kubectl rollout undo deployment/my-deployment
kubectl get replicaset
kubectl create deployment my_deployment --image:nginx --replicas=2 --dry-run=client -o yaml > file.yml
Command | Syntax |
---|---|
Create | kubectl create -f deployment-definition.yml kubectl create deployment nginx --image=nginx:1.16 |
Get | kubectl get deployments |
Update | kubectl apply -f deployment-definition.yml kubectl set image deployment/my-app-deployment nginx=nginx:1.1.8 |
Status | kubectl rollout status deployment/myapp-deployment kubectl rollout history deployment/myapp-deployment |
Rollback | kubectl rollout undo deployment/myapp-deployment |
Here the revision 1 is the first version where the deployment was created.
You can check the status of each revision individually by using the --revision flag:
master $ kubectl rollout history deployment nginx --revision=1
deployment.extensions/nginx with revision #1
Pod Template:
Labels: app=nginx pod-template-hash=6454457cdb
Containers: nginx: Image: nginx:1.16
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
master $
You would have noticed that the "change-cause" field is empty in the rollout history output. We can use the --record flag to save the command used to create/update a deployment against the revision number.
master $ kubectl set image deployment nginx nginx=nginx:1.17 --record
deployment.extensions/nginx image updated
master $master $
master $ kubectl rollout history deployment nginx
deployment.extensions/nginx
REVISION CHANGE-CAUSE
1 <none>
2 kubectl set image deployment nginx nginx=nginx:1.17 --record=true
master $
You can now see that the change-cause is recorded for the revision 2 of this deployment.
Let's make some more changes. In the example below, we are editing the deployment and changing the image from nginx:1.17 to nginx:latest while making use of the --record flag.
master $ kubectl edit deployments. nginx --record
deployment.extensions/nginx edited
master $ kubectl rollout history deployment nginx
REVISION CHANGE-CAUSE
1 <none>
2 kubectl set image deployment nginx nginx=nginx:1.17 --record=true
3 kubectl edit deployments. nginx --record=true
master $ kubectl rollout history deployment nginx --revision=3
deployment.extensions/nginx with revision #3
Pod Template: Labels: app=nginx
pod-template-hash=df6487dc Annotations: kubernetes.io/change-cause: kubectl edit deployments. nginx --record=true
Containers:
nginx:
Image: nginx:latest
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
master $
Lets now rollback to the previous revision:
master $ kubectl rollout undo deployment nginx
deployment.extensions/nginx rolled back
master $ kubectl rollout history deployment nginx
deployment.extensions/nginxREVISION CHANGE-CAUSE
1 <none>
3 kubectl edit deployments. nginx --record=true
4 kubectl set image deployment nginx nginx=nginx:1.17 --record=true
master $ kubectl rollout history deployment nginx --revision=4
deployment.extensions/nginx with revision #4Pod Template:
Labels: app=nginx pod-template-hash=b99b98f9
Annotations: kubernetes.io/change-cause: kubectl set image deployment nginx nginx=nginx:1.17 --record=true
Containers:
nginx:
Image: nginx:1.17
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
master $ kubectl describe deployments. nginx | grep -i image:
Image: nginx:1.17
master $
With this, we have rolled back to the previous version of the deployment with the image = nginx:1.17.