Reputation: 56
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.
Upvotes: 3
Views: 6722
Reputation: 131
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()
Upvotes: 13
Reputation: 128777
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.
Upvotes: 3