Python 客户端相当于 `kubectl rollout restart <deployment>

Sai*_*lla 2 python kubernetes

我正在尝试在 Python 中运行自动化作业,以重新启动 Kubernetes 集群中的部署。kubectl由于权限有限,我无法在盒子上安装。有人对此有建议或解决方案吗?

谢谢。

Gab*_*rga 9

对于配置,请遵循 - 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()
Run Code Online (Sandbox Code Playgroud)