Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rollout restart deployment through the API?

Tags:

kubernetes

Kubernetes 1.15 introduced the command

kubectl rollout restart deployment my-deployment

Which would be the endpoint to call through the API? For example if I want to scale a deployment I can call

PATCH /apis/apps/v1/namespaces/my-namespace/deployments/my-deployment/scale
like image 289
Leandro Cofre Avatar asked Nov 26 '19 12:11

Leandro Cofre


People also ask

What is a rollout restart?

Rolling Restart Method The command given above shuts down and restarts each container in your deployment one by one. Because most of the containers are still functioning, your app will be accessible.

What is rolling restart in Kubernetes?

Rolling restart is utilized to resume every pod after deployment. For a rolling restart, we run the following command: After running the command mentioned above, Kubernetes shuts down slowly and substitutes pods, but some containers are always running.


2 Answers

If you dig around in the kubectl source you can eventually find (k8s.io/kubectl/pkg/polymorphichelpers).defaultObjectRestarter. All that does is change an annotation:

apiVersion: apps/v1
kind: Deployment
spec:
  template:
    metadata:
      annotations:
        kubectl.kubernetes.io/restartedAt: '2006-01-02T15:04:05Z07:00'

Anything that changes a property of the embedded pod spec in the deployment object will cause a restart; there isn't a specific API call to do it.

The useful corollary to this is that, if your kubectl and cluster versions aren't in sync, you can use kubectl rollout restart in kubectl 1.14 against older clusters, since it doesn't actually depend on any changes in the Kubernetes API.

like image 139
David Maze Avatar answered Sep 20 '22 00:09

David Maze


TLDR

curl --location --request PATCH 'https://kubernetes.docker.internal:6443/apis/apps/v1/namespaces/default/deployments/keycloak?fieldManager=kubectl-rollout&pretty=true' \
--header 'Content-Type: application/strategic-merge-patch+json' \
--data-raw '{
    "spec": {
        "template": {
            "metadata": {
                "annotations": {
                    "kubectl.kubernetes.io/restartedAt": <time.Now()>
                }
            }
        }
    }
}'

If you have kubectl you can debug the calls on a local minikube by providing the extra flag --v 9 to your command. That said you can try to do a dummy rollout restart on your local cluster to see the results.

For future readers: This can vary between versions, but if you are in apps/v1 it should be ok.

like image 39
DB.Null Avatar answered Sep 21 '22 00:09

DB.Null