2

問題部署兩臺Azure的應用程序服務相同的應用程序服務計劃

如何使用VSTS冪等持續集成/連續部署過程,當兩個不同的Azure的應用服務部署到同一個應用程序服務的計劃。

環境

  • 我已經寫了兩ARM TEMPLATES其中每個部署Web應用程序到Azure的應用服務。

  • 爲了部署應用程序服務,必須首先創建服務計劃。

  • ARM TEMPLATES當前爲每個Web App創建一個獨特的服務計劃。

  • 我正在使用VSTS版本定義來部署每個成功的VSTS版本。即發佈被設計爲冪等。

  • 當前每個網絡應用都有自己的資源組,其中包含自己的應用服務計劃。理想情況下,每個Web應用程序都有自己的資源組,但是應用程序服務計劃可以位於其自己的資源組中(但如果可能的話)。

以下模板是用於將Web App服務部署到App Service計劃的模板之一的示例。

它顯示了應用程序服務計劃的創建使用命名轉換:

appname中-計劃-q2dkkaaaaaaaa

這是通過使用創建的:

  • 七字符標識符 「應用程序名稱」在ARM參數文件中定義。
  • 資源標識符「plan」。
  • 資源組名稱,它來自隨機命名的存儲帳戶名稱「q2dkkaaaaaaaa」創建時的名稱。

{ 
"parameters": { 
    "appName": { 
     "type": "string", 
     "maxLength": 7, 
     "metadata": { 
      "description": "The name of the app that you wish to create." 
     } 
    }, 
    "appServicePlanSku": { 
     "type": "string", 
     "defaultValue": "Standard", 
     "metadata": { 
      "description": "The Service Plan SKU" 
     } 
    }, 
    "appServicePlanWorkerSize": { 
     "type": "string", 
     "defaultValue": "0", 
     "metadata": { 
      "description": "The App Service Plan Worker Size (?)" 
     } 
    }, 
    "appServicePlanSkuCode": { 
     "type": "string", 
     "defaultValue": "S1", 
     "metadata": { 
      "description": "The App Service Plan SKU Code" 
     } 
    }, 
    "appServicePlanNumWorkers": { 
     "type": "string", 
     "defaultValue": "2", 
     "metadata": { 
      "description": "The Number of App Service Workers." 
     } 

}, 
"variables": { 
    "webAppName": "[concat(parameters('appName'),'-wa-', uniqueString(resourceGroup().id))]", 
    "hostingPlanName": "[concat(parameters('appName'),'-Plan-', uniqueString(resourceGroup().id))]", 
    "stageSlotName": "stageSlot", 
    "devSlotName": "devSlot" 
    } 
}, 
"resources": [ 
    { 
     "apiVersion": "2016-09-01", 
     "name": "[variables('hostingPlanName')]", 
     "type": "Microsoft.Web/serverfarms", 
     "location": "[resourceGroup().location]", 
     "properties": { 
      "name": "[variables('hostingPlanName')]", 
      "workerSizeId": "[parameters('appServicePlanWorkerSize')]", 
      "numberOfWorkers": "[parameters('appServicePlanNumWorkers')]" 
     }, 
     "sku": { 
      "Tier": "[parameters('appServicePlanSku')]", 
      "Name": "[parameters('appServicePlanSkuCode')]" 
     }, 
     "dependsOn": [] 
    }, 
    { 
     "apiVersion": "2015-08-01", 
     "type": "Microsoft.Web/sites", 
     "name": "[variables('webAppName')]", 
     "location": "[resourceGroup().location]", 
     "kind": "webapp", 
     "tags": { 
      "Environment": "production", 
      "displayName": "WebAppService" 
     }, 
     "dependsOn": [ 
      "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]", 
     ], 
     "properties": { 
      "name": "[variables('webAppName')]", 
      "serverFarmId": "[resourceId('Microsoft.Web/serverfarms',variables('hostingPlanName'))]" 
     }, 
     "resources": [ 
      { 
       "name": "slotConfigNames", 
       "type": "config", 
       "apiVersion": "2015-08-01", 
       "dependsOn": [ 
        "[resourceId('Microsoft.Web/sites', variables('webAppName'))]" 
       ], 
       "tags": { 
        "displayName": "slotConfigNames" 
       }, 
       "properties": { 
        "appSettingNames": [] 
       } 
      }, 
      { 
       "apiVersion": "2015-08-01", 
       "name": "[variables('stageSlotName')]", 
       "type": "slots", 
       "location": "[resourceGroup().location]", 
       "dependsOn": [ 
        "[resourceId('Microsoft.Web/sites', variables('webAppName'))]"], 
       "properties": {}, 
       "resources": [] 
      }, 
      { 
       "apiVersion": "2015-08-01", 
       "name": "[variables('devSlotName')]", 
       "type": "slots", 
       "location": "[resourceGroup().location]", 
       "dependsOn": [ 
        "[resourceId('Microsoft.Web/sites', variables('webAppName'))]"], 
       "properties": {}, 
       "resources": [] 
      } 
     ] 
    } 
] 
} 

QUESTION

我試圖執行兩個ARM TEMPLATES(類似於上面的例子)部署兩個不同Web Apps到同一個Servi ce計劃。

顯然,這兩個Web Apps都必須調用相同的中央資源,以確保它們都部署到相同的App Service資源名稱並執行任何更改。

  • 如果App Service計劃存在= deploy web app。
  • 如果App Service計劃不存在=創建服務計劃,然後部署Web應用程序。
  • 如果應用程序服務計劃已更改=部署服務計劃更改(例如更改等級),然後部署該Web應用程序。

考慮到上面的環境描述,我有什麼選擇讓這個工作?

  • VSTS發佈定義中的全局參數可能是?
  • ARM TEMPLATES調用一個PowerShell腳本來創建應用服務計劃?

渴望遵循最佳實踐。

我希望以上內容足夠詳細。對不起,如果有遺漏。謝謝。

回答

1

SOLUTION

在我的情況的解決方案是創建三個模板:

  • 模板1創建應用程序服務計劃。此ARM模板存儲在BLOB容器中,可通過SAS URI訪問發佈管道。
  • 模板2創建網絡應用程序A.此模板使用LINKED TEMPLATE功能來調用和執行共享模板。
  • 模板3創建Web應用程序B.此模板使用LINKED TEMPLATE功能來調用和執行共享模板。

結果

  • 兩個Web應用程序,然後發佈到同一服務器場,共享 實例。
  • 部署的冪等性保持不變。
  • 任何應用程序服務計劃部署和任何修改的單點事實。
  • 需要在服務器場的數量上節省的錢。

共享服務計劃 - 共享服務計劃的ARM模板例如:

{ 
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", 
"contentVersion": "1.0.0.0", 
"parameters": { 
    "planLabel": { 
     "defaultValue": "shared-service-plan", 
     "type": "string" 
    } 
}, 
"variables": { 
    "servicePlanName": "[concat(parameters('planLabel'),'-Plan-', uniqueString(resourceGroup().id))]" 
}, 
"resources": [ 
    { 
     "comments": "Creates an App Service Plan on the Standard (S1) SKU.", 
     "type": "Microsoft.Web/serverfarms", 
     "sku": { 
      "name": "S1", 
      "tier": "Standard", 
      "size": "S1", 
      "family": "S", 
      "capacity": 2 
     }, 
     "kind": "app", 
     "name": "[variables('servicePlanName')]", 
     "apiVersion": "2016-09-01", 
     "location": "[resourceGroup().location]", 
     "properties": { 
      "name": "[variables('servicePlanName')]" 
     }, 
     "dependsOn": [] 
    } 
], 
"outputs": { 
    "servicePlanResourceId": { 
     "type": "string", 
     "value": "[resourceId('Microsoft.Web/serverfarms', variables('servicePlanName'))]" 
    }, 
    "servicePlanName":{ 
     "type": "string", 
     "value": "[variables('servicePlanName')]" 
    }, 
    "resourceGroupName":{ 
     "type": "string", 
     "value": "[resourceGroup().name]" 
    } 
} 
} 

Web應用程序A - 含有連接ARM模板示例模板:

{ 
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", 
"contentVersion": "1.0.0.0", 
"parameters": { 
    "servicePlanLabel": { 
     "type": "string", 
     "metadata": { 
      "description": "The base name for the App Service Plan to be used in the linked template." 
     }, 
     "defaultValue": "plan" 
    }, 
    "appServicePlanResourceGroup": { 
     "type": "string", 
     "metadata": { 
      "Description": "The name of the Resource Group the shared App Service Plan will be deployed to." 
     }, 
     "defaultValue": "group" 
    }, 
    "appServicePlanTemplateUri": { 
     "type": "string", 
     "metadata": { 
      "description": "The URI to the App Service Plan linked template in BLOB" 
     } 
    } 
}, 
"variables": {}, 
"resources": [ 
    { 
     "apiVersion": "2017-05-10", 
     "name": "appServicePlanTemplate", 
     "type": "Microsoft.Resources/deployments", 
     "resourceGroup": "[parameters('appServicePlanResourceGroup')]", 
     "properties": { 
      "mode": "Incremental", 
      "templateLink": { 
       "uri": "[parameters('appServicePlanTemplateUri')]", 
       "contentVersion": "1.0.0.0" 
      }, 
      "parameters": { 
       "planLabel": { 
        "value": "[parameters('servicePlanLabel')]" 
       } 
      } 
     } 
    }, 
    { 
     "apiVersion": "2015-08-01", 
     "type": "Microsoft.Web/sites", 
     "name": "[variables('webAppName')]", 
     "location": "[resourceGroup().location]", 
     "kind": "webapp", 
     "tags": { 
      "Environment": "production", 
      "displayName": "App" 
     }, 
     "dependsOn": [ 
      "[resourceId(parameters('appServicePlanResourceGroup'), 'Microsoft.Resources/deployments', 'appServicePlanTemplate')]" 
     ], 
     "properties": {} 
    } 
} 
} 

希望這對某人有用。

感謝 斯科特

REF https://github.com/MicrosoftDocs/azure-docs/blob/master/articles/azure-resource-manager/resource-group-linked-templates.md

1

您的需求默認支持ARM模板。基本上,如果ARM模板遇到存在的資源,它將在屬性不匹配時更新資源。否則,它將使用您在ARM模板中設置的屬性創建資源。

+0

上午奧馬爾,感謝您的帖子。我同意學位,但是我有ARM模板來部署2個獨立的應用程序,我不想在每個應用程序中定義應用程序服務計劃資源,因爲任何更改都需要在模板中進行更新。任何應用程序都可以通過CI/CD過程每天部署任意次數。定義應用服務計劃兩次可能會導致問題。對不起,如果這在我的文章中不明確。謝謝。斯科特+1順便說一句。 –

+0

您可以註冊一個VSTS事件來處理ARM執行作業。例如,VSTS [發佈部署開始](https://www.visualstudio.com/en-us/docs/integrate/get-started/service-hooks/events#ms.vss-release.deployment-started-event)事件。 – Amor

+0

謝謝阿莫爾,這將工作。也開始查看「LINKED TEMPLATES」https://github.com/MicrosoftDocs/azure-docs/blob/master/articles/azure-resource-manager/resource-group-linked-templates.md。將測試此饋送和反饋結果。 –

相關問題