[英]Restart all k8s pods in cluster?
What is the best way to restart all pods in a cluster?重启集群中所有 Pod 的最佳方法是什么? I was thinking that setting a cronjob task within kubernetes to do this on a normal basis and make sure that the cluster is load balanced evenly, but what is the best practice to do this on a normal basis?我在想在 kubernetes 中设置一个 cronjob 任务以正常执行此操作并确保集群负载均衡,但是正常执行此操作的最佳实践是什么? Also, what is the best way to do this as a one-time task?此外,作为一次性任务执行此操作的最佳方法是什么?
This is a bad idea.这是一个坏主意。 Check out https://github.com/kubernetes-sigs/descheduler instead to do it selectively and with actual analysis:)查看https://github.com/kubernetes-sigs/descheduler以选择性地进行实际分析:)
But that said, kubectl delete pod --all --all-namespaces
or similar.但这就是说, kubectl delete pod --all --all-namespaces
或类似的。
If you want to run it from the CronJob, it means you need to have admin's kubeconfig inside for connecting to the kube-api from the POD.如果你想从 CronJob 运行它,这意味着你需要有管理员的kubeconfig来从 POD 连接到 kube-api。 For me it's a risk because anyone who connects to the POD gets your kubeconfig .对我来说这是一个风险,因为任何连接到 POD 的人都会得到你的kubeconfig 。 So I prefer to run it from the local machine via kubectl
command.所以我更喜欢通过kubectl
命令从本地机器上运行它。
For sure - all PODs for me mean the Control Plane's components and all Deployments, Statefulsets, and DaemonSets.当然——对我来说,所有的 POD 都意味着控制平面的组件和所有的 Deployment、Statefulset 和 DaemonSet。
You can choose delete pod
or rollout restart
( kubectl cheetsheet ).您可以选择delete pod
或rollout restart
( kubectl cheetsheet )。 I prefer the second way because it restarts POD after POD and keeps the HA of the applications.我更喜欢第二种方式,因为它在 POD 之后重新启动 POD 并保留应用程序的 HA。
Because I today solved a similar issue so I wrote my own script which you can find out below.因为我今天解决了一个类似的问题,所以我编写了自己的脚本,您可以在下面找到。 Nothing special - it asks you about K8s context and without the --restart
parameter it dry-runs all restart commands.没什么特别的——它会询问你关于 K8s 上下文的信息,并且在没有--restart
参数的情况下它会空运行所有重启命令。 :] :]
#! /bin/env bash
# Help
[[ ${1} == "--help" ]] && echo "Usage: $0 [--restart]" && exit 0
# Run restart process
if [[ ${1} != "" ]]; then
if [[ ${1} == "--restart" ]]; then
RUN_RESTART="true"
else
echo "This parameter is strange, check it: ${}"
exit 1
fi
fi
###
### VARIABLES
###
SLEEP=10
KUBECTL="kubectl"
###
### CONFIRM CONTEXT
###
echo -e "\nConfirm your context: $(${KUBECTL} config current-context): [enter / ctrl+c]"
echo -e "~~~~~~~~~~~~~~~~~~~~~~~\n"
read
###
### RESTART ALL K8s SERVICES
###
echo -e "\n\n>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<"
echo ">>> RESTART ALL K8s SERVICES <<<"
echo ">>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<"
CONTROL_PLANE=$(${KUBECTL} get node -l node-role=master -o jsonpath='{range .items[*]}{@.metadata.name}{"\n"}')
for master in ${CONTROL_PLANE}; do
echo "Master: ${master}"
echo "~~~~~~~~~~~~~~~~~~~~~"
CP_PODS=$(${KUBECTL} -n kube-system get pods --field-selector spec.nodeName=${master} -o jsonpath='{range .items[*]}{@.metadata.name}{"\n"}' | grep -E -- 'etcd|^kube-')
for pod in ${CP_PODS}; do
CP_RESTART="${KUBECTL} -n kube-system delete pod ${pod}"
if [[ ${RUN_RESTART} == "true" ]]; then
echo " * [$(date '+%F %T')] Run command: ${CP_RESTART}"
${CP_RESTART} && echo
sleep ${SLEEP}
else
echo " * ${CP_RESTART}"
fi
done
echo -e "~~~~~~~~~~~~~~~~~~~~~~~\n\n"
done
###
### RESTART ALL Deployments, ReplicaSets and DaemonSets
###
echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
echo ">>> RESTART ALL Deployments, StatefullSets and DaemonSets <<<"
echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
for ns in $(${KUBECTL} get ns -o jsonpath='{range .items[*]}{@.metadata.name}{"\n"}'); do
# Check if in the namespace are PODs
# PODS=$(${KUBECTL} -n ${ns} get pod -o jsonpath='{range .items[*]}{@.metadata.name}{"\n"}')
# echo ">>> ${ns}: $PODS"
# if [[ "${PODS}" != "" ]]; then
# ns="ms"
echo "Namespace: $ns"
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo " >> Deployments:"
for deployment in $(${KUBECTL} -n $ns get deployments.apps -o jsonpath='{range .items[*]}{@.metadata.name}{"\n"}'); do
DEPL_RESTART="${KUBECTL} -n ${ns} rollout restart deployments.apps ${deployment}"
if [[ ${RUN_RESTART} == "true" ]]; then
echo " * [$(date '+%F %T')] Run command: ${DEPL_RESTART}"
${DEPL_RESTART} && echo
sleep ${SLEEP}
else
echo " * ${DEPL_RESTART}"
fi
done
echo " >> StatefulSets:"
for rs in $(${KUBECTL} -n $ns get statefulset -o jsonpath='{range .items[*]}{@.metadata.name}{"\n"}'); do
RS_RESTART="${KUBECTL} -n ${ns} rollout restart statefulset ${rs}"
if [[ ${RUN_RESTART} == "true" ]]; then
echo " * [$(date '+%F %T')] Run command: ${RS_RESTART}"
${RS_RESTART} && echo
sleep ${SLEEP}
else
echo " * ${RS_RESTART}"
fi
done
echo " >> DaemonSets:"
for ds in $(${KUBECTL} -n $ns get daemonsets.apps -o jsonpath='{range .items[*]}{@.metadata.name}{"\n"}'); do
DS_RESTART="${KUBECTL} -n ${ns} rollout restart daemonsets.apps ${ds}"
if [[ ${RUN_RESTART} == "true" ]]; then
echo " * [$(date '+%F %T')] Run command: ${DS_RESTART}"
${DS_RESTART} && echo
sleep ${SLEEP}
else
echo " * ${DS_RESTART}"
fi
done
echo -e "~~~~~~~~~~~~~~~~~~~~~~~\n\n"
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.