はじめに
こちらは、下記で公開したツールを利用したユースケースの1例になります。
IBM CloudのVPCで利用可能な仮想サーバーは、サーバーを停止した状態であればそのプロファイル(vCPU、メモリの組合せ)を変更することが可能です。繁忙期や月次処理を実行する時は処理能力を上げて、通常稼働時は処理能力を抑える運用が出来ると嬉しいですね。そのほうが費用削減にも繋がるでしょう。
しかし、定期的なプロファイルの変更操作が可能かというと、IBM Cloudの標準機能では提供されていないため、利用者で変更操作を実施するプログラムを作成し、それを定期実行する必要があります。
これを作成した汎用操作ツールで実現する方法を紹介します。
Code Engineでの設定
インスタンスを停止、プロファイルを変更、インスタンス起動を合わせて実行するJobです。コマンド実行するだけの環境なので、スペックは最小にしましょう。
- Job Name :
任意の名称
- Image Reference :
docker.io/hiroapps/ibmcloud-cli-image:1.1.0
- Job instances :
1
- Instance resources
- CPU and memory :
0.125 vCPU / 0.25 GB
- Ephemeral storage (GB) :
0.04
- CPU and memory :
- Resiliency settings
- Mode :
Task
- Number of job retries : 3
- Mode :
- Optional settings
- Environment Variables (optional)
-
COMMAND00
:ibmcloud config --check-version=false
-
COMMAND01
:ibmcloud login -- apikey [権限のあるAPIキー] -r [VPCのあるリージョン]
-
COMMAND02
:if [ `ibmcloud is in $INSTANCE_NAME --output json | jq -r ".status"` == "stopped" ]&&[ `ibmcloud is in $INSTANCE_NAME --output json | jq -r ".startable"` == true ]&&[ `ibmcloud is in $INSTANCE_NAME --output json | jq -r ".lifecycle_state"` == "stable" ]; then echo "instance is already stopped" ; exit 0 ; elif [ `ibmcloud is in $INSTANCE_NAME --output json | jq -r ".status"` == "running" ]; then ibmcloud is instance-stop -f $INSTANCE_NAME ; else echo "instance status is not normal" ; exit 1 ; fi
-
COMMAND03
:if [ `ibmcloud is in $INSTANCE_NAME --output json | jq -r ".profile.name"` != ${PROFILE} ]; then while true ; do if [ `ibmcloud is in $INSTANCE_NAME --output json | jq -r ".status"` == "stopped" ]&&[ `ibmcloud is in $INSTANCE_NAME --output json | jq -r ".startable"` == true ]&&[ `ibmcloud is in $INSTANCE_NAME --output json | jq -r ".lifecycle_state"` == "stable" ]; then ibmcloud is instance-update $INSTANCE_NAME --profile $PROFILE ; break ; else echo "5 seconds waiting for instance status" ; sleep 5 ; fi ; done ; else echo "target instance profile is already $PROFILE" ; fi
-
COMMAND04
:while true ; do if [ `ibmcloud is in $INSTANCE_NAME --output json | jq -r ".status"` == "stopped" ]&&[ `ibmcloud is in $INSTANCE_NAME --output json | jq -r ".startable"` == true ]&&[ `ibmcloud is in $INSTANCE_NAME --output json | jq -r ".lifecycle_state"` == "stable" ]; then ibmcloud is instance-start $INSTANCE_NAME ; break ; else echo "5 seconds waiting for instance status" ; sleep 5 ; fi ; done
-
INSTANCE_NAME
:[仮想サーバーのインスタンス名]
-
PROFILE
:[変更したいプロファイル名]
-
- Environment Variables (optional)
2024/3/5 COMMAND00
を追加しました。CLIのバージョンアップを問われるので、それを無視するための実行が必要です。
執筆時点で利用するイメージは docker.io/hiroapps/ibmcloud-cli-image:1.1.0
としていますが、最新のイメージがある場合はそちらを利用ください。
また、利用者でツールのイメージを別のコンテナレジストリに格納している場合はそちらを利用ください。
とりあえず、各コマンドが長くなってしまいました。1つ目はログインするだけですが、2つ目からは以下のShell Scriptを1行で指定しています。
COMMAND02
はサーバーが停止可能な状態か確認して、停止を実行します。
if [ `ibmcloud is in $INSTANCE_NAME --output json | jq -r ".status"` == "stopped" ]&&
[ `ibmcloud is in $INSTANCE_NAME --output json | jq -r ".startable"` == true ]&&
[ `ibmcloud is in $INSTANCE_NAME --output json | jq -r ".lifecycle_state"` == "stable" ];
then
echo "instance is already stopped"
exit 0
elif [ `ibmcloud is in $INSTANCE_NAME --output json | jq -r ".status"` == "running" ];
then
ibmcloud is instance-stop -f $INSTANCE_NAME
else
echo "instance status is not normal"
exit 1
fi
COMMAND03
はインスタンスが停止状態に問題が無いかステータスを確認して問題が無ければ指定されたプロファイルへの変更を実施します。
if [ `ibmcloud is in $INSTANCE_NAME --output json | jq -r ".profile.name"` != ${PROFILE} ];
then
while true
do
if [ `ibmcloud is in $INSTANCE_NAME --output json | jq -r ".status"` == "stopped" ]&&
[ `ibmcloud is in $INSTANCE_NAME --output json | jq -r ".startable"` == true ]&&
[ `ibmcloud is in $INSTANCE_NAME --output json | jq -r ".lifecycle_state"` == "stable" ];
then
ibmcloud is instance-update $INSTANCE_NAME --profile $PROFILE
break
else
echo "5 seconds waiting for instance status"
sleep 5
fi
done
else
echo "target instance profile is already $PROFILE"
fi
COMMAND04
はインスタンスが起動可能状態かを確認して、問題が無ければサーバーを起動します。
while true ;
do
if [ `ibmcloud is in $INSTANCE_NAME --output json | jq -r ".status"` == "stopped" ]&&
[ `ibmcloud is in $INSTANCE_NAME --output json | jq -r ".startable"` == true ]&&
[ `ibmcloud is in $INSTANCE_NAME --output json | jq -r ".lifecycle_state"` == "stable" ];
then
ibmcloud is instance-start $INSTANCE_NAME
break
else
echo "5 seconds waiting for instance status"
sleep 5
fi
done
今回は頻繁にインスタンス名をコマンド内に利用するので、 INSTANCE_NAME
の環境変数を設定して稼働させたほうが良いです。
また、元のプロファイルに戻す観点でも PROFILE
を設定して、変更後のプロファイルを設定するようにすれば良いと思います。
設定可能なプロファイルを確認する方法
ibmcloud
のCLIが実行可能で、VPCの権限がある環境であれば、以下のコマンドで一覧が確認できます。
ibmcloud is instance-profiles
コマンドに渡すプロファイルの名前は、下記コマンド結果の一覧で一番左にある Name
の項目を指定するようにしてください。
# ibmcloud is instance-profiles
Listing instance profiles in region jp-tok under account IBM as user ユーザー名...
Name vCPU Manufacturer Architecture Family vCPUs Memory(GiB) Bandwidth(Mbps) Volume bandwidth(Mbps) GPUs Storage(GB) Min NIC Count
Max NIC Count Status
bx2-2x8 intel amd64 balanced 2 8 4000 1000 - - 1
5 current
bx2d-2x8 intel amd64 balanced 2 8 4000 1000 - 1x75 1
5 current
bx2-4x16 intel amd64 balanced 4 16 8000 2000 - - 1
5 current
bx2d-4x16 intel amd64 balanced 4 16 8000 2000 - 1x150 1
5 current
bx2-8x32 intel amd64 balanced 8 32 16000 4000 - - 1
5 current
bx2d-8x32 intel amd64 balanced 8 32 16000 4000 - 1x300 1
5 current
bx2-16x64 intel amd64 balanced 16 64 32000 8000 - - 1
5 current
bx2d-16x64 intel amd64 balanced 16 64 32000 8000 - 1x600 1
5 current
bx2-32x128 intel amd64 balanced 32 128 64000 16000 - - 1
10 current
bx2d-32x128 intel amd64 balanced 32 128 64000 16000 - 2x600 1
10 current
bx2-48x192 intel amd64 balanced 48 192 80000 20000 - - 1
10 current
bx2d-48x192 intel amd64 balanced 48 192 80000 20000 - 2x900 1
10 current
bx2-64x256 intel amd64 balanced 64 256 80000 20000 - - 1
15 current
bx2d-64x256 intel amd64 balanced 64 256 80000 20000 - 2x1200 1
15 current
bx2-96x384 intel amd64 balanced 96 384 80000 20000 - - 1
15 current
bx2d-96x384 intel amd64 balanced 96 384 80000 20000 - 2x1800 1
15 current
bx2-128x512 intel amd64 balanced 128 512 80000 20000 - - 1
15 current
bx2d-128x512 intel amd64 balanced 128 512 80000 20000 - 2x2400 1
15 current
cx2-2x4 intel amd64 compute 2 4 4000 1000 - - 1
5 current
cx2d-2x4 intel amd64 compute 2 4 4000 1000 - 1x75 1
5 current
cx2-4x8 intel amd64 compute 4 8 8000 2000 - - 1
5 current
cx2d-4x8 intel amd64 compute 4 8 8000 2000 - 1x150 1
5 current
cx2-8x16 intel amd64 compute 8 16 16000 4000 - - 1
5 current
cx2d-8x16 intel amd64 compute 8 16 16000 4000 - 1x300 1
5 current
cx2-16x32 intel amd64 compute 16 32 32000 8000 - - 1
5 current
cx2d-16x32 intel amd64 compute 16 32 32000 8000 - 1x600 1
5 current
cx2-32x64 intel amd64 compute 32 64 64000 16000 - - 1
10 current
cx2d-32x64 intel amd64 compute 32 64 64000 16000 - 2x600 1
10 current
cx2-48x96 intel amd64 compute 48 96 80000 20000 - - 1
10 current
cx2d-48x96 intel amd64 compute 48 96 80000 20000 - 2x900 1
10 current
cx2-64x128 intel amd64 compute 64 128 80000 20000 - - 1
15 current
cx2d-64x128 intel amd64 compute 64 128 80000 20000 - 2x1200 1
15 current
cx2-96x192 intel amd64 compute 96 192 80000 20000 - - 1
15 current
cx2d-96x192 intel amd64 compute 96 192 80000 20000 - 2x1800 1
15 current
cx2-128x256 intel amd64 compute 128 256 80000 20000 - - 1
15 current
cx2d-128x256 intel amd64 compute 128 256 80000 20000 - 2x2400 1
15 current
gx2-8x64x1v100 intel amd64 gpu-v100 8 64 16000 4000 1 - 1
5 current
gx2-16x128x1v100 intel amd64 gpu-v100 16 128 32000 8000 1 - 1
5 current
gx2-16x128x2v100 intel amd64 gpu-v100 16 128 32000 8000 2 - 1
5 current
gx2-32x256x2v100 intel amd64 gpu-v100 32 256 64000 16000 2 - 1
10 current
ux2d-2x56 intel amd64 high-memory 2 56 4000 1000 - 1x60 1
5 current
ux2d-4x112 intel amd64 high-memory 4 112 8000 2000 - 1x120 1
5 current
ux2d-8x224 intel amd64 high-memory 8 224 16000 4000 - 1x240 1
5 current
ux2d-16x448 intel amd64 high-memory 16 448 32000 8000 - 1x480 1
5 current
ux2d-36x1008 intel amd64 high-memory 36 1008 72000 18000 - 1x1080 1
10 current
ux2d-48x1344 intel amd64 high-memory 48 1344 80000 20000 - 2x720 1
10 current
ux2d-72x2016 intel amd64 high-memory 72 2016 80000 20000 - 2x1080 1
15 current
ux2d-100x2800 intel amd64 high-memory 100 2800 80000 20000 - 2x1500 1
15 current
ux2d-200x5600 intel amd64 high-memory 200 5600 80000 20000 - 2x3000 1
15 current
mx2-2x16 intel amd64 memory 2 16 4000 1000 - - 1
5 current
mx2d-2x16 intel amd64 memory 2 16 4000 1000 - 1x75 1
5 current
mx2-4x32 intel amd64 memory 4 32 8000 2000 - - 1
5 current
mx2d-4x32 intel amd64 memory 4 32 8000 2000 - 1x150 1
5 current
mx2-8x64 intel amd64 memory 8 64 16000 4000 - - 1
5 current
mx2d-8x64 intel amd64 memory 8 64 16000 4000 - 1x300 1
5 current
mx2-16x128 intel amd64 memory 16 128 32000 8000 - - 1
5 current
mx2d-16x128 intel amd64 memory 16 128 32000 8000 - 1x600 1
5 current
mx2-32x256 intel amd64 memory 32 256 64000 16000 - - 1
10 current
mx2d-32x256 intel amd64 memory 32 256 64000 16000 - 2x600 1
10 current
mx2-48x384 intel amd64 memory 48 384 80000 20000 - - 1
10 current
mx2d-48x384 intel amd64 memory 48 384 80000 20000 - 2x900 1
10 current
mx2-64x512 intel amd64 memory 64 512 80000 20000 - - 1
15 current
mx2d-64x512 intel amd64 memory 64 512 80000 20000 - 2x1200 1
15 current
mx2-96x768 intel amd64 memory 96 768 80000 20000 - - 1
15 current
mx2d-96x768 intel amd64 memory 96 768 80000 20000 - 2x1800 1
15 current
mx2-128x1024 intel amd64 memory 128 1024 80000 20000 - - 1
15 current
mx2d-128x1024 intel amd64 memory 128 1024 80000 20000 - 2x2400 1
15 current
ox2-16x128 intel amd64 storage-optimized 16 128 32000 8000 - 2x2400 1
5 current
vx2d-2x28 intel amd64 very-high-memory 2 28 4000 1000 - 1x60 1
5 current
vx2d-4x56 intel amd64 very-high-memory 4 56 8000 2000 - 1x120 1
5 current
vx2d-8x112 intel amd64 very-high-memory 8 112 16000 4000 - 1x240 1
5 current
vx2d-16x224 intel amd64 very-high-memory 16 224 32000 8000 - 1x480 1
5 current
vx2d-44x616 intel amd64 very-high-memory 44 616 80000 20000 - 1x1320 1
10 current
vx2d-88x1232 intel amd64 very-high-memory 88 1232 80000 20000 - 2x1320 1
15 current
vx2d-144x2016 intel amd64 very-high-memory 144 2016 80000 20000 - 2x2160 1
15 current
vx2d-176x2464 intel amd64 very-high-memory 176 2464 80000 20000 - 2x2640 1
15 current
bz2-1x4 ibm s390x balanced 1 4 2000 500 - - 1
5 current
bz2e-1x4 ibm s390x balanced 1 4 2000 500 - - 1
5 current
bz2-2x8 ibm s390x balanced 2 8 4000 1000 - - 1
5 current
bz2e-2x8 ibm s390x balanced 2 8 4000 1000 - - 1
5 current
bz2-4x16 ibm s390x balanced 4 16 8000 2000 - - 1
5 current
bz2e-4x16 ibm s390x balanced 4 16 8000 2000 - - 1
5 current
bz2-8x32 ibm s390x balanced 8 32 16000 4000 - - 1
5 current
bz2e-8x32 ibm s390x balanced 8 32 16000 4000 - - 1
5 current
bz2-16x64 ibm s390x balanced 16 64 32000 8000 - - 1
5 current
bz2e-16x64 ibm s390x balanced 16 64 32000 8000 - - 1
5 current
cz2-2x4 ibm s390x compute 2 4 4000 1000 - - 1
5 current
cz2e-2x4 ibm s390x compute 2 4 4000 1000 - - 1
5 current
cz2-4x8 ibm s390x compute 4 8 8000 2000 - - 1
5 current
cz2e-4x8 ibm s390x compute 4 8 8000 2000 - - 1
5 current
cz2-8x16 ibm s390x compute 8 16 16000 4000 - - 1
5 current
cz2e-8x16 ibm s390x compute 8 16 16000 4000 - - 1
5 current
cz2-16x32 ibm s390x compute 16 32 32000 8000 - - 1
5 current
cz2e-16x32 ibm s390x compute 16 32 32000 8000 - - 1
5 current
mz2-2x16 ibm s390x memory 2 16 4000 1000 - - 1
5 current
mz2e-2x16 ibm s390x memory 2 16 4000 1000 - - 1
5 current
mz2-4x32 ibm s390x memory 4 32 8000 2000 - - 1
5 current
mz2e-4x32 ibm s390x memory 4 32 8000 2000 - - 1
5 current
mz2-8x64 ibm s390x memory 8 64 16000 4000 - - 1
5 current
mz2e-8x64 ibm s390x memory 8 64 16000 4000 - - 1
5 current
mz2-16x128 ibm s390x memory 16 128 32000 8000 - - 1
5 current
mz2e-16x128 ibm s390x memory 16 128 32000 8000 - - 1
5 current
実行してみよう
さて、上記の権限設定に問題なければ、先ほど指定した環境変数のコマンドに従ってVPCのインスタンスに対してプロファイル変更操作が実行されます。
Jobを作成して、Submit Job
でJobを手動実行してみましょう。
以下はJobの実行ログをLogDNAのサービスで確認したものです。
Jan 25 13:01:26 Code Engine cli-test-jobrun-55fs9-0-0 ## COMMAND01 ##
Jan 25 13:01:26 Code Engine cli-test-jobrun-55fs9-0-0 $ ibmcloud login *****
Jan 25 13:01:26 Code Engine cli-test-jobrun-55fs9-0-0 API endpoint: https://cloud.ibm.com
Jan 25 13:01:26 Code Engine cli-test-jobrun-55fs9-0-0 Authenticating...
Jan 25 13:01:26 Code Engine cli-test-jobrun-55fs9-0-0 OK
Jan 25 13:01:26 Code Engine cli-test-jobrun-55fs9-0-0 Targeted account IBM (アカウントID)
Jan 25 13:01:36 Code Engine cli-test-jobrun-55fs9-0-0 Targeted region jp-tok
Jan 25 13:01:36 Code Engine cli-test-jobrun-55fs9-0-0 API endpoint: https://cloud.ibm.com
Jan 25 13:01:36 Code Engine cli-test-jobrun-55fs9-0-0 Region: jp-tok
Jan 25 13:01:36 Code Engine cli-test-jobrun-55fs9-0-0 User: ServiceId-サービスID (サービスID名)
Jan 25 13:01:36 Code Engine cli-test-jobrun-55fs9-0-0 Account: IBM (アカウントID)
Jan 25 13:01:36 Code Engine cli-test-jobrun-55fs9-0-0 Resource group: No resource group targeted, use 'ibmcloud target -g RESOURCE_GROUP'
Jan 25 13:01:36 Code Engine cli-test-jobrun-55fs9-0-0 CF API endpoint:
Jan 25 13:01:36 Code Engine cli-test-jobrun-55fs9-0-0 Org:
Jan 25 13:01:36 Code Engine cli-test-jobrun-55fs9-0-0 Space:
Jan 25 13:01:36 Code Engine cli-test-jobrun-55fs9-0-0 ## COMMAND02 ##
Jan 25 13:01:36 Code Engine cli-test-jobrun-55fs9-0-0 $ if [ `ibmcloud is in $INSTANCE_NAME --output json | jq -r ".status"` == "stopped" ]&&[ `ibmcloud is in $INSTANCE_NAME --output json | jq -r ".startable"` == true ]&&[ `ibmcloud is in $INSTANCE_NAME --output json | jq -r ".lifecycle_state"` == "stable" ]; then echo "instance is already stopped" ; exit 0 ; elif [ `ibmcloud is in $INSTANCE_NAME --output json | jq -r ".status"` == "running" ]; then ibmcloud is instance-stop -f $INSTANCE_NAME ; else echo "instance status is not normal" ; exit 1 ; fi
Jan 25 13:02:06 Code Engine cli-test-jobrun-55fs9-0-0 Creating action stop for instance インスタンス名 under account IBM as user サービスID...
Jan 25 13:02:06 Code Engine cli-test-jobrun-55fs9-0-0 Type stop
Jan 25 13:02:06 Code Engine cli-test-jobrun-55fs9-0-0 Created 2024-01-25T04:02:04+00:00
Jan 25 13:02:06 Code Engine cli-test-jobrun-55fs9-0-0 ## COMMAND03 ##
Jan 25 13:02:06 Code Engine cli-test-jobrun-55fs9-0-0 $ if [ `ibmcloud is in $INSTANCE_NAME --output json | jq -r ".profile.name"` != ${PROFILE} ]; then while true ; do if [ `ibmcloud is in $INSTANCE_NAME --output json | jq -r ".status"` == "stopped" ]&&[ `ibmcloud is in $INSTANCE_NAME --output json | jq -r ".startable"` == true ]&&[ `ibmcloud is in $INSTANCE_NAME --output json | jq -r ".lifecycle_state"` == "stable" ]; then ibmcloud is instance-update $INSTANCE_NAME --profile $PROFILE ; break ; else echo "5 seconds waiting for instance status" ; sleep 5 ; fi ; done ; else echo "target instance profile is already $PROFILE" ; fi
Jan 25 13:02:56 Code Engine cli-test-jobrun-55fs9-0-0 Updating instanceインスタンス名 under account IBM as user ServiceId-サービスID...
Jan 25 13:02:56 Code Engine cli-test-jobrun-55fs9-0-0 ID 02f7_75e5e585-13b1-41fb-931c-c9ab01ff1b72
Jan 25 13:02:56 Code Engine cli-test-jobrun-55fs9-0-0 Name インスタンス名
Jan 25 13:02:56 Code Engine cli-test-jobrun-55fs9-0-0 CRN crn:v1:bluemix:public:is:jp-tok-2:a/アカウントID::instance:インスタンスID
Jan 25 13:02:56 Code Engine cli-test-jobrun-55fs9-0-0 Status stopped
Jan 25 13:02:56 Code Engine cli-test-jobrun-55fs9-0-0 Availability policy on host failure restart
Jan 25 13:02:56 Code Engine cli-test-jobrun-55fs9-0-0 Startable true
Jan 25 13:02:56 Code Engine cli-test-jobrun-55fs9-0-0 Profile bx2-16x64
Jan 25 13:02:56 Code Engine cli-test-jobrun-55fs9-0-0 Architecture amd64
Jan 25 13:02:56 Code Engine cli-test-jobrun-55fs9-0-0 vCPU Manufacturer intel
Jan 25 13:02:56 Code Engine cli-test-jobrun-55fs9-0-0 vCPUs 16
Jan 25 13:02:56 Code Engine cli-test-jobrun-55fs9-0-0 Memory(GiB) 64
Jan 25 13:02:56 Code Engine cli-test-jobrun-55fs9-0-0 Bandwidth(Mbps) 32000
Jan 25 13:02:56 Code Engine cli-test-jobrun-55fs9-0-0 Volume bandwidth(Mbps) 1000
Jan 25 13:02:56 Code Engine cli-test-jobrun-55fs9-0-0 Network bandwidth(Mbps) 31000
Jan 25 13:02:56 Code Engine cli-test-jobrun-55fs9-0-0 Lifecycle Reasons Code Message
- -
Jan 25 13:02:56 Code Engine cli-test-jobrun-55fs9-0-0 Lifecycle State updating
Jan 25 13:02:56 Code Engine cli-test-jobrun-55fs9-0-0 Metadata service Enabled Protocol Response hop limit
false http 1
Jan 25 13:02:56 Code Engine cli-test-jobrun-55fs9-0-0 Image ID Name
イメージID ibm-centos-7-9-minimal-amd64-12
Jan 25 13:02:56 Code Engine cli-test-jobrun-55fs9-0-0 Numa Count 1
Jan 25 13:02:56 Code Engine cli-test-jobrun-55fs9-0-0 VPC ID Name
VPCID hkataoka-vpc-tok
Jan 25 13:02:56 Code Engine cli-test-jobrun-55fs9-0-0 Zone jp-tok-2
Jan 25 13:02:56 Code Engine cli-test-jobrun-55fs9-0-0 Resource group ID Name
リソースグループID unknown
Jan 25 13:02:56 Code Engine cli-test-jobrun-55fs9-0-0 Created 2024-01-23T01:59:50+00:00
Jan 25 13:02:56 Code Engine cli-test-jobrun-55fs9-0-0 Boot volume ID Name Attachment ID Attachment name
ボリュームID インスタンス名-boot-1705975128000 アタッチメントID worrier-chatting-enactment-retouch
Jan 25 13:02:56 Code Engine cli-test-jobrun-55fs9-0-0 ## COMMAND04 ##
Jan 25 13:02:56 Code Engine cli-test-jobrun-55fs9-0-0 $ while true ; do if [ `ibmcloud is in $INSTANCE_NAME --output json | jq -r ".status"` == "stopped" ]&&[ `ibmcloud is in $INSTANCE_NAME --output json | jq -r ".startable"` == true ]&&[ `ibmcloud is in $INSTANCE_NAME --output json | jq -r ".lifecycle_state"` == "stable" ]; then ibmcloud is instance-start $INSTANCE_NAME ; break ; else echo "5 seconds waiting for instance status" ; sleep 5 ; fi ; done
Jan 25 13:03:26 Code Engine cli-test-jobrun-55fs9-0-0 Creating action start for instance インスタンス名 under account IBM as user ServiceId-サービスID...
Jan 25 13:03:26 Code Engine cli-test-jobrun-55fs9-0-0 Type start
Jan 25 13:03:26 Code Engine cli-test-jobrun-55fs9-0-0 Created 2024-01-25T04:03:24+00:00
問題なく仮想サーバーが停止された後にプロファイルが変更され、起動操作も問題なく実行できました。
これで仮想サーバーのプロファイルを変更するJobが作成できました。
今回はリソースを拡張するJobを作成したのですが、縮小する場合は PROFILE
を元に戻す値を設定した戻すためのJobを作成してください。でないと拡張したままになってしまうので。
Event Subsctiptionへの登録
Event Subscriptionへの登録は次のリンク記事の途中から記載しているので、そちらを参考にしてください。
cron形式で起動する時間を指定できるので、毎月xx日にプロファイルを拡張するJobを登録して、翌日に縮小するJobを登録すれば良いでしょう。
こちらの紹介内容だとIBM Cloud側の操作でプロファイル変更後に起動するだけなので、サーバー内の特定のサービスを起動するように指定することは出来ないです。
サーバー側で起動時にサービスを立ち上げるような設定を行うことを推奨します
プロファイル変更権限が無い場合
直近に作成した下記の記事では、VPCのインスタンスに対する起動、停止のみなので、APIキーに必要な権限は Operator
権限があれば良いのですが、インスタンスのプロファイルを変更する場合は Editor
権限が必要になります。なので、先の記事の権限だけ付与していた場合は動かないので注意が必要です。というか、私はそれで1度嵌りました。
今回紹介した設定で、 Editor
の権限が無い場合は、プロファイルの変更は失敗しますが、インスタンスを停止した後のインスタンス起動コマンドは実行されるので、仮に権限設定を誤ってプロファイル変更に失敗したとしても、インスタンスが起動されないままになる、ということは無いので安心頂ければと思います。
さいごに
いかがでしたでしょうか?
IBM Cloud汎用操作ツールで、VPC環境をなるべく費用節約する形で運用できる可能性が実現できました。ぜひ、このユースケースを参考にIBM Cloudで理想的な運用ができると幸いです。