简体   繁体   中英

Python client euqivelent of `kubectl rollout restart <deployment>

I'm trying to run an automation job in Python that restarts a deployment in Kubernetes cluster. I cannon install kubectl on the box due to limited permissions. Does anyone have a suggestion or solution for this?

Thank you.

There is no atomic corresponding operation to the kubectl rollout restart <deployment> in the Kubernetes clients. This is an operation that is composed of multiple API calls.

What to do, depends on what you want. To just get a new Pod of the same Deployment you can delete a Pod alternatively you could add or change an annotation on the Deployment to trigger a new rolling-deployment.

For configuration follow - https://github.com/kubernetes-client/python/blob/master/examples/remote_cluster.py

# This is equivalent to `kubectl rollout restart deployment/dashboard-kubernetes-dashboard -n default`


from kubernetes import client, config
from kubernetes.client.rest import ApiException
import datetime

def restart_deployment(v1_apps, deployment, namespace):
    now = datetime.datetime.utcnow()
    now = str(now.isoformat("T") + "Z")
    body = {
        'spec': {
            'template':{
                'metadata': {
                    'annotations': {
                        'kubectl.kubernetes.io/restartedAt': now
                    }
                }
            }
        }
    }
    try:
        v1_apps.patch_namespaced_deployment(deployment, namespace, body, pretty='true')
    except ApiException as e:
        print("Exception when calling AppsV1Api->read_namespaced_deployment_status: %s\n" % e)


def main():
    config.load_kube_config(context="minikube")
    # Enter name of deployment and "namespace"
    deployment = "dashboard-kubernetes-dashboard"
    namespace = "default"
    v1_apps = client.AppsV1Api()
    restart_deployment(v1_apps, deployment, namespace)


if __name__ == '__main__':
    main()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM