【Azure微服務 Service Fabric 】使用az命令建立Service Fabric集羣

問題描述

在使用Service Fabric的快速入門文檔: 將 Windows 容器部署到 Service Fabric。 其中在建立Service Fabric時候,示例代碼中使用的是PowerShell腳本調用AZ模塊來執行建立命令。可是在本地執行時,碰見了沒法運行'Connect-AzAccount'等命令。node

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命令來替換。主要替換的命令有三個。shell

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

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

3) New-AzServiceFabricCluster 替換爲 az sf cluster createide

# 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中替換對應的參數便可。ui

 

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

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

使用help命令查看正確參數debug

 

重要部分(使用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 窗口。 而後,爲腳本中的變量提供值:subscriptionIdcertpwdcertfolderadminuseradminpwd 等等。 運行該腳本以前,爲 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 Fabrichttps://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

相關文章
相關標籤/搜索