# Install in custom infrastructure

Install Platforma into a Kubernetes cluster that the company already has, in any cloud or data center.

Other ways: [Install in cloud with Terraform](/install-cloud-terraform/), [Install in cloud with installer](/install-cloud-installer/), [Install in Single Server mode](/install-single-server/).

The cluster must meet the [Kubernetes contract](/infrastructure-contract/). Two paths install Platforma into it: the Terraform modules (The Terraform code that MiLaboratories provides to install Platforma. The cloud-specific Terraform modules make the cluster and its cloud parts in AWS or Google Cloud; the cloud-agnostic Terraform modules install Platforma into the cluster.), which MiLaboratories recommends, and the Helm chart alone. To make a new cluster in AWS or Google Cloud, see [Install in cloud with Terraform](/install-cloud-terraform/).

## What you get

#### Variant: Terraform modules

For a Kubernetes (A system that runs programs on a group of machines (a cluster). Platforma can run in a Kubernetes cluster.) cluster that the company already has, MiLaboratories recommends the cloud-agnostic Terraform modules (The Terraform modules platforma-tf-services-cluster and platforma-tf-services. They do not depend on the infrastructure and install Platforma into any cluster that meets the Kubernetes contract, in a cloud or in the company's own infrastructure.). You describe the cluster one time in the input `cluster_config`, and the modules do the rest: `platforma-tf-services-cluster` creates the namespace, installs Kueue (A Kubernetes add-on that holds jobs in a queue until the cluster has free CPU, memory and GPU for them.) and AppWrapper (A Kubernetes add-on that wraps the pods of one job into a single unit, so that Kueue admits and starts them together.), and creates the job (One run of a tool on data. The Backend starts jobs on the server where it runs, or in a Kubernetes cluster.) queues and the RBAC roles; `platforma-tf-services` installs the Backend (The server program of Platforma. It keeps the projects, runs the Blocks and starts jobs.). Before they change the cluster, the modules check the inputs and stop at a wrong value with its path.

- **Primary storage (The bucket (Amazon S3 or Google Cloud Storage) or folder where Platforma keeps all results and uploaded files.)**: an S3-compatible bucket, with the access keys from the Secret `platforma-storage`. Platforma also supports Amazon S3 and Google Cloud Storage with the cloud identity of the Backend (see [Primary storage](/configuration-storage/#primary-storage)). **Workspace (The storage where the Backend and all jobs keep the working files of running jobs. The Backend and the jobs use it at the same time. In Kubernetes, it is a network file system; in Single Server mode, it is a folder on the server.)**: a claim of 1 TiB from the StorageClass `platforma-workspace` of the workspace. The database and the logs get claims from the StorageClasses `platforma-database` and `platforma-logs`.
- **Sign-in (To show the Backend who you are, with a sign-in provider or a single-user token. The verb is "sign in"; the noun and the adjective are "sign-in".)**: the static list of users (A sign-in provider that is a file with the logins and the password hashes of the users, in the htpasswd format. The Backend reads it again when it changes.) from the Secret `platforma-users`; `admin` is an administrator.
- **Entry point (The way in to the Backend from outside the cluster, a load balancer or an Ingress with a DNS name. It usually ends TLS and sends the traffic to the Backend pod.) and TLS**: the Desktop App (The program that each user installs on their own computer to work with Platforma. It is a client of the Backend.) connects to `platforma.example.com` through the Gateway of the cluster. The listener `platforma` of the Gateway holds the certificate, so TLS ends there. With `generic`, the Gateway must also have the gRPC health check and the long stream timeouts (see [Gateway](/infrastructure-contract-entry-point/#gateway)).
- **What runs where**: the Backend pod runs on the node with the label `platforma.bio/pool=backend`. The jobs go through the job queues to the nodes with the labels `platforma.bio/pool=ui` and `platforma.bio/pool=batch`, and each job node also has the label `platforma.bio/node-group=<group>` (see [Node pools](/infrastructure-contract-jobs/#node-pools)). With `autoscaling.enabled` set to `false`, the module reads the nodes of each pool and calculates from them the quotas of the job queues and the largest job.

The modules use the StorageClasses, the Gateway and the bucket that the cluster has, and do not create them. The license key and the master secret come from the Secrets `platforma-license` and `platforma-master-secret`, the defaults of the modules, so the inputs do not name them.

#### Variant: Helm chart

In a cluster that meets the [Kubernetes contract](/infrastructure-contract/), the Helm chart `platforma-backend` installs the Backend in one namespace and does not change the cluster. MiLaboratories recommends the Terraform modules. With the chart alone, the team that runs the cluster prepares by hand all that the [Kubernetes contract](/infrastructure-contract/) asks, such as Kueue, AppWrapper, the job queues, the StorageClasses, the entry point and the RBAC roles.

- **Primary storage**: an S3-compatible bucket, with the access keys from the Secret `platforma-storage`. It works in any cluster, with no cloud identity (The identity that the cloud gives to the Platforma pod (IAM roles for service accounts on AWS, Workload Identity on Google Cloud). With it, Platforma needs no access keys.). Platforma also supports Amazon S3 and Google Cloud Storage with the cloud identity of the Backend (see [Primary storage](/configuration-storage/#primary-storage)). **Workspace**: a claim of 1 TiB from the StorageClass `platforma-workspace`. The database and the logs get claims from the StorageClasses `platforma-database` and `platforma-logs`.
- **Sign-in**: the static list of users from the Secret `platforma-users`; `admin` is an administrator.
- **Entry point and TLS**: the Desktop App connects to `platforma.example.com` through the Gateway of the cluster. The listener of the Gateway holds the certificate, so TLS ends there. With `generic`, the Gateway must also have the gRPC health check and the long stream timeouts (see [Gateway](/infrastructure-contract-entry-point/#gateway)).
- **What runs where**: the Backend pod runs on the node with the label `platforma.bio/pool=backend`, and the jobs go through the job queues of the cluster to the job nodes. `jobs.maxRequest` is the allocatable CPU and memory of one batch node of 32 CPUs (The compute units of a node or of the server, as Kubernetes and the operating system count them. A job asks for a number of CPUs. The cloud quotas call them vCPUs.) and 256 GiB: the Backend makes a larger request of a job smaller.

The license key and the master secret come from the Secrets `platforma-license` and `platforma-master-secret`, the defaults of the chart, so the values do not name them.

**Size.** The size follows the nodes of the cluster. With 8 CPUs and 32 GiB, the Backend serves up to about 30 users who work at the same time. Kubernetes keeps a part of each node for itself, so a node of 8 CPUs and 32 GiB cannot hold this request. Use a larger Backend node, or set the Backend to the CPU and the memory that its node gives to pods (the step “Prepare the cluster”). The biggest jobs of a sample ask for 16 CPUs, so they need batch nodes of 32 CPUs and 256 GiB, one such job on each node. For the time and the cost of the analyses on these nodes, see [Size and cost of an analysis](/plan-analysis-size-and-cost/#examples-10-and-100-million-reads-per-sample).

## Before you start

Prepare these items before the first apply:

- **The cluster** meets the [Kubernetes contract](/infrastructure-contract/): Kubernetes 1.35 or later, the node pools with their labels, the StorageClasses, the Gateway and the bucket. With the Terraform modules, the modules add Kueue, AppWrapper and the job queues; with the Helm chart alone, the team that runs the cluster adds them.
- **Rights.** A kubeconfig with cluster admin rights to prepare the cluster, and the role `admin` in the namespace to install the Backend. See [Rights to install](/plan-permissions/#rights-to-install).
- **A host and TLS.** The host name that users type in the Desktop App, for example `platforma.example.com`. A DNS record from the host to the address of the Gateway. A certificate for the host on the listener of the Gateway. See [Domain and TLS](/configuration-domain-and-tls/).
- **The bucket** of the primary storage and its access keys. See [Primary storage](/configuration-storage/#primary-storage).
- **The license key.** See [License](/configuration-license/).
- **Network access** from the computer that installs Platforma to `github.com` (the modules) and `ghcr.io` (the charts), or a clone and a chart mirror in the corporate network. For all hosts, see [Outbound connections](/security-outbound-connections/).
- **A clone of platforma-install** at a release tag: the modules and the charts. See [Where the modules are](/install-cloud-terraform/#where-the-modules-are).
- **Tools** on the computer that runs the installation: `kubectl` (at most one minor version from the version of the cluster), `openssl` and `htpasswd` (the package `apache2-utils` or `httpd-tools`). Also Terraform 1.12 or later (or OpenTofu 1.12 or later) for the modules, or Helm 3 for the chart.

When the cluster cannot reach the public registries, make a mirror of each of them before the first apply, and keep the mirrors for as long as Platforma runs: see [Registries mirroring](/registries-mirroring/).

## Steps

MiLaboratories recommends the Terraform modules for a cluster that exists: they prepare the cluster for Platforma and check the inputs before they change it. With the Helm chart alone, the team that runs the cluster prepares by hand all that the [Kubernetes contract](/infrastructure-contract/) asks. In the examples, `<platforma-install>` is the folder of a clone of the repository platforma-install at the release tag (see [Where the modules are](/install-cloud-terraform/#where-the-modules-are)), `<release tag>` is this tag, and `<kubeconfig context>` is the context of the cluster in `~/.kube/config`. Select this context before the steps, so that the `kubectl` commands and the Terraform providers use the same cluster:

```sh
kubectl config use-context <kubeconfig context>
```

The modules come from `github.com` and get the charts from `oci://ghcr.io/milaboratory/platforma-install`. On a machine without internet access, use the clone: set `chart_repository` of `backend_service` and of `components.appwrapper` to `<platforma-install>/helm/charts`.

1. **Make the secret values.** See [Secret values](/install-cloud-terraform/#secret-values). Also put the access keys of the bucket into two files in the same folder:

   ```sh
   cd ~/platforma-secrets
   printf '%s' '<access key>' > access-key.txt
   printf '%s' '<secret key>' > secret-key.txt
   ```

2. **Prepare the cluster.**

   #### Variant: Terraform modules

   Describe the cluster in the input `cluster_config` of `platforma-tf-services-cluster`, as a JSON file. Set the labels of the node pools, the names of the ServiceAccounts, the StorageClass of the workspace, the bucket and the Gateway of the cluster:

   *cluster/cluster-config.json*

   ```json
   {
     "contract_version": 1,
     "namespaces": ["platforma"],
     "node_pools": {
       "ui": {
         "node_selector": { "platforma.bio/pool": "ui" },
         "tolerations": [],
         "node_groups": [{ "name": "ui", "max_size": 2 }]
       },
       "batch": {
         "node_selector": { "platforma.bio/pool": "batch" },
         "tolerations": [],
         "node_groups": [{ "name": "batch", "max_size": 4 }]
       }
     },
     "autoscaling": { "enabled": false, "provisioning_request": {} },
     "cloud_values": {
       "jobs": { "service_account": { "name": "platforma-jobs" } },
       "backend": {
         "service_account": { "name": "platforma-backend" },
         "allocatable": { "cpu": 7.2, "memory_gib": 26 }
       },
       "volumes": { "workspace": { "storage_class": "platforma-workspace" } },
       "primary_storage": {
         "s3": {
           "bucket": "platforma-results",
           "endpoint": "https://s3.example.com",
           "region": "us-east-1",
           "secret": {
             "access_key": { "name": "platforma-storage", "key": "access-key" },
             "secret_key": { "name": "platforma-storage", "key": "secret-key" }
           }
         }
       },
       "network": {
         "hosts": ["platforma.example.com"],
         "entry_points": [{
           "host": "platforma.example.com",
           "gateway": {
             "name": "platforma-gateway",
             "namespace": "platforma-gateway",
             "section_name": "platforma",
             "implementation": "generic"
           }
         }]
       }
     }
   }
   ```

   `backend.allocatable` is the CPU and the memory that the Backend pod can use on its node: the `Allocatable` values in the output of `kubectl describe node`. The Backend pod requests this value. Without it, the pod requests 8 CPUs and 32 GiB, and a node of this size does not have room for it.

   Apply the root `cluster` with cluster admin rights, from the folder `~/platforma` with the subfolders `cluster`, `services` and `handoff`. It creates the namespace `platforma`, installs Kueue and AppWrapper, and creates the job queues and the RBAC roles. Do not create the namespace by hand: the apply then fails.

   *cluster/main.tf*

   ```hcl
   terraform {
     required_providers {
       helm       = { source = "hashicorp/helm", version = "~> 3.1" }
       kubernetes = { source = "hashicorp/kubernetes", version = "~> 3.0" }
       kubectl    = { source = "alekc/kubectl", version = "~> 2.1" }
     }
   }


   provider "kubernetes" {
     config_path    = "~/.kube/config"
     config_context = "<kubeconfig context>"
   }


   provider "helm" {
     kubernetes = {
       config_path    = "~/.kube/config"
       config_context = "<kubeconfig context>"
     }
   }


   provider "kubectl" {
     config_path    = "~/.kube/config"
     config_context = "<kubeconfig context>"
   }


   module "services_cluster" {
     source = "git::https://github.com/milaboratory/platforma-install.git//terraform/cloud-agnostic/platforma-tf-services-cluster?ref=<release tag>"


     cluster_config = jsondecode(file("cluster-config.json"))
   }


   output "infrastructure" { value = module.services_cluster.infrastructure }
   output "versions" { value = module.services_cluster.versions }
   ```

   ```sh
   cd ~/platforma/cluster
   terraform init
   terraform apply
   terraform output -json infrastructure > ../handoff/infrastructure.json
   ```

   To change the node pools, the quotas of the job queues or to add GPU nodes, see [Jobs and resources](/configuration-jobs-and-resources/). For an Ingress in place of the Gateway, see [Domain and TLS](/configuration-domain-and-tls/).

   #### Variant: Helm chart

   Prepare what the [Kubernetes contract](/infrastructure-contract/) asks, then create the namespace.

   ```sh
   kubectl create namespace platforma
   ```

   Result: `kubectl get namespace platforma` shows `Active`.

3. **Create the Secrets**, from the files of step 1. These are the names and keys of the examples; the license, the master secret and the users have the default names of the chart and of the modules.

   ```sh
   cd ~/platforma-secrets
   kubectl -n platforma create secret generic platforma-license --from-file=MI_LICENSE=license.txt
   kubectl -n platforma create secret generic platforma-master-secret --from-file=master-secret=master-secret.txt
   kubectl -n platforma create secret generic platforma-users --from-file=htpasswd=users.htpasswd
   kubectl -n platforma create secret generic platforma-storage --from-file=access-key=access-key.txt --from-file=secret-key=secret-key.txt
   ```

   Result: `kubectl -n platforma get secrets` lists the four Secrets.

   For the Secrets of other sign-in providers (A configured way for users to sign in to the Backend: single sign-on (SSO), LDAP or a static list of users. One Backend can have more than one.), see [Authentication](/configuration-authentication/). For a bucket that the Backend reads with its cloud identity, with no keys, see [Primary storage](/configuration-storage/#primary-storage).

4. **Install the Backend.**

   #### Variant: Terraform modules

   Apply the root `services` with the role `admin` in the namespace. It reads the hand-off file of the root `cluster`, and waits until the Backend is ready.

   *services/main.tf*

   ```hcl
   terraform {
     required_providers {
       helm = { source = "hashicorp/helm", version = "~> 3.1" }
     }
   }


   provider "helm" {
     kubernetes = {
       config_path    = "~/.kube/config"
       config_context = "<kubeconfig context>"
     }
   }


   module "platforma" {
     source = "git::https://github.com/milaboratory/platforma-install.git//terraform/cloud-agnostic/platforma-tf-services?ref=<release tag>"


     namespace      = "platforma"
     infrastructure = jsondecode(file("../handoff/infrastructure.json"))
     backend_service = {
       auth = {
         providers = [
           {
             name        = "local"
             title       = "Local password"
             htpasswd    = { secret = { name = "platforma-users", key = "htpasswd" } }
             admin_users = ["admin"]
           },
         ]
       }
     }
   }


   output "entry_points" { value = module.platforma.entry_points }
   output "release" { value = module.platforma.release }
   output "versions" { value = module.platforma.versions }
   ```

   ```sh
   cd ~/platforma/services
   terraform init
   terraform apply
   ```

   #### Variant: Helm chart

   The values of the chart: the ServiceAccounts, the largest job (the allocatable CPU and memory of one batch node of 32 CPUs and 256 GiB), the bucket, the Gateway and one sign-in provider. The Backend pod requests 8 CPUs and 32 GiB (`backend.resources`). On a Backend node of this size, set `backend.resources` to the allocatable CPU and memory of the node, or use a larger node.

   *values.yaml*

   ```yaml
   backend:
     serviceAccount: { name: platforma-backend }


   jobs:
     serviceAccount: { name: platforma-jobs }
     maxRequest:
       cpu: 31
       memory: 234Gi


   primaryStorage:
     s3:
       bucket: platforma-results
       endpoint: https://s3.example.com
       region: us-east-1
       secret:
         accessKey: { name: platforma-storage, key: access-key }
         secretKey: { name: platforma-storage, key: secret-key }


   volumes:
     workspace:
       storageClass: platforma-workspace   # the default: the workspace


   network:
     entryPoints:
       - host: platforma.example.com
         gateway:
           name: platforma-gateway
           namespace: platforma-gateway
           implementation: generic


   auth:
     providers:
       - name: local
         title: Local password
         htpasswd:
           secret: { name: platforma-users, key: htpasswd }
         adminUsers: ["admin"]
   ```

   ```sh
   helm install platforma oci://ghcr.io/milaboratory/platforma-install/platforma-backend \
     --version <chart version> -n platforma -f values.yaml
   ```

   The chart version is `version` in `<platforma-install>/helm/charts/platforma-backend/Chart.yaml`. On a machine without internet access, install from the folder of the clone: `helm install platforma <platforma-install>/helm/charts/platforma-backend -n platforma -f values.yaml`.

   Result: `kubectl -n platforma get pods` shows the Backend pod `Running`.

   To change the sign-in, see [Authentication](/configuration-authentication/) and [Users and roles](/configuration-users-and-roles/). For the other settings of the Backend, see [Storage](/configuration-storage/), [Data libraries](/configuration-data-libraries/), [Jobs and resources](/configuration-jobs-and-resources/), [Proxy and custom CA](/configuration-proxy-and-custom-ca/) and [Registries](/configuration-registries/).

5. **Check DNS and the certificate.**

   ```sh
   kubectl -n platforma-gateway get gateway platforma-gateway   # PROGRAMMED: True
   dig +short platforma.example.com
   openssl s_client -connect platforma.example.com:443 -servername platforma.example.com \
     -verify_hostname platforma.example.com -verify_return_error -alpn h2 </dev/null >/dev/null 2>&1 \
     && echo "certificate OK"
   ```

   Result: the host resolves to the address of the Gateway, and the last command shows `certificate OK`. When it shows nothing, the certificate is not valid for the host name: run `openssl` again without `>/dev/null 2>&1` to see the error. For the certificate and the DNS record, see [Domain and TLS](/configuration-domain-and-tls/).

6. **Verify the installation.** See [Verify the installation](/install-verify/).

7. **Connect the Desktop App** to `https://platforma.example.com`, and sign in as `admin` with the password of the list of users. See [Connect clients](/install-connect-clients/).

## Next

- [Verify the installation](/install-verify/)
- [Authentication](/configuration-authentication/)
- [Backup and restore](/maintain-backup-and-restore/)

Source: https://docs.platforma.bio/install-custom-infrastructure/
