【Azure微服務 Service Fabric 】使用az命令創建Service Fabric集群

問題描述

在使用Service Fabric的快速入門文檔: 將 Windows 容器部署到 Service Fabric。 其中在創建Service Fabric時候,示例程式碼中使用的是PowerShell腳本調用AZ模組來執行創建命令。但是在本地執行時,遇見了無法運行'Connect-AzAccount'等命令。

Connect-AzAccount : The term 'Connect-AzAccount' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At line:1 char:1
+ Connect-AzAccount -Environment AzureChinaCloud
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Connect-AzAccount:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

 

所以決定使用az cli命令來代替,但官方文檔中只給出了PowerShell的命令,所以需要使用對應的az命令來替換。主要替換的命令有三個。

1) Connect-AzAccount -Environment AzureChinaCloud 替換為 az cloud set --name AzureChinaCloud

2) Select-AzSubscription -SubscriptionId $subscriptionId 替換為 az account set --subscription $subscriptionId

3) New-AzServiceFabricCluster 替換為 az sf cluster create

# Create the Service Fabric cluster.
New-AzServiceFabricCluster -Name $clustername -ResourceGroupName $groupname -Location $clusterloc `
-ClusterSize $clustersize -VmUserName $adminuser -VmPassword $adminpwd -CertificateSubjectName $subname `
-CertificatePassword $certpwd -CertificateOutputFolder $certfolder `
-OS WindowsServer2016DatacenterwithContainers -VmSku $vmsku -KeyVaultName $vaultname

注:在命令New-AzServiceFabricCluster中替換對應的參數即可。

 

如出現參數名不存在的情況,可以使用-h 幫助命令來獲取正常的參數。如az sf cluster create -h

參數錯誤:cli.azure.cli.core.parser : az: error: unrecognized arguments

使用help命令查看正確參數

 

重要部分(使用az CLI命令替換後的全部命令)

#Provide the subscription Id
$subscriptionId = 'yourSubscriptionId'

# Certificate variables.
$certpwd="Password#1234" | ConvertTo-SecureString -AsPlainText -Force
$certfolder="c:\mycertificates\"

# Variables for VM admin.
$adminuser="vmadmin"
$adminpwd="Password#1234" | ConvertTo-SecureString -AsPlainText -Force 

# Variables for common values
$clusterloc="chinaeast"
$clustername = "mysfcluster"
$groupname="mysfclustergroup"       
$vmsku = "Standard_D2_v2"
$vaultname = "mykeyvault"
$subname="$clustername.$clusterloc.cloudapp.chinacloudapi.cn"

# Set the number of cluster nodes. Possible values: 1, 3-99
$clustersize=5 

# Set the context to the subscription Id where the cluster will be created
az cloud set --name AzureChinaCloud
az login
az account set --subscription $subscriptionId

# Create the Service Fabric cluster.
az sf cluster create --cluster-name $clustername --resource-group $groupname --location $clusterloc `
 --cluster-size $clustersize --vm-user-name $adminuser --vm-password $adminpwd --certificate-subject-name $subname `
 --certificate-password $certpwd --certificate-output-folder  $certfolder `
 --os WindowsServer2016DatacenterwithContainers --vm-sku $vmsku --vault-name $vaultname --debug

註:

  • 腳本創建一個由五個節點組成的 Service Fabric 群集(使用 X.509 證書保護的群集)。該命令將創建一個自簽名證書,並將其上傳到新的 Key Vault。 該證書也會複製到本地目錄"c:\mycertificates\"中。
  • 在執行中如需要查看日誌輸出,可以添加 --debug。

 

成功結果

當Service Fabric創建完成後,可以通過Visual Studio 2019發布創建好的Container到集群中。發布成功後,通過Service Fabric Explorer查看效果:

當根據文檔部署Container後,訪問SF集群URL並加上80埠(埠由發布Container時指定),既可以查看到IIS的默認頁面.

 

在ServiceManifest.xml文件中配置Endpoint埠,在訪問時候需要非常注意的一點是:確保SF的Load Balance中已開啟該埠。可以通過psping方式來驗證。

  <Resources>
    <Endpoints>
      <!-- This endpoint is used by the communication listener to obtain the port on which to 
           listen. Please note that if your service is partitioned, this port is shared with 
           replicas of different partitions that are placed in your code. -->
      <Endpoint Name="MyContainerServiceTypeEndpoint" Port="80" />
    </Endpoints>

 

引用使用PowerShell AzModule命令創建SF集群的全部程式碼為:

創建群集

以下示例腳本創建一個由五個節點組成的 Service Fabric 群集(使用 X.509 證書保護的群集)。 該命令將創建一個自簽名證書,並將其上傳到新的 Key Vault。 該證書也會複製到本地目錄。 可在創建 Service Fabric 群集中詳細了解如何使用此腳本創建群集。

必要時,請使用 Azure PowerShell 指南中的說明安裝 Azure PowerShell。

在運行以下腳本之前,請在 PowerShell 中運行 Connect-AzAccount -Environment AzureChinaCloud 來與 Azure 建立連接。

將以下腳本複製到剪貼板,並打開 Windows PowerShell ISE 。 將內容粘貼到空的 Untitled1.ps1 窗口。 然後,為腳本中的變數提供值:subscriptionId、certpwd、certfolder、adminuser、adminpwd 等等。 運行該腳本之前,為 certfolder 指定的目錄必須存在。

#Provide the subscription Id
$subscriptionId = 'yourSubscriptionId'

# Certificate variables.
$certpwd="Password#1234" | ConvertTo-SecureString -AsPlainText -Force
$certfolder="c:\mycertificates\"

# Variables for VM admin.
$adminuser="vmadmin"
$adminpwd="Password#1234" | ConvertTo-SecureString -AsPlainText -Force 

# Variables for common values
$clusterloc="chinaeast"
$clustername = "mysfcluster"
$groupname="mysfclustergroup"       
$vmsku = "Standard_D2_v2"
$vaultname = "mykeyvault"
$subname="$clustername.$clusterloc.cloudapp.chinacloudapi.cn"

# Set the number of cluster nodes. Possible values: 1, 3-99
$clustersize=5 

# Set the context to the subscription Id where the cluster will be created
Select-AzSubscription -SubscriptionId $subscriptionId

# Create the Service Fabric cluster.
New-AzServiceFabricCluster -Name $clustername -ResourceGroupName $groupname -Location $clusterloc `
-ClusterSize $clustersize -VmUserName $adminuser -VmPassword $adminpwd -CertificateSubjectName $subname `
-CertificatePassword $certpwd -CertificateOutputFolder $certfolder `
-OS WindowsServer2016DatacenterwithContainers -VmSku $vmsku -KeyVaultName $vaultname

為變數提供值後,按 F5 運行該腳本。

運行腳本並創建群集後,在輸出中查找 ClusterEndpoint。 例如:

 

參考文檔:

將 Windows 容器部署到 Service Fabric://docs.azure.cn/zh-cn/service-fabric/service-fabric-quickstart-containers

az sf cluster: //docs.microsoft.com/en-us/cli/azure/sf/cluster?view=azure-cli-latest#az_sf_cluster_create