Seeking assistance with Terraform GKE ingress for n8n deployment

I’m trying to set up n8n on a GKE cluster using its Helm chart. The main issue I’m facing is that the ingress keeps losing the session when pointing to the app. It works fine when I access the pod directly, so I think the problem is with the ingress.

I want to set up session affinity on the ingress, but I’m not sure how to do this with Terraform. I’ve also considered using an Nginx ingress, but I’m new to that. Can anyone help me figure this out or suggest a better way to handle the ingress?

Here’s a simplified version of my Terraform config:

resource "helm_release" "workflow_app" {
  chart      = "workflow-automation"
  name       = "auto-workflow"
  namespace  = "automations"
  
  set {
    name  = "config.db.host"
    value = data.remote_state.cluster.outputs.db_connection
  }
}

resource "kubernetes_ingress" "workflow_ingress" {
  metadata {
    name = "auto-workflow-ingress"
    annotations = {
      "ingress.kubernetes.io/ssl-redirect" = "true"
    }
  }
  spec {
    backend {
      service_name = helm_release.workflow_app.name
      service_port = 8080
    }
  }
}

Any ideas on how to fix the session issue or improve this setup?

hey, i’ve dealt with similar issues. have u tried adding the ‘kubernetes.io/ingress.class: gce’ annotation to ur ingress? also, for session affinity, u can use ‘nginx.ingress.kubernetes.io/affinity: cookie’ annotation. might solve ur problem without switching to nginx ingress. goodluck!

I’ve encountered similar challenges with GKE ingress and session persistence. One effective approach is to leverage the built-in capabilities of Google Cloud Load Balancing. You can modify your Terraform configuration to include the necessary annotations for session affinity:

resource "kubernetes_ingress" "workflow_ingress" {
  metadata {
    name = "auto-workflow-ingress"
    annotations = {
      "kubernetes.io/ingress.class" = "gce"
      "kubernetes.io/ingress.global-static-ip-name" = "your-static-ip-name"
      "networking.gke.io/v1beta1.FrontendConfig" = "ingress-frontend-config"
    }
  }
  // ... rest of your configuration
}

Additionally, create a FrontendConfig resource to enable session affinity:

resource "kubernetes_manifest" "frontend_config" {
  manifest = {
    apiVersion = "networking.gke.io/v1beta1",
    kind       = "FrontendConfig",
    metadata = {
      name = "ingress-frontend-config",
      namespace = "automations"
    },
    spec = {
      sessionAffinity = {
        clientIP = {
          clientIPConfig = {
            timeoutSeconds = 600
          }
        }
      }
    }
  }
}

This configuration should help maintain session persistence for your n8n deployment.

As someone who’s worked extensively with GKE and n8n, I can say that session affinity issues are pretty common. One approach that’s worked well for me is using a Google Cloud Load Balancer (GCLB) with a backend config. Here’s what you could try:

Add a backendConfig to your Terraform:

resource "kubernetes_manifest" "backend_config" {
  manifest = {
    apiVersion = "cloud.google.com/v1",
    kind       = "BackendConfig",
    metadata = {
      name = "n8n-backend-config",
      namespace = "automations"
    },
    spec = {
      sessionAffinity = {
        affinityType = "GENERATED_COOKIE",
        affinityCookieTtlSec = 3600
      }
    }
  }
}

Then, update your service to use this backendConfig:

resource "kubernetes_service" "n8n_service" {
  metadata {
    name = "n8n-service",
    annotations = {
      "cloud.google.com/backend-config" = "{\"default\": \"n8n-backend-config\"}"
    }
  }
  // ... rest of your service config
}

This should help maintain session persistence without switching to Nginx. Let me know if you need any clarification!