我正在使用 Google Cloud / Google Container Engine,並且有基礎架構腳本,可以在其中建立叢集和節點池,然後在設定後對節點池執行操作。
在我的腳本中,我需要確保節點池已設定並且叢集處於就緒狀態,然後再繼續。
看起來該gcloud container node-pools create
指令沒有--wait
or--no-async
選項,與非常相似的gcloud container node-pools delete
.有這樣的選擇嗎create
?否則,是否有建議的方法來「等待」直到節點池準備好?
我相信我的bash腳本中,在創建節點池後,我可以做一個while循環,定期檢查eg的值,gcloud container clusters describe myclustername --zone myzone | tail -n 2 | grep "status" | awk '{print $2}'
直到我得到「RUNNING
」回來,但也許有一個更優雅的方法?
(如果在建立和刪除節點池的選項中具有相同的選項那就太好了!)
答案1
截至撰寫本文時gcloud container node-pools create
命令預設是同步的,但沒有--async
or--no-wait
選項。從 shell 腳本編寫的角度來看,這並不是那麼糟糕,因為它很容易將命令置於後台,並且可以解決您的特定問題。
處理原始行為的另一種方法是使用--log-http
獲取操作 ID 並將其提供給gcloud container operations wait
(這有點混亂並且需要抓取輸出)。這表示另一個值得擁有的功能,即非同步命令回顯操作 ID。
答案2
我創建了這個腳本,它將等待任何尚未完成的容器操作。
等待容器操作.sh
#!/bin/bash
# This scripts runs gcloud container `operations describe` and `wait` for all found operations with `list --filter=STATUS!=DONE`
# API outlined here https://cloud.google.com/sdk/gcloud/reference/compute/operations/
set -euo pipefail
IFS=$'\n\t'
source_dir="$(dirname "$0")";
current_dir="$(pwd)";
echo "Listing, describing and awaiting NOT-DONE container-operations";
function sourceClusterZone(){
cd $source_dir;
source ./cluster_zone.sh;
cd $current_dir;
}
queryNotDone(){ gcloud container operations list --filter=STATUS!=DONE --sort-by='~START_TIME'; }
listNotDone(){ queryNotDone | awk '{if (NR!=1) {print $1;}}'; }
sleep 2;
LISTNOTDONE=(`listNotDone`);
echo "\""${LISTNOTDONE[@]}"\"";
if (( ${#LISTNOTDONE[@]} )); then
sourceClusterZone;
for notDone in ${LISTNOTDONE[@]}
do
echo "Waiting for $notDone";
gcloud container operations describe $notDone --zone="${ZONE}";
gcloud container operations wait $notDone --zone="${ZONE}";
echo "Done with $notDone";
done
else
echo 'Not Waiting';
fi
cluster_zone.sh(在同一目錄中)
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
kubeClusterOptions(){ kubectl config current-context | awk -F '_' 'BEGIN { ORS=" " }; {print $4} {print $3}'; }
declare -a OPTIONS;
IFS=' ' read -a OPTIONS <<< `kubeClusterOptions`; IFS=$'\n\t';
while getopts c:z: option
do
case "${option}"
in
c) CLUSTER=${OPTARG:-${OPTIONS[0]}};;
z) ZONE=${OPTARG:-${OPTIONS[1]}};;
esac
done
export CLUSTER=${CLUSTER:-${OPTIONS[0]}};
export ZONE=${ZONE:-${OPTIONS[1]}};
-c YOUR_CLUSTER
您可以使用和來配置具有自訂叢集和區域的腳本-z YOUR_ZONE
。kubectl config current-context
如果您未指定任何配置,它將取得配置。