oliver006 / elasticsearch-test-data

Generate and upload test data to Elasticsearch for performance and load testing
MIT License
257 stars 124 forks source link

Added a Dockerfile #24

Closed unfor19 closed 3 years ago

unfor19 commented 3 years ago

@oliver006 Thank you for this great script, it works perfectly.

Just thought you might want to use this Dockerfile that I've created. Please note that I'm copying from tests/es_test_data so you'll need to remove the tests prefix if you build it from this repository.

DockerHub unfor19/es-test-data Size unpacked: 47.9MB Size @ DockerHub: 17.21MB

Example:

$ docker-compose up -d # docker-compose.yml at the bottom
$ docker run --rm -it --network host unfor19/es-test-data  \
   --es_url=http://localhost:9200  \
   --batch_size=10000  \
   --username=elastic \
   --password="esbackup-password"

[I 210317 22:22:19 es_test_data:54] Trying to create index http://localhost:9200/test_data
[I 210317 22:22:19 es_test_data:61] Looks like the index exists already
[I 210317 22:22:19 es_test_data:238] Generating 100000 docs, upload batch size is 10000
[I 210317 22:22:20 es_test_data:82] Upload: OK - upload took:   731ms, total docs uploaded:   10000
[I 210317 22:22:21 es_test_data:82] Upload: OK - upload took:   749ms, total docs uploaded:   20000
[I 210317 22:22:22 es_test_data:82] Upload: OK - upload took:   679ms, total docs uploaded:   30000
[I 210317 22:22:24 es_test_data:82] Upload: OK - upload took:   729ms, total docs uploaded:   40000
[I 210317 22:22:25 es_test_data:82] Upload: OK - upload took:   691ms, total docs uploaded:   50000
[I 210317 22:22:26 es_test_data:82] Upload: OK - upload took:   729ms, total docs uploaded:   60000
[I 210317 22:22:27 es_test_data:82] Upload: OK - upload took:   766ms, total docs uploaded:   70000
[I 210317 22:22:28 es_test_data:82] Upload: OK - upload took:   698ms, total docs uploaded:   80000
[I 210317 22:22:30 es_test_data:82] Upload: OK - upload took:   709ms, total docs uploaded:   90000
[I 210317 22:22:31 es_test_data:82] Upload: OK - upload took:   705ms, total docs uploaded:  100000
[I 210317 22:22:31 es_test_data:272] Done - total docs uploaded: 100000, took 12 seconds

Dockerfile

### --------------------------------------------------------------------
### Docker Build Arguments
### Available only during Docker build - `docker build --build-arg ...`
### --------------------------------------------------------------------
ARG DEBIAN_VERSION="buster"
ARG ALPINE_VERSION="3.12"
ARG PYTHON_VERSION="3.9.1"
ARG APP_NAME="tornado"
ARG APP_VERSION="4.5.3"
ARG APP_PYTHON_USERBASE="/app"
ARG APP_USER_NAME="appuser"
ARG APP_USER_ID="1000"
ARG APP_GROUP_NAME="appgroup"
ARG APP_GROUP_ID="1000"
# Reminder- the ENTRYPOINT is hardcoded so make sure you change it (remove this comment afterwards)
### --------------------------------------------------------------------

### --------------------------------------------------------------------
### Build Stage
### --------------------------------------------------------------------
FROM python:"$PYTHON_VERSION"-slim-"${DEBIAN_VERSION}" as build

ARG APP_PYTHON_USERBASE
ARG APP_VERSION
ARG APP_NAME

# Define env vars
ENV PIP_DISABLE_PIP_VERSION_CHECK=1 \
    PYTHONUSERBASE="$APP_PYTHON_USERBASE" \
    PATH="${APP_PYTHON_USERBASE}/bin:${PATH}"

# Upgrade pip and then install build tools
RUN pip install --upgrade pip && \
    pip install --upgrade wheel setuptools wheel

# Define workdir
WORKDIR "$APP_PYTHON_USERBASE"

# Install the app
RUN pip install --ignore-installed --no-warn-script-location --prefix="/dist" "$APP_NAME"=="$APP_VERSION"

WORKDIR /dist/

COPY tests/es_test_data.py .

# For debugging the Build Stage
CMD ["bash"]
### --------------------------------------------------------------------

### --------------------------------------------------------------------
### App Stage
### --------------------------------------------------------------------
FROM python:"$PYTHON_VERSION"-alpine"${ALPINE_VERSION}" as app

# Fetch values from ARGs that were declared at the top of this file
ARG APP_NAME
ARG APP_PYTHON_USERBASE
ARG APP_USER_ID
ARG APP_USER_NAME
ARG APP_GROUP_ID
ARG APP_GROUP_NAME

# Define env vars
ENV HOME="$APP_PYTHON_USERBASE" \
    PYTHONUSERBASE="$APP_PYTHON_USERBASE" \
    APP_NAME="$APP_NAME" \
    PYTHONUNBUFFERED=0
ENV PATH="${PYTHONUSERBASE}/bin:${PATH}"

# Define workdir
WORKDIR "$PYTHONUSERBASE"

# Run as a non-root user
RUN \
    addgroup -g "${APP_GROUP_ID}" "${APP_GROUP_NAME}" && \
    adduser -H -D -u "$APP_USER_ID" -G "$APP_GROUP_NAME" "$APP_USER_NAME" && \
    chown -R "$APP_USER_ID":"$APP_GROUP_ID" "$PYTHONUSERBASE"
USER "$APP_USER_NAME"

# Copy artifacts from Build Stage
COPY --from=build --chown="$APP_USER_NAME":"$APP_GROUP_ID" /dist/ "$PYTHONUSERBASE"/

# The container runs the application, or any other supplied command, such as "bash" or "echo hello"
# CMD python -m ${APP_NAME}

# Use ENTRYPOINT instead CMD to force the container to start the application
ENTRYPOINT ["python", "es_test_data.py"]

docker-compose.yml

version: "3.7"

### ------------------------------------------------------------------
### Variables
### ------------------------------------------------------------------
x-variables:
  exposed-port: &exposed-port 9200
  es-base: &es-base
    image: docker.elastic.co/elasticsearch/elasticsearch:7.9.1
    ulimits:
      memlock:
        soft: -1
        hard: -1
    networks:
      - elastic
  data-path: &data-path /usr/share/elasticsearch/data      
  snapshots-repository-path: &snapshots-repository-path /usr/share/elasticsearch/backup
  volume-snapshots-repository: &volume-snapshots-repository 
    - type: volume
      source: snapshots-repository
      target: *snapshots-repository-path  
  services-es-env: &es-env-base
    "cluster.name": "es-docker-cluster"
    "cluster.initial_master_nodes": "es01,es02"
    "bootstrap.memory_lock": "true"
    "ES_JAVA_OPTS": "-Xms512m -Xmx512m"
    "ELASTIC_PASSWORD": "esbackup-password"
    "xpack.security.enabled": "true"
    "path.repo": *snapshots-repository-path
### ------------------------------------------------------------------

services:
  es01: # master
    <<: *es-base
    container_name: es01
    environment:
      <<: *es-env-base
      node.name: es01
      discovery.seed_hosts: es02
    volumes:
      - <<: *volume-snapshots-repository 
      - type: volume
        source: data01
        target: *data-path
    ports:
      - published: *exposed-port
        target: 9200
        protocol: tcp
        mode: host

  es02:
    <<: *es-base
    container_name: es02
    environment:
      <<: *es-env-base
      node.name: es02
      discovery.seed_hosts: es01
    volumes:
      - <<: *volume-snapshots-repository 
      - type: volume
        source: data02
        target: *data-path

volumes:
  data01:
    driver: local
  data02:
    driver: local
  snapshots-repository:
    driver: local

networks:
  elastic:
    driver: bridge
oliver006 commented 3 years ago

This is great, thank you! I'll definitely add this when I have a minute.

unfor19 commented 3 years ago

Roger that, I'll add a PR