快速入門:將 Linux 容器部署至 Service FabricQuickstart: Deploy Linux containers to Service Fabric

Azure Service Fabric 是一個分散式系統平台,可讓您部署及管理可調整和可信賴的微服務與容器。Azure Service Fabric is a distributed systems platform for deploying and managing scalable and reliable microservices and containers.

本快速入門示範如何將 Linux 容器部署到 Azure 上的 Service Fabric 叢集。This quickstart shows how to deploy Linux containers to a Service Fabric cluster on Azure. 完成後,您會有一個投票應用程式,它是由在 Service Fabric 叢集中執行的 Python Web 前端和 Redis 後端所組成。Once complete, you have a voting application consisting of a Python web front end and a Redis back end running in a Service Fabric cluster. 此外,您也將了解如何容錯移轉應用程式,以及如何調整叢集中的應用程式。You also learn how to fail over an application and how to scale an application in your cluster.

投票應用程式網頁

必要條件Prerequisites

若要完成本快速入門:To complete this quickstart:

  1. 如果您沒有訂用帳戶,請在開始前建立免費 Azure 帳戶Create a free Azure account before you begin if you don't have a subscription.

  2. 安裝 Azure CLIInstall the Azure CLI

  3. 安裝 Service Fabric SDK 和 CLIInstall the Service Fabric SDK and CLI

  4. 安裝 GitInstall Git

取得應用程式套件Get the application package

若要將容器部署至 Service Fabric,您需要一組可描述個別容器和應用程式的資訊清單檔案 (應用程式定義)。To deploy containers to Service Fabric, you need a set of manifest files (the application definition), which describe the individual containers and the application.

在主控台中,使用 git 複製應用程式定義的複本;然後將目錄切換至您複本中的 Voting 目錄。In a console, use git to clone a copy of the application definition; then change directories to the Voting directory in your clone.

git clone https://github.com/Azure-Samples/service-fabric-containers.git

cd service-fabric-containers/Linux/container-tutorial/Voting

建立 Service Fabric 叢集Create a Service Fabric cluster

若要將應用程式部署至 Azure,您需要用來執行該應用程式的 Service Fabric 叢集。To deploy the application to Azure, you need a Service Fabric cluster to run the application. 下列命令會在 Azure 中建立包含五個節點的叢集。The following commands create a five-node cluster in Azure. 這些命令也會建立自我簽署的憑證,然後將其新增至金鑰保存庫並在本機下載憑證。The commands also create a self-signed certificate, adds it to a key vault and downloads the certificate locally. 新的憑證用來在部署叢集時保護叢集,而且用來驗證用戶端。The new certificate is used to secure the cluster when it deploys and is used to authenticate clients.

#!/bin/bash

# Variables
ResourceGroupName="containertestcluster" 
ClusterName="containertestcluster" 
Location="eastus" 
Password="q6D7nN%6ck@6" 
Subject="containertestcluster.eastus.cloudapp.azure.com" 
VaultName="containertestvault" 
VmPassword="Mypa$$word!321"
VmUserName="sfadminuser"

# Login to Azure and set the subscription
az login

az account set --subscription <mySubscriptionID>

# Create resource group
az group create --name $ResourceGroupName --location $Location 

# Create secure five node Linux cluster. Creates a key vault in a resource group
# and creates a certficate in the key vault. The certificate's subject name must match 
# the domain that you use to access the Service Fabric cluster.  The certificate is downloaded locally.
az sf cluster create --resource-group $ResourceGroupName --location $Location --certificate-output-folder . --certificate-password $Password --certificate-subject-name $Subject --cluster-name $ClusterName --cluster-size 5 --os UbuntuServer1604 --vault-name $VaultName --vault-resource-group $ResourceGroupName --vm-password $VmPassword --vm-user-name $VmUserName

注意

Web 前端服務設定為在連接埠 80 上接聽傳入流量。The web front end service is configured to listen on port 80 for incoming traffic. 根據預設,連接埠 80 在您的叢集 VM 和 Azure 負載平衡器上是開啟的。By default, port 80 is open on your cluster VMs and the Azure load balancer.

設定您的環境Configure your environment

Service Fabric 提供了數項可用來管理叢集及其應用程式的工具:Service Fabric provides several tools that you can use to manage a cluster and its applications:

  • Service Fabric Explorer,此為以瀏覽器為基礎的工具。Service Fabric Explorer, a browser-based tool.
  • Service Fabric 命令列介面 (CLI),此 CLI 會在 Azure CLI 之上執行。Service Fabric Command Line Interface (CLI), which runs on top of Azure CLI.
  • PowerShell 命令。PowerShell commands.

在此快速入門中,您會使用 Service Fabric CLI 和 Service Fabric Explorer (Web 型工具)。In this quickstart, you use the Service Fabric CLI and Service Fabric Explorer (a web based tool). 若要使用 Service Fabric Explorer,您需要將憑證 PFX 檔案匯入瀏覽器。To use Service Fabric Explorer, you need to import the certificate PFX file into the browser. 根據預設,PFX 檔案沒有密碼。By default, the PFX file has no password.

Mozilla Firefox 是 Ubuntu 16.04 中的預設瀏覽器。Mozilla Firefox is the default browser in Ubuntu 16.04. 若要將憑證匯入 Firefox 中,請按一下瀏覽器右上角的功能表按鈕,然後按一下 [選項] 。To import the certificate into Firefox, click the menu button in the upper right corner of your browser, then click Options. 在 [喜好設定] 頁面上,使用搜尋方塊搜尋「憑證」。On the Preferences page, use the search box to search for "certificates". 按一下 [檢視憑證] ,選取 [您的憑證] 索引標籤上,按一下 [匯入] ,並依照提示匯入憑證。Click View Certificates, select the Your Certificates tab, click Import and follow the prompts to import the certificate.

在 Firefox 上安裝憑證

部署 Service Fabric 應用程式Deploy the Service Fabric application

  1. 使用 CLI 連線到 Azure 中的 Service Fabric 叢集。Connect to the Service Fabric cluster in Azure using the CLI. 此端點是叢集的管理端點。The endpoint is the management endpoint for your cluster. 您在上一節中建立了 PEM 檔案。You created the PEM file in the previous section.

    sfctl cluster select --endpoint https://containertestcluster.eastus.cloudapp.azure.com:19080 --pem containertestcluster22019013100.pem --no-verify
    
  2. 使用安裝指令碼將投票應用程式定義複製到叢集、註冊應用程式類型,以及建立應用程式的執行個體。Use the install script to copy the Voting application definition to the cluster, register the application type, and create an instance of the application. PEM 憑證檔案應該位於與 install.sh 檔案相同的目錄。The PEM certificate file should be located in the same directory as the install.sh file.

    ./install.sh
    
  3. 開啟網頁瀏覽器,並瀏覽至叢集的 Service Fabric Explorer 端點。Open a web browser and navigate to the Service Fabric Explorer endpoint for your cluster. 此端點具有下列格式:https://<my-azure-service-fabric-cluster-url>:19080/Explorer;例如 https://containertestcluster.eastus.cloudapp.azure.com:19080/ExplorerThe endpoint has the following format: https://<my-azure-service-fabric-cluster-url>:19080/Explorer; for example, https://containertestcluster.eastus.cloudapp.azure.com:19080/Explorer.

  4. 展開 [應用程式] 節點,您可以看到投票應用程式類型和您建立的執行個體現在有一個項目。Expand the Applications node to see that there is now an entry for the Voting application type and the instance you created.

    Service Fabric Explorer

  5. 若要連線至執行中的容器,請開啟網頁瀏覽器,並瀏覽至叢集的 URL,例如 http://containertestcluster.eastus.cloudapp.azure.com:80To connect to the running container, open a web browser and navigate to the URL of your cluster; for example, http://containertestcluster.eastus.cloudapp.azure.com:80. 您應會在瀏覽器中看到投票應用程式。You should see the Voting application in the browser.

    投票應用程式網頁

注意

您也可以使用 Docker Compose 來部署 Service Fabric 應用程式。You can also deploy Service Fabric applications with Docker compose. 例如,您可以使用 Docker Compose 搭配下列命令,在叢集上部署及安裝應用程式。For example, the following command could be used to deploy and install the application on the cluster using Docker Compose.

sfctl compose create --deployment-name TestApp --file-path ../docker-compose.yml

容錯移轉叢集中的容器Fail over a container in a cluster

Service Fabric 可確保如果發生失敗,容器執行個體會自動移至叢集中的其他節點。Service Fabric makes sure that your container instances automatically move to other nodes in the cluster if a failure occurs. 您也可以手動清空容器的節點,並將它們依正常程序移至叢集中的其他節點。You can also manually drain a node for containers and move then gracefully to other nodes in the cluster. Service Fabric 提供數種方式讓您調整服務。Service Fabric provides several ways to scale your services. 在下列步驟中,您會使用 Service Fabric Explorer。In the following steps, you use Service Fabric Explorer.

若要容錯移轉前端容器,請執行下列步驟:To fail over the front-end container, do the following steps:

  1. 在您的叢集中開啟 Service Fabric Explorer,例如 https://containertestcluster.eastus.cloudapp.azure.com:19080/ExplorerOpen Service Fabric Explorer in your cluster; for example, https://containertestcluster.eastus.cloudapp.azure.com:19080/Explorer.

  2. 按一下樹狀檢視中的 fabric:/Voting/azurevotefront 節點,然後展開分割區節點 (以 GUID 表示)。Click the fabric:/Voting/azurevotefront node in the tree view and expand the partition node (represented by a GUID). 請注意樹狀檢視中的節點名稱,其中顯示哪些節點上的容器目前正在執行,例如 _nodetype_1Notice the node name in the treeview, which shows you the nodes that the container is currently running on; for example, _nodetype_1.

  3. 展開樹狀檢視中的 [節點] 節點。Expand the Nodes node in the tree view. 按一下正在執行容器之節點旁邊的省略符號 (...)。Click the ellipsis (...) next to the node that is running the container.

  4. 選擇 [重新啟動] 以重新啟動節點並確認重新啟動動作。Choose Restart to restart that node and confirm the restart action. 重新啟動會造成容器容錯移轉至叢集中的其他節點。The restart causes the container to fail over to another node in the cluster.

    Service Fabric Explorer 中的節點檢視

調整叢集中的應用程式和服務Scale applications and services in a cluster

您可以在整個叢集內輕鬆地調整 Service Fabric 服務,以符合服務的負載。Service Fabric services can easily be scaled across a cluster to accommodate for the load on the services. 您可以藉由變更叢集中執行的執行個體數目來調整服務。You scale a service by changing the number of instances running in the cluster.

若要調整 Web 前端服務,請執行下列步驟:To scale the web front-end service, do the following steps:

  1. 在您的叢集中開啟 Service Fabric Explorer,例如 https://containertestcluster.eastus.cloudapp.azure.com:19080Open Service Fabric Explorer in your cluster; for example,https://containertestcluster.eastus.cloudapp.azure.com:19080.

  2. 按一下樹狀檢視中 fabric:/Voting/azurevotefront 節點旁邊的省略符號 (三個點),然後選擇 [調整服務] 。Click the ellipsis (three dots) next to the fabric:/Voting/azurevotefront node in the treeview and choose Scale Service.

    Service Fabric Explorer 調整服務啟動

    您現在可以選擇調整 Web 前端服務的執行個體數目。You can now choose to scale the number of instances of the web front-end service.

  3. 將數字變更為 2,然後按一下 [調整服務] 。Change the number to 2 and click Scale Service.

  4. 按一下樹狀檢視中的 fabric:/Voting/azurevotefront 節點,然後展開分割區節點 (以 GUID 表示)。Click the fabric:/Voting/azurevotefront node in the tree-view and expand the partition node (represented by a GUID).

    Service Fabric Explorer 調整服務完成

    您現在可以看到服務有兩個執行個體。You can now see that the service has two instances. 在樹狀檢視中,您可看到執行個體在哪些節點上執行。In the tree view, you can see which nodes the instances run on.

藉由這項簡單的管理工作,您已讓前端服務可用來處理使用者負載的資源倍增。Through this simple management task, you've doubled the resources available for the front-end service to process user load. 請務必了解,您不需要多個服務執行個體,就能讓服務確實可靠地執行。It's important to understand that you don't need multiple instances of a service for it to run reliably. 如果服務失敗,Service Fabric 可確保會有新的服務執行個體在叢集中執行。If a service fails, Service Fabric makes sure that a new service instance runs in the cluster.

清除資源Clean up resources

使用範本中提供的解除安裝指令碼 (uninstall.sh),刪除叢集中的應用程式執行個體並取消註冊應用程式類型。Use the uninstall script (uninstall.sh) provided in the template to delete the application instance from the cluster and unregister the application type. 此指令碼會花一些時間來清除執行個體,因此您不應在此指令碼之後立即執行安裝指令碼。This script takes some time to clean up the instance, so you should not run the install script immediately after this script. 您可以使用 Service Fabric Explorer 來判斷何時已移除執行個體以及取消註冊應用程式類型。You can use Service Fabric Explorer to determine when the instance has been removed and the application type unregistered.

./uninstall.sh

刪除叢集及其取用之所有資源的最簡單方式,就是刪除資源群組。The simplest way to delete the cluster and all the resources it consumes is to delete the resource group.

登入 Azure 並選取您要移除叢集的訂用帳戶識別碼。Sign in to Azure and select the subscription ID with which you want to remove the cluster. 您可以登入 Azure 入口網站以尋找您的訂用帳戶識別碼。You can find your subscription ID by logging in to the Azure portal. 使用 az group delete 命令來刪除資源群組和所有叢集資源。Delete the resource group and all the cluster resources using the az group delete command.

az login
az account set --subscription <guid>
ResourceGroupName="containertestcluster"
az group delete --name $ResourceGroupName

如果您已完成叢集的處理,可以從憑證存放區中移除憑證。If you are finished working with your cluster, you can remove the certificate from your certificate store. 例如︰For example:

  • 在 Windows 上:使用憑證 MMC 嵌入式管理單元On Windows: Use the Certificates MMC snap-in. 在新增嵌入式管理單元時,請務必選取 [我的使用者帳戶] 。Be sure to select My user account when adding the snap-in. 瀏覽至 Certificates - Current User\Personal\Certificates 並移除憑證。Navigate to Certificates - Current User\Personal\Certificates and remove the certificate.
  • 在 Mac 上:使用 Keychain 應用程式。On Mac: Use the Keychain app.
  • 在 Ubuntu 上:依照您用來檢視憑證的步驟,將憑證移除。On Ubuntu: Follow the steps you used to view certificates and remove the certificate.

後續步驟Next steps

在此快速入門中,您已在 Azure 中將 Linux 容器應用程式部署至 Service Fabric 叢集、對應用程式執行容錯移轉,並調整叢集中的應用程式。In this quickstart, you've deployed a Linux container application to a Service Fabric cluster in Azure, performed fail-over on the application, and scaled the application in the cluster. 若要深入了解如何在 Service Fabric 中使用 Linux 容器,請繼續進行教學課程以了解 Linux 容器應用程式。To learn more about working with Linux containers in Service Fabric, continue to the tutorial for Linux container apps.