如何在Azure Arc加入多台地端VM伺服器

如何在Azure Arc加入多台地端VM伺服器

我們使用 Azure Log Analytics agent(Microsoft Monitoring Agent (MMA)) 來收集監控地端 VM 多年,但從 2023 年初開始,不斷看到 Azure 文件或系統上提示 MMA 在2024年8月31日將不再受到支援。近來地端有申請新的 VM,因此規劃新的 VM 改用Manage Azure Monitor Agent,並且熟悉了解整個設定及升級流程。

Log Analytics Agent Deprecation Message

在說明文件的必要條件馬上碰到第一件事:「Non-Azure: To install the agent on physical servers and virtual machines hosted outside of Azure (that is, on-premises) or in other clouds, you must install the Azure Arc Connected Machine agent first, at no added cost.。」

簡單來說,地端 VM 要安裝 Azure Monitor Agent,要先加入 Machine - Azure Arc,加入之後才能安裝 Azure Monitor Agent。不像之前 MMA 的即裝即用,所以才會有這篇的出現。

Machines - Azure Arc

Azure Arc 本身服務項目就非常多元,我們這裡只專注在「主機(Host)」身上。這裡找到Machines - Azure Arc這個項目。

Search Azure Arc

選擇選單的 Azure Arc 也沒關係,再選擇裡面的 Machines 也是一樣。

Azure Arc - Infrastructure - Machines

選擇畫面上的Add/CreateAdd a machine

Machines-Azure Arc-Add/Create
Add servers with Azure Arc

Add a single server

這裡可以看到有 Add a single server 與 Add multiple servers 及 Add servers from Update Management 三個選擇。

Add a single server

在我們的實測中,Add a single server最簡單,幾乎填完資料直接拿到指令碼執行即可。但有一個問題,重覆走 Add a single server 這條路,會造成建立多個資源群組。也就是說,當你真的只有一台 VM 時才走 Add a single server 這條路。

Add multiple servers

我們要走的是第二條 Add multiple servers。

Add multiple servers - Authentication

在要產生指令碼選項,跟 Add a single server 很明顯有個選項差異,多了一個 Authentication 的內容,它需要指定一個 service principal。你可以直接按 Create New 來建立,但我們回到 Azure Arc 清單上一下。

Azure Arc - service principal

按下 Add 後,畫面跟前面直接按 Create New 一樣,哈哈。

Azure Arc - service principal - Role assignment

這裡重點在 Role assignment,我們必須指定 Azure Connected Machine Onboarding 這個角色給 service principal 。建立之後會請你下載保存好 PrincipalId、PrincipalClientSecret。

Add multiple servers - Authentication - service principal

回到建立指令碼流程,並指定剛剛建立好的 service principal。依照你選擇的平台,可以取得安裝及設定的平台指令碼。例如以下 Linux Shell Script:

# Add the service principal application ID and secret here
servicePrincipalClientId="ClientId";
servicePrincipalSecret="Secret";


export subscriptionId="sId";
export resourceGroup="YourRRG";
export tenantId="tId";
export location="eastasia";
export authType="principal";
export correlationId="cId";
export cloud="AzureCloud";


# Download the installation package
output=$(wget https://aka.ms/azcmagent -O ~/install_linux_azcmagent.sh 2>&1);
if [ $? != 0 ]; then wget -qO- --method=PUT --body-data="{\"subscriptionId\":\"$subscriptionId\",\"resourceGroup\":\"$resourceGroup\",\"tenantId\":\"$tenantId\",\"location\":\"$location\",\"correlationId\":\"$correlationId\",\"authType\":\"$authType\",\"operation\":\"onboarding\",\"messageType\":\"DownloadScriptFailed\",\"message\":\"$output\"}" "https://gbl.his.arc.azure.com/log" &> /dev/null || true; fi;
echo "$output";

# Install the hybrid agent
bash ~/install_linux_azcmagent.sh;

# Run connect command
sudo azcmagent connect --service-principal-id "$servicePrincipalClientId" --service-principal-secret "$servicePrincipalSecret" --resource-group "$resourceGroup" --tenant-id "$tenantId" --location "$location" --subscription-id "$subscriptionId" --cloud "$cloud" --correlation-id "$correlationId";

你拿到的指令碼,應該只差填寫 servicePrincipalSecret,然後拿著指令碼到地端 VM,一台一台執行即可加入到 Machines - Azure Arc。

Machines - Azure Arc - List

注意一下,單純只把 Server 加入到 Machines - Azure Arc 清單中是沒有額外的成本。整合後續的 Monitoring agent 才會開始有成本的產生。

問題排除

如果你在跑指令碼的過程發生錯誤,那麼都會產生一個對應的錯誤碼,你可以到 Troubleshoot Azure Connected Machine agent connection issues查詢對應的處理方式。

像我就有發生一個 AZCM0041 錯誤,這才發現,原來執行指令碼之前還有個預準備的內容沒有處理好。

在使用 Azure Arc-enabled servers(也是Machines - Azure Arc) 之前,必須先將你的訂閱增加以下 Azure resource providers:

az account set --subscription "{Your Subscription Name}"
az provider register --namespace 'Microsoft.HybridCompute'
az provider register --namespace 'Microsoft.GuestConfiguration'
az provider register --namespace 'Microsoft.HybridConnectivity'
az provider register --namespace 'Microsoft.AzureArcData'

當然,你也以透過 Azure Portal 來進行 resource providers 設定。

resource providers 啟用之後,在 VM 重跑指令碼。

$ sudo azcmagent connect --service-principal-id "$servicePrincipalClientId" --service-principal-secret "$servicePrincipalSecret" --resource-group "$resourceGroup" --tenant-id "$tenantId" --location "$location" --subscription-id "$subscriptionId" --cloud "$cloud" --correlation-id "$correlationId";
INFO    Connecting machine to Azure... This might take a few minutes.
INFO    Testing connectivity to endpoints that are needed to connect to Azure... This might take a few minutes.
INFO    Exit Code:  AZCM0026: Network Error
INFO    For troubleshooting, see https://aka.ms/arc/azcmerror
FATAL   required endpoints unavailable: https://ea.his.arc.azure.com,https://gbl.his.arc.azure.com,https://eastasia-gas.guestconfiguration.azure.com,https://AsiaPacific-ats.his.arc.azure.com,https://agentserviceapi.guestconfiguration.azure.com,https://management.azure.com

這個 AZCM0026 錯誤,有碰過幾次,不是很確定問題點,因為我再重新執行一次指令碼,就正常的加入 Machines - Azure Arc 清單中了。

移除 Azure Arc Connected Machine agent

有安裝,我們也順便瞭解一下如何移除。這裡以 Linux 為例。

sudo azcmagent disconnect

這會你請登入 https://microsoft.com/devicelogin 去驗證。它會授權從訂閱裡去刪除資源。

sudo apt purge azcmagent

之後將套件移除,這樣就完成了 Azure Arc Connected Machine agent 的移除。

Remove Azure Arc Connected Machine agent

Machines - Azure Arc 清單就從 3 台減為 2 台了。其他刪除細節,參考 Managing and maintaining the Connected Machine agent

Service principals 權限不足

Service principals - don't have permission

這是公司訂閱的畫面,需要要找管理者補足權限給你。實際要設定 Service principals 權限,嗯,我還不知道,後來為了上線需求,我是請管理者設定一份時間長一點的 Service principals 給我設定使用。管理者也說它找不到設定 Service principals 權限的地方,哈哈哈。

沒有留言:

張貼留言

感謝您的留言,如果我的文章你喜歡或對你有幫助,按個「讚」或「分享」它,我會很高興的。