Hosting n8n on Azure#
คู่มือนี้จะสอนวิธีติดตั้ง n8n แบบ self-host บน Azure โดยใช้ n8n กับ Postgres เป็น database backend และใช้ Kubernetes จัดการ resource ต่าง ๆ และ reverse proxy
Prerequisites#
ต้องมี Azure command line tool
ความรู้พื้นฐานที่จำเป็นสำหรับการ 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
Hosting options#
Azure มีหลายวิธีให้ deploy n8n เช่น Azure Container Instances, Linux VM, หรือ Azure Kubernetes Service (AKS)
คู่มือนี้จะใช้ AKS ซึ่งเหมาะกับการ scale ตามความต้องการ
ขั้นตอนในคู่มือนี้จะใช้ทั้ง Azure UI และ command line tool สามารถเลือกใช้วิธีไหนก็ได้
Open the Azure Kubernetes Service#
เข้า Azure portal แล้วเลือก Kubernetes services
Create a cluster#
ในหน้า Kubernetes services กด Create > Create a Kubernetes cluster
ตั้งค่าตามต้องการ แล้วกด Create
Set Kubectl context#
ขั้นตอนต่อไปต้องตั้งค่า Azure instance ให้เป็น Kubectl context ดูรายละเอียดได้จากปุ่ม Connect ในหน้า cluster จะมี code snippet ให้ copy ไปวางใน terminal เพื่อเปลี่ยน context
Clone configuration repository#
Kubernetes กับ n8n ต้องใช้ไฟล์ config หลายไฟล์ สามารถ clone repo ตัวอย่างจาก ที่นี่
รันคำสั่งนี้เพื่อ clone:
1 |
|
แล้วเข้าไปที่โฟลเดอร์ที่ clone มา:
1 |
|
Configure Postgres#
สำหรับการใช้งาน n8n ขนาดใหญ่ แนะนำให้ใช้ Postgres เป็น database backend
Configure volume for persistent storage#
เพื่อให้ข้อมูลไม่หายเวลามี pod restart, Postgres ต้องใช้ persistent volume ค่า default storage class ของ Azure กำหนดไว้ในไฟล์ postgres-claim0-persistentvolumeclaim.yaml
Specialized storage classes
ถ้าต้องการ storage class แบบพิเศษหรือประสิทธิภาพสูง อ่านรายละเอียดได้ที่ Azure documentation
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 |
|
Pod resources#
Kubernetes lets you สามารถกำหนด resource ขั้นต่ำ/สูงสุดให้แต่ละ container ได้ ตัวอย่างในไฟล์ YAML ที่ clone มาจะมีแบบนี้ใน resources
section ของ n8n-deployment.yaml
:
1 2 3 4 5 6 7 |
|
กำหนดขั้นต่ำ 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 ด้วยคำสั่งนี้:
1 |
|
Namespace error
ถ้าเจอ error ว่าไม่เจอ namespace "n8n" ให้รันคำสั่งนี้ก่อน แล้วค่อยรัน apply อีกรอบ:
1 |
|
Set up DNS#
โดยปกติ n8n จะรันบน subdomain ให้สร้าง DNS record ชี้ subdomain ไปที่ IP ของ n8n service ดู IP ได้จาก Services & ingresses ของ cluster ใน column External IP ต้องเติม port "5678" ต่อท้าย URL ด้วย
Static IP addresses with AKS
อ่านวิธีใช้ static IP กับ AKS ได้ที่ ที่นี่
Delete resources#
ถ้าต้องการลบ resource ที่สร้างไว้ ให้ใช้คำสั่ง:
1 |
|
Next steps#
- เรียนรู้เพิ่มเติมเกี่ยวกับ configuring และ scaling n8n
- หรือสำรวจการใช้งาน n8n: ลองดู Quickstarts