Skip to content

Hosting n8n on Amazon Web Services#

คู่มือนี้จะสอนวิธีติดตั้ง n8n แบบ self-host บน Amazon Web Services (AWS) โดยใช้ n8n กับ Postgres เป็น database backend และใช้ Kubernetes จัดการ resource ต่าง ๆ และ reverse proxy

Hosting options#

AWS มีหลายวิธีให้เลือก deploy n8n เช่น EC2 (virtual machine) หรือ EKS (Kubernetes)

คู่มือนี้จะใช้ EKS ซึ่งเหมาะกับการ scale ตามความต้องการ

Prerequisites#

ขั้นตอนในคู่มือนี้จะใช้ทั้ง AWS UI และ eksctl CLI tool สำหรับ EKS

นอกจากนี้ต้อง ติดตั้ง AWS CLI tool และ ตั้งค่า authentication ด้วย

ความรู้พื้นฐานที่จำเป็นสำหรับการ Self-hosting

การ Self-hosting n8n จำเป็นต้องมีความรู้ทางเทคนิค รวมถึง:

  • การตั้งค่าและกำหนดค่า Server และ Container
  • การจัดการทรัพยากรของแอปพลิเคชันและการปรับขนาด (Scaling)
  • การรักษาความปลอดภัยของ Server และแอปพลิเคชัน
  • การกำหนดค่า n8n

n8n แนะนำให้ผู้ใช้ที่มีความเชี่ยวชาญทำการ Self-hosting การตั้งค่าผิดพลาดอาจนำไปสู่การสูญเสียข้อมูล ปัญหาด้านความปลอดภัย และ Downtime หากคุณไม่มีประสบการณ์ในการจัดการ Server ทาง n8n แนะนำให้ใช้ n8n Cloud แทน

เวอร์ชั่น Latest และ Next

n8n ออกเวอร์ชั่น Minor ใหม่เกือบทุกสัปดาห์ เวอร์ชั่น latest มีไว้สำหรับใช้งานจริง (Production) ส่วน next คือเวอร์ชั่นล่าสุดที่เพิ่งออกมา คุณควรพิจารณา next ว่าเป็นเวอร์ชั่น Beta ซึ่งอาจยังไม่เสถียร หากต้องการรายงานปัญหา โปรดใช้ forum

Current latest: 1.91.2 Current next: 1.92.1

Create a cluster#

ใช้ eksctl สร้าง cluster โดยระบุชื่อและ region:

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

อาจใช้เวลาสักพัก

เมื่อสร้างเสร็จ eksctl จะตั้งค่า kubectl context ให้ใช้ cluster นี้โดยอัตโนมัติ

Clone configuration repository#

Kubernetes กับ n8n ต้องใช้ไฟล์ config หลายไฟล์ สามารถ clone repo ตัวอย่างจาก ที่นี่

รันคำสั่งนี้เพื่อ clone:

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

แล้วเข้าไปที่โฟลเดอร์ที่ clone มา:

1
cd n8n-kubernetes-hosting

Configure Postgres#

สำหรับการใช้งาน n8n ขนาดใหญ่ แนะนำให้ใช้ Postgres เป็น database backend

Configure volume for persistent storage#

เพื่อให้ข้อมูลไม่หายเวลามี pod restart, Postgres ต้องใช้ persistent volume ค่า default storage class ของ AWS คือ gp2 ซึ่งกำหนดไว้ในไฟล์ postgres-claaim0-persistentvolumeclaim.yaml

1
2
3
4
5
6

spec:
  storageClassName: gp2
  accessModes:
    - ReadWriteOnce

Postgres environment variables#

Postgres ต้องการ environment variable บางตัวใน container ตัวอย่างไฟล์ postgres-secret.yaml มี placeholder ให้แก้ไข

postgres-deployment.yaml จะใช้ค่าจากไฟล์นี้ส่งเข้า pod

Configure n8n#

Create a volume for file storage#

ไม่จำเป็นต้องมี persistent volume ก็รัน n8n ได้ แต่ถ้าอยากเก็บไฟล์ที่อัปโหลด หรือเก็บ encryption key ของ n8n แบบ manual ระหว่าง restart ต้องใช้ persistent volume

ไฟล์ n8n-claim0-persistentvolumeclaim.yaml จะสร้าง volume นี้ และ deployment ของ n8n จะ mount volume ใน section volumes ของ n8n-deployment.yaml

1
2
3
4
5
6

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

Pod resources#

Kubernetes สามารถกำหนด resource ขั้นต่ำ/สูงสุดให้แต่ละ container ได้ ตัวอย่างในไฟล์ YAML ที่ clone มาจะมีแบบนี้ใน resources section ของ n8n-deployment.yaml:

1
2
3
4
5
6
7

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

กำหนดขั้นต่ำ 250mb ต่อ container, สูงสุด 500mb, ส่วน CPU ให้ Kubernetes จัดการเอง สามารถปรับค่าตามต้องการได้

  • 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 เพิ่มเติมด้วย environment variable

สร้างไฟล์ n8n-secret.yaml ดูรายละเอียด environment variable ได้ที่ Environment variables

Deployments#

deployment manifest 2 ไฟล์ (n8n-deployment.yaml กับ postgres-deployment.yaml) จะกำหนดรายละเอียดของ n8n กับ Postgres ใน Kubernetes

  • ส่ง environment variable ที่กำหนดเข้าแต่ละ pod
  • กำหนด container image ที่ใช้
  • กำหนด resource limit
  • กำหนด volume และ path ที่จะ mount
  • กำหนดจำนวน pod และ restart policy (ตัวอย่างนี้ใช้ 1 pod ต่อ service สามารถปรับได้)

Services#

service manifest 2 ไฟล์ (postgres-service.yaml กับ n8n-service.yaml) จะ expose service ออกไปผ่าน Kubernetes load balancer ที่ port 5432 และ 5678 ตามลำดับ

Send to Kubernetes cluster#

deploy manifest ทั้งหมดเข้า cluster ด้วยคำสั่งนี้ในโฟลเดอร์ n8n-kubernetes-hosting:

1
kubectl apply -f .

Namespace error

ถ้าเจอ error ว่าไม่เจอ namespace "n8n" ให้รันคำสั่งนี้ก่อน แล้วค่อยรัน apply อีกรอบ:

1
kubectl apply -f namespace.yaml

Set up DNS#

โดยปกติ n8n จะรันบน subdomain ให้สร้าง DNS record ชี้ subdomain ไปที่ static address ของ instance

ดู address ของ n8n service ได้โดย:

  1. เปิด Clusters ใน Amazon Elastic Kubernetes Service บน AWS console
  2. เลือกชื่อ cluster ที่ต้องการ
  3. ไปที่แท็บ Resources แล้วเลือก Service and networking > Services
  4. เลือก n8n service แล้ว copy ค่า Load balancer URLs ใช้ค่านี้ตามด้วย port 5678 ตั้ง DNS

Use HTTP

คู่มือนี้ใช้ HTTP ใน service ที่กำหนดไว้ (เช่นใน n8n-deployment.yaml) แต่ถ้าคลิก Load balancer URLs ใน EKS จะพาไปที่ HTTPS ซึ่งจะ error ให้เปลี่ยนเป็น HTTP ตอนเข้าใช้งาน n8n

Delete resources#

ถ้าต้องการลบ setup นี้ สามารถลบ resource ที่สร้างไว้ด้วยคำสั่ง:

1
kubectl delete -f .

Next steps#

  • เรียนรู้เพิ่มเติมเกี่ยวกับ configuring และ scaling n8n
  • หรือสำรวจการใช้งาน n8n: ลองดู Quickstarts