gpt4 book ai didi

python - 将 Python 可执行文件部署到 Azure Service Fabric

转载 作者:太空狗 更新时间:2023-10-30 02:02:40 24 4
gpt4 key购买 nike

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

node js的文档here但我找不到任何与python

相关的内容

最佳答案

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

设置

将 Python 部署到 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/脚本添加到节点路径。
  2. 使用 guest 可执行服务创建 Service Fabric 应用程序。
    1. 将其指向您的 python 代码文件夹
    2. 选择要运行的启动文件 - 将此留空
    3. 选择 CodePackage 作为您的工作文件夹
    4. 按“确定”。
    您现在拥有一个 Service Fabric 应用程序,它将您的代码复制到 Service Fabric 集群,但不执行任何操作。 (实际上它可能会因为你没有定义端点而对你大喊大叫,但那是下一个)

现在我们开始一些黑客攻击。Service Fabric 不会以与 examples from Microsoft 中运行 node.exe 相同的方式运行 Python.exe。 。因此,您需要做的是创建一个 run.cmd 脚本,该脚本基本上执行以下操作:

python.exe main.py

将其用作您的入口点。

现在 ServiceManifest.xml 中的 EntryPoint 如下所示:

...
<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,你可以这样做:

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

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

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

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

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

我希望这对某人有帮助,但请阅读 guest 可执行文件以了解我错过的任何内容。 1

政策

有些东西需要在 Service Fabric 节点上取消权限,这就是如何取消服务才能运行 .cmd、.bat、Python.exe 等。以下编辑来自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,而第二个策略是elicate EntryPoint,又名。您的服务。确保在 ApplicationManifest.xml 底部定义主体。

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

最终想法

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

netstat -na | find "5000"

在节点上验证 API 确实已启动并正在运行。

完成这一切之后,您当然需要在 Azure 门户中为您的 Service Fabric 集群打开负载均衡器中的端口。

关于python - 将 Python 可执行文件部署到 Azure Service Fabric,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39573315/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com