我正在尝试在Python中运行一个自动化作业,该作业将重新启动Kubernetes集群中的部署。由于权限有限,我不能在盒子上安装kubectl
。有人对此有什么建议或解决方案吗?
谢谢。
配置参考- 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: %sn" % 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()
Kubernetes客户端中没有与kubectl rollout restart <deployment>
对应的原子操作。这是由多个API调用组成的操作。
Deployment
的新Pod,您可以删除Pod
,或者您可以在Deployment
上添加或更改注释以触发新的滚动部署。