# Hosting n8n on Google Cloud This hosting guide shows you how to self-host n8n on Google Cloud (GCP). It uses n8n with Postgres as a database backend using Kubernetes to manage the necessary resources and reverse proxy. ## Prerequisites - [The gcloud command line tool](https://cloud.google.com/sdk/gcloud/){:target="_blank" .external-link} ## Hosting options Google Cloud offers several ways suitable for hosting n8n, including Cloud Run (optimized for running containers), Compute Engine (VMs), and Kubernetes Engine (containers running with Kubernetes). This guide uses the Google Kubernetes Engine (GKE) as the hosting option. Using Kubernetes requires some additional complexity and configuration, but is the best method for scaling n8n as demand changes. Most of the steps in this guide use the Google Cloud UI, but you can also use the [gcloud command line tool](https://cloud.google.com/sdk/gcloud/){:target="_blank" .external-link} instead to undertake all the steps. ## Create project GCP encourages you to create projects to logically organize resources and configuration. Create a new project for your n8n deployment by clicking the project dropdown menu and then the _NEW PROJECT_ button. Then select the newly created project and as you follow other steps in this guide, make sure you have the correct project selected. ## Enable the Kubernetes Engine API GKE isn't enabled by default, search for "Kubernetes" in the top search bar and select "Kubernetes Engine" from the results. Enable the Kubernetes Engine API by clicking the **Enable** button. ## Create a cluster From the GKE service page, click the **Clusters** menu item and then the **CREATE** button. Make sure you select the "Standard" cluster option, n8n doesn't work with an "Autopilot" cluster. You can leave the cluster configuration on defaults unless there's anything specifically you need to change, such as location. ## Set Kubectl context The remainder of the steps in this guide require you to set the GCP instance as the Kubectl context. You can find the connection details for a cluster instance by opening its details page and then the **CONNECT** button. The resulting code snippet shows a connection string for the gcloud CLI tool. Paste and run that code snippet into a terminal to change your local Kubernetes settings to use the new gcloud cluster. ## Clone configuration repository Kubernetes and n8n require a series of configuration files. You can clone these from [this repository](https://github.com/n8n-io/n8n-kubernetes-hosting/tree/gcp){:target=_blank .external-link} locally. The following steps will tell you which file configures what and what you need to change. Clone the repository with the following command: ```shell git clone https://github.com/n8n-io/n8n-kubernetes-hosting.git -b gcp ``` And change directory to the root of the repository you cloned: ```shell cd gcp ``` ## Configure Postgres For larger scale n8n deployments, Postgres provides a more robust database backend than SQLite. ### Create a volume for persistent storage To maintain data between pod restarts, the Postgres deployment needs a persistent volume. Running Postgres on GCP requires a specific Kubernetes Storage Class, [you can read this guide for specifics](https://cloud.google.com/architecture/deploying-highly-available-postgresql-with-gke){:target="_blank" .external-link}, but the `storage.yaml` manifest creates it for you. You may want to change the regions to create the storage in under the `allowedTopologies` > `matchedLabelExpressions` > `values` key. By default, they're set to "us-central". ```yaml … allowedTopologies: - matchLabelExpressions: - key: failure-domain.beta.kubernetes.io/zone values: - us-central1-b - us-central1-c ``` ### Environment variables Postgres needs some environment variables set to pass to the application running in the containers. 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. The `postgres-deployment.yaml` manifest then uses the values from this manifest file to send to the application pods. ## Configure n8n ### Create a volume for file storage 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](https://docs.n8n.io/hosting/configuration/#encryption-key){:target="_blank" .external-link} between restarts, which saves a file containing the key into file storage during startup. 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. ```yaml … volumes: - name: n8n-claim0 persistentVolumeClaim: claimName: n8n-claim0 … ``` ### Pod resources [Kubernetes lets you](https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/) optionally specify the minimum resources application containers need and the limits they can run to. The example YAML files cloned above contain the following in the `resources` section of the `n8n-deployment.yaml` file: ```yaml … resources: requests: memory: "250Mi" limits: memory: "500Mi" … ``` 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**: 640mb RAM, 20 millicore CPU burstable - **Power**: 1280mb RAM, 80 millicore CPU burstable ### Environment variables n8n needs some environment variables set to pass to the application running in the containers. The example `n8n-secret.yaml` file contains placeholders you need to replace with values of your own for authentication details. ## Deployments The two deployment manifests (`n8n-deployment.yaml` and `postgres-deployment.yaml`) define the n8n and Postgres applications to Kubernetes. The manifests define the following: - Send the environment variables defined to each application pod - Define the container image to use - Set resource consumption limits. This is left empty in the example manifests, but you should set them to something appropriate for your deployment. - The `volumes` defined earlier and `volumeMounts` to define the path in the container to mount volumes. - Scaling and restart policies. The example manifests define only one instance of each pod, you should change this to meet your needs. ## Services 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. ## Send to Kubernetes cluster Send all the manifests to the cluster with the following command: ```shell kubectl apply -f . ``` !!! note "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: ```shell kubectl apply -f namespace.yaml ``` ## Setup DNS n8n typically operates on a subdomain. Create a DNS record with your provider for the subdomain and point it to the IP address of the n8n service. Find the IP address of the n8n service from the **Services & Ingress** menu item of the cluster you want to use under the **Endpoints** column. !!! note "GKE and IP addresses" [Read this GKE tutorial](https://cloud.google.com/kubernetes-engine/docs/tutorials/configuring-domain-name-static-ip#configuring_your_domain_name_records){:target="_blank" .external-link} for more details on how reserved IP addresses work with GKE and Kubernetes resources. ## Delete resources Remove the resources created by the manifests with the following command: ```shell kubectl delete -f . ```