Managing service resources

Working with defaults and overrides for resources

Both the default configuration and the environment-specific configuration include a resources section, which contains resource defaults for:

  • CPU limits and requests (required)

  • Memory limits and requests (required)

  • Replicas (required)

  • Storage type and size

These resources are based on Kubernetes definitions of resources. You can read more about resources here.

Here is an example default resources stanza:

resources:
  cpu:
    limits: 2000m
    requests: 100m
  memory:
    limits: 16Gi
    requests: 100Mi
  replicas: 1
  storage:
    size: 9Gi
    type: aws-efs

Overriding resources

You can override the resource defaults in the Application Template, either in the services section or in the template for a particular environment.

Let's take a look at how to make the following changes to the defaults:

  • Add extra memory for a frontend service in all environments.

  • Deploy two backend replicas in all environments.

  • Specify five backend replicas and ten frontend replicas in our production environment.

For the examples that follow, the resources defaults look like this:

resources:
  cpu:
    limits: 1000m
    requests: 100m
  memory:
    limits: 2Gi
    requests: 100Mi
  replicas: 1

Add extra memory for a frontend service in all environments

In the following snippet, we set memory limits to 4Gi and requests to 500Mi:

services:
- name: frontend
  image: releaseapp/spacedust/frontend
  command:
  - "./start.sh"
  registry: local
  ports:
  - type: node_port
    target_port: '4000'
    port: '4000'
  memory:
    limits: 4Gi
    requests: 500Mi
  static: true
  build_command: GENERATE_SOURCEMAP=false yarn build
  build_base: frontend
  build_output_directory: build/

Now every environment will have a 4Gi memory limit and a 500Mi request value for each frontend instance. Every other service instance will have a 2Gi memory limit and 100Mi request value.

Deploy two backend replicas in all environments

In the default environment configuration, override the default value for replicas in the services section for backend:

services
- name: backend
  image: releaseapp/spacedust/backend
  command:
  - "./run-spacedust.sh"
  registry: local
  depends_on:
  - postgres
  - redis
  ports:
  - type: node_port
    target_port: '3000'
    port: '3000'
  replicas: 2

Now every environment we deploy based on our default environment configuration will deploy two instances of the backend service and one instance of each other service.

Specify five backend replicas and ten frontend replicas in our production environment

In order to override resource defaults for our production environment, we need to create a production space and modify the environment-specific configuration.

Here's how we would set the number of replicas for frontend and backend:

services:
- name: frontend
  image: releaseapp/spacedust/frontend
  command:
  - "./start.sh"
  registry: local
  ports:
  - type: node_port
    target_port: '4000'
    port: '4000'
  replicas: 10
  memory:
    limits: 4Gi
    requests: 500Mi
  static: true
  build_command: GENERATE_SOURCEMAP=false yarn build
  build_base: frontend
  build_output_directory: build/
- name: backend
  image: releaseapp/spacedust/backend
  command:
  - "./run-spacedust.sh"
  registry: local
  depends_on:
  - postgres
  - redis
  ports:
  - type: node_port
    target_port: '3000'
    port: '3000'
  replicas: 5

By overriding the defaults for replicas in the environment-specific configuration, your production environment will deploy with ten frontend and five backend instances running in the cluster. These changes are specific to your production environment, so other environments will not be affected.

Last updated