Skip to content

透過 GCP Scheduler,輕鬆實現每小時自動啟動特定虛擬機器

Published: at 下午06:23

如何使用

{{YOUR_GOOGLE_PROJECT_ID}} 替換為你的 gcp project id

{{SERCIE_ACCOUNT}} 替換為你的service account

這樣就可以 整點半小時 開機器


gcloud scheduler jobs create http test-job \
      --schedule="30 * * * *" \
      --uri="https://compute.googleapis.com/compute/v1/projects/{{YOUR_GOOGLE_PROJECT_ID}}/zones/asia-east1-a/instances" \
      --time-zone="Asia/Taipei" \
      --description="$description" \
      --location="asia-east1" \
      --oauth-service-account-email="{{SERCIE_ACCOUNT}}" \
      --message-body='{
      "disks": [{
          "boot": true,
          "autoDelete": true,
          "initializeParams": {
                        "sourceImage": "projects/debian-cloud/global/images/ubuntu-2204-jammy-v20250112",
                        "diskSizeGb": 100
                    }
        }],
      "machineType": "projects/{{YOUR_GOOGLE_PROJECT_ID}}/zones/asia-east1-a/machineTypes/g1-small",
      "name": "test",
      "networkInterfaces": [
        {
          "network": "projects/{{YOUR_GOOGLE_PROJECT_ID}}/global/networks/default",
          "accessConfigs": [
            {
              "name": "External NAT",
              "type": "ONE_TO_ONE_NAT",
              "networkTier": "PREMIUM"
            }
          ],
        }
      ],
      "serviceAccounts": [{
          "email": "{{SERCIE_ACCOUNT}}",
          "scopes": ["https://www.googleapis.com/auth/cloud-platform"]
        }],
      "zone": "asia-east1-a"
    }'

相關指令

然後也可以用指令刪除

檢查某實例是否存在 若存在要刪除

    if gcloud compute instances describe test-machine --zone=asia-east1-a &>/dev/null; then
        echo "Deleting instance: test-machine"
        gcloud compute instances delete test-machine --zone=asia-east1-a --quiet
    else
        echo "Instance does not exist: test-machine"
    fi

看目前有多少實例

gcloud

看目前有多少ubuntu的作業系統

gcloud compute images list --filter ubuntu-os

看某一區目前有怎樣規格的機器

gcloud compute machine-types list --zones=asia-east1-a

如何使用 gcs 放sh 然後等機器起來時自動執行shell script

gcloud scheduler jobs create http test-job \
      --schedule="30 * * * *" \
      --uri="https://compute.googleapis.com/compute/v1/projects/{{YOUR_GOOGLE_PROJECT_ID}}/zones/asia-east1-a/instances" \
      --time-zone="Asia/Taipei" \
      --description="$description" \
      --location="asia-east1" \
      --oauth-service-account-email="{{SERCIE_ACCOUNT}}" \
      --message-body='{
      "disks": [{
          "boot": true,
          "autoDelete": true,
          "initializeParams": {
                        "sourceImage": "projects/debian-cloud/global/images/ubuntu-2204-jammy-v20250112",
                        "diskSizeGb": 100
                    }
        }],
      "machineType": "projects/{{YOUR_GOOGLE_PROJECT_ID}}/zones/asia-east1-a/machineTypes/g1-small",
       "metadata": {
        "items": [
          {
            "value": "gs://{{YOUR_GC_BUCKET_NAME}}/{{SCIPT_PATH}},
            "key": "startup-script-url"
          }
        ]
      },
      "name": "test-job",
      "networkInterfaces": [
        {
          "network": "projects/{{YOUR_GOOGLE_PROJECT_ID}}/global/networks/default",
          "accessConfigs": [
            {
              "name": "External NAT",
              "type": "ONE_TO_ONE_NAT",
              "networkTier": "PREMIUM"
            }
          ],
        }
      ],
      "serviceAccounts": [{
          "email": "{{SERCIE_ACCOUNT}}",
          "scopes": ["https://www.googleapis.com/auth/cloud-platform"]
        }],
      "zone": "asia-east1-a"
    }'