Skip to content

在 Azure 上托管 n8n(Hosting n8n on Azure)#

本托管指南向你展示如何在 Azure 上自托管 n8n。它使用 n8n 配合 Postgres 作为数据库后端,并使用 Kubernetes 管理所需的资源和反向代理。

🌐 This hosting guide shows you how to self-host n8n on Azure. It uses n8n with Postgres as a database backend using Kubernetes to manage the necessary resources and reverse proxy.

先决条件(Prerequisites)#

你需要 Azure 命令行工具

🌐 You need The Azure command line tool

Self-hosting knowledge prerequisites

Self-hosting n8n requires technical knowledge, including:

  • Setting up and configuring servers and containers
  • Managing application resources and scaling
  • Securing servers and applications
  • Configuring n8n

n8n recommends self-hosting for expert users. Mistakes can lead to data loss, security issues, and downtime. If you aren't experienced at managing servers, n8n recommends n8n Cloud.

Stable and Beta versions

n8n releases a new minor version most weeks. The stable version is for production use. beta is the most recent release. The beta version may be unstable. To report issues, use the forum.

Current stable: 2.4.7 Current beta: 2.6.2

托管选项(Hosting options)#

Azure 提供多种适合托管 n8n 的方式,包括 Azure 容器实例(针对运行容器进行了优化)、Linux 虚拟机和 Azure Kubernetes 服务(使用 Kubernetes 运行的容器)。

🌐 Azure offers several ways suitable for hosting n8n, including Azure Container Instances (optimized for running containers), Linux Virtual Machines, and Azure Kubernetes Service (containers running with Kubernetes).

本指南使用 Azure Kubernetes 服务 (AKS) 作为托管选项。使用 Kubernetes 需要一些额外的复杂性和配置,但这是根据需求变化扩展 n8n 的最佳方法。

🌐 This guide uses the Azure Kubernetes Service (AKS) as the hosting option. Using Kubernetes requires some additional complexity and configuration, but is the best method for scaling n8n as demand changes.

本指南中的步骤结合使用了 Azure UI 和命令行工具,但你可以使用其中任何一种来完成大多数任务。

🌐 The steps in this guide use a mix of the Azure UI and command line tool, but you can use either to accomplish most tasks.

打开 Azure Kubernetes 服务(Open the Azure Kubernetes Service)#

Azure 门户 中选择 Kubernetes 服务

🌐 From the Azure portal select Kubernetes services.

创建集群(Create a cluster)#

在 Kubernetes 服务页面,选择 创建 > 创建 Kubernetes 集群

🌐 From the Kubernetes services page, select Create > Create a Kubernetes cluster.

您可以选择任何适合您需求的配置选项,完成后请选择 创建

🌐 You can select any of the configuration options that suit your needs, then select Create when done.

设置 Kubectl 上下文(Set Kubectl context)#

本指南的其余步骤需要将 Azure 实例设置为 Kubectl 上下文。你可以通过打开集群实例的详细信息页面,然后点击 Connect 按钮来找到连接详情。生成的代码片段显示了将其粘贴到终端中运行的步骤,以更改本地 Kubernetes 设置以使用新集群。

🌐 The remainder of the steps in this guide require you to set the Azure instance as the Kubectl context. You can find the connection details for a cluster instance by opening its details page and then the Connect button. The resulting code snippets shows the steps to paste and run into a terminal to change your local Kubernetes settings to use the new cluster.

克隆配置库(Clone configuration repository)#

Kubernetes 和 n8n 需要一系列配置文件。你可以从 这个仓库 克隆它们。以下步骤将告诉你哪个文件配置了什么,以及你需要修改哪些内容。

🌐 Kubernetes and n8n require a series of configuration files. You can clone these from this repository. The following steps tell you which file configures what and what you need to change.

使用以下命令克隆存储库:

🌐 Clone the repository with the following command:

1
git clone https://github.com/n8n-io/n8n-hosting.git

切换目录:

🌐 And change directory:

1
cd n8n-hosting/kubernetes

配置 Postgres(Configure Postgres)#

对于更大规模的 n8n 部署,Postgres 比 SQLite 提供更强大的数据库后端。

🌐 For larger scale n8n deployments, Postgres provides a more robust database backend than SQLite.

配置持久存储卷(Configure volume for persistent storage)#

为了在 Pod 重启时保持数据,Postgres 部署需要一个持久卷。默认的存储类适合这一目的,并且在 postgres-claim0-persistentvolumeclaim.yaml 清单中定义。

🌐 To maintain data between pod restarts, the Postgres deployment needs a persistent volume. The default storage class is suitable for this purpose and is defined in the postgres-claim0-persistentvolumeclaim.yaml manifest.

专用存储类

如果你对存储类别有专业或更高的要求,请在文档中了解 Azure 提供的选项

Postgres 环境变量(Postgres environment variables)#

Postgres 需要设置一些环境变量才能将其传递给在容器中运行的应用。

🌐 Postgres needs some environment variables set to pass to the application running in the containers.

示例 postgres-secret.yaml 文件包含需要你替换为自己值的占位符。Postgres 在创建数据库时会使用这些信息。

🌐 The example postgres-secret.yaml file contains placeholders you need to replace with your own values. Postgres will use these details when creating the database..

postgres-deployment.yaml 清单然后使用此清单文件中的值发送到应用 Pod。

🌐 The postgres-deployment.yaml manifest then uses the values from this manifest file to send to the application pods.

配置 n8n(Configure n8n)#

创建文件卷存储(Create a volume for file storage)#

虽然并非运行 n8n 的必要条件,但使用持久卷是必需的:

🌐 While not essential for running n8n, using persistent volumes is required for:

  • 使用与文件交互的节点,例如二进制数据节点。
  • 如果你想在重启之间保留手动 n8n 加密密钥,这会在启动期间将包含密钥的文件保存到文件存储中。

n8n-claim0-persistentvolumeclaim.yaml 清单创建了这个,而 n8n 部署在 n8n-deployment.yaml 清单的 volumes 部分挂载了该声明。

🌐 The n8n-claim0-persistentvolumeclaim.yaml manifest creates this, and the n8n Deployment mounts that claim in the volumes section of the n8n-deployment.yaml manifest.

1
2
3
4
5
6

volumes:
  - name: n8n-claim0
    persistentVolumeClaim:
      claimName: n8n-claim0

Pod 资源(Pod resources)#

Kubernetes 允许你 可选地指定应用容器所需的最小资源以及它们可以使用的资源上限。上面克隆的示例 YAML 文件在 n8n-deployment.yaml 文件的 resources 部分包含以下内容:

1
2
3
4
5
6
7

resources:
  requests:
    memory: "250Mi"
  limits:
    memory: "500Mi"
    

这定义了每个容器最低 250mb,最高 500mb,并让 Kubernetes 管理 CPU。你可以根据自己的需求更改这些值。作为参考,以下是 n8n 云服务的资源值:

🌐 This defines a minimum of 250mb per container, a maximum of 500mb, and lets Kubernetes handle CPU. You can change these values to match your own needs. As a guide, here are the resources values for the n8n cloud offerings:

  • Start: 320mb RAM, 10 millicore CPU burstable
  • Pro (10k executions): 640mb RAM, 20 millicore CPU burstable
  • Pro (50k executions): 1280mb RAM, 80 millicore CPU burstable

可选:环境变量(Optional: Environment variables)#

你可以使用环境变量配置 n8n 设置和行为。

🌐 You can configure n8n settings and behaviors using environment variables.

创建一个 n8n-secret.yaml 文件。有关 n8n 环境变量的详细信息,请参阅 环境变量

🌐 Create an n8n-secret.yaml file. Refer to Environment variables for n8n environment variables details.

部署(Deployments)#

这两个部署清单(n8n-deployment.yamlpostgres-deployment.yaml)定义了 n8n 和 Postgres 应用在 Kubernetes 上的部署。

🌐 The two deployment manifests (n8n-deployment.yaml and postgres-deployment.yaml) define the n8n and Postgres applications to Kubernetes.

清单定义了以下内容:

🌐 The manifests define the following:

  • 将定义的环境变量发送到每个应用 pod
  • 定义要使用的容器图片。
  • 使用 resources 对象设置资源消耗限制
  • 之前定义的 volumesvolumeMounts 用于定义容器中挂载卷的路径。
  • 扩展和重启策略。示例清单定义了每个 Pod 的一个实例。你应该根据自己的需求进行修改。

服务(Services)#

这两个服务清单(postgres-service.yamln8n-service.yaml)通过 Kubernetes 负载均衡器将服务暴露给外部,分别使用端口 5432 和 5678。

🌐 The two service manifests (postgres-service.yaml and n8n-service.yaml) expose the services to the outside world using the Kubernetes load balancer using ports 5432 and 5678 respectively.

发送到 Kubernetes 集群(Send to Kubernetes cluster)#

使用以下命令将所有清单发送到集群:

🌐 Send all the manifests to the cluster with the following command:

1
kubectl apply -f .

Namespace error

You may see an error message about not finding an "n8n" namespace as that resources isn't ready yet. You can run the same command again, or apply the namespace manifest first with the following command:

1
kubectl apply -f namespace.yaml

设置 DNS(Set up DNS)#

n8n 通常运行在子域名上。在你的服务提供商处为该子域名创建一个 DNS 记录,并将其指向 n8n 服务的 IP 地址。你可以在要使用的集群的 服务与入口(Services & ingresses) 菜单项下的 外部 IP(External IP) 列中找到 n8n 服务的 IP 地址。你需要在 URL 中添加 n8n 端口“5678”。

🌐 n8n typically operates on a subdomain. Create a DNS record with your provider for the subdomain and point it to the IP address of the n8n service. Find the IP address of the n8n service from the Services & ingresses menu item of the cluster you want to use under the External IP column. You need to add the n8n port, "5678" to the URL.

使用 AKS 的静态 IP 地址

阅读本教程 了解有关如何在 AKS 中使用静态 IP 地址的更多详细信息。

删除资源(Delete resources)#

使用以下命令移除清单创建的资源:

🌐 Remove the resources created by the manifests with the following command:

1
kubectl delete -f .

下一步步骤(Next steps)#