Last active
August 7, 2023 10:33
-
-
Save jmound/ff6fa539385d1a057c82fa9fa739492e to your computer and use it in GitHub Desktop.
Bash function to refresh all pods in all deployments by namespace
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# based on the "patch deployment" strategy in this comment: | |
# https://github.com/kubernetes/kubernetes/issues/13488#issuecomment-372532659 | |
# requires jq | |
# $1 is a valid namespace | |
function refresh-all-pods() { | |
echo | |
DEPLOYMENT_LIST=$(kubectl -n $1 get deployment -o json|jq -r .items[].metadata.name) | |
echo "Refreshing pods in all Deployments" | |
for deployment_name in $DEPLOYMENT_LIST ; do | |
TERMINATION_GRACE_PERIOD_SECONDS=$(kubectl -n $1 get deployment "$deployment_name" -o json|jq .spec.template.spec.terminationGracePeriodSeconds) | |
if [ "$TERMINATION_GRACE_PERIOD_SECONDS" -eq 30 ]; then | |
TERMINATION_GRACE_PERIOD_SECONDS='31' | |
else | |
TERMINATION_GRACE_PERIOD_SECONDS='30' | |
fi | |
patch_string="{\"spec\":{\"template\":{\"spec\":{\"terminationGracePeriodSeconds\":$TERMINATION_GRACE_PERIOD_SECONDS}}}}" | |
kubectl -n $1 patch deployment $deployment_name -p $patch_string | |
done | |
echo | |
} | |
refresh-all-pods $NAMESPACE |
FWIW, Kubernetes 1.15 has now a built-in CLI command to accomplish that :)
Created a new
kubectl rollout restart
command that does a rolling restart of a deployment. (#76062)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@APuertaSales
echo $COMPLETE_NAMESPACE_LIST $EXCLUDE_NAMESPACE_LIST | sed 's/ /\n/g'
not work on mac oswork way: $(echo $COMPLETE_NAMESPACE_LIST $EXCLUDE_NAMESPACE_LIST | xargs -n1 | sort -u | xargs )
link: https://unix.stackexchange.com/questions/353321/remove-all-duplicate-word-from-string-using-shell-script