Easily Rolling Restart Kubernetes Pod (Pod Deletion Command)

kubernetes
2021-12-11 13:28 (4 years ago)
Easily Rolling Restart Kubernetes Pod (Pod Deletion Command)

In Kubernetes, within a Pod's Deployment, the container configuration looks like this:

apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deployment
namespace: mynamespace
spec:
replicas: 2
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myrepo/myapp:latest
imagePullPolicy: Always

If you are operating with the container tag set to latest and imagePullPolicy: Always.

In this case, to reflect the latest code, you would use kubectl delete pod, but by deleting the pods at staggered intervals, you can avoid downtime.

While kubectl delete pod itself waits for the deletion and restart of the pod, if there is downtime until the application starts up after the pod restarts, this can be avoided.

A common approach is using this script:

restart-pod.sh

#!/usr/bin/env zsh

if [ -f "${HOME}/.kube/config-xxxx" ]; then
export KUBECONFIG=${HOME}/.kube/config-xxxx
fi

pods=( $(kubectl get pod -n mynamespace | egrep -o "myapp-deployment-[a-zA-Z0-9-]+") )

_sleep=
for pod in "${pods[@]}" ; do
${_sleep}
kubectl -n mynamespace delete pod ${pod}
_sleep="sleep 10s"
done
Please rate this article
Currently unrated
The author runs the application development company Cyberneura.
We look forward to discussing your development needs.

Categories

Archive