Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include Production WSGI (Gunicorn) to replace Flask Default Server #263

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 59 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ IMAGE_VERSION := 0.7
IMAGE ?= $(IMAGE_REGISTRY)/$(IMAGE_NAME):v$(IMAGE_VERSION)
LATEST_TAG_IMAGE := $(IMAGE_REGISTRY)/$(IMAGE_NAME):latest
TEST_IMAGE := $(IMAGE)-test
MODEL_SERVER_TEST_IMAGE := $(TEST_IMAGE)-model-server
OFFLINE_TRAINER_TEST_IMAGE := $(TEST_IMAGE)-offline-trainer

CTR_CMD = docker

Expand All @@ -20,6 +22,12 @@ build-test-nobase:
build-test:
$(CTR_CMD) build -t $(TEST_IMAGE) -f $(DOCKERFILES_PATH)/Dockerfile.test .

build-test-model-server:
$(CTR_CMD) build -t $(MODEL_SERVER_TEST_IMAGE) -f $(DOCKERFILES_PATH)/Dockerfile.test-model-server .

build-test-offline-trainer:
$(CTR_CMD) build -t $(OFFLINE_TRAINER_TEST_IMAGE) -f $(DOCKERFILES_PATH)/Dockerfile.test-offline-trainer .

push:
$(CTR_CMD) push $(IMAGE)

Expand Down Expand Up @@ -51,28 +59,75 @@ run-model-server:
$(CTR_CMD) run -d --platform linux/amd64 -e "MODEL_TOPURL=http://localhost:8110" -v ${MODEL_PATH}:/mnt/models -p 8100:8100 --name model-server $(TEST_IMAGE) /bin/bash -c "python3.8 tests/http_server.py & sleep 10 && python3.8 src/server/model_server.py"
while ! docker logs model-server | grep -q Serving; do echo "waiting for model-server to serve"; sleep 5; done

run-model-server-prod:
$(CTR_CMD) run -d \
--platform linux/amd64 \
-e "MODEL_TOPURL=http://localhost:8110" \
-v ${MODEL_PATH}:/mnt/models \
-p 8105:8105 \
--name model-server-prod \
$(MODEL_SERVER_TEST_IMAGE)

run-estimator-client:
$(CTR_CMD) exec model-server /bin/bash -c "python3.8 -u ./tests/estimator_model_request_test.py"

clean-model-server:
@$(CTR_CMD) stop model-server
@$(CTR_CMD) rm model-server

test-model-server: run-model-server run-estimator-client clean-model-server
clean-model-server-prod:
@$(CTR_CMD) stop model-server-prod
@$(CTR_CMD) rm model-server-prod

test-model-server:
@if [ "$(ENV)" = "prod" ]; then \
run-model-server-prod run-estimator-client clean-model-server-prod
else \
run-model-server run-estimator-client clean-model-server
fi

# test offline trainer
run-offline-trainer:
$(CTR_CMD) run -d --platform linux/amd64 -p 8102:8102 --name offline-trainer $(TEST_IMAGE) python3.8 src/train/offline_trainer.py
$(CTR_CMD) run -d --platform linux/amd64 \
-p 8102:8102 \
--name offline-trainer \
$(TEST_IMAGE) \
python3.8 src/train/offline_trainer.py
sleep 5

run-offline-trainer-client:
$(CTR_CMD) exec offline-trainer /bin/bash -c "python3.8 -u ./tests/offline_trainer_test.py"
$(CTR_CMD) exec offline-trainer \
/bin/bash -c \
"python3.8 -u ./tests/offline_trainer_test.py"


run-offline-trainer-client-prod:
$(CTR_CMD) exec offline-trainer-prod \
/bin/bash -c \
"python3.8 -u ./tests/offline_trainer_test.py"

run-offline-trainer-prod:
$(CTR_CMD) run -d \
--platform linux/amd64 \
-p 8107:8107 \
--name offline-trainer-prod \
$(OFFLINE_TRAINER_TEST_IMAGE)
sleep 5

clean-offline-trainer:
@$(CTR_CMD) stop offline-trainer
@$(CTR_CMD) rm offline-trainer

test-offline-trainer: run-offline-trainer run-offline-trainer-client clean-offline-trainer
clean-offline-trainer-prod:
@$(CTR_CMD) stop offline-trainer-prod
@$(CTR_CMD) rm offline-trainer-prod

test-offline-trainer:
@if [ "$(ENV)" = "prod" ]; then \
run-offline-trainer-prod run-offline-trainer-client-prod clean-offline-trainer-prod
else \
run-offline-trainer run-offline-trainer-client clean-offline-trainer
fi

test: build-test test-pipeline test-estimator test-model-server test-offline-trainer

Expand Down
9 changes: 9 additions & 0 deletions config/gunicorn_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import os


workers = int(os.environ.get('GUNICORN_PROCESSES', '2'))
threads = int(os.environ.get('GUNICORN_THREADS', '4'))
port = os.environ.get('GUNICORN_PORT', '8100')
bind = os.environ.get('GUNICORN_BIND', '0.0.0.0:' + port)
forwarded_allow_ips = '*'
secure_scheme_headers = { 'X-Forwarded-Proto': 'https' }
11 changes: 7 additions & 4 deletions dockerfiles/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ COPY src/util src/util
COPY cmd cmd

# port for Model Server
EXPOSE 8100
ENV GUNICORN_PORT=8105

EXPOSE ${GUNICORN_PORT}
# port for Online Trainer (TODO: reserved for event-based online training)
EXPOSE 8101
#EXPOSE 8101
# port for Offline Trainer
EXPOSE 8102
#EXPOSE 8102


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated change?

ENTRYPOINT ["python3.8", "cmd/main.py"]
CMD ["gunicorn", "--config", "config/gunicorn_config.py", "src.server.model_server:app"]
2 changes: 1 addition & 1 deletion dockerfiles/Dockerfile.test
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ EXPOSE 8101
# port for Offline Trainer
EXPOSE 8102

CMD [ "python3.8", "-u", "src/server/model_server.py" ]
CMD [ "python3.8", "-u", "src/server/model_server.py" ]
26 changes: 26 additions & 0 deletions dockerfiles/Dockerfile.test-model-server
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
FROM --platform=linux/amd64 quay.io/sustainable_computing_io/kepler_model_server_base:v0.7

WORKDIR /usr/local

RUN mkdir -p /usr/local/src
RUN mkdir -p /usr/local/resource

COPY dockerfiles/requirements.txt dockerfiles/requirements.txt
RUN pip install --no-cache-dir -r dockerfiles/requirements.txt

COPY src/estimate src/estimate
COPY src/server src/server
COPY src/train src/train
COPY src/util src/util
COPY cmd cmd
COPY config config

RUN mkdir -p tests/data
COPY tests/data/prom_output tests/data/prom_output
COPY tests/*.py tests/

ENV GUNICORN_PORT=8105

EXPOSE ${GUNICORN_PORT}

CMD ["gunicorn", "--config", "config/gunicorn_config.py", "src.server.model_server:app"]
26 changes: 26 additions & 0 deletions dockerfiles/Dockerfile.test-offline-trainer
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
FROM --platform=linux/amd64 quay.io/sustainable_computing_io/kepler_model_server_base:v0.7

WORKDIR /usr/local

RUN mkdir -p /usr/local/src
RUN mkdir -p /usr/local/resource

COPY dockerfiles/requirements.txt dockerfiles/requirements.txt
RUN pip install --no-cache-dir -r dockerfiles/requirements.txt

COPY src/estimate src/estimate
COPY src/server src/server
COPY src/train src/train
COPY src/util src/util
COPY cmd cmd
COPY config config

RUN mkdir -p tests/data
COPY tests/data/prom_output tests/data/prom_output
COPY tests/*.py tests/

ENV GUNICORN_PORT=8107

EXPOSE ${GUNICORN_PORT}

CMD ["gunicorn", "--config", "config/gunicorn_config.py", "src.train.offline_trainer:app"]
1 change: 1 addition & 0 deletions dockerfiles/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
flask==2.1.2
gunicorn==22.0.0
Werkzeug==2.2.2
protobuf==3.19.4
pandas==1.4.4
Expand Down