# kubectl rollout status deployment/probe-busybox -n tenant-test Waiting for rollout to finish: 2 out of 3 new replicas have been updated... deployment "probe-busybox" successfully rolled out
# kubectl get deployments deployment/probe-busybox -n tenant-test NAME READY UP-TO-DATE AVAILABLE AGE probe-busybox 3/3 3 3 18s
那么在这种情况下,Helm 的等价物是什么?
--wait Helm 将等到部署中启动了最少预期数量的 Pod,然后再将发布标记为成功。
--timeout --wait 超时时间时间,默认情况下,超时时间为 300 秒。
1 2 3 4 5 6 7
$ helm upgrade --install --wait --timeout 30s probe-busybox probe-busybox/ -n tenant-test Release "probe-busybox" does not exist. Installing it now. NAME: probe-busybox LAST DEPLOYED: Tue Mar 14 15:04:38 2023 NAMESPACE: tenant-test STATUS: deployed REVISION: 1
部署按预期成功完成。让我们看看当我们尝试使用失败的部署时会发生什么。设置就绪检查错误端口
1 2 3 4 5 6 7 8
$ helm upgrade --install --wait --timeout 30s probe-busybox probe-busybox/ -n tenant-test --set readinessProbe.tcpSocket.port=8081 Error: UPGRADE FAILED: timed out waiting for the condition
$ helm history probe-busybox -n tenant-test REVISION UPDATED STATUS CHART APP VERSION DESCRIPTION 1 Tue Mar 14 15:04:38 2023 deployed chart-tpl-0.1.0 1.16.0 Install complete 2 Tue Mar 14 15:07:49 2023 failed chart-tpl-0.1.0 1.16.0 Upgrade "probe-busybox" failed: timed out waiting for the condition
回滚
手动部署一个正确的 release
1 2 3 4 5 6 7 8 9 10 11 12 13
$ helm upgrade --install --wait --timeout 30s probe-busybox probe-busybox/ -n tenant-test Release "probe-busybox" has been upgraded. Happy Helming! NAME: probe-busybox LAST DEPLOYED: Tue Mar 14 15:10:10 2023 NAMESPACE: tenant-test STATUS: deployed REVISION: 3
$ helm history probe-busybox -n tenant-test REVISION UPDATED STATUS CHART APP VERSION DESCRIPTION 1 Tue Mar 14 15:04:38 2023 superseded chart-tpl-0.1.0 1.16.0 Install complete 2 Tue Mar 14 15:07:49 2023 failed chart-tpl-0.1.0 1.16.0 Upgrade "probe-busybox" failed: timed out waiting for the condition 3 Tue Mar 14 15:10:10 2023 deployed chart-tpl-0.1.0 1.16.0 Upgrade complete
手动回滚helm rollback 将版本回滚到以前的版本
1 2 3 4 5 6 7 8 9
$ helm rollback --wait --timeout 30s probe-busybox 1 -n tenant-test Rollback was a success! Happy Helming!
$ helm history probe-busybox -n tenant-test REVISION UPDATED STATUS CHART APP VERSION DESCRIPTION 1 Tue Mar 14 15:04:38 2023 superseded chart-tpl-0.1.0 1.16.0 Install complete 2 Tue Mar 14 15:07:49 2023 failed chart-tpl-0.1.0 1.16.0 Upgrade "probe-busybox" failed: timed out waiting for the condition 3 Tue Mar 14 15:10:10 2023 superseded chart-tpl-0.1.0 1.16.0 Upgrade complete 4 Tue Mar 14 15:13:07 2023 deployed chart-tpl-0.1.0 1.16.0 Rollback to 1
自动回滚--atomic 在升级失败的情况下回滚所做的更改
1 2 3 4 5 6 7 8 9 10 11
$ helm upgrade --install --atomic --timeout 30s probe-busybox probe-busybox/ -n tenant-test --set readinessProbe.tcpSocket.port=8081 Error: UPGRADE FAILED: release probe-busybox failed, and has been rolled back due to atomic being set: timed out waiting for the condition
$ helm history probe-busybox -n tenant-test REVISION UPDATED STATUS CHART APP VERSION DESCRIPTION 1 Tue Mar 14 15:04:38 2023 superseded chart-tpl-0.1.0 1.16.0 Install complete 2 Tue Mar 14 15:07:49 2023 failed chart-tpl-0.1.0 1.16.0 Upgrade "probe-busybox" failed: timed out waiting for the condition 3 Tue Mar 14 15:10:10 2023 superseded chart-tpl-0.1.0 1.16.0 Upgrade complete 4 Tue Mar 14 15:13:07 2023 superseded chart-tpl-0.1.0 1.16.0 Rollback to 1 5 Tue Mar 14 15:15:10 2023 failed chart-tpl-0.1.0 1.16.0 Upgrade "probe-busybox" failed: timed out waiting for the condition 6 Tue Mar 14 15:15:43 2023 deployed chart-tpl-0.1.0 1.16.0 Rollback to 4
问题
Helm 支持开箱即用的检查部署和自动回滚,但有下面两个问题。
没有提供类似于kubectl rollout status 检查部署的状态,echo $?返回为 0 代表成功的命令,可以通过轮询 helm status 检测 status 为 deployed (有点麻烦)。
functionvaluesPrase() { local filepath=$1 local local_type=$(dirname"${filepath}" | cut -d '/' -f 2)
local local_app_name=$(basename"${filepath}" | cut -d '.' -f 1) for image in $(cat${RELEASE_DIR}/images/java_images.txt ${RELEASE_DIR}/images/other_images.txt); do temp_app_name=$(echo${image} | cut -d ':' -f 1) if [[ "${local_app_name}" == "${temp_app_name}" ]]; then helmUpgrade "${local_app_name}""${image}" break fi done }
functionhelmUpgrade() { local local_app_name=$1 local local_image=$2
cd"${HELM_DIR}/conf/values" || exit 1 local local_filepath=$(find . -type f -maxdepth 2 -mindepth 2 -name "${local_app_name}.yaml") [ -z "${local_filepath}" ] && echo"${local_app_name} not found" && exit 1 local local_type=$(dirname"${local_filepath}" | cut -d '/' -f 2)
cd"${HELM_DIR}" || exit 1
HELM_COMMAND="helm secrets upgrade --install" if [[ "${DEBUG_ENABLE}" = true ]]; then HELM_COMMAND="${HELM_COMMAND} --debug" fi HELM_COMMAND="${HELM_COMMAND} -n ${NAMESPACE}" HELM_COMMAND="${HELM_COMMAND} -f ./conf/values/common.yaml" HELM_COMMAND="${HELM_COMMAND} -f ./conf/values/${local_type}/${local_app_name}.yaml" local secret_file="./conf/secrets/secrets.${local_app_name}.yaml" if [[ -f "$secret_file" ]]; then HELM_COMMAND="${HELM_COMMAND} -f ${secret_file}" fi HELM_COMMAND="${HELM_COMMAND}${local_app_name} ./chart-tpl" HELM_COMMAND="${HELM_COMMAND} --set image.address=${IMAGE_PREFIX}/${local_image}" HELM_COMMAND="${HELM_COMMAND}${COMMAND_OPT}"
echo"${HELM_COMMAND}" eval"${HELM_COMMAND}" }
if [ "${APP_IMAGE}" ]; then # 指定镜像启动 helmUpgrade "${APP_NAME}""${APP_IMAGE}" elif [ "${RELEASE_VERSION}" ]; then cd"${HELM_DIR}/conf/values" || exit 1 # 指定迭代启动 if [[ "${APP_NAME}" =~ "-" ]]; then filepath=$(find . -type f -maxdepth 2 -mindepth 2 -name "${APP_NAME}.yaml") [ -z "${filepath}" ] && echo"${APP_NAME} not found" && exit 1 valuesPrase "${filepath}" elif [[ "${APP_NAME}" == "all" ]]; then filepaths=$(find . -type f -maxdepth 2 -mindepth 2 -name "*.yaml") for filepath in${filepaths} do valuesPrase "${filepath}" done else filepaths=$(find "./${APP_NAME}" -type f -name "*.yaml") for filepath in${filepaths} do valuesPrase "${filepath}" done fi else echo"-r 版本迭代、-i 镜像 至少需要一个" exit 1 fi