将Python可执行文件部署到Azure Service Fabric [英] Deploy Python executable to Azure Service Fabric

查看:71
本文介绍了将Python可执行文件部署到Azure Service Fabric的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找有用的解决方案,以将 Python 可执行文件部署到 Azure Service Fabric

I am looking for helpful solutions to deploy Python executable to Azure Service Fabric

有关于 node js 此处,但我找不到与 python

There is documentation for node js here but I could not find anything related to python

推荐答案

提示:节省一些时间,并尝试首先使其在本地运行.我的经验是,很多事情都会出错,最难确定的问题之一就是节点上的执行权限.

A tip: Save yourself some time and try to get this running locally first. My experience is that plenty of things can go wrong, one of the hardest ones to determine was execution rights on the nodes.

将Python部署到Service Fabric所需的步骤:

Steps needed to deploy Python to Service Fabric:

  1. 确保在SF节点上安装了Python(带有所需的软件包).有两种方法可以做到这一点:
    • 使用远程桌面使用端点"sf_cluster.westeurope.cloudapp.azure.com:node_port"导航到节点其中node_port对我而言是3389-3393.然后通过从Python.org下载安装程序来手动安装python.
    • 创建一个在Guest Executable SetupEntryPoint中调用的python安装脚本,有关更多内容,请参见下文.
    • 无论如何,您都需要将Python放入节点并确保将Python和Python/脚本添加到节点路径.
  1. Make sure that Python is installed on the SF Nodes (with the packages you need). There are two ways to do this:
    • Use Remote Desktop to navigate to the nodes using your endpoint "sf_cluster.westeurope.cloudapp.azure.com:node_port" where node_port is (for me) 3389-3393. And then install python manually by downloading a installer from Python.org.
    • Create a python install script that you call in the Guest Executable SetupEntryPoint, more on this further down.
    • In any case you need to get Python unto the Nodes and Make sure to add Python and Python/Scripts to the node paths.
  1. 将其指向您的python代码文件夹
  2. 选择要运行的启动文件-将此留空
  3. 选择CodePackage作为您的工作文件夹
  4. 按确定.

现在,您有一个服务结构应用程序,该应用程序会将您的代码复制到您的服务结构群集中,但是什么也不做.(实际上,它可能会因为没有定义端点而大吼大叫,但这是下一个)

You now have a service fabric application that will copy your code to your service fabric cluster but do nothing. (actually it will probably yell at you for not defining a Endpoint, but that's next)

现在,我们开始进行一些黑客攻击.Service Fabric将无法以与运行

Now we get to some hacking. Service Fabric will not run Python.exe in the same way that it runs node.exe in the examples from Microsoft. So what you need to do is create a run.cmd script that basically does this:

python.exe main.py

python.exe main.py

使用它作为您的EntryPoint.

use that as your EntryPoint.

现在,您在ServiceManifest.xml中的EntryPoint看起来像这样:

Now your EntryPoint in ServiceManifest.xml looks like this:

...
    <EntryPoint>
        <ExeHost>
            <Program>run.cmd</Program>
            <Arguments></Arguments>
            <WorkingFolder>CodePackage</WorkingFolder>
            <ConsoleRedirection FileRetentionCount="5" FileMaxSizeInKb="2048"/> //use this to get logging (very helpful ;)
        </ExeHost>
    </EntryPoint>
</CodePackage>

如果您想让 SetupEntryPoint 为您安装python,则可以执行以下操作:

if you want a SetupEntryPoint to install python for you you could do this:

<SetupEntryPoint>
  <ExeHost>
    <Program>Setup\setup.bat</Program>
    <WorkingFolder>CodePackage</WorkingFolder>
  </ExeHost>
</SetupEntryPoint>

我的setup.bat看起来像这样(路径有问题):

My setup.bat looks like this (had some issues with the path):

C:\ Windows \ System32 \ WindowsPowerShell \ v1.0 \ powershell.exe -ExecutionPolicy Bypass-命令.\ Setup \ pythonInstall.ps1"

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -Command ".\Setup\pythonInstall.ps1"

我的解决方案中的代码被复制到以下结构中:

The code in my solution is copied into this structure:

MyGuestExecutablePkg\
    |
    - Code\
        - run.cmd
        - main.py
        - Setup\
            - setup.bat
        - ....
    - Config\
        - Settings.xml
    - ServiceManifest.xml
ApplicationManifest.xml

我希望这对某人有帮助,但是请务必阅读Guest Executable文档以了解我错过的任何内容. 1

I hope this helps someone but do read the Guest Executable documentation for anything I've missed. 1

有些事情需要在Service Fabric节点上消除特权,这就是您消除服务以使其运行.cmd,.bat,Python.exe等的方式.以下编辑来自ApplicationManifest.xml

Some things need elivated priviledges on the Service Fabric Nodes and this is how you elivate your service in order for it to run .cmd, .bat, Python.exe etc. The following edits are from ApplicationManifest.xml

....
<ServiceManifestImport>
    <ServiceManifestRef ServiceManifestName="MyGuestExecutablePkg" ServiceManifestVersion="1.0.0" />
    <ConfigOverrides />
    <Policies>
      <RunAsPolicy CodePackageRef="Code" UserRef="SetupAdminUser" EntryPointType="Setup" />
      <RunAsPolicy CodePackageRef="Code" UserRef="SetupAdminUser" EntryPointType="Main" />
    </Policies>
  </ServiceManifestImport>
....

第一个策略是提升SetupEntryPoint,而第二个策略是提升EntryPoint.您的服务.确保在ApplicationManifest.xml的底部定义主体.

The first policy is to elevate the SetupEntryPoint while the second is to elicate the EntryPoint, aka. your service. Make sure you define the Principals at the bottom of ApplicationManifest.xml.

</DefaultServices>
  <Principals>
    <Users>
      <User Name="SetupAdminUser">
        <MemberOf>
          <SystemGroup Name="Administrators" />
        </MemberOf>
      </User>
    </Users>
  </Principals>
</ApplicationManifest>

最终想法

我已经以Flask Api的身份运行我的服务,因为这非常简单.因此,所有" Service Fabric要做的就是启动Flask api,然后确保崩溃时将其重新启动.通过执行以下操作,可以轻松测试和验证部署的哪一部分崩溃以及它是否已启动并正在运行

Final thoughts

I've run my service as a Flask Api as this was ridiculously easy. So "all" Service Fabric does is start the Flask api and then make sure it is restarted if it crashes. This makes it easy to test and verify which part of my deployment was crashing and if it was up and running by performing

netstat -na | find "5000"

在节点上

验证api是否确实已启动并正在运行.

on the nodes to verify that the api was indeed up and running.

所有这些之后,您当然需要在Azure门户中为服务结构群集打开负载均衡器中的端口.

After all this you, of course, need to open up the ports in the load balanser for your service fabric cluster in the Azure Portal.

这篇关于将Python可执行文件部署到Azure Service Fabric的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆