私は Google Cloud / Google Container Engine を使用しており、クラスタとノードプールを作成し、セットアップ後にノードプールに対して操作を実行するインフラストラクチャ スクリプトがあります。
私のスクリプトでは、続行する前に、ノード プールが設定され、クラスターが準備完了状態であることを確認する必要があります。
非常によく似た とは異なり、コマンドにはまたはオプションがgcloud container node-pools create
ないようです。 にはそのようなオプションがありますか? または、ノード プールの準備ができるまで「待機」する推奨される方法はありますか?--wait
--no-async
gcloud container node-pools delete
create
gcloud container clusters describe myclustername --zone myzone | tail -n 2 | grep "status" | awk '{print $2}'
私の bash スクリプトでは、ノード プールを作成した後、while ループを実行して、たとえば" " が返されるまで定期的に値をチェックできると思いますRUNNING
が、もっとエレガントな方法があるでしょうか?
(ノード プールの作成と削除のオプションが同等であれば便利です!)
答え1
本稿執筆時点ではgcloud container node-pools create
--async
コマンドはデフォルトで sync ですが、 orオプションがありません--no-wait
。シェル スクリプトの観点からは、コマンドをバックグラウンドで実行するのは簡単なので、これはそれほど悪いことではありません。また、特定の問題も解決します。
--log-http
元の動作に対処する別の方法としては、 を使用して操作 ID を取得し、それを に渡すという方法がありますgcloud container operations wait
(これは少し面倒で、出力をスクレイピングする必要があります)。これは、非同期コマンドが操作 ID をエコーするという、もう 1 つの便利な機能を示唆しています。
答え2
まだ完了していないコンテナ操作を待機するスクリプトを作成しました。
コンテナオペレーションを待機します。
#!/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