ãã®èšäºã§ã¯ã皌åäžã®ïŒå®åïŒãµãŒããŒäžã§ãããžã§ã¯ããç·šæããããã®ããã€ãã®äŸ¿å©ãªæ¹æ³ãå
±æããŸãã
ç§ã¯äž»ã«Python / Djangoã¹ã¿ãã¯ã§äœæ¥ããŠããããããã¹ãŠã®äŸã¯äž»ã«ãã®ã»ããã«é¢é£ããŠããŸãã ãŸããäž»èŠãªãã¯ãããžãŒïŒUbuntuïŒ17.10ïŒãPython3ïŒ3.6ïŒã
å
容ïŒ
- ãã°ïŒlogrotateïŒ
- æªéïŒsystemdïŒ
- ããŒã«ã«èšå®
ãã¹ãŠãæ£ããå®è¡ããŠããããšãåæãšããŠããŸããã¢ããªã±ãŒã·ã§ã³ã¯ãªããžããªã«ä¿åããããµãŒããŒäžã®å¥ã®ãã©ã«ããŒã«ãããã€ãããŸããããšãã°ãvirtualenvã䜿çšãããŸãã å®è¡ããã«ã¯ãåå¥ã«äœæããããŠãŒã¶ãŒã䜿çšãããŸããããã¯ååãªæš©éãæã¡ãŸãããããŸãå€ããããŸããïŒããšãã°ãsudoããªããsshãã°ã€ã³ã¯èš±å¯ãããŸããïŒã
ãŸãããªããžããªã«ä¿åãããŠãããã®ã¯ãã¹ãŠã¯ãªãŒã³ãªã³ãŒããšéçããŒã¿ã§ãããšããäžè¬çãªçå®ãç¹°ãè¿ããŸãã ãã¹ãŠã®èšå®ã«ã¯ãæªäœ¿çšã®ãã¡ã€ã«ã§ãã£ãŠããããã©ã«ãã®ã¿ãå«ãŸãããã¹ã¯ãŒããšããŒã¯å«ãŸããŸããã
ã³ãŒããèšè¿°ããŠãã皌åäžã®ã³ã³ãã¥ãŒã¿ãŒïŒã©ãããããïŒã§ããããããžã§ã¯ããã©ã«ããŒã«éçšç°å¢ã«ã¢ããããŒãã§ããªããã®ãååšããªãããã«ããŸãã ããã¯ããããžã§ã¯ãå
ã®èšå®ãã©ã«ããŒïŒãªãã·ã§ã³ãšããŠãdevelopment_settings.pyïŒã«ãããã¡ã€ã«ãlocal_settings.pyãã䜿çšãããšããæªæã®ããææ³ãæããŸãã ãã®äŸã以äžã§åæããŸãã
ãã°
確ãã«ããªãã¯ãã®ã³ã°ã䜿çšããŠããŸãã çµã¿èŸŒã¿ã®ãã®ã³ã°ã¢ãžã¥ãŒã«ã¯éåžžã«åªããŠããŸããããããæŽç·ŽããŠä»ã®ãã¹ãŠã«äœ¿çšãã䟡å€ããããšã¯éããŸããã
ããšãã°ããã°ã®ããŒããŒã·ã§ã³ã ã€ã³ã¿ãŒãããã§ã¯ãæšæºã®RotatingFileHandlerããå§ãŸããããã€ãã®ãœãŒã¹ããã®ãã°ãèšé²ããããã®ãœã±ããã«ç¬èªã®ããŒã¢ã³ãæžã蟌ãããšã§çµãããè€éã§æŽç·Žãããæ¹æ³ããããŸãã åé¡ã¯ãçŽç²ãªPythonã§ãã¹ãŠãå®è¡ããããšããé¡æã®ããã«å§ãŸããŸãã ããã¯æãã§éå¹ççã§ããããšã©ãŒãçºçããå¯èœæ§ã®ããå ŽæããããããããããŸãã
logrotateãµãŒãã¹ã䜿çšããŸãã 以äžã¯ãã»ããªã®ãã°ã®ç°¡åãªèšå®ã§ãã
æšæºã§ã¯ããã¡ã€ã«/var/log/myproject/celery.logãæžã蟌ã¿ãæ¯æ¥/ var / log / myproject / archive /ãã©ã«ããŒã«é
眮ããåæ¥ã®ãµãã£ãã¯ã¹ãååã«è¿œå ããŸãã
/var/log/myproject/celery.log { size 1 su myuser myuser copytruncate create rotate 10 missingok postrotate timeext=`date -d '1 day ago' "+%Y-%m-%d"` # daily # timeext=$(date +%Y-%m-%d_%H) # hourly mv /var/log/myproject/celery.log.1 /var/log/myproject/archive/celery_$timeext.log endscript }
ãã°ãéåžžã«è¿
éã«æžã蟌ãŸãã1æéããšã«ããŒããŒã·ã§ã³ããå Žåã¯ãæ§æã®ãdailyãããã³ãhourlyãã®è¡ãã³ã¡ã³ãã¢ãŠãããŸãã ãŸããlogrotateã1æéããšã«å®è¡ããããã«æ§æããå¿
èŠããããŸãïŒããã©ã«ãã§ã¯ãéåžžã¯æ¯æ¥ïŒã bashã§å®è¡ïŒ
sudo cp /etc/cron.daily/logrotate /etc/cron.hourly/logrotate sudo sed -i -r "s/^[[:digit:]]*( .+cron.hourly)/0\1/" /etc/crontab
æ§æïŒmyserviceãã¡ã€ã«ïŒã¯logrotateãã©ã«ããŒã«é
眮ããå¿
èŠããããŸã
sudo cp config/logrotate/myservice /etc/logrotate.d/myservice
éèŠãªãã€ã³ãïŒ
- æ§æãã³ããŒããå¿
èŠããããã·ã³ããªãã¯ãªã³ã¯ã¯æ©èœããŸãã
- ååãšããŠãèšå®ã¯ã»ãšãã©logrotateã«ãã£ãŠããã¯ããååŸãããŸããã
copytruncate
ãã£ã¬ã¯ãã£ããèšå®ããããšã¯éåžžã«éèŠã§ã - ïŒ rusnasonovã®ã³ã¡ã³ãããè¿œå ïŒlogrotateã¯éŠ¬é¹¿ããŠããããããã¡ã®ãªãã·ã³ãã«ãªããŒã¿ãªãŒã·ã¹ãã ã䜿çšããŠããŸãã ããŒããŒã·ã§ã³ã¯2段éã§è¡ãããŸã-æåã«å€ããã¡ã€ã«ãã³ããŒããã次ã«å€ãå Žæã«ããªãã³ã°ãããŸãã ããã«ãããééã§èšé²ããããã°ã倱ãããå¯èœæ§ããããŸãïŒ ããã¥ã¡ã³ãã«åæ ïŒ
copytruncateã¯ãå転äžã«ãã¡ã€ã«ãéããããªãããéèŠã§ãã ãã©ã€ããã·ã¹ãã äžã§ãã¡ã€ã«ãããŒããŒã·ã§ã³ããããããã¡ã€ã«ã¯éããŠããããã¡ã€ã«ã«æžã蟌ããµãŒãã¹ã¯äœããã®çš®é¡ã®ãã¡ã€ã«èšè¿°åã«åŸã£ãŠãããè¡ããŸãã ãã¡ã€ã«ã移åããŠæ°ãã空ã®ãã¡ã€ã«ãäœæããã ãã®å Žåããã®ãã¡ã€ã«ã¯äœ¿çšãããŸããã copytruncateã¯ãå
容ãã³ããŒããŠãããã¡ã€ã«ãã¯ãªã¢ããå¿
èŠãããããéããªãããšã瀺ããŠããŸãã
ãµãŒãã¹
ã¢ããªã±ãŒã·ã§ã³ãã©ã®ããã«èµ·åããŸããïŒ ããŸããŸãªæ¹æ³ããããŸãã ç§ã®èŠ³å¯ã«ãããšãäž»ãªãã®ã¯æ¬¡ã®ãšããã§ãã
- screen / tmuxãèµ·åããå
éšã§ã€ã³ã¿ã©ã¯ãã£ãã¹ã¯ãªãããèµ·åããŸã
- gunicornãŸãã¯ã»ããªã®ã-Dãã¿ã€ãã®ããŒã¢ã³ã¢ãŒã
- ç£ç£
- init.dã¹ã¯ãªãã
- Docker
ãããã«ã¯ãã¹ãŠå©ç¹ããããŸãããç§ã®æèŠã§ã¯ãããã«æ¬ ç¹ããããŸãã
ããã§ã¯Dockerãæ€èšããŸããã ãŸããããŸãçµéšããããŸããã 第äºã«ãã³ã³ããã䜿çšããå Žåãããªããšãã®èšäºã®ãã®ä»ã®ãã³ãã¯å®éã«ã¯å¿
èŠãããŸããã ããã§ã¯ãã¢ãããŒãã¯ãã§ã«ç°ãªã£ãŠããŸãã
ã·ã¹ãã ã䟿å©ãªããŒã«ãæäŸããŠããããªãããªãããã䜿ããªãã®ããšæããŸãã
Ubuntuã§ã¯ãããŒãžã§ã³15.04以éãããã©ã«ãã§systemdããµãŒãã¹ã®ç®¡ççšã«æäŸãããŠããŸãïŒããã ãã§ã¯ãããŸããïŒã
systemdã¯ããã¹ãŠãæ£ããè¡ããšããç¹ã§éåžžã«äŸ¿å©ã§ãã
- å¿
èŠãªãŠãŒã¶ãŒã®äžã§ã¢ããªã±ãŒã·ã§ã³ãèµ·åããå¿
èŠã«å¿ããŠç°å¢å€æ°ãèšå®ããŸã
- ã¢ããªã±ãŒã·ã§ã³ãçªç¶åæ¢ããå Žåãæå®ãããåæ°ã ãåèµ·åããŸã
- éåžžã«æè»ã§ãäŸåé¢ä¿ãšèµ·åé åºãèšå®ã§ããŸã
ãã¡ãããsystemdããæã¡ã§ãªãå Žåã¯ãsupervisordã®æ¹ãèŠãããšãã§ããŸããããã®ããŒã«ã«ã¯ããªã倧ããªå«æªãããã䜿çšãé¿ããŠããŸãã
systemdã®äœ¿çšãæ害ã§ãããšç£ç£ãããŠããããšãçã人ãããªãããšãé¡ã£ãŠããŸãã
以äžã«ãå®è¡ããæ§æã®äŸã瀺ããŸãã
gunicornãèµ·åããŸãïŒããŒã«ã«nginxãä»ããŠãããã·ãããŸãããããã§ã¯é¢ä¿ãããŸããïŒã
[Unit] Description=My Web service Documentation= StartLimitIntervalSec=11 [Service] Type=simple Environment=DJANGO_SETTINGS_MODULE=myservice.settings.production ExecStart=/opt/venv/bin/python3 -W ignore /opt/venv/bin/gunicorn -c /opt/myservice/config/gunicorn/gunicorn.conf.py --chdir /opt/myservice myservice.wsgi:application Restart=always RestartSec=2 StartLimitBurst=5 User=myuser Group=myuser ExecStop=/bin/kill -s TERM $MAINPID WorkingDirectory=/opt/myservice ReadWriteDirectories=/opt/myservice [Install] WantedBy=multi-user.target Alias=my-web.service
ããã§ã¯ãæªéã¢ãŒãã䜿çšããªãããšãéèŠã§ãã éåžžã®ããã»ã¹ã§gunicornãèµ·åããsystemdèªäœããããæªéåããã¯ã©ãã·ã¥ããå Žåã®åèµ·åãç£èŠããŸãã
virtualenvãã©ã«ããŒã«çžå¯Ÿçãªpythonããã³gunicornãžã®ãã¹ã䜿çšããããšã«æ³šæããŠãã ããã
ã»ããªã®å Žåããã¹ãŠãåãã«ãªããŸããã次ã®ããã«èµ·åã©ã€ã³ããå§ãããŸãïŒç¬èªã®ãã¹ãšå€ãå
¥åããŠãã ããïŒïŒ
ExecStart=/opt/venv/bin/celery worker -A myservice.settings.celery_settings -Ofair --concurrency=3 --queues=celery --logfile=/var/log/myservice/celery.log --max-tasks-per-child 1 --pidfile=/tmp/celery_myservice.pid -n main.%h -l INFO -B
åèµ·åã®ãã©ã¡ãŒã¿ã«æ³šæãã䟡å€ããããŸãã
StartLimitIntervalSec=11 RestartSec=2 StartLimitBurst=5
èŠããã«ãããã¯æ¬¡ã®ããšãæå³ããŸãããµãŒãã¹ãã¯ã©ãã·ã¥ããå Žåã2ç§åŸã«ãµãŒãã¹ãåèµ·åããŸããã11ç§ã§5åãè¶
ããŸããã StartLimitIntervalSecã®å€ãããšãã°9ç§ã§ããå ŽåããµãŒãã¹ãé£ç¶ããŠ5ååæ¢ããå ŽåïŒèµ·åçŽåŸïŒã5åç®ã®ããããã®åŸãsystemdã¯ããããããããçºçãããªãïŒ2 * 5ïŒããšãç解ããããšãéèŠã§ãã 11ã®å€ã¯ããã®ãããªåçš®ãé€å€ããããã«éžæãããŸãã ããšãã°ã15ç§éãããã¯ãŒã¯é害ãçºçããéå§çŽåŸã«ã¢ããªã±ãŒã·ã§ã³ãïŒã¿ã€ã ã¢ãŠããªãã§ïŒã¯ã©ãã·ã¥ããå Žåãåæ¢ãããããåå©ã«åããæ¹ãããã§ãããã
ãã®æ§æãã·ã¹ãã ã«ã€ã³ã¹ããŒã«ããã«ã¯ãäœæ¥ãã©ã«ããŒããã·ã³ããªãã¯ãªã³ã¯ãäœæããã ãã§ãã
~~sudo ln -s /opt/myservice/config/systemd/*.service /etc/systemd/system/~~ sudo systemctl daemon-reload
ãã ããã·ã³ããªãã¯ãªã³ã¯ã«ã¯æ³šæããå¿
èŠããããŸã-ãããžã§ã¯ããã·ã¹ãã ãã©ã€ãã«ãªãå ŽåããµãŒãã¹ã®éå§åŸã«ããŠã³ãã§ããå¯èœæ§ããããŸãïŒããšãã°ããããã¯ãŒã¯ãã©ã€ããã¡ã¢ãªãããïŒã ãã®å Žåãåã«éå§ãããŸããã ããã§ã¯ãäŸåé¢ä¿ãæ£ããæ§æããæ¹æ³ãã°ãŒã°ã«ã§æ€çŽ¢ããå¿
èŠããããŸããå®éã«ã¯ãconfigdãsystemdãã©ã«ããŒã«ã³ããŒããããšããå§ãããŸãã
æŽæ°ïŒ andreymalã«ããçºèšã®åŸãæ§æããã©ã«ããŒã«ã³ããŒããé©åãªæš©éãèšå®ããæ¹ãæ£ãããšæããŸãã
sudo chown root: /opt/myservice/config/systemd/*.service sudo chmod 770 /opt/myservice/config/systemd/*.service sudo cp /opt/myservice/config/systemd/*.service /etc/systemd/system/ sudo systemctl daemon-reload
ãŸããã³ã³ãœãŒã«ãžã®åºåãç¡å¹ã«ããããšããå§ãããŸããããããªããšããã¹ãŠãsyslogã«éãããŸãã
systemdã«ãã¹ãŠã®ã³ã³ããŒãã³ããããå Žåãåã³ã³ããŒãã³ãã®äœ¿çšã¯æ¬¡ã®ããã«ãªããŸãã
sudo systemctl stop my-web.service sudo systemctl stop my-celery.service sudo systemctl start my-web.service sudo systemctl start my-celery.service
è€æ°ã®ã³ã³ããŒãã³ããããå ŽåããµãŒããŒäžã®ãšã³ãããŒå
šäœã®å€§èŠæš¡ãªç®¡çã®ããã®ã¹ã¯ãªãããäœæããããšã¯çã«ããªã£ãŠããŸãã
bash manage.sh migrate bash manage.sh start
ã³ã³ãœãŒã«çµç±ã®ãªã¢ãŒããããã°ã®å ŽåïŒdjango-extensionsããshell_plusãå®è¡ããŸãïŒïŒ
bash manage.sh debug
è€æ°ã®ãµãŒããŒãããå Žåã¯ããã¡ãããæ¢ã«ã¹ã¯ãªããã䜿çšããŠå±éããŠããŸãããããã¯åãªãäŸã§ãã
ããŒã«ã«èšå®
ããã¯holivarã®ãããã¯ã§ããããã®æ®µèœã¯ç§ã®èŠç¹ã«ããããç§ã®çµéšã«åºã¥ããŠããããšã«æ³šæããŠãã ããã ããªãã奜ããªããã«ãç§ã¯ã¡ããã©ãå§ãããŸãã
åé¡ã®æ¬è³ªã¯ãã¢ããªã±ãŒã·ã§ã³ã®äœææ¹æ³ã«é¢ä¿ãªããã©ããã«ä¿åãããŠããããŒã¿ãåŠçããå¿
èŠãããå¯èœæ§ãé«ãããšã§ãã ãŠãŒã¶ãŒã®ãªã¹ããããŒãžã®ãã¹ããã¹ã¯ãŒããããŒã¿ããŒã¹ãžã®ãã¹ãªã©...ãããã®èšå®ã¯ãã¹ãŠãªããžããªã«ä¿åã§ããŸããã 誰ããä¿ç®¡ããŸãããããã¯æªè³ªãªè¡çºã§ãã å®å
šã§ã¯ãªããæè»æ§ããããŸããã
ãã®ãããªèšå®ãä¿åããæãäžè¬çãªæ¹æ³ãšãããã®åé¡ã¯äœã§ããïŒ
- local_settings.pyãã¡ã€ã«ãããã©ã«ãã®setting.pyã®é£ã®ãããžã§ã¯ããã©ã«ããŒã«ä¿åãããŸãã
åé¡ïŒèª€ã£ãŠãã¡ã€ã«ãã³ãããããå¯èœæ§ããããŸãããã©ã«ããŒãã³ããŒ/æŽæ°ãããšãã«ãã¡ã€ã«ãæ¶å»ã§ããŸãïŒrsyncãŸãã¯ã¢ãŒã«ã€ãããïŒ - ç°å¢å€æ°ã ãŸããããŸãå®å
šã§ã¯ãªããããŸã䟿å©ã§ã¯ãããŸããïŒããšãã°ããœãããªããŒããªã©ïŒ
- ãããžã§ã¯ããã©ã«ããŒå€ã®å¥ã®ãã¡ã€ã«
ãã®ç¹å®ã®æ¹æ³ããå§ãããŸãã ç§ã¯éåžžããããžã§ã¯ãã®yamlãã¡ã€ã«ããã©ã«ããŒã/ usr / local / etc /ãã«äœæããŸãã ç§ã¯å°ããªã¢ãžã¥ãŒã«ãæžããŸããã éæ³ ãããã³ã°ã¯ããã¡ã€ã«ããã€ã³ããŒãã¢ãžã¥ãŒã«ã®locals()
ãŸãã¯globals()
å€æ°ãããŒãããŸãã
éåžžã«ç°¡åã«äœ¿çšãããŸãã Djangoã®settings.pyã®æ·±ãã®ã©ããïŒæåŸã®æ¹ãè¯ãïŒã§åŒã³åºããŸãïŒ
import_settings("/usr/local/etc/myservice.yaml")
ãããŠããã¹ãŠã®ã³ã³ãã³ããã°ããŒãã«èšå®ã«ããã¯ã¹ãããŸãã ãªã¹ããšèŸæžã«ããŒãžã䜿çšããŠããŸããã誰ã«ãšã£ãŠã䟿å©ã§ã¯ãªããããããŸããã Djangoã¯å€§æåã®å®æ°ã®ã¿ãã€ã³ããŒãããããšãèŠããŠããããšãéèŠã§ããã€ãŸãããã¡ã€ã«ã§ã¯ãããã«å€§æåã«ããå¿
èŠãããæåã®ã¬ãã«ã®èšå®ã§ãã
ããã¯ãã¹ãŠã®äººã
ã§ãïŒ
ã³ã¡ã³ãã®æ®ãã®éšåã«ã€ããŠèª¬æããŸãã