-
-
Save jmound/ff6fa539385d1a057c82fa9fa739492e to your computer and use it in GitHub Desktop.
# 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 |
You can remove the dependency on jq like this I think:
# based on the "patch deployment" strategy in this comment:
# https://github.com/kubernetes/kubernetes/issues/13488#issuecomment-372532659
# $1 is a valid namespace
function refresh-all-pods() {
echo
DEPLOYMENT_LIST=$(kubectl -n $1 get deployment -o jsonpath='{.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 jsonpath='{.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
I ran Ubuntu 18.10 and hit problem.
if running, ./refresh.sh default
it will prompt below as $1 cannot be passed to the script.
Error: unknown command "deployment" for "kubectl"
Run 'kubectl --help' for usage.
unknown command "deployment" for "kubectl"
I modified the script
#!/bin/bash
# based on the "patch deployment" strategy in this comment:
# https://github.com/kubernetes/kubernetes/issues/13488#issuecomment-372532659
# $1 is a valid namespace
if [ $# -ne 1 ];then
echo $0": usage: ./refresh.sh <namespace>"
exit 1
fi
NS=$1
function refresh-all-pods() {
echo
DEPLOYMENT_LIST=$(kubectl -n $NS get deployment -o jsonpath='{.items[*].metadata.name}')
for deployment_name in $DEPLOYMENT_LIST ; do
TERMINATION_GRACE_PERIOD_SECONDS=$(kubectl -n $NS get deployment "$deployment_name" -o jsonpath='{.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 $NS patch deployment $deployment_name -p $patch_string
done
echo
}
refresh-all-pods $NAMESPACE`
Version that will search in each namespace. You can also set namespaces to exclude or the list of namespaces where you want to search for PODs with Istio sidecar. It will add only 1 sec to the actual "terminationGracePeriodSeconds" of the deployment.
You must set your kubectl application and kubeconfig file.
# based on the "patch deployment" strategy in this comment:
# https://github.com/kubernetes/kubernetes/issues/13488#issuecomment-372532659
# based on Istio refresh bsh script:
# https://gist.github.com/jmound/ff6fa539385d1a057c82fa9fa739492e
KUBECTL="./kubectl.exe --kubeconfig=admin-nonprod.kubeconfig"
EXCLUDE_NAMESPACE_LIST=""
while [ "$1" != "" ]
do
case $1 in
-o | --only ) shift
NAMESPACE_LIST=$1
;;
-e | --exclude ) shift
EXCLUDE_NAMESPACE_LIST=$1
;;
-h | --help ) echo "usage ./refresh_istio_sidecars.sh -o <only namespaces comma separated> -e <excluded namespaces comma separated>"
echo "example ./refresh_istio_sidecars.sh -o intadaptive-cb"
echo "example ./refresh_istio_sidecars.sh -e istio-system,kube-system"
exit
;;
* ) echo "usage ./refresh_istio_sidecars.sh -o <only namespaces comma separated> -e <excluded namespaces comma separated>"
echo "example ./refresh_istio_sidecars.sh -o intadaptive-cb"
echo "example ./refresh_istio_sidecars.sh -e istio-system,kube-system"
exit 1
esac
shift
done
function refresh-all-pods() {
echo CHECKING NAMESPACE: $NS
DEPLOYMENT_LIST=$($KUBECTL -n $NS get deployment -o jsonpath='{.items[*].metadata.name}')
for deployment_name in $DEPLOYMENT_LIST
do
# Get PODs deployed
APP=$($KUBECTL -n $NS get deploy "$deployment_name" -o jsonpath='{.metadata.labels.app}')
POD_LIST=$($KUBECTL -n $NS get po --selector=app="$APP" -ojsonpath='{.items[*].metadata.name}')
for pod_name in $POD_LIST
do
# Check if this POD has Istio proxy sidecar
if $KUBECTL -n $NS get po "$pod_name" -o jsonpath='{.status.containerStatuses[*].image}' | grep -q "istio/proxy"
then
REFRESH_DEPLOYMENT_LIST="$REFRESH_DEPLOYMENT_LIST $deployment_name"
fi
done
done
# Add 1 second to the termination grace period
for refresh_deployment in $REFRESH_DEPLOYMENT_LIST
do
TERMINATION_GRACE_PERIOD_SECONDS=$($KUBECTL -n $NS get deployment "$refresh_deployment" -o jsonpath='{.spec.template.spec.terminationGracePeriodSeconds}')
NEW_TERMINATION_GRACE_PERIOD_SECONDS=$(( TERMINATION_GRACE_PERIOD_SECONDS + 1 ))
echo .............REFRESING: ${refresh_deployment}: $NEW_TERMINATION_GRACE_PERIOD_SECONDS
patch_string="{\"spec\":{\"template\":{\"spec\":{\"terminationGracePeriodSeconds\":$NEW_TERMINATION_GRACE_PERIOD_SECONDS}}}}"
$KUBECTL -n $NS patch deployment $refresh_deployment -p $patch_string
done
echo
}
if [ -z $NAMESPACE_LIST ]
then
COMPLETE_NAMESPACE_LIST=$($KUBECTL get namespaces -ojsonpath='{.items[*].metadata.name}')
EXCLUDE_NAMESPACE_LIST=$(echo $EXCLUDE_NAMESPACE_LIST | sed 's/,/ /g')
NAMESPACE_LIST=$(echo $COMPLETE_NAMESPACE_LIST $EXCLUDE_NAMESPACE_LIST | sed 's/ /\n/g' | sort | uniq -u)
else
NAMESPACE_LIST=$(echo $NAMESPACE_LIST | sed 's/,/ /g')
fi
for NS in $NAMESPACE_LIST
do
REFRESH_DEPLOYMENT_LIST=""
refresh-all-pods
done```
@APuertaSales echo $COMPLETE_NAMESPACE_LIST $EXCLUDE_NAMESPACE_LIST | sed 's/ /\n/g'
not work on mac os
work 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
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)
See kubernetes/kubernetes#13488 (comment) and https://stackoverflow.com/a/40368520/90442