K8S 應(yīng)用啟停通用腳本

Kubernetes 集群中運(yùn)行的應(yīng)用中的每一個(gè)服務(wù)組件通常是以 Deployment 的形式存在的,本文中提供的管理腳本假設(shè)讀者部署在 Kubernetes 中的應(yīng)用服務(wù)的 Deployment 對象均已特定的前綴命名,比如 demo,那么集群中可能存在一下的 Deployment 對象:

  • demo-register
  • demo-gateway
  • demo-oauth
  • demo-config
  • demo-swagger
  • ...

在這個(gè)前提下,我這里提供了一個(gè)腳本可以對這些 deployment 對象進(jìn)行一鍵啟停操作。舉例說明,加絨我的腳本名稱為 k8s-apps.sh 那么可以執(zhí)行如下命令:

# 啟動(dòng)所有應(yīng)用,default 為命名空間
./k8s-apps.sh start default

# 停止所有應(yīng)用
./k8s-apps.sh stop default

啟停腳本的內(nèi)容如下:

#!/usr/bin/env bash
#
# Copyright 2020 Liu Hongyu (eliuhy@163.com)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
set -e

prefix="demo"        # pattern that names of deployment start with
GRN="\e[32m"        # green color
YLW="\e[33m"        # yellow color
RED="\e[91m"        # red color
RST="\e[39m"        # reset color

# --- common functions definition ---
info() { echo -e "$GRN[INFO]$RST" $@
}
warn() { echo -e "$YLW[WARN]$RST" $@
}
fata() { echo -e "$RED[FATA]$RST" $@; exit 1
}
has_command() { command -v $@ >/dev/null
}

# --- print help ---
show_help() {
echo -e "
USAGE:
  ${GRN}${PROG}${RST} ${YLW}start${RST}|${YLW}stop${RST} -n|--namespace <${YLW}namespace${RST}>
"
exit 0
}

# --- Check dependencies and arguments ---
pre_check() {
  has_command kubectl || fata "This script needs 'kubectl' installed on your computer."
  [ -n "$action"    ] || fata "Need provide at least one action: start|stop"
  [[ $action =~ ^(start|stop)$ ]] || fata "Invalid action '$action'. Use one of the following actions: start|stop"
  [ -n "$namespace" ] || fata "Need provide namespace!"
  kubectl get ns | grep $namespace >/dev/null || fata "'$namespace' not found in current cluster."
  deployments=$(kubectl get deploy -n $namespace | grep ^$prefix | awk '{print $1}')
}

scale() {
  kubectl -n $namespace scale --current-replicas=$2 --replicas=$3 deployment/$1
}

scale_apps() {
  for app in $deployments; do
    scale $app $1 $2
  done
}

{
  PROG="$(basename $0)"
  PARAMS=""
  while [ $# -gt 0 ]; do
    case "$1" in
      -n|--namespace)
        namespace=$2
        shift 2
        ;;
      -h|--help)
        show_help
        ;;
      -*|--*)
        fata "Invalid option '$1'"
        ;;
      *)
        PARAMS="$1 $PARAMS"
        shift
        ;;
    esac
  done
  eval set -- "$PARAMS"
  action=$1

  pre_check

  [[ $action = "start" ]] && scale_apps 0 1 # scale up replicas from 0 to 1
  [[ $action = "stop"  ]] && scale_apps 1 0 # scale down replicas from 1 to 0
  exit 0
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容