是否可以在特定时间关闭虚拟机托管 google-cloud-composer?

Is it possible to turn off the VM´s hosting the google-cloud-composer at certain hours?

为了减少 运行 与 google-cloud-composer 相关的计费,我想知道是否可以关闭 运行 虚拟机的 VM 实例特定时间的环境。例如:我们的大多数 DAG 运行 要么在早上要么在下午,因此我们希望在夜间关闭虚拟机,如果可能的话甚至在中午关闭。我知道我们可以从 Google 云控制台手动禁用环境,但最好能找到一种方法来自动执行此操作

谢谢!

遗憾的是,无法使用 Google 云平台以编程方式进行配置。你最好的选择是将脚本 运行 作为来自另一台主机的 cronjob,它会在不使用时打开或关闭 Composer 环境。

我们已经并行设置了一个微型 k8s 集群,并使用 CronJob 部署来管理将池节点缩小到 0,然后使用第二个 cronjob 将其恢复。

部署是这样的(上下改变节点数)。您可以将操作更改为您正在处理 VM(您是否通过控制台中的计算暂停实例?)

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 秘密。