Mattermost

In this example, we'll deploy the Mattermost tool with its PostgreSQL database.
In a second step, we will add the backup options

1. Create shared variables

Go to "Environment variables" menu and create 3 variables.

  • POSTGRES_USER
  • POSTGRES_DB
  • POSTGRES_PASSWORD (don't forget to check the "hide value" box)

create postgres env vars

You can also create shared environment variables directly from the API:

curl -X 'POST' \
  'https://api.layerops.io/v1/environments/c9eee4c7-1a31-471b-81f4-d7f128fe5a09/sharedVariables' \
  -H 'accept: application/json' \
  -H 'X-API-KEY: LYP7FBC7J8CMM68NYUPWSMYX3:8ee75797-7fa5-4f9f-9522-537dfd3e89b7' \
  -H 'Content-Type: application/json' \
  -d '{
  "key": "POSTGRES_DB",
  "value": "mattermost",
  "isSensitive": false
}'
For more information about API, please consult the documentation.

2. Create PostgreSQL service

Import this service:

services:
  - id: postgres
    name: postgres
    dockerConfiguration:
      image: postgres
      imageVersion: 16.0-alpine3.18
    countMin: 1
    countMax: 1
    ports:
      - listeningPort: 5432
        healthCheckEnabled: false
    sharedEnvironmentVariables:
      - POSTGRES_USER
      - POSTGRES_DB
      - POSTGRES_PASSWORD
    cooldownMinutes: 5
    capacityCpuMhz: 512
    cpuLimitHigh: 120
    cpuLimitLow: 100
    cpuDurationMinutes: 15
    cpuWindowMinutes: 5
    capacityMemoryMB: 512
    memoryLimitHigh: 120
    memoryLimitLow: 100
    memoryDurationMinutes: 15
    memoryWindowMinutes: 5
    allocationQueuedMinutes: 2

This block is used to import previously created variables and inject them into the service:

sharedEnvironmentVariables:
  - POSTGRES_USER
  - POSTGRES_DB
  - POSTGRES_PASSWORD

3. Create Mattermost service

Import this service:

services:
  - id: mattermost
    name: mattermost
    dockerConfiguration:
      image: mattermost/mattermost-team-edition
      imageVersion: latest
    countMin: 1
    countMax: 1
    ports:
      - listeningPort: 8065
        loadBalancerRules:
          -  publicPort: 443
        healthCheckType: HTTP
        healthCheckMethod: GET
        healthCheckEnabled: true
        healthCheckTimeoutSeconds: 30
        healthCheckPath: /
        healthCheckIntervalSeconds: 60
    sharedEnvironmentVariables:
      - POSTGRES_USER
      - POSTGRES_DB
      - POSTGRES_PASSWORD
    environmentVariables:
      - key: TZ
        value: UTC
      - key: MM_SQLSETTINGS_DRIVERNAME
        value: postgres
      - key: MM_SQLSETTINGS_DATASOURCE
        value: "postgres://%POSTGRES_USER%:%POSTGRES_PASSWORD%@%POSTGRES_ADDR%/%POSTGRES_DB%?sslmode=disable&connect_timeout=10"
    links:
      - toServiceId: postgres
        toServicePort: 5432
        variableHost: POSTGRES_HOST
        variablePort: POSTGRES_PORT
        variableAddress: POSTGRES_ADDR
    cooldownMinutes: 5
    capacityCpuMhz: 512
    cpuLimitHigh: 120
    cpuLimitLow: 100
    cpuDurationMinutes: 15
    cpuWindowMinutes: 5
    capacityMemoryMB: 512
    memoryLimitHigh: 120
    memoryLimitLow: 100
    memoryDurationMinutes: 15
    memoryWindowMinutes: 5
    allocationQueuedMinutes: 2

We have added an environment variable which is made up of several other environment variables thanks to the %%.

  - key: MM_SQLSETTINGS_DATASOURCE
    value: "postgres://%POSTGRES_USER%:%POSTGRES_PASSWORD%@%POSTGRES_ADDR%/%POSTGRES_DB%?sslmode=disable&connect_timeout=10"

We want this service to have a public endpoint, so we've added the following block:

ports:
  - listeningPort: 8065
    loadBalancerRules:
      -  publicPort: 443

You can now access the URL that was automatically associated with port 8065.

public port exposed

You can later add a domain name specific to this service in the "Load Balancer" menu.

4. Backup

In this example, we'll use scaleway's S3 to send backups, but this example is compatible with any S3.

Create shared variables

Go to "Environment variables" menu and create 3 variables.

  • BACKUP_S3_ACCESS_KEY: get this info from your S3 provider
  • BACKUP_S3_SECRET_KEY: get this info from your S3 provider (don't forget to check the "hide value" box)
  • BACKUP_ENCRYPT_KEY: can be anything (don't forget to check the "hide value" box)

Add side task to Mattermost

For the Mattermost service to restore the data, the service must be changed to add a side task and environment variables for this side task.

services:
  - id: mattermost
    name: mattermost
    dockerConfiguration:
      image: mattermost/mattermost-team-edition
      imageVersion: latest
    countMin: 1
    countMax: 1
    ports:
      - listeningPort: 8065
        loadBalancerRules:
          -  publicPort: 443
        healthCheckType: HTTP
        healthCheckMethod: GET
        healthCheckEnabled: true
        healthCheckTimeoutSeconds: 30
        healthCheckPath: /
        healthCheckIntervalSeconds: 60
    sharedEnvironmentVariables:
      - POSTGRES_USER
      - POSTGRES_DB
      - POSTGRES_PASSWORD
      - BACKUP_S3_ACCESS_KEY
      - BACKUP_S3_SECRET_KEY
      - BACKUP_ENCRYPT_KEY
    environmentVariables:
      - key: TZ
        value: UTC
      - key: MM_SQLSETTINGS_DRIVERNAME
        value: postgres
      - key: MM_SQLSETTINGS_DATASOURCE
        value: "postgres://%POSTGRES_USER%:%POSTGRES_PASSWORD%@%POSTGRES_ADDR%/%POSTGRES_DB%?sslmode=disable&connect_timeout=10"
      - key: DB_ENGINE
        value: postgres
      - key: DB_HOST
        value: "%POSTGRES_HOST%"
      - key: DB_PORT
        value: "%POSTGRES_PORT%"
      - key: DB_USER
        value: "%POSTGRES_USER%"
      - key: DB_PASSWORD
        value: "%POSTGRES_PASSWORD%"
      - key: DB_NAMES
        value: "%POSTGRES_DB%"
      - key: BACKUP_S3_PROVIDER
        value: Scaleway
      - key: BACKUP_S3_ENDPOINT
        value: s3.fr-par.scw.cloud
      - key: BACKUP_S3_BUCKET
        value: mattermost-postgresql-backup
      - key: BACKUP_S3_REGION
        value: fr-par
    links:
      - toServiceId: postgres
        toServicePort: 5432
        variableHost: POSTGRES_HOST
        variablePort: POSTGRES_PORT
        variableAddress: POSTGRES_ADDR
    sideTasks:
      - dockerConfiguration:
          image: registry.nimeops.net/layerops-public/marketplace/backup-sql
          imageVersion: 1.0.0
          command: /usr/local/bin/restore
        type: preStart
        isLongLived: false
    cooldownMinutes: 5
    capacityCpuMhz: 512
    cpuLimitHigh: 120
    cpuLimitLow: 100
    cpuDurationMinutes: 15
    cpuWindowMinutes: 5
    capacityMemoryMB: 512
    memoryLimitHigh: 120
    memoryLimitLow: 100
    memoryDurationMinutes: 15
    memoryWindowMinutes: 5
    allocationQueuedMinutes: 2

The side task runs before the main service and is responsible for importing data if the database is empty.

sideTasks:
  - dockerConfiguration:
      image: registry.nimeops.net/layerops-public/marketplace/backup-sql
      imageVersion: 1.0.0
      command: /usr/local/bin/restore
    type: preStart
    isLongLived: false

This side task needs to connect to S3 and postgreSQL, which is why other variables have been added.

  • type: preStart: starts the side task before the main service.
  • isLongLived: false: describes the side task as a task that will stop running (after importing, the container stops). Set to "true" if the side task must continue to run continuously alongside the main service.
The container used here is a public container that fetches a backup from S3 and imports it into postgreSQL. But you can create your own container for specific needs.

Import service postgres-backup

services:
  - id: backup
    name: postgres-backup
    dockerConfiguration:
      image: registry.nimeops.net/layerops-public/marketplace/backup-sql
      imageVersion: 1.0.0
    countMin: 1
    countMax: 1
    environmentVariables:
      - key: DB_ENGINE
        value: postgres
      - key: DB_HOST
        value: "%POSTGRES_HOST%"
      - key: DB_USER
        value: "%POSTGRES_USER%"
      - key: DB_PASSWORD
        value: "%POSTGRES_PASSWORD%"
      - key: DB_NAMES
        value: "%POSTGRES_DB%"
      - key: DB_PORT
        value: "%POSTGRES_PORT%"
      - key: BACKUP_S3_PROVIDER
        value: Scaleway
      - key: BACKUP_S3_ENDPOINT
        value: s3.fr-par.scw.cloud
      - key: BACKUP_S3_BUCKET
        value: mattermost-postgresql-backup
      - key: BACKUP_S3_REGION
        value: fr-par
    sharedEnvironmentVariables:
      - POSTGRES_USER
      - POSTGRES_DB
      - POSTGRES_PASSWORD
      - BACKUP_S3_ACCESS_KEY
      - BACKUP_S3_SECRET_KEY
      - BACKUP_ENCRYPT_KEY
    links:
      - toServiceId: postgres
        toServicePort: 5432
        variableHost: POSTGRES_HOST
        variablePort: POSTGRES_PORT
        variableAddress: POSTGRES_ADDR
    cronExpression: 0 * * * *
    cronAllowOverlap: false
    cooldownMinutes: 5
    capacityCpuMhz: 512
    cpuLimitHigh: 120
    cpuLimitLow: 100
    cpuDurationMinutes: 15
    cpuWindowMinutes: 5
    capacityMemoryMB: 512
    memoryLimitHigh: 120
    memoryLimitLow: 100
    memoryDurationMinutes: 15
    memoryWindowMinutes: 5
    allocationQueuedMinutes: 2

Import all services at once

With the "import service" option, you can import several services at the same time. Here is the YML file used to create all services: Mattermost, Postgres and postgres backup.

services:
  - id: mattermost
    name: mattermost
    dockerConfiguration:
      image: mattermost/mattermost-team-edition
      imageVersion: latest
    countMin: 1
    countMax: 1
    ports:
      - listeningPort: 8065
        loadBalancerRules:
          -  publicPort: 443
        healthCheckType: HTTP
        healthCheckMethod: GET
        healthCheckEnabled: true
        healthCheckTimeoutSeconds: 30
        healthCheckPath: /
        healthCheckIntervalSeconds: 60
    sharedEnvironmentVariables:
      - POSTGRES_USER
      - POSTGRES_DB
      - POSTGRES_PASSWORD
      - BACKUP_S3_ACCESS_KEY
      - BACKUP_S3_SECRET_KEY
      - BACKUP_ENCRYPT_KEY
    environmentVariables:
      - key: TZ
        value: UTC
      - key: MM_SQLSETTINGS_DRIVERNAME
        value: postgres
      - key: MM_SQLSETTINGS_DATASOURCE
        value: "postgres://%POSTGRES_USER%:%POSTGRES_PASSWORD%@%POSTGRES_ADDR%/%POSTGRES_DB%?sslmode=disable&connect_timeout=10"
      - key: DB_ENGINE
        value: postgres
      - key: DB_HOST
        value: "%POSTGRES_HOST%"
      - key: DB_PORT
        value: "%POSTGRES_PORT%"
      - key: DB_USER
        value: "%POSTGRES_USER%"
      - key: DB_PASSWORD
        value: "%POSTGRES_PASSWORD%"
      - key: DB_NAMES
        value: "%POSTGRES_DB%"
      - key: BACKUP_S3_PROVIDER
        value: Scaleway
      - key: BACKUP_S3_ENDPOINT
        value: s3.fr-par.scw.cloud
      - key: BACKUP_S3_BUCKET
        value: mattermost-postgresql-backup
      - key: BACKUP_S3_REGION
        value: fr-par
    links:
      - toServiceId: postgres
        toServicePort: 5432
        variableHost: POSTGRES_HOST
        variablePort: POSTGRES_PORT
        variableAddress: POSTGRES_ADDR
    sideTasks:
      - dockerConfiguration:
          image: registry.nimeops.net/layerops-public/marketplace/backup-sql
          imageVersion: 1.0.0
          command: /usr/local/bin/restore
        type: preStart
        isLongLived: false
    cooldownMinutes: 5
    capacityCpuMhz: 512
    cpuLimitHigh: 120
    cpuLimitLow: 100
    cpuDurationMinutes: 15
    cpuWindowMinutes: 5
    capacityMemoryMB: 512
    memoryLimitHigh: 120
    memoryLimitLow: 100
    memoryDurationMinutes: 15
    memoryWindowMinutes: 5
    allocationQueuedMinutes: 2
  - id: postgres
    name: postgres
    dockerConfiguration:
      image: postgres
      imageVersion: 16.0-alpine3.18
    countMin: 1
    countMax: 1
    ports:
      - listeningPort: 5432
        healthCheckEnabled: false
    sharedEnvironmentVariables:
      - POSTGRES_USER
      - POSTGRES_DB
      - POSTGRES_PASSWORD
    cooldownMinutes: 5
    capacityCpuMhz: 512
    cpuLimitHigh: 120
    cpuLimitLow: 100
    cpuDurationMinutes: 15
    cpuWindowMinutes: 5
    capacityMemoryMB: 512
    memoryLimitHigh: 120
    memoryLimitLow: 100
    memoryDurationMinutes: 15
    memoryWindowMinutes: 5
    allocationQueuedMinutes: 2
  - id: backup
    name: postgres-backup
    dockerConfiguration:
      image: registry.nimeops.net/layerops-public/marketplace/backup-sql
      imageVersion: 1.0.0
    countMin: 1
    countMax: 1
    environmentVariables:
      - key: DB_ENGINE
        value: postgres
      - key: DB_HOST
        value: "%POSTGRES_HOST%"
      - key: DB_USER
        value: "%POSTGRES_USER%"
      - key: DB_PASSWORD
        value: "%POSTGRES_PASSWORD%"
      - key: DB_NAMES
        value: "%POSTGRES_DB%"
      - key: DB_PORT
        value: "%POSTGRES_PORT%"
      - key: BACKUP_S3_PROVIDER
        value: Scaleway
      - key: BACKUP_S3_ENDPOINT
        value: s3.fr-par.scw.cloud
      - key: BACKUP_S3_BUCKET
        value: mattermost-postgresql-backup
      - key: BACKUP_S3_REGION
        value: fr-par
    sharedEnvironmentVariables:
      - POSTGRES_USER
      - POSTGRES_DB
      - POSTGRES_PASSWORD
      - BACKUP_S3_ACCESS_KEY
      - BACKUP_S3_SECRET_KEY
      - BACKUP_ENCRYPT_KEY
    links:
      - toServiceId: postgres
        toServicePort: 5432
        variableHost: POSTGRES_HOST
        variablePort: POSTGRES_PORT
        variableAddress: POSTGRES_ADDR
    cronExpression: 0 * * * *
    cronAllowOverlap: false
    cooldownMinutes: 5
    capacityCpuMhz: 512
    cpuLimitHigh: 120
    cpuLimitLow: 100
    cpuDurationMinutes: 15
    cpuWindowMinutes: 5
    capacityMemoryMB: 512
    memoryLimitHigh: 120
    memoryLimitLow: 100
    memoryDurationMinutes: 15
    memoryWindowMinutes: 5
    allocationQueuedMinutes: 2