如何快速给pod添加健康检查?

◇◆丶佛笑我妖孽 提交于 2019-12-09 12:01:51

脚本如下

#!/usr/bin/env bash
[[ -n $DEBUG ]] && set -x
set -eou pipefail

useage(){
  cat <<HELP
USAGE:
    kubeprobe.sh TYPE WORKLOAD CONTAINER [LIVENESSPROBE READINESSPROBE]
    eg: 
      kubeprobe deploy ui nginx
      kubeprobe deploy ui nginx "" "/ 80 3 2 1 1 3"
      kubeprobe deploy ui nginx "/ 80 10 2 1 1 3" "/ 80 3 2 1 1 3"
HELP
}

exit_err() {
   echo >&2 "${1}"
   exit 1
}

if [ $# -lt 3 ];then
    useage
    exit 1
fi

TYPE=$1
WORKLOAD=$2
CONTAINER=$3
LIVENESSPROBE_CONFIG=${4:-""}
READINESSPROBE_CONFIG=${5:-""}

SPEC='{"spec": {"template": {"spec": {"containers": [{"name": "'${CONTAINER}'"'

if [ -n "${LIVENESSPROBE_CONFIG}" ];then
  LIVENESSPROBE=(${LIVENESSPROBE_CONFIG})
  LIVENESSPROBE_SPEC='{"httpGet": {"path": "'${LIVENESSPROBE[0]}'","port": '${LIVENESSPROBE[1]}',"scheme": "HTTP"},"initialDelaySeconds": '${LIVENESSPROBE[2]}',"periodSeconds": '${LIVENESSPROBE[3]}',"successThreshold": '${LIVENESSPROBE[4]}',"timeoutSeconds": '${LIVENESSPROBE[5]}',"failureThreshold": '${LIVENESSPROBE[6]}'}'
  SPEC=${SPEC}',"livenessProbe":'${LIVENESSPROBE_SPEC}
else
  SPEC=${SPEC}',"livenessProbe":null'
fi
if [ -n "${READINESSPROBE_CONFIG}" ];then
  READINESSPROBE=(${READINESSPROBE_CONFIG})
  READINESSPROBE_SPEC='{"httpGet": {"path": "'${READINESSPROBE[0]}'","port": '${READINESSPROBE[1]}',"scheme": "HTTP"},"initialDelaySeconds": '${READINESSPROBE[2]}',"periodSeconds": '${READINESSPROBE[3]}',"successThreshold": '${READINESSPROBE[4]}',"timeoutSeconds": '${READINESSPROBE[5]}',"failureThreshold": '${READINESSPROBE[6]}'}'
  SPEC=${SPEC}',"readinessProbe":'${READINESSPROBE_SPEC}
else
  SPEC=${SPEC}',"readinessProbe":null'
fi
SPEC=${SPEC}'}]}}}}'
echo "${SPEC}"
kubectl patch "${TYPE}" "${WORKLOAD}" --patch "${SPEC}"

使用方法

      kubeprobe deploy ui nginx
      kubeprobe deploy ui nginx "" "/ 80 3 2 1 1 3"
      kubeprobe deploy ui nginx "/ 80 10 2 1 1 3" "/ 80 3 2 1 1 3"
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!