Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Dockerを利用したローカル環境から本番環境までの構築設計 / Project struct...

Kou
February 03, 2018

Dockerを利用したローカル環境から本番環境までの構築設計 / Project structure design for docker application

Kou

February 03, 2018
Tweet

More Decks by Kou

Other Decks in Programming

Transcript

  1. Α͋͘ΔWeb։ൃݱ৔ͷಈ࡞؀ڥ 1. ࣗ෼ͷϚγϯ಺Ͱ࣮૷தͷΞϓϦέʔγϣϯΛελϯυΞ ϩϯʹಈ࡞ͤ͞Δ؀ڥ (ϩʔΧϧ؀ڥ) 2. ࣮૷͕׬ྃͨ͠ΞϓϦέʔγϣϯΛଞͷϓϩδΣΫτϝϯό ʔ΍ςελʔʹಈ࡞֬ೝͯ͠΋Β͏ࣾ಺޲͚؀ڥ (։ൃ؀ڥ) 3.

    ࣾ಺Ͱಈ࡞֬ೝͨ͠ΞϓϦέʔγϣϯΛຊ൪ϦϦʔεલʹ ͓٬͞Μʹݕऩͯ͠΋Β͏ͨΊͷ؀ڥ (εςʔδϯά؀ڥ) 4. ςετ΋ݕऩ΋׬ྃͨ͠ΞϓϦέʔγϣϯΛՔಇͤ͞Δͨ Ίͷ؀ڥ (ຊ൪؀ڥ)
  2. ֤αʔόͰಈ࡞ͤ͞Δϛυϧ΢ΣΞ/ΞϓϦέʔγϣϯ ▪ϩʔυόϥϯαʔ
 ɾAWSͷApplication Load Balancer (ALB)Λ࢖༻
 
 ▪Ωϟογϡαʔό
 ɾAWSͷElastiCache (Redis)Λ࢖༻


    
 ▪σʔλϕʔε
 ɾAWSͷRDS (MySQL)Λ࢖༻
 
 ▪web01/02 (EC2αʔό)
 ɾNginx (HTTPαʔό)
 ɾWebΞϓϦέʔγϣϯ (DjangoͰಈ͘Python3ΞϓϦέʔγϣϯ)
 
 ▪batch01/02 (EC2αʔό)
 ɾPython3Ͱಈ͘όονΞϓϦέʔγϣϯ
 ɾδϣϒεέδϡʔϥ(Celery beat)
 ɾRabbitMQ (CeleryͷδϣϒΩϡʔͱͯ͠࢖༻)
  3. DockerΛ࢖Θͳ͍ϩʔΧϧ؀ڥͷߏஙखॱ 1. ALB͸ϩʔΧϧͩͱཱͯΒΕͳ͍ͷͰɺ୅ΘΓʹHAProxyΛ ࢖͏
 2. HAProxyɺRedisɺMySQLɺNginxɺRabbitMQɺPython3Λ HomebrewͰΠϯετʔϧ͢Δ
 3. ϩʔΧϧ༻ͷ֤ϛυϧ΢ΣΞͷઃఆϑΝΠϧΛهड़͢Δ
 4.

    WebΞϓϦέʔγϣϯͱόονΞϓϦέʔγϣϯͰґଘؔ܎ Λ෼཭ͤ͞ΔͨΊʹ venv Λ࢖༻͢Δ
 5. WebΞϓϦέʔγϣϯͱόονΞϓϦέʔγϣϯͷґଘؔ܎ ΛͦΕͧΕ pip ͰΠϯετʔϧ͠ɺͦΕͧΕىಈ͓ͯ͘͠ɻ
  4. DockerΛ࢖͏ϩʔΧϧ؀ڥͷߏஙखॱ 1. ALB͸ϩʔΧϧͩͱཱͯΒΕͳ͍ͷͰɺ୅ΘΓʹHAProxyΛ࢖͏
 2. HAProxyɺRedisɺMySQLɺNginxɺRabbitMQɺPythonͷ࢖༻͢ΔΠϝʔδ໊ͱ λά໊(όʔδϣϯ)ɺઃఆ஋Λ docker-compose ͷઃఆϑΝΠϧ(docker- compose.yml)ʹهड़͢Δ
 3.

    ϩʔΧϧ༻ͷ֤ϛυϧ΢ΣΞͷઃఆϑΝΠϧΛهड़͢Δ
 4. मਖ਼Λ͙͢ʹ֬ೝͰ͖ΔΑ͏ʹ͢ΔͨΊɺϗετଆʹ͋ΔΞϓϦέʔγϣϯͷι ʔεϑΝΠϧΛVOLUME໋ྩͰϚ΢ϯτͯ͠ར༻͢Δ
 5. WebΞϓϦέʔγϣϯͱόονΞϓϦέʔγϣϯͷґଘؔ܎ΛͦΕͧΕDockerͰ औಘͨ͠Pythonͷ pip ͰΠϯετʔϧ͠ɺϗετଆͷͦΕͧΕผσΟϨΫτϦ΁อ ଘͰ͖ΔΑ͏ʹ͓ͯ͘͠
 6. docker-composeΛىಈ͠ɺdocker-compose.ymlʹهड़ͨ͠ϛυϧ΢ΣΞͱΞϓ ϦέʔγϣϯΛىಈ͢Δ
  5. DockerΛ࢖Θͳ͍։ൃ/ຊ൪؀ڥͷߏஙखॱ 1. ࢖༻͢ΔALBɺElastiCache (Redis)ɺRDS (MySQL) Λ࡞੒͢Δ
 2. NginxɺRabbitMQɺPythonͷ࢖༻͢ΔόʔδϣϯΛAnsibleͳͲΛ࢖ͬ ͯΠϯετʔϧ͢Δ
 3.

    ։ൃ/ຊ൪༻ͷ֤ϛυϧ΢ΣΞͷઃఆϑΝΠϧΛهड़͢Δ
 4. ։ൃػʹWebΞϓϦέʔγϣϯͱόονΞϓϦέʔγϣϯΛύοέʔ δϯάͯ͠ɺϑΝΠϧసૹ͢Δ (Capistrano΍Ansistrano͋ͨΓΛ࢖͏)
 5. WebΞϓϦέʔγϣϯͱόονΞϓϦέʔγϣϯͰґଘؔ܎Λ෼཭͞ ͤΔͨΊʹ venv Λ࢖༻͢Δ
 6. WebΞϓϦέʔγϣϯͱόονΞϓϦέʔγϣϯͷґଘؔ܎Λσϓϩ Π࣌ʹͦΕͧΕ pip ͰΠϯετʔϧ͠ɺىಈ͓ͯ͘͠ɻ
  6. DockerΛ࢖͏։ൃ/ຊ൪؀ڥͷߏஙखॱ 1. ࢖༻͢ΔALBɺElastiCache (Redis)ɺRDS (MySQL)Λ࡞੒ ͢Δ
 2. NginxɺRabbitMQɺϏϧυͨ͠PythonΞϓϦέʔγϣϯͷ ࢖༻͢ΔΠϝʔδ໊ͱλά໊(όʔδϣϯ)ɺઃఆ஋Λ docker-

    compose ͷઃఆϑΝΠϧ(docker-compose.yml)ʹهड़͢Δ
 3. ։ൃɾຊ൪༻ͷ֤ϛυϧ΢ΣΞͷઃఆϑΝΠϧΛهड़͢Δ
 4. docker-composeΛىಈ͠ɺdocker-compose.ymlʹهड़͠ ͨϛυϧ΢ΣΞͱΞϓϦέʔγϣϯΛىಈ͢Δ
  7. ϩʔΧϧ؀ڥʹ͓͚Δґଘؔ܎ͷ؅ཧͷྫ 1. ར༻͢ΔPythonͷDockerΠϝʔδΛ༻ҙ͢Δ 2. 1ͷΠϝʔδʹґଘؔ܎Λ֨ೲ͢ΔσΟϨΫτϦΛ VOLUME໋ྩͰϚ΢ϯτՄೳͳঢ়ଶʹ͓ͯ͘͠
 (pip ͸ --user ΦϓγϣϯΛར༻͢Δ͜ͱͰɺ

    PYTHONUSERBASE؀ڥม਺Ͱࢦఆ͞ΕͨσΟϨΫτ Ϧʹґଘؔ܎Λ֨ೲ͢Δ͜ͱ͕ՄೳͱͳΔ) 3. 2Ͱࢦఆͨ͠PYTHONUSERBASEͷσΟϨΫτϦ಺ ʹ͋ΔbinσΟϨΫτϦΛPATHʹؚΊΔΑ͏ʹ͓ͯ͘͠
  8. ґଘؔ܎ͷΠϯετʔϧͱىಈ (ϩʔΧϧ) # ґଘؔ܎ΛΠϯετʔϧ͢Δ
 docker-compose run --rm web pip install

    --user -r requirements.txt # ىಈ͢Δ
 docker-compose -f docker-compose.local.yml up -d
  9. ΞϓϦέʔγϣϯͷDockerfileͷྫ ϩʔΧϧ؀ڥͰ΋ར༻͍ͯ͠ΔPython༻ ͷΠϝʔδΛݩΠϝʔδʹઃఆ͢Δ (Ϗ ϧυ࣌ؒ୹ॖͷͨΊʹ௒ॏཁ) ADDͰϓϩδΣΫτͷιʔεશମΛΠ ϝʔδʹ௥Ճ͢Δɻ/var/opt/example ͕ ΞϓϦέʔγϣϯͷϧʔτͱͳΔ pipͷΠϯετʔϧઌσΟϨΫτϦ͸σϑΥϧτ(ϢʔβσΟ

    ϨΫτϦ഑Լͷ .local σΟϨΫτϦͱ͢Δ) ͜Ε͸ADDର ৅ͱͳͬͨσΟϨΫτϦ഑ԼΛΠϯετʔϧઌͱͨ͠৔߹ɺ ADDͨ͠ॠؒͷঢ়ଶʹϩʔϧόοΫ͞Εͯ͠·͏ͨΊɺ ϩʔΧϧͱ͸ҟͳΔσΟϨΫτϦͱ͍ͯ͠Δ
  10. ΞϓϦέʔγϣϯͷىಈ (։ൃɾຊ൪) # ىಈ͢Δ (։ൃ)
 docker-compose -f docker-compose.development.yml pull docker-compose

    -f docker-compose.development.yml run --rm web python manage.py collectstatic -- noinput
 docker-compose -f docker-compose.development.yml up -d # ىಈ͢Δ (ຊ൪Web)
 docker-compose -f docker-compose.production_web.yml pull
 docker-compose -f docker-compose.production_web.yml run --rm web python manage.py collectstatic -- noinput docker-compose -f docker-compose.production_web.yml up -d
 # ىಈ͢Δ (ຊ൪Batch)
 docker-compose -f docker-compose.production_batch.yml pull docker-compose -f docker-compose.production_batch.yml up -d