OS 政策和 OS 政策指派作業

OS 政策是指包含 OS 資源宣告式設定的檔案,例如套件、存放區、檔案或由指令碼定義的自訂資源。詳情請參閱 OSPolicy 的資源定義。

OS 政策指派是 VM 管理員用來將 OS 政策套用至 VM 的 API 資源。詳情請參閱 OSPolicyAssignment 的資源定義。

OS 政策

OS 政策是 JSON 或 YAML 檔案,包含三個部分:

  • Mode」(模式)。政策行為。可用的模式如下:

    • Validation:在這個模式中,政策會檢查資源是否處於所選狀態,但不會採取任何行動。
    • Enforcement:在這個模式中,政策會檢查資源是否處於所選狀態,如果不是,則會執行必要的動作,將資源設為所選狀態。

    無論是哪種模式,VM Manager 都會回報 OS 政策和相關資源的符合性。

  • 資源群組:關聯資源規格適用的作業系統名稱和版本。舉例來說,您可以定義單一政策,在不同作業系統發行版和版本中安裝或部署代理程式。

  • 資源。VM 取得所選設定所需的規格。您最多可以在每個資源群組中指定 10 個資源 ID。支援的資源類型如下:

    • pkg:用於安裝或移除 Linux 和 Windows 套件
    • repository:用於指定可從哪些存放區安裝軟體套件
    • exec:用於啟用 ad hoc shell (/bin/sh) 或 PowerShell 指令碼的執行
    • file:用於管理系統上的檔案

OS 政策範例

下列範例說明如何建立 OS 政策。建立 OS 政策指派作業時,您可以將這些 OS 政策上傳至 Google Cloud 控制台。

  • 範例 1:安裝套件。
  • 範例 2:執行指令碼。
  • 範例 3:執行儲存在 Cloud Storage 值區中的指令碼,並將輸出檔案複製到 Cloud Storage 值區。
  • 範例 4:指定下載存放區,並從該存放區安裝套件。
  • 範例 5:在執行 Container-Optimized OS (COS) 的 VM 上設定 CIS 基準掃描。如要進一步瞭解如何使用 OS 政策進行 CIS 基準掃描,請參閱「自動啟用並檢查 CIS 相容性狀態」。

如需可在環境中套用的 OS 政策範例完整清單,請參閱 GoogleCloudPlatform/osconfig GitHub 存放區。

範例 1

建立 OS 政策,安裝從 Cloud Storage 值區下載的 Windows MSI。

# An OS policy to install a Windows MSI downloaded from a Google Cloud Storage bucket.
id: install-msi-policy
mode: ENFORCEMENT
resourceGroups:
  - resources:
      - id: install-msi
        pkg:
          desiredState: INSTALLED
          msi:
            source:
              gcs:
                bucket: my-bucket
                object: my-app.msi
                generation: 1619136883923956

範例 2

建立 OS 政策,確認 Apache 網路伺服器是否在 Linux VM 上執行。

# An OS policy that ensures Apache web server is running on Linux OSes.
id: apache-always-up-policy
mode: ENFORCEMENT
resourceGroups:
  - resources:
      id: ensure-apache-is-up
      exec:
        validate:
          interpreter: SHELL
          # If Apache web server is already running, return an exit code 100 to indicate
          # that exec resource is already in desired state. In this scenario,
          # the `enforce` step will not be run.
          # Otherwise return an exit code of 101 to indicate that exec resource is not in
          # desired state. In this scenario, the `enforce` step will be run.
          script: if systemctl is-active --quiet httpd; then exit 100; else exit 101; fi
        enforce:
          interpreter: SHELL
          # Start Apache web server and return an exit code of 100 to indicate that the
          # resource is now in its desired state.
          script: systemctl start httpd && exit 100

範例 3

建立 OS 政策,確認 Apache 網路伺服器是否在 Linux VM 上執行。在這個範例中,apache-validate.sh 指令碼會儲存在 Cloud Storage 值區中。如要將輸出內容複製到 Cloud Storage bucket,apache-enforce.sh 指令碼必須包含類似以下的指令:

      gcsutil cp my-exec-output-file gs://my-gcs-bucket
      

# An OS policy that ensures Apache web server is running on Linux OSes.
id: gcs-test-apache-always-up-policy
mode: ENFORCEMENT
resourceGroups:
  - resources:
      id: ensure-apache-is-up
      exec:
        validate:
          interpreter: SHELL
          # If Apache web server is already running, return an exit code 100 to indicate
          # that exec resource is already in desired state. In this scenario,
          # the enforce step will not be run.
          # Otherwise return an exit code of 101 to indicate that exec resource is not in
          # desired state. In this scenario, the enforce step will be run.
          file:
            gcs:
              bucket: my-gcs-bucket
              object: apache-validate.sh
              generation: 1726747503303299
        enforce:
          interpreter: SHELL
          # Start Apache web server and return an exit code of 100 to indicate that the
          # resource is now in its desired state.
          file:
            gcs:
              bucket: my-gcs-bucket
              object: apache-enforce.sh
              generation: 1726747503250884

範例 4

建立 OS 政策,在 CentOS VM 上安裝 Google Cloud 觀測代理程式。

id: cloudops-policy
mode: ENFORCEMENT
resourceGroups:
  - resources:
      - id: add-repo
        repository:
          yum:
            id: google-cloud-ops-agent
            displayName: Google Cloud Ops Agent Repository
            baseUrl: https://packages.cloud.google.com/yum/repos/google-cloud-ops-agent-el7-x86_64-all
            gpgKeys:
              - https://packages.cloud.google.com/yum/doc/yum-key.gpg
              - https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
      - id: install-pkg
        pkg:
          desiredState: INSTALLED
          yum:
            name: google-cloud-ops-agent
      - id: exec-script
        exec:
          validate:
            script: |-
              if [[ $(rpm --query --queryformat '%{VERSION}
              ' google-cloud-ops-agent) == '1.0.2' ]]; then exit 100; else exit 101; fi
            interpreter: SHELL
          enforce:
            script:
              sudo yum remove -y google-cloud-ops-agent || true; sudo yum install
              -y 'google-cloud-ops-agent-1.0.2*' && exit 100
            interpreter: SHELL
      - id: ensure-agent-running
        exec:
          validate:
            script:
              if (ps aux | grep 'opt[/].*google-cloud-ops-agent.*bin/'); then exit
              100; else exit 101; fi
            interpreter: SHELL
          enforce:
            script: sudo systemctl start google-cloud-ops-agent.target && exit 100
            interpreter: SHELL

範例 5

設定定期的 CIS 層級 1 掃描作業,預設間隔為每天一次。

# An OS policy to check CIS level 1 compliance once a day.
id: ensure-cis-level1-compliance-once-a-day-policy
mode: ENFORCEMENT
resourceGroups:
  - resources:
      id: ensure-cis-level1-compliance-once-a-day
      exec:
        validate:
          interpreter: SHELL
          # If cis-compliance-scanner.service is active, return an exit code
          # 100 to indicate that the instance is in compliant state.
          # Otherwise, return an exit code of 101 to run `enforce` step.
          script: |-
            is_active=$(systemctl is-active cis-compliance-scanner.timer)
            result=$(systemctl show -p Result --value cis-compliance-scanner.service)

            if [ "$is_active" == "active" ] && [ "$result" == "success" ]; then
              exit 100;
            else
              exit 101;
            fi
        enforce:
          interpreter: SHELL
          # COS 97 images are by-default CIS Level 1 compliant and there is no
          # additional configuration needed. However, if certain changes
          # cause non-compliance because of the workload on the instance, this
          # section can be used to automate to make fixes. For example, the
          # workload might generate a file that does not comply with the
          # recommended file permissions.
          # Return an exit code of 100 to indicate that the desired changes
          # successfully applied.
          script: |-
            # optional <your code>
            # Check the compliance of the instance once a day.
            systemctl start cis-compliance-scanner.timer && exit 100

OS 政策指派作業

OS 政策指派作業包含下列部分:

  • OS 政策。您想套用至 VM 的一或多項 OS 政策。如要下載或建立政策,請參閱「OS 政策」。

  • 目標 VM:您要套用政策的單一可用區內的 VM 組合。在區域中,您可以使用 OS 系列和包含或排除標籤,限制或限制 VM。您可以選取下列選項的組合:

    • OS 系列:指定 OS 政策適用的目標作業系統。如需支援 OS 政策的作業系統和版本完整清單,請參閱「作業系統詳細資料」。
    • 納入集:根據 VM 或系統標籤指定 OS 政策套用的 VM。
    • 排除集:指定 OS 政策應根據 VM 或系統標籤略過的 VM。

    無論是包含和排除標籤組,只要單一字串標籤符合系統使用的命名慣例,系統就會接受該標籤。不過,大部分標籤都是以 key:value 組合指定。如要進一步瞭解標籤,請參閱「標示資源」。

    舉例來說,您可以選取測試環境中的所有 Ubuntu VM,並指定下列項目,排除執行 Google Kubernetes Engine 的 VM:

    • OS 系列:ubuntu
    • 包含:env:testenv:staging
    • 排除:goog-gke-node
  • 推出率。指定將 OS 政策套用至 VM 的速度。作業系統政策會逐步推出,讓您追蹤系統健康狀況,並在更新導致環境回歸時進行修改。推行計畫包含下列元件:

    • 波次大小 (中斷預算):一次可推行更新的 VM 固定數量或百分比。也就是說,在推行期間的任何時間點,系統只會指定特定數量的 VM。
    • 等待時間:服務將政策套用至 VM 與 VM 從中斷門檻移除之間的時間。舉例來說,如果等待時間為 15 分鐘,表示在將政策套用至 VM 後,推出程序必須等待 15 分鐘,才能將 VM 從中斷門檻中移除,並繼續推出。等待時間有助於控制推出速度,也能讓您及早發現並解決潛在的推出問題。請選取足夠長的時間,以便您監控推出作業的狀態。

    舉例來說,如果您將目標設為 10 個 VM、將中斷門檻設為 20%,並將烘焙時間設為 15 分鐘,那麼在任何特定時間,系統只會排定 2 個 VM 的更新作業。每個 VM 更新完畢後,必須等待 15 分鐘,系統才會將該 VM 從中斷門檻中移除,並將另一個 VM 新增至新版。

    如要進一步瞭解階段推出,請參閱「階段推出」。

OS 政策指派範例

下列範例說明如何建立 OS 政策指派。您可以使用這些範例,透過 Google Cloud CLI 或 OS Config API 建立 OS 政策指派作業。

  • 範例 1:安裝套件。
  • 範例 2:執行指令碼。
  • 範例 3:指定下載存放區,並從該存放區安裝套件。

如需在環境中套用的 OS 政策指派範例清單,請參閱 GoogleCloudPlatform/osconfig GitHub 存放區。

範例 1

建立 OS 政策指派作業,安裝從 Cloud Storage 值區下載的 Windows MSI。

# An OS policy assignment to install a Windows MSI downloaded from a Google Cloud Storage bucket
# on all VMs running Windows Server OS.
osPolicies:
  - id: install-msi-policy
    mode: ENFORCEMENT
    resourceGroups:
      - resources:
          - id: install-msi
            pkg:
              desiredState: INSTALLED
              msi:
                source:
                  gcs:
                    bucket: my-bucket
                    object: my-app.msi
                    generation: 1619136883923956
instanceFilter:
  inventories:
    - osShortName: windows
rollout:
  disruptionBudget:
    fixed: 10
  minWaitDuration: 300s

範例 2

建立 OS 政策指派,驗證 Apache 網路伺服器是否在所有 Linux VM 上執行。

# An OS policy assignment that ensures Apache web server is running on Linux OSes.
# The assignment is applied only to those VMs that have the label `type:webserver` assigned to them.
osPolicies:
  - id: apache-always-up-policy
    mode: ENFORCEMENT
    resourceGroups:
      - resources:
          id: ensure-apache-is-up
          exec:
            validate:
              interpreter: SHELL
              # If Apache web server is already running, return an exit code 100 to indicate
              # that exec resource is already in desired state. In this scenario,
              # the `enforce` step will not be run.
              # Otherwise return an exit code of 101 to indicate that exec resource is not in
              # desired state. In this scenario, the `enforce` step will be run.
              script: if systemctl is-active --quiet httpd; then exit 100; else exit 101; fi
            enforce:
              interpreter: SHELL
              # Start Apache web server and return an exit code of 100 to indicate that the
              # resource is now in its desired state.
              script: systemctl start httpd && exit 100
instanceFilter:
  inclusionLabels:
    - labels:
        type: webserver
rollout:
  disruptionBudget:
    fixed: 10
  minWaitDuration: 300s

範例 3

建立 OS 政策指派作業,在 CentOS VM 上安裝 Google Cloud 觀測代理程式。

# An OS policy assignment that ensures google-cloud-ops-agent is running on all Centos VMs in the project
osPolicies:
  - id: cloudops-policy
    mode: ENFORCEMENT
    resourceGroups:
        resources:
          - id: add-repo
            repository:
              yum:
                id: google-cloud-ops-agent
                displayName: Google Cloud Ops Agent Repository
                baseUrl: https://packages.cloud.google.com/yum/repos/google-cloud-ops-agent-el7-x86_64-all
                gpgKeys:
                  - https://packages.cloud.google.com/yum/doc/yum-key.gpg
                  - https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
          - id: install-pkg
            pkg:
              desiredState: INSTALLED
              yum:
                name: google-cloud-ops-agent
          - id: exec-script
            exec:
              validate:
                script: |-
                  if [[ $(rpm --query --queryformat '%{VERSION}
                  ' google-cloud-ops-agent) == '1.0.2' ]]; then exit 100; else exit 101; fi
                interpreter: SHELL
              enforce:
                script:
                  sudo yum remove -y google-cloud-ops-agent || true; sudo yum install
                  -y 'google-cloud-ops-agent-1.0.2*' && exit 100
                interpreter: SHELL
          - id: ensure-agent-running
            exec:
              validate:
                script:
                  if (ps aux | grep 'opt[/].*google-cloud-ops-agent.*bin/'); then exit
                  100; else exit 101; fi
                interpreter: SHELL
              enforce:
                script: sudo systemctl start google-cloud-ops-agent.target && exit 100
                interpreter: SHELL
instanceFilter:
  inventories:
    - osShortName: centos
rollout:
  disruptionBudget:
    fixed: 10
  minWaitDuration: 300s

後續步驟