为了减少与运行 google-cloud-composer 相关的费用,我想知道是否有可能在特定时间关闭运行虚拟环境的 VM 实例。例如:我们的大多数 DAG 都在早上或下午运行,因此我们希望在夜间关闭 VM,如果可能的话甚至在中午关闭。我知道我们可以从谷歌云控制台手动禁用环境,但是找到一种自动执行此操作的方法会很棒
谢谢!
为了减少与运行 google-cloud-composer 相关的费用,我想知道是否有可能在特定时间关闭运行虚拟环境的 VM 实例。例如:我们的大多数 DAG 都在早上或下午运行,因此我们希望在夜间关闭 VM,如果可能的话甚至在中午关闭。我知道我们可以从谷歌云控制台手动禁用环境,但是找到一种自动执行此操作的方法会很棒
谢谢!
不幸的是,无法使用 Google Cloud Platform 以编程方式进行配置。您最好的选择是让脚本作为来自另一台主机的 cronjob 运行,该主机将在 Composer 环境不使用时打开或关闭它。
我们已经并行设置了一个小型 k8s 集群,并使用 CronJob 部署来管理将池节点缩小到 0,然后使用第二个 cronjob 将其恢复。
部署是这样的(向上和向下更改节点数)。您可以将操作更改为您正在处理虚拟机的方式(您是否通过控制台中的计算暂停实例?)
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: disable-composer
spec:
schedule: "0 10 * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: enable-composer
image: google/cloud-sdk:latest
volumeMounts:
- name: google-app-credentials-volume
mountPath: /etc/gcp
readOnly: true
env:
- name: GOOGLE_APPLICATION_CREDENTIALS
value: /etc/gcp/credentials.json
args:
- /bin/bash
- -c
- gcloud auth activate-service-account service-account@gcp-project.iam.gserviceaccount.com --key-file=$GOOGLE_APPLICATION_CREDENTIALS; COMPOSER_ENV=composer-environment-name; COMPOSER_LOCATION=us-central1; COMPOSER_CLUSTER=`gcloud composer environments describe $COMPOSER_ENV --format="csv[no-heading](config.gkeCluster)" --location $COMPOSER_LOCATION | cut -d '/' -f 6`; COMPOSER_ZONE=`gcloud composer environments describe $COMPOSER_ENV --format="csv[no-heading](config.nodeConfig.location)" --location $COMPOSER_LOCATION | cut -d '/' -f 4`; gcloud container clusters resize $COMPOSER_CLUSTER --zone $COMPOSER_ZONE --size=0 --quiet;
restartPolicy: OnFailure
volumes:
- name: google-app-credentials-volume
secret:
secretName: google-app-credentials
items:
- key: credentials.json
path: credentials.json
其中 google-app-credentials 是包含我们服务帐户密钥文件的 kubernetes 机密。