u123
u123

Reputation: 16331

Correct way to scale/restart an application down/up in kubernetes (replicaset, deployments and pod deletion)?

I usually restart my applications by:

kubectl scale deployment my-app --replicas=0

Followed by:

kubectl scale deployment my-app --replicas=1

which works fine. I also have another running application but when I look at its replicaset I see:

$ kubectl get rs
NAME                                        DESIRED   CURRENT   READY     AGE
another-app                                 2         2         2         2d

So to restart that correctly I would of course need to:

kubectl scale deployment another-app --replicas=0
kubectl scale deployment another-app --replicas=2

But is there a better way to do this so I don't have to manually look at the repliasets before scaling/restarting my application (that might have replicas > 1)?

Upvotes: 4

Views: 19224

Answers (2)

kool
kool

Reputation: 3613

To make changes in your current deployment you can use kubectl rollout pause deployment/YOUR_DEPLOYMENT. This way the deployment will be marked as paused and won't be reconciled by the controller. After it's paused you can make necessary changes to your configuration and then resume it by using kubectl rollout resume deployment/YOUR_DEPLOYMENT. This way it will create a new replicaset with updated configuration.

Pod with new configuration will be started and when it's in running status, pod with old configuration will be terminated.

Using this method you will be able to rollout the deployment to previous version by using:

kubectl rollout history deployment/YOUR_DEPLOYMENT

to check history of the rollouts and then execute following command to rollback:

kubectl rollout undo deployment/YOUR_DEPLOYMENT --to-revision=REVISION_NO

Upvotes: 5

hoque
hoque

Reputation: 6471

You can restart pods by using level

kubectl delete pods -l name=myLabel

You can rolling restart of all pods for a deployments, so that you don't take the service down

kubectl patch deployment your_deployment_name -p \
  "{\"spec\":{\"template\":{\"metadata\":{\"annotations\":{\"date\":\"`date +'%s'`\"}}}}}"

And After kubernetes version 1.15 you can

kubectl rollout restart deployment your_deployment_name

Upvotes: 6

Related Questions