How to Terminate a Kubernetes Pod on The Host Network After Updating Its Deployment?
In this guide, I'm showing you how to edit the deployment for a Kubernetes pod on the host network and ensure it's recreated immediately.
If you have a pod on the host network, you may find it difficult to modify its deployment and see your changes reflected on your cluster. The new pod created following your update will appear as pending, while your other pod will continue to live unaffected.
How do you fix that? Let's get to it right now.
How to Edit a Kubernetes Deployment?
To edit a kubernetes deployment, you need to use this command in your terminal:
kubectl edit deploy DEPLOYMENT_NAME -n NAMESPACE
Replace the DEPLOYMENT_NAME and NAMESPACE values with your own values. If you don't know them, you can use the command below to see all deployments from all namespaces in your terminal:
kubectl get deployment -A
How to terminate a host network pod following a deployment update?
To terminate a pod following a deployment update, you must reduce its grace period seconds and change its strategy to Recreate. This means adding the following line to the same level where you configure the host network to be true like so:
hostNetwork: true
terminationGracePeriod: 5
And add this under the strategy section:
type: Recreate
Make sure the line above is the only line inside the strategy section.
You may need to reduce the replicas to zero the first time for this change to take effect:
kubectl scale deploy <your-deployment-name> --replicas=0
Then, after all pods have been terminated and removed, use the same command to choose the number of replicas (i.e., pods) your cluster should have (1 if you're not specifically scaling).
And that's it! After these changes, if you're updating your deployment you should see your changes reflected quickly.
Conclusion
To ensure you don't wait indefinitely after updating your deployment of a pod using the host network, there are two things you need to update in your deployment:
- The "strategy" type (Rollout -> Recreate).
- The terminationGracePeriod to 5 or even 0 if you're brave.
I've included a way for you to test your changes using Kubernetes's CLI.
Let me know if you have any other questions.