Easily Rolling Restart Kubernetes Pod (Pod Deletion Command)

kubernetes
2021-12-11 22:28 (3 years ago) ytyng

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
Currently unrated

Comments

Archive

2024
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
2012
2011