驗證 VM 管理員設定

請參閱這份文件,確認 VM 管理員設定正確無誤。如要瞭解如何設定 VM 管理員,請參閱「設定 VM 管理員」。

如要驗證設定,您可以使用疑難排解指令,或對虛擬機器 (VM) 執行個體執行手動檢查

事前準備

  • 如為 Windows VM,請使用 PowerShell 3.0 以上版本
  • 如果尚未設定,請先設定驗證機制。驗證是指驗證身分,以便存取 Google Cloud 服務和 API 的程序。如要在本機開發環境中執行程式碼或範例,您可以選取下列任一選項,向 Compute Engine 進行驗證:

    Select the tab for how you plan to use the samples on this page:

    Console

    When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.

    gcloud

    1. After installing the Google Cloud CLI, initialize it by running the following command:

      gcloud init

      If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

    2. Set a default region and zone.

使用疑難排解指令

使用 os-config troubleshoot 指令驗證設定。如檢查未通過,系統會提供意見回饋,說明如何修正問題。

gcloud compute os-config troubleshoot VM_NAME \
   --zone=ZONE

更改下列內容:

  • VM_NAME:要進行疑難排解的 VM 執行個體名稱
  • ZONE:執行個體所在的區域

範例

  • 範例 1:當 VM 執行個體的設定出現問題時,顯示 os-config troubleshoot 指令的輸出結果
  • 範例 2:顯示 VM 執行個體設定正確時,os-config troubleshoot 指令的輸出內容

示例 1

如要針對可用區 asia-east2-b 中名為 my-instance-1 的 VM 執行個體進行疑難排解,請執行以下指令:

gcloud compute os-config troubleshoot my-instance-1 \
    --zone=asia-east2-b

輸出內容會類似以下內容:

OS Config troubleshooter tool is checking if there are issues with the
VM Manager setup for this VM instance.

> Is the OS Config API enabled? Yes
> Is the OS Config agent enabled? Yes
> Is the OS Config agent up to date? No

The version of OS Config agent running on this VM instance is not the
latest version.
See https://cloud.google.com/compute/docs/manage-os/upgrade-vm-manager#update-agent
on how to update the agent.

範例 2

如要針對可用區 us-west1-b 中名為 my-instance-2 的 VM 執行個體進行疑難排解,請執行以下指令:

gcloud compute os-config troubleshoot my-instance-2 \
    --zone=us-west1-b

輸出內容會類似以下內容:

OS Config troubleshooter tool is checking if there are issues with the
VM Manager setup for this VM instance.

> Is the OS Config API enabled? Yes
> Is the OS Config agent enabled? Yes
> Is the OS Config agent up to date? Yes
> Is a service account present on the instance? Yes
> Is the OS Config Service account present for this instance? Yes
> Does this instance have a public IP or Private Google Access? Yes
This instance has a public IP.

手動驗證

如要手動驗證 VM 管理員是否正確設定,請完成下列檢查:

如果 VM 管理員已正確設定,但仍發生問題,請參閱排解 VM 管理員問題

檢查是否已啟用 OS Config API

在 Google Cloud 專案中,檢查 API 是否已啟用。

主控台

在 Google Cloud 控制台中,前往「OS Config API」頁面。

前往 OS Config API

gcloud

gcloud services list --enabled

如果已啟用 API,輸出內容會類似以下內容:

osconfig.googleapis.com              OS Config API

如果 API 未啟用,請啟用 OS Config API

檢查中繼資料是否已啟用

Linux

如要設定專案中繼資料,請在 VM 上完成下列步驟:

  1. 查詢專案屬性端點:

    curl "http://metadata.google.internal/computeMetadata/v1/project/attributes/" \
    -H "Metadata-Flavor: Google"
    

    如果已設定代理程式中繼資料值,輸出內容會類似以下:

    enable-osconfig
    
  2. 如果顯示 enable-osconfig 值,請查詢端點:

    curl "http://metadata.google.internal/computeMetadata/v1/project/attributes/enable-osconfig" \
    -H "Metadata-Flavor: Google"

    如果啟用,端點會傳回 TRUE

如果未啟用中繼資料,請啟用 OS 設定中繼資料

Windows

如要取得專案中繼資料,請在 VM 上以管理員身分開啟 PowerShell 終端機,然後執行下列指令:

  1. 查詢專案屬性端點:

    $value = (Invoke-RestMethod `
             -Headers @{'Metadata-Flavor' = 'Google'} `
             -Uri "http://metadata.google.internal/computeMetadata/v1/project/attributes/")
    $value
    

    如果已設定代理程式中繼資料值,輸出內容會類似以下:

    enable-osconfig
    
  2. 如果顯示 enable-osconfig 值,請查詢端點:

    $value = (Invoke-RestMethod `
             -Headers @{'Metadata-Flavor' = 'Google'} `
             -Uri "http://metadata.google.internal/computeMetadata/v1/project/attributes/enable-osconfig")
    $value
    

    如果已啟用,端點會傳回 TRUE

如果未啟用中繼資料,請啟用 OS 設定中繼資料

檢查 OS 設定代理程式是否已安裝及執行

Linux

如要檢查 Linux VM 是否已安裝代理程式,請執行下列指令:

sudo systemctl status google-osconfig-agent

如果已安裝及執行這個代理程式,輸出內容會類似以下內容:

google-osconfig-agent.service - Google OSConfig Agent
Loaded: loaded (/lib/systemd/system/google-osconfig-agent.service; enabled; vendor preset:
Active: active (running) since Wed 2020-01-15 00:14:22 UTC; 6min ago
Main PID: 369 (google_osconfig)
 Tasks: 8 (limit: 4374)
Memory: 102.7M
CGroup: /system.slice/google-osconfig-agent.service
        └─369 /usr/bin/google_osconfig_agent

如果尚未安裝代理程式,請安裝 OS 設定代理程式

Windows

如要檢查 Windows VM 是否已安裝代理程式,請執行下列指令:

PowerShell Get-Service google_osconfig_agent

如果已安裝及執行這個代理程式,輸出內容會類似以下內容:

Status   Name               DisplayName
------   ----               -----------
Running  google_osconfig... Google OSConfig Agent

如果尚未安裝代理程式,請安裝 OS 設定代理程式

檢查是否已啟用服務帳戶

如要瞭解服務帳戶的相關規定,請參閱設定總覽

Linux

請在 VM 上執行下列指令:

curl "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/" \
-H "Metadata-Flavor: Google"

輸出內容至少應包含預設服務帳戶。

default/

Windows

在 VM 上,以系統管理員身分開啟 PowerShell 終端機,然後執行下列指令:

$value = (Invoke-RestMethod `
         -Headers @{'Metadata-Flavor' = 'Google'} `
         -Uri "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/")
$value

輸出內容至少應包含預設服務帳戶。

default/

檢查 VM 是否可與 OS Config API 通訊

如要檢查 VM 是否可與 OS Config API 通訊,請在 Linux 或 Windows VM 上執行下列指令:

ping osconfig.googleapis.com

如要停止對 OS Config API 執行 ping 作業,請按下 Control + C

如果 VM 是在私人虛擬私有雲網路中運作,且沒有公開網際網路存取權,請確認您已啟用 Private Google Access

啟用私人 Google 存取權後,請檢查 VM 是否可與 OS Config API 通訊。

Linux

請在 VM 上執行下列指令:

 curl --ssl 'https://osconfig.googleapis.com/$discovery/rest' | head

如果 VM 可以與 OS Config API 通訊,指令輸出內容會類似以下:

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                             Dload  Upload   Total   Spent    Left  Speed
0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0{
"discoveryVersion": "v1",
"baseUrl": "https://osconfig.googleapis.com/",
"ownerName": "Google",
"version": "v1beta",
"schemas": {
  "GooSettings": {
    "description": "Googet patching is performed by running `googet update`.",
    "properties": {},
    "id": "GooSettings",
100  9569    0  9569    0     0   198k      0 --:--:-- --:--:-- --:--:--  198k

Windows

在 VM 中,以管理員身分開啟 PowerShell 終端機,然後執行下列指令:

 Invoke-RestMethod  -Headers @{'Metadata-Flavor' = 'Google'}
 -Uri 'https://osconfig.googleapis.com/$discovery/rest'

後續步驟