how to throttle elasticsearch cpu and memory usage in docker?

Viewed 5051
  es01:
    env_file: .env
    image: docker.elastic.co/elasticsearch/elasticsearch:7.6.2
    container_name: ${DOCKER_CONTAINER_ES_NAME}
    restart: always
    environment:
      - node.name=es01
      - cluster.name=es-docker-cluster
      - cluster.initial_master_nodes=es01
      - bootstrap.memory_lock=true
      - action.auto_create_index=+*
      - "ES_JAVA_OPTS=-Xms2g -Xmx2g"
    ulimits:
      nproc: 65535
      nofile:
        soft: 65535
        hard: 65535
      memlock:
        soft: -1
        hard: -1
    volumes:
      - ./data01:/usr/share/elasticsearch/data
    ports:
      - 127.0.0.1:${DOCKER_CONTAINER_ES_PORT}:9200
    networks:
      - ${DOCKER_NETWORK_NAME}

I copied this from somewhere, but all my elasticsearch containers are clogging up cpu.

1 Answers

The Docker Compose documentation suggests setting deploy.resources.limits to limit CPU and memory usage. This is new for version 3 of the Docker Compose spec and seems to be specific to using Docker swarm. See below for a method using version 2 of the spec and that is not specific to swarm.

--- 
es01: 
  container_name: "${DOCKER_CONTAINER_ES_NAME}"
  env_file: .env
  environment: 
    - node.name=es01
    - cluster.name=es-docker-cluster
    - cluster.initial_master_nodes=es01
    - bootstrap.memory_lock=true
    - action.auto_create_index=+*
    - "ES_JAVA_OPTS=-Xms2g -Xmx2g"
  image: "docker.elastic.co/elasticsearch/elasticsearch:7.6.2"
  networks: 
    - "${DOCKER_NETWORK_NAME}"
  ports: 
    - "127.0.0.1:${DOCKER_CONTAINER_ES_PORT}:9200"
  deploy:
    resources: 
      limits: 
        cpus: "0.50"  # Use at most 50% of one CPU core
        memory: 50M  # Use at most 50 MB of RAM
  restart: always
  ulimits: 
    memlock: 
      hard: -1
      soft: -1
    nofile: 
      hard: 65535
      soft: 65535
    nproc: 65535
  volumes: 
    - "./data01:/usr/share/elasticsearch/data"

With version 2 of the Docker Compose spec, you can use the following (taken from the documentation):

cpu_count: 2
cpu_percent: 50
cpus: 0.5
cpu_shares: 73
cpu_quota: 50000
cpu_period: 20ms
cpuset: 0,1

mem_limit: 1000000000
memswap_limit: 2000000000
mem_reservation: 512m
Related