問題描述
在使用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>
psping下載地址(https://docs.microsoft.com/en-us/sysinternals/downloads/psping) 及psping后驗證端口是否已通。如出現Timeout period expired,則需要到負載均衡資源中添加Load balancing rules。

引用使用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:https://docs.azure.cn/zh-cn/service-fabric/service-fabric-quickstart-containers
az sf cluster: https://docs.microsoft.com/en-us/cli/azure/sf/cluster?view=azure-cli-latest#az_sf_cluster_create



