Skip to content

在 Google Kubernetes Engine 上托管 n8n(Hosting n8n on Google Kubernetes Engine)#

Google Cloud 提供多种适合托管 n8n 的选项,包括 Cloud Run(针对运行容器进行了优化)、Compute Engine(虚拟机)和 Kubernetes Engine(使用 Kubernetes 运行容器)。

🌐 Google Cloud offers several options suitable for hosting n8n, including Cloud Run (optimized for running containers), Compute Engine (VMs), and Kubernetes Engine (containers running with Kubernetes).

本指南使用 Google Kubernetes Engine (GKE) 作为托管选项。如果你想使用 Cloud Run,请参阅这些说明

🌐 This guide uses the Google Kubernetes Engine (GKE) as the hosting option. If you want to use Cloud Run, refer to these instructions.

本指南中的大多数步骤都使用 Google Cloud 用户界面,但你也可以使用 gcloud 命令行工具 来执行所有步骤。

🌐 Most of the steps in this guide use the Google Cloud UI, but you can also use the gcloud command line tool instead to undertake all the steps.

先决条件(Prerequisites)#

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

创建项目(Create project)#

GCP 鼓励你创建项目来逻辑性地组织资源和配置。在 Google Cloud 控制台中为你的 n8n 部署创建一个新项目:点击项目下拉菜单,然后点击 新建项目 按钮。然后选择新创建的项目。在你按照本指南的其他步骤操作时,确保选择了正确的项目。

🌐 GCP encourages you to create projects to logically organize resources and configuration. Create a new project for your n8n deployment from your Google Cloud Console: select the project dropdown menu and then the NEW PROJECT button. Then select the newly created project. As you follow the other steps in this guide, make sure you have the correct project selected.

启用 Kubernetes Engine API(Enable the Kubernetes Engine API)#

GKE 默认未启用。在顶部搜索栏中搜索“Kubernetes”,然后从搜索结果中选择“Kubernetes Engine”。

🌐 GKE isn't enabled by default. Search for "Kubernetes" in the top search bar and select "Kubernetes Engine" from the results.

选择 启用 以为此项目启用 Kubernetes 引擎 API。

🌐 Select ENABLE to enable the Kubernetes Engine API for this project.

创建集群(Create a cluster)#

GKE 服务页面 中,选择 Clusters > CREATE。确保选择“标准”集群选项,n8n 不支持“自动驾驶”集群。除非有需要更改的特定内容(比如位置),否则可以保持集群配置为默认设置。

🌐 From the GKE service page, select Clusters > CREATE. Make sure you select the "Standard" cluster option, n8n doesn't work with an "Autopilot" cluster. You can leave the cluster configuration on defaults unless there's anything specifically you need to change, such as location.

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

本指南的其余步骤需要你将 GCP 实例设置为 Kubectl 上下文。你可以通过打开集群实例的详细信息页面并选择 连接 来找到该实例的连接详细信息。显示的代码片段展示了 gcloud CLI 工具的连接字符串。将代码片段粘贴到 gcloud CLI 中运行,以更改本地 Kubernetes 设置以使用新的 gcloud 集群。

🌐 The rest of the steps in this guide require you to set the GCP instance as the Kubectl context. You can find the connection details for a cluster instance by opening its details page and selecting CONNECT. The displayed code snippet shows a connection string for the gcloud CLI tool. Paste and run the code snippet in the gcloud CLI to change your local Kubernetes settings to use the new gcloud cluster.

克隆配置库(Clone configuration repository)#

Kubernetes 和 n8n 需要一系列配置文件。你可以从 此仓库 本地克隆这些文件。以下步骤说明了文件配置及如何添加你的信息。

🌐 Kubernetes and n8n require a series of configuration files. You can clone these from this repository locally. The following steps explain the file configuration and how to add your information.

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

🌐 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.

创建持久存储卷(Create a volume for persistent storage)#

为了在 Pod 重启之间保持数据,Postgres 部署需要一个持久卷。在 GCP 上运行 Postgres 需要特定的 Kubernetes 存储类。你可以阅读 本指南 获取具体信息,但 storage.yaml 清单会为你创建它。你可能希望在 allowedTopologies > matchedLabelExpressions > values 键下更改区域以创建存储。默认情况下,它们设置为 us-central

🌐 To maintain data between pod restarts, the Postgres deployment needs a persistent volume. Running Postgres on GCP requires a specific Kubernetes Storage Class. You can read this guide for specifics, but the storage.yaml manifest creates it for you. You may want to change the regions to create the storage in under the allowedTopologies > matchedLabelExpressions > values key. By default, they're set to us-central.

1
2
3
4
5
6
7

allowedTopologies:
  - matchLabelExpressions:
      - key: failure-domain.beta.kubernetes.io/zone
        values:
          - us-central1-b
          - us-central1-c

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.yamlpostgres-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 & Ingress 菜单项中的 Endpoints 列找到 n8n 服务的 IP 地址。

🌐 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 & Ingress menu item of the cluster you want to use under the Endpoints column.

GKE 和 IP 地址

阅读此 GKE 教程 以了解有关保留 IP 地址如何与 GKE 和 Kubernetes 资源一起使用的更多详细信息。

删除资源(Delete resources)#

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

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

1
kubectl delete -f .

下一步步骤(Next steps)#