您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

快速入门:将 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.

本快速入门介绍如何在 Azure 上将 Linux 容器部署到 Service Fabric 群集。This quickstart shows how to deploy Linux containers to a Service Fabric cluster on Azure. 完成后,Service Fabric 群集中会运行一个由 Python Web 前端和 Redis 后端组成的 Voting 应用程序。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.

Voting 应用网页

先决条件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. 默认情况下,会在群集 VM 和 Azure 负载均衡器上打开端口 80。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),在 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. 使用安装脚本将 Voting 应用程序定义复制到群集,注册应用程序类型,并创建应用程序的实例。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. 打开 Web 浏览器,导航到群集的 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. 展开“应用程序”节点,可以看到 Voting 应用程序类型的条目以及创建的实例。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. 若要连接到正在运行的容器,请打开 Web 浏览器,导航到群集的 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.

    Voting 应用网页

备注

也可使用 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. 此脚本需要一定的时间来清理实例,因此不应在运行此脚本后立即运行 install script。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,选择要删除的群集的订阅 ID。Log in to Azure and select the subscription ID with which you want to remove the cluster. 可通过登录到 Azure 门户查找订阅 ID。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

在本快速入门中,你已将 Linux 容器应用程序部署到 Azure 中的 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.