Skip to content

Commit

Permalink
K8S Deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvain-morin authored and sylvain-morin committed Dec 21, 2023
1 parent 430cbb0 commit d3dd6cb
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 6 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ log/*
static/*
venv
config.yml
models
36 changes: 30 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM python:3.11.6
FROM python:3.11.6 as base

RUN apt-get update && apt-get install -y libgdal-dev
RUN apt-get update && apt-get install -y libgdal-dev uwsgi-plugin-python3

RUN useradd -ms /bin/bash inaturalist
USER inaturalist
Expand All @@ -10,15 +10,39 @@ ENV PATH="/home/inaturalist/.local/bin:${PATH}"
RUN pip install --upgrade pip

# set the working directory in the container
WORKDIR /code
WORKDIR /home/inaturalist/vision

# copy the dependencies file to the working directory
COPY --chown=inaturalist:inaturalist ./requirements.txt /code/requirements.txt
COPY --chown=inaturalist:inaturalist ./requirements-production.txt /home/inaturalist/vision/requirements.txt

# install dependencies
RUN pip install -r requirements.txt

COPY --chown=inaturalist:inaturalist . /code
# Copy app and libs
COPY --chown=inaturalist:inaturalist app.py /home/inaturalist/vision
COPY --chown=inaturalist:inaturalist lib /home/inaturalist/vision/lib

# command to run on container start
# Create directories for the log and static content
RUN mkdir /home/inaturalist/vision/log
RUN mkdir /home/inaturalist/vision/static

# Development target
FROM base AS development

# Run with built-in Flask server
CMD [ "python", "app.py" ]

# Production target with uwsgi
FROM development AS production

COPY --chown=inaturalist:inaturalist ./uwsgi_main /home/inaturalist/vision/uwsgi_code

RUN pip install /home/inaturalist/vision/uwsgi_code

# Configure uwsgi
ENV UWSGI_PLUGIN_DIR /usr/lib/uwsgi/plugins
RUN mkdir /home/inaturalist/vision/uwsgi
COPY docker/uwsgi.ini /home/inaturalist/vision/uwsgi.ini

# Run with uwsgi
CMD ["uwsgi", "--ini", "/home/inaturalist/vision/uwsgi.ini", "--stats", ":1717", "--stats-http"]
20 changes: 20 additions & 0 deletions Dockerfile-cleanup
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM alpine:latest

RUN apk add --no-cache logrotate

RUN mkdir -p /var/vision/log
RUN mkdir -p /var/vision/static
RUN mkdir -p /var/vision/script

# Configure logrotate hourly
COPY docker/logrotate.conf /var/vision/script/logrotate.conf
COPY docker/logrotate-cron.sh /var/vision/script/logrotate-cron.sh
RUN chmod 400 /var/vision/script/logrotate.conf
RUN crontab -l | { cat; echo "0 * * * * sh /var/vision/script/logrotate-cron.sh"; } | crontab -

# Configure job to clean static folder
COPY docker/clean-static-cron.sh /var/vision/script/clean-static-cron.sh
RUN crontab -l | { cat; echo "*/10 * * * * sh /var/vision/script/clean-static-cron.sh"; } | crontab -

# Run cron
CMD ["/bin/sh", "-c", "crond -f"]
2 changes: 2 additions & 0 deletions docker/clean-static-cron.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
find /var/vision/static -type f -mmin +10 -exec rm {} \;
2 changes: 2 additions & 0 deletions docker/logrotate-cron.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
/usr/sbin/logrotate -s /var/vision/script/logrotate.status /var/vision/script/logrotate.conf
10 changes: 10 additions & 0 deletions docker/logrotate.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/var/vision/log/logstash.log {
hourly
rotate 5
size 10M
missingok
compress
delaycompress
notifempty
copytruncate
}
15 changes: 15 additions & 0 deletions docker/uwsgi.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[uwsgi]
wsgi-file = /home/inaturalist/vision/app.py
protocol = uwsgi
callable = app
chmod-socket = 777
socket= :8888
#socket = /home/inaturalist/vision/uwsgi/socket
master = true
lazy-apps = true
processes = 1
threads = 1
uid = www-data
gid = www-data
skip-atexit = true
skip-atexit-teardown = true
26 changes: 26 additions & 0 deletions requirements-production.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
flake8
Flask
Flask-WTF
h3
h3pandas
itsdangerous
jinja2
keras
matplotlib
numpy
pandas
Pillow
prison
python-magic
pyyaml
scikit_learn
scipy
six
tensorflow
tifffile
umap_learn
uWSGI
werkzeug
WTForms
#-f https://download.pytorch.org/whl/torch_stable.html
#torch==2.1.0+cpu

0 comments on commit d3dd6cb

Please sign in to comment.