重启集群中的所有 k8s pods?

Restart all k8s pods in cluster?

重启集群中所有 pods 的最佳方法是什么?我在想在 kubernetes 中设置一个 cronjob 任务以正常执行此操作并确保集群负载均衡,但正常执行此操作的最佳做​​法是什么?另外,将此作为一次性任务执行的最佳方法是什么?

这是个坏主意。请查看 https://github.com/kubernetes-sigs/descheduler 而不是有选择地进行实际分析:)

但是,kubectl delete pod --all --all-namespaces 或类似。

如果你想从 CronJob 运行 它,这意味着你需要有管理员的 kubeconfig 来连接到 kube-api豆荚。对我来说这是一个风险,因为任何连接到 POD 的人都会得到你的 kubeconfig。所以我更喜欢通过 kubectl 命令从本地机器 运行 它。

当然 - 所有 PODs 对我来说都是指控制平面的组件和所有 Deployments、Statefulsets 和 DaemonSets。

您可以选择 delete podrollout restart (kubectl cheetsheet)。我更喜欢第二种方式,因为它会在 POD 之后重新启动 POD,并保持应用程序的 HA。

因为我今天解决了一个类似的问题所以我写了我自己的脚本,你可以在下面找到它。没什么特别的——它会询问你关于 K8s 上下文的信息,并且在没有 --restart 参数的情况下它会干掉 -运行s 所有重启命令。 :]

#! /bin/env bash

# Help
[[  == "--help" ]] && echo "Usage: [=10=] [--restart]" && exit 0

# Run restart process
if [[  != "" ]]; then
  if [[  == "--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