0

I am using python to interact with Kubernetes. I need to delete an existing pod and recreate it with different name and PVC. From Python API documentation I got to know that Python provides API create_namespaced_pod which accepts body as argument which is suppose to be Pod specific data. But I dont want to construct this Body Parameter fetching data from old pod and setting again in body. Is there any other way where in i can read the old pod content, update PVC and pod name and call create_namespaced_pod method to create new pod ?

I tried to pass content of pod to create_namespaced_pod method. But it does not give any exception but Pod does not gets created. Below code prints "-- created pod---"

targetPodResult = v1.read_namespaced_pod(podname, obj.metadata.namespace, async_req=True)  
targetPod = targetPodResult.get()
targetPod.metadata.name = targetPod.metadata.name + "_new"
print(targetPod)
v1.create_namespaced_pod('default', targetPod, async_req=True)
print("--  created pod---")
1
  • Usually I wouldn't expect to create Pods directly. Can you use a Deployment object, update its content without deleting it, and let the built-in Kubernetes controller deal with replacing the Pod? Or, if you need a PersistentVolumeClaim attached, use a StatefulSet instead? Commented Jan 4, 2023 at 12:31

1 Answer 1

1

i am able to create pod now using different variant of method with body = targetPod like below. also, i need to set resource_version of old pod to 'None'

v1.create_namespaced_pod(body = targetPod, namespace = 'default')
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.