桌面应用程序 azure
Serverless infrastructure allows developers to focus on their code without worrying about application infrastructure.
无服务器基础架构允许开发人员专注于他们的代码,而不必担心应用程序基础架构。
TIP: The Azure functions runtime is open source and you can run it outside of Azure cloud (https://github.com/Azure/Azure-Functions).
提示: Azure函数运行库是开源的,您可以在Azure云( https://github.com/Azure/Azure-Functions )之外运行它。
Normally, you don’t have to use docker to deploy your Azure Function project, but sometimes your project requires a specific language version or have a specific dependency or configuration that isn’t provided by just code functionality, for example, ODBC drivers, call operative system programs, connect by VPN to another network, etc.
通常,您不必使用docker来部署Azure Function项目,但是有时您的项目需要特定的语言版本或具有特定的依赖项或配置,而不仅仅是代码功能(例如ODBC驱动程序,调用操作系统程序,通过VPN连接到另一个网络等。
To solve this, you can create Docker custom images with the Azure Function runtime and publish in the Azure Cloud (or in an on-premise infrastructure).
要解决此问题,您可以使用Azure Function运行时创建Docker自定义映像,然后在Azure云中(或在内部部署的基础结构中)发布。
乐在其中! (Let’s rock it!)
in an empty folder, you have to create a python virtual environment and activate it.Open a terminal and run (the activation method can change if you are using PowerShell, bash, cmd, or another terminal).
在一个空文件夹中,您必须创建一个python虚拟环境并将其激活。打开一个终端并运行(如果使用PowerShell,bash,cmd或其他终端,则激活方法可能会更改)。
python -m venv .venv
.venv\scripts\activate
To initialize the Azure Function project run.
要初始化Azure Function项目,请运行。
func init --worker-runtime python --docker
Create an HTTP triggered function.
创建一个HTTP触发函数。
func new --name httpLab --template "HTTP trigger"
Install the python packages.
安装python软件包。
pip install -r requirements.txt
Now, you can open the project in your favorite IDE, for example, VS Code.
现在,您可以在自己喜欢的IDE中打开项目,例如VS Code。
![Image for post](https://miro.medium.com/max/9999/1*6U2mOPhL7P8aJqfvRyjkOA.png)
As you can see, the project has a folder called httpLab with the function files.
如您所见,该项目有一个名为httpLab的文件夹, 其中包含函数文件。
![Image for post](https://miro.medium.com/max/9999/1*PhJaOiBnhgGwSXsa1SLWdQ.png)
Open the __init__.py file and replace it with the following code.
打开__init__.py文件,并将其替换为以下代码。
import logging
import azure.functions as func
def main(req: func.HttpRequest) -> func.HttpResponse:
return func.HttpResponse("This Azure function rocks")
Now, let’s run the Azure Function, in the terminal use the next command.
现在,让我们运行Azure功能,在终端中使用下一条命令。
func start
In your browser open http://localhost:7071/api/httpLab and check the result.
在浏览器中,打开http:// localhost:7071 / api / httpLab并检查结果。
![Image for post](https://miro.medium.com/max/9999/1*x-znPKVBfut9cXGwqMbgzA.png)
Docker化Azure功能 (Dockerizing the Azure Function)
As you can see we ran the code with the “normal ”serverless runtime. Now we will create a custom docker image to install pyodbc with FreeTDS Driver.
如您所见,我们使用“ 常规 ”无服务器运行时运行代码。 现在,我们将创建一个自定义docker映像,以使用FreeTDS驱动程序安装pyodbc 。
Examine the DockerFile and check the base image.
检查DockerFile并检查基本映像。
FROM mcr.microsoft.com/azure-functions/python:3.0-python3.8
Replace the file content with the following code:
用以下代码替换文件内容:
# To enable ssh & remote debugging on app service change the base image to the one below
# FROM mcr.microsoft.com/azure-functions/python:3.0-python3.8-appservice
FROM mcr.microsoft.com/azure-functions/python:3.0-python3.8
# install FreeTDS and dependencies
RUN apt-get update \
&& apt-get install unixodbc -y \
&& apt-get install unixodbc-dev -y \
&& apt-get install freetds-dev -y \
&& apt-get install freetds-bin -y \
&& apt-get install tdsodbc -y \
&& apt-get install --reinstall build-essential -y
# populate "ocbcinst.ini"
RUN echo "[FreeTDS]\n\
Description = FreeTDS unixODBC Driver\n\
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so\n\
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so" >> /etc/odbcinst.ini
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
AzureFunctionsJobHost__Logging__Console__IsEnabled=true
COPY requirements.txt /
RUN pip install -r /requirements.txt
COPY . /home/site/wwwroot
Open the requirementes.txt file and add pyodbc ad sqlalchemy packages.
打开requirementes.txt文件,并添加pyodbc广告SQLAlchemy的包。
azure-functions
pyodbc
sqlalchemy
Open the /httpLab/__init__.py file and replace it with the following code:
打开/ httpLab / __ init__.py文件,并将其替换为以下代码:
import logging
import pyodbc
import azure.functions as func
def main(req: func.HttpRequest) -> func.HttpResponse:
return func.HttpResponse(f"This Azure function rocks. With pyodbc version {pyodbc.version}")
To test the local container, in the /httpLab/function.json file you must change the "authLevel": "function"
to "authLevel": "anonymous"
as the following code:
要测试本地容器,必须在/httpLab/function.json文件中将"authLevel": "function"
更改为"authLevel": "anonymous"
,如下所示:
{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
]
}
To build the docker image you have to tag it with the docker hub or azure container registry id, in your terminal run the next command (replace <docker_id> with your docker registry id).
要构建docker映像,您必须使用docker hub或azure容器注册表ID对其进行标记,在终端中运行下一个命令(将<docker_id>替换为docker注册表ID)。
docker build --tag <docker_id>/globant_lab_image:v1.0.0 .
For example (using Azure Container Registry):
例如(使用Azure容器注册表):
docker build --tag globanlab.azurecr.io/globant_lab_image:v1.0.0 .
To test the build, run the image in a local container using the next command. (replace <docker_id> with your docker registry id).
要测试构建,请使用下一个命令在本地容器中运行映像。 (将<docker_id>替换为您的Docker注册表ID)。
docker run -p 8080:80 -it <docker_id>/globant_lab_image:v1.0.0
For example:
例如:
docker run -p 8080:80 -it globanlab.azurecr.io/globant_lab_image:v1.0.0
To check that everything is running open a browser to http://localhost:8080
要检查一切是否正常,请打开浏览器访问http:// localhost:8080
![Image for post](https://miro.medium.com/max/9999/1*S_PvsQGDDTGbHDyZfOrIHw.png)
To test the function open a browser to http://localhost:8080/api/httpLab
要测试该功能,请打开浏览器到http:// localhost:8080 / api / httpLab
![Image for post](https://miro.medium.com/max/9999/1*vSW-n7X9jgbPxGg85pfxUA.png)
After you’ve verified the function app in the container, stop docker with Ctrl+C.
验证容器中的功能应用后,请使用Ctrl + C停止docker。
Now let’s reset "authLevel": "function"
in /httpLab/function.json :
现在,让我们在/httpLab/function.json中重置"authLevel": "function"
:
{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
]
}
Rebuild the docker image.
重建docker映像。
docker build --tag <docker_id>/globant_lab_image:v1.0.0 .
To share your image, which includes deploying to Azure, you must push it to a registry.
若要共享映像(包括部署到Azure),必须将其推送到注册表。
docker login
docker push <docker_id>/globant_lab_image:v1.0.0
For example (using an Azure Container Registry) :
例如(使用Azure容器注册表):
docker login globanlab.azurecr.io
docker push globanlab.azurecr.io/globant_lab_image:v1.0.0
发布Azure功能 (Publishing the Azure Function)
Create an Azure Functions service, and in the publish option choose Docker Container.
创建一个Azure Functions服务,然后在“ 发布”选项中选择“ Docker容器”。
![Image for post](https://miro.medium.com/max/9999/1*55tEVcEOnj-zqorrp8V17w.png)
Open the created resource and navigate to the container settings blade.
打开创建的资源,然后导航到容器设置刀片。
![Image for post](https://miro.medium.com/max/9999/1*oylVwvmPEBKyMZDxuRdXlw.png)
Fill the form with the information of the container registry where your image was uploaded. As you can see, you can choose between Azure Container Registry, Docker Hub, or a private Registry. (here we are using an Azure Container Registry). Press the save button to start to install the docker image in the Function App.
用上载图像的容器注册表的信息填写表格。 如您所见,您可以在Azure容器注册表,Docker Hub或私有注册表之间进行选择。 (此处使用的是Azure容器注册表)。 按保存按钮开始在功能应用程序中安装docker映像。
![Image for post](https://miro.medium.com/max/9999/1*l5gvyUGNW9Vr9lYM3SMS0w.png)
Refresh the log to check the container installation process. The first time will take more or less 5 minutes while the image is downloaded and loaded.
刷新日志以检查容器安装过程。 在下载和加载图像时,第一次或多或少需要5分钟。
![Image for post](https://miro.medium.com/max/9999/1*mkO7N3hXeHlgROVhRItBcw.png)
Navigate to the function blade and as you can see the httpLab function appears in the list. click in the function name.
导航到功能刀片,您会看到httpLab函数出现在列表中。 单击函数名称。
![Image for post](https://miro.medium.com/max/9999/1*lRUAj-rA_9F7gkJ13RmfkA.png)
Select Get Function Url.
选择获取函数网址 。
![Image for post](https://miro.medium.com/max/9999/1*tCr5FURKZUbxv5w7USMhlw.png)
In the pop-up window, select default (function key) and then copy the URL to the clipboard. The key is the string of characters following ?code=
.
在弹出窗口中,选择默认值(功能键) ,然后将URL复制到剪贴板。 关键是?code=
之后的字符串。
Paste the function URL into your browser’s address bar.
将功能URL粘贴到浏览器的地址栏中。
![Image for post](https://miro.medium.com/max/9999/1*wHN130LvfuFh0U0bydiVOQ.png)
Now you can use CI/CD pipelines to the automatic image build and deployment. In a next story, we will show you how to do it using Azure DevOps pipelines.
现在,您可以使用CI / CD管道来自动构建和部署映像。 在下一个故事中,我们将向您展示如何使用Azure DevOps管道来做到这一点。
ENJOY YOUR CODING.
享受您的编码。
桌面应用程序 azure