How to ovewrite an existing pod in kubernetes cluster
Asked by yosser_mahfoudh:

I want to deploy a pod name core-x but that pod is already running in my cluster what I want is to overwrite the old one with the new one.

Knowing that my image version has the same tag?

I need your help please


Answers:
author: yosser_mahfoudh
body: - pipe: atlassian/kubectl-run:3.2.0  
variables:  
KUBE_CONFIG: $KUBE_CONFIG  
KUBECTL_COMMAND: 'delete -f masster/build/kubernetes/core-srv.yaml  
\| apply'  
RESOURCE_PATH: 'core-srv.yaml'  
sleep: 10s  
[@Yevhen](/t5/user/viewprofilepage/user-id/3965218) like this?


author: Yevhen
body: [@yosser_mahfoudh](/t5/user/viewprofilepage/user-id/3803216) you can't create a new pod with the same name and you can't change name of the existing pod too since name and namespace fields are [immutable](https://kubernetes.io/docs/concepts/workloads/pods/#pod-update-and-replacement). Some of the fields can be changed though.

comments: author: yosser_mahfoudh
body: So if I create a new pod named core-Y. how the core-X stop runing in my kubernetes cluster?


author: Yevhen
body: [@yosser_mahfoudh](/t5/user/viewprofilepage/user-id/3803216) you will have to explicitly delete it.


author: yosser_mahfoudh
body: could you pleqse give me an example.


author: Yevhen
body: That would be "kubectl delete pod $podName -n $namespace" if you're using kubectl.


author: yosser_mahfoudh
body: This is what I have  
step:  
name: deploy  
trigger: manual  
script:  
- echo VERSION_BACK=$(grep '\<version\>' pom.xml \| head -1 \| tail -1 \| sed 's/\[\<\>version/ \\t\]//g') \> env.sh  
- source env.sh  
- cd /opt/atlassian/pipelines/agent/build/ \&\& ls  
- sed -i "s\|{{image}}\|imagename/core-srv:${VERSION_BACK}\|g" core-srv.yaml  
- pipe: atlassian/kubectl-run:3.2.0  
variables:  
KUBE_CONFIG: $KUBE_CONFIG  
KUBECTL_COMMAND: 'apply'  
RESOURCE_PATH: 'core-srv.yaml'  
so should I replace apply by delete ?


author: Yevhen
body: Yes. Perhaps, to delete the pod you don't need to run any sed against yaml. It'll be deleted as long as a pod with such a name exists in the namespace (its spec does not really matter)


author: yosser_mahfoudh
body: But it will know which pod I want to delete ?


author: Yevhen
body: It's will delete whatever is in metadata.name in your yaml.


author: yosser_mahfoudh
body: I tried what you said but it deletes it and never restart creating the pod from the begin.


author: Yevhen
body: [@yosser_mahfoudh](/t5/user/viewprofilepage/user-id/3803216) yes, this is what delete command is expected to do. In the next step you can apply again (I'd recommend adding a 10-15 second sleep to give apiserver time to delete the pod).


author: yosser_mahfoudh
body: [@Yevhen](/t5/user/viewprofilepage/user-id/3965218)

do you mean like this  
pipe: atlassian/kubectl-run:3.2.0  
variables:  
KUBE_CONFIG: $KUBE_CONFIG  
KUBECTL_COMMAND: 'delete -f masster/vuild/core-srv.yaml' \| apply  
RESOURCE_PATH: 'core-srv.yaml'


author: Yevhen
body: [@yosser_mahfoudh](/t5/user/viewprofilepage/user-id/3803216) I was more thinking about a new step. So your steps in the pipeline would differ by one command only.

Also, I'd recommend looking at an official K8s documentation and play with kubectl and your pod yaml in the command line to better understand kubectl and cluster behavior.


Tags: bitbucket-cloud

cloud

kubernetes