Skip to content

在 Amazon Web Services 上托管 n8n(Hosting n8n on Amazon Web Services)#

本托管指南向你展示如何使用亚马逊网络服务(AWS)自托管 n8n。它使用 Postgres 作为数据库后端,并通过 Kubernetes 管理所需的资源和反向代理。

🌐 This hosting guide shows you how to self-host n8n with Amazon Web Services (AWS). It uses n8n with Postgres as a database backend using Kubernetes to manage the necessary resources and reverse proxy.

托管选项(Hosting options)#

AWS 提供多种适合托管 n8n 的方式,包括 EC2(虚拟机)和 EKS(运行在 Kubernetes 上的容器)。

🌐 AWS offers several ways suitable for hosting n8n, including EC2 (virtual machines), and EKS (containers running with Kubernetes).

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

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

先决条件(Prerequisites)#

本指南中的步骤结合使用了 AWS 用户界面和 用于 EKS 的 eksctl CLI 工具

🌐 The steps in this guide use a mix of the AWS UI and the eksctl CLI tool for EKS.

虽然在 eksctl 的文档中没有提到,你还需要安装 AWS CLI 工具,并配置该工具的身份验证

🌐 While not mentioned in the documentation for eksctl, you also need to install the AWS CLI tool, and configure authentication of the 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

创建集群(Create a cluster)#

使用 eksctl 工具创建集群,并指定名称和区域,命令如下:

🌐 Use the eksctl tool to create a cluster specifying a name and a region with the following command:

1
eksctl create cluster --name n8n --region <your-aws-region>

创建集群可能需要一些时间。

🌐 This can take a while to create the cluster.

创建集群后,eksctl 会自动将 kubectl 上下文设置为集群。

🌐 Once the cluster is created, eksctl automatically sets the kubectl context to the 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 what each file does, and what settings 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 部署需要一个持久卷。默认的 AWS 存储类 gp3 适合此用途。这在 postgres-claim0-persistentvolumeclaim.yaml 清单中有定义。

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

1
2
3
4
5
6

spec:
  storageClassName: gp3
  accessModes:
    - ReadWriteOnce

Postgres 环境变量(Postgres environment variables)#

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

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

示例 postgres-secret.yaml 文件包含占位符,你需要将其替换为你自己的用户详细信息和要使用的数据库的值。

🌐 The example postgres-secret.yaml file contains placeholders you need to replace with values of your own for user details and the database to use.

PostgreSQL 使用一个用于设置和管理的 root 用户(POSTGRES_USER),但最佳做法是为 n8n 创建一个单独的非 root 用户(POSTGRES_NON_ROOT_USER)。root 用户拥有完全控制权限,而 n8n 只需要非 root 用户权限即可运行。配置两者可以提高安全性,并有助于防止对数据库系统的意外更改。

🌐 PostgreSQL uses a root user (POSTGRES_USER) for setup and administration, but it’s best practice to create a separate non-root user (POSTGRES_NON_ROOT_USER) for n8n. The root user has full control, while n8n only needs the non-root user permissions to run. Configuring both improves security and helps prevent accidental changes to the database system.

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 并不是必须的,但使用持久卷有助于在使用 n8n 时保留上传的文件,并且如果你想在重启之间保留手动 n8n 加密密钥,它会在启动时将包含密钥的文件保存到文件存储中。

🌐 While not essential for running n8n, using persistent volumes helps maintain files uploaded while using n8n and if you want to persist manual n8n encryption keys between restarts, which saves a file containing the key into file storage during startup.

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
  • 定义要使用的容器图片。
  • 设置资源消耗限制
  • 之前定义的 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 by default.

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

n8n-kubernetes-hosting 目录下运行以下命令,将所有清单发送到集群:

🌐 Send all the manifests to the cluster by running the following command in the n8n-kubernetes-hosting directory:

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 typically operates on a subdomain. Create a DNS record with your provider for the subdomain and point it to a static address of the instance.

要查找在实例上运行的 n8n 服务的地址:

🌐 To find the address of the n8n service running on the instance:

  1. 在 AWS 控制台中打开 Amazon Elastic Kubernetes Service 页面的 集群 部分。
  2. 选择集群名称以打开其配置页面。
  3. 选择 资源 选项卡,然后点击 服务与网络 > 服务
  4. 选择 n8n 服务并复制 负载均衡器 URL 值。将此值与 n8n 服务端口(5678)一起作为 DNS 使用。

使用 HTTP

本指南在其定义的服务中使用 HTTP 连接,例如在 n8n-deployment.yaml 中。然而,如果你点击 负载均衡器 URL 的值,EKS 会将你带到一个 "HTTPS" URL,这会导致错误。为了解决这个问题,当你打开 n8n 子域名时,请确保使用 HTTP。

删除资源(Delete resources)#

如果你需要删除设置,可以使用以下命令移除清单创建的资源:

🌐 If you need to delete the setup, you can remove the resources created by the manifests with the following command:

1
kubectl delete -f .

下一步步骤(Next steps)#