Skip to content

Instantly share code, notes, and snippets.

@bacarini
Last active March 16, 2024 22:00
Show Gist options
  • Save bacarini/fa12ed61ca8fadfd7f0121cca3fb5fa1 to your computer and use it in GitHub Desktop.
Save bacarini/fa12ed61ca8fadfd7f0121cca3fb5fa1 to your computer and use it in GitHub Desktop.
N8N - Kubernetes complete configuration
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: n8n-deployment
namespace: standard
labels: &labels
app: n8n
component: deployment
spec:
replicas: 1
selector:
matchLabels: *labels
template:
metadata:
labels: *labels
annotations:
prometheus.io/scrape: "true"
prometheus.io/port: "5678"
spec:
containers:
- name: n8n
image: n8nio/n8n
imagePullPolicy: IfNotPresent
ports:
- name: http-metrics
containerPort: 5678
envFrom:
- secretRef:
name: n8n-secrets
livenessProbe:
httpGet:
path: /healthz
port: 5678
readinessProbe:
httpGet:
path: /healthz
port: 5678
resources:
limits:
cpu: "1.0"
memory: "1024Mi"
requests:
cpu: "0.5"
memory: 512Mi
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: n8n-ingress
namespace: standard
labels: &labels
app: n8n
component: ingress
annotations:
kubernetes.io/ingress.class: "nginx"
spec:
tls:
- hosts:
- your.domain.com
secretName: {{ YOUR_SECRET_TLS_CONFIG }}
rules:
- host: your.domain.com
http:
paths:
- path: /
backend:
serviceName: n8n-service
servicePort: 80
apiVersion: v1
kind: Secret
type: Opaque
metadata:
name: n8n-secrets
namespace: standard
labels:
app: n8n
component: secrets
stringData:
DB_TYPE: "postgresdb"
DB_POSTGRESDB_USER: "n8n"
DB_POSTGRESDB_DATABASE: "n8n"
DB_POSTGRESDB_PASSWORD: "{{ PASSWORD }}"
DB_POSTGRESDB_HOST: "{{ HOST }}"
DB_POSTGRESDB_PORT: "5432"
# Basic auth credentials
N8N_BASIC_AUTH_ACTIVE: "true"
N8N_BASIC_AUTH_USER: "n8n"
N8N_BASIC_AUTH_PASSWORD: "{{ PASSWORD }}"
N8N_HOST: "your.domain.com"
N8N_ENCRYPTION_KEY: "{{ PASSWORD }}"
GENERIC_TIMEZONE: "Europe/Lisbon"
WEBHOOK_TUNNEL_URL: "https://your.domain.com/"
NODE_ENV: "production"
N8N_METRICS: "true"
# Increase node max memory
NODE_OPTIONS: "--max_old_space_size=1024"
# Set n8n to work as single thread instead of forking to worker threads
EXECUTIONS_PROCESS: "main"
---
apiVersion: v1
kind: Service
metadata:
name: n8n-service
namespace: standard
annotations:
prometheus.io/probe: "true"
prometheus.io/probe-path: "/healthz"
labels:
app: n8n
component: service
spec:
type: ClusterIP
selector:
app: n8n
component: deployment
ports:
- protocol: TCP
name: http
port: 80
targetPort: 5678
---
apiVersion: v1
kind: Service
metadata:
name: n8n-postgres
namespace: standard
labels: &labels
app: n8n
component: database
spec:
ports:
- name: postgres
port: 5432
targetPort: 5432
clusterIP: None
selector:
app: n8n
component: database
---
apiVersion: apps/v1beta2
kind: StatefulSet
metadata:
name: n8n-postgres
namespace: standard
labels: &labels
app: n8n
component: database
spec:
serviceName: "n8n-postgres"
replicas: 1
selector:
matchLabels: *labels
template:
metadata:
labels: *labels
spec:
containers:
- name: postgresql
image: postgres:10
ports:
- name: postgres
containerPort: 5432
env:
- name: PGDATA
value: /var/lib/postgresql/data/pgdata
- name: POSTGRES_USER
value: n8n
- name: POSTGRES_DB
value: n8n
- name: POSTGRES_PASSWORD
value: {{ PASSWORD }}
volumeMounts:
- name: data
mountPath: /var/lib/postgresql/data
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: "gp2"
resources:
requests:
storage: 1Gi
@Kokolokoli
Copy link

Do you know why a path like "/n8n" won't work?
Thank you for this btw, helped guide my configuration.

@bacarini
Copy link
Author

bacarini commented Nov 5, 2021

Hi @Kokolokoli
Could you elaborate more on the problem? Have you tried to run docker run -it --rm n8nio/n8n /bin/bash?

@Kokolokoli
Copy link

Kokolokoli commented Nov 5, 2021

Simply put, I have set up n8n in a k8s pod and it runs with no issue. I have made it accessible through the kubernetes maintained Nginx Ingress Controller and the following ingress ressource:

# Ingress config file 
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx-ingress
spec:
  ingressClassName: nginx
  tls:
  - hosts: 
      - subdomain.myhost.com
  rules:
  - host: subdomain.myhost.com
    http:
      paths:
        - path: /
          pathType: Prefix
          backend:
            service:
              name: n8n-service
              port: 
                number: 8080

and i can access it via my subdomain.myhost.com (although I notice it adds a "/workflow" after the url as if n8n redirected me).
I have ran it with docker as well in the past and worked out nicely. I also made it work with a NodePort service instead of the current clusterIP service + Ingress.

However, if I change the path in my ingress ressource file from "/" to "/n8n":

  • accessing the host returns 404 (as it should)
  • accessing subdomain.myhost.com/n8n returns a blank page with no redirection to "subdomain.myhost.com/n8n/workflow" and no 404 message (understandable since nginx finds the path to send the request to).

What might be causing this blank page?

@bacarini
Copy link
Author

bacarini commented Nov 5, 2021

hmm got it
have you tried changing ENV vars?
have a look at the configuration doc https://docs.n8n.io/reference/environment-variables.html#deployment

@Kokolokoli
Copy link

Sorry for the late reply, haven't had much time to try it as the focus of my tasks somewhat switched. That is a very interesting idea! I think this might work, I will let you know how it goes.
Thank you!

@Kokolokoli
Copy link

Kokolokoli commented Nov 16, 2021

Well, I guess it didn't work.
Tried changing the N8N_PATH to subdomain.myhost.com/n8n and it didn't work; now both the subdomain.myhost.com and subdomain.myhost.com/n8n return blank pages...

I also tried N8N_HOST=subdomain.myhost.com/ && N8N_PATH=/n8n, same result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment