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

PHP on Kubernetes - PHP Conference 2020 Re:born #phpcon

825ca1ab7531dddc7948d14867a945a9?s=47 Kouta Ozaki
December 12, 2020

PHP on Kubernetes - PHP Conference 2020 Re:born #phpcon

825ca1ab7531dddc7948d14867a945a9?s=128

Kouta Ozaki

December 12, 2020
Tweet

Transcript

  1. 1)1$POGFSFODF+BQBO3&CPSO 1)1PO,VCFSOFUFT

  2. ొஃऀ঺հ   ▸ Kouta Ozaki ▸ Chatwork, YAML Engineer

  3. 01 ·ͱΊ

  4. ·ͱΊ  ,VCFSOFUFTͰ΋ී௨ʹ1)1ಈ͘  ಛʹ΄͔ʹ͍͏͜ͱ͕ͳ͍͙Β͍ී௨ʹಈ͘

  5. ಇ͘Λ΋ͬͱָ͘͠ɺ૑଄తʹ

  6. 02 ,VCFSOFUFTʁ

  7. w ෳ਺ͷαʔόʔʹɺෳ਺ͷίϯςφΛྑ͍ײ͡ʹ഑ஔͯ͘͠ΕΔͷ͕ίϯςφΦʔέετϨʔγϣϯ w ͦͷதͷσϑΝΫτελϯμʔτͳͷ͕,VCFSOFUFT w &,4ɺ(,&ɺ",&ͱ֤छΫϥ΢υͰ,VCFSOFUFTͷϚωʔδυαʔϏε΋ఏڙ͞Ε͍ͯΔ Kubernetes? ίϯςφΦʔέετϨʔγϣϯπʔϧͷσϑΝΫτελϯμʔυ

  8. Kubernetes? 7.ͱ4FSWFSMFTTͱͷൺֱ ىಈ଎౓ ֶशίετ ੍໿ ߴ ௿ ௿ த ߴ

    த ௿ த ߴ 7. ,VCFSOFUFT 4FSWFSMFTT
  9. 03 1)1ΞϓϦέʔγϣϯͷίϯςφԽ

  10. PHPΞϓϦέʔγϣϯͷίϯςφԽ ίϯςφͷߏஙํ๏ ɹFROM php:7.4-fpm ɹRUN docker-php-ext-install \ opcache \ pdo_mysql

    \ && pecl install \ redis \ xdebug \ && docker-php-ext-enable \ redis w %PDLFS)VCެࣜͰ1)1Πϝʔδ͕഑෍͞Ε͍ͯΔ w $-*"QBDIF'1.ͳͲ༻్ʹ߹Θͤͯ w &YUFOTJPO΍1&$-ͷ௥Ճ΋Մೳ w ઃఆϑΝΠϧ͸VTSMPDBMFUDҎԼʹ഑ஔ
  11. PHPΞϓϦέʔγϣϯͷίϯςφԽ ઃఆͷ஫ೖํ๏ ؀ڥม਺ ઃఆϑΝΠϧ <?php return [ 'param1' => getenv('APP_PARAM1')

    ?: 'ABC', 'param2' => getenv('APP_PARAM2') ?: 'DEF', ]; <?php return [ 'param1' => 'ABC', 'param2' => 'DEF', ]; w ίϯςφىಈ࣌ʹ؀ڥม਺Λࢦఆ͢Δ͚ͩ w ؀ڥม਺͕ͳ͚Ε͹ॳظ஋Ͱ͸ͳ͘EJF͢ΔͱΑΓ ҆શ w ؀ڥม਺͕਺ඦʹͳΔͱΘ͚Θ͔Μͳ͘ͳΔ w ίϯςφىಈ࣌ʹϑΝΠϧΛϚ΢ϯτɺ΋͘͠͸ ؀ڥม਺ͰಡΈࠐΉϑΝΠϧΛ੾Γସ͑ w ϑΝΠϧ੾Γସ͑ͷ৔߹ɺઃఆΛม͑Δ౓ʹίϯ ςφͷϏϧυ͕ඞཁ w ઃఆϑΝΠϧͷਖ਼͠͞ΛͲ͏ݕূ͢Δ͔͕伴
  12. PHPΞϓϦέʔγϣϯͷίϯςφԽ ϩάͷग़ྗํ๏ <?php use Monolog\Logger; use Monolog\Handler\StreamHandler; $logger = new

    Logger('containerlog'); $logger->pushHandler(new StreamHandler('php://stdout', Logger::DEBUG)); $logger->info("output log"); w ίϯςφϩάʹ͸ίϯςφͷϝΠϯϓϩηεͷ45%065ͱ45%&33͕ग़ྗ͞ΕΔ w ͦͷͨΊɺΞϓϦέʔγϣϯϩά͸45%06545%&33ʹग़ྗ͢Ε͹0, w 45%065ͱ45%&33͸࢖͍෼͚ͯ΋͍͍͚Ͳɺίϯςφϩάͷऩूج൫ʢ'MVFOUEͳͲʣͳͲͰৼ Γ෼͚΋͓͜ͳ͑ΔͷͰͦ͜·Ͱͩ͜ΘΒͳͯ͘΋Α͍
  13. PHPΞϓϦέʔγϣϯͷίϯςφԽ ϩάͷग़ྗํ๏ error_log = /dev/stderr log_errors = On log_errors_max_len =

    16384 w 1)1ͷ'BUBMFSSPSͳͲ͸QIQJOJͷFSSPS@MPHͰग़ྗ w ͜ͷͱ͖ϩά͸ෳ਺ߦͰग़ྗ͞ΕΔͨΊͲ͔͜ͰϩάΛ݁߹͠ͳ͚Ε͹͍͚ͳ͍͜ͱʹ஫ҙ w ϩάͷ௕͞͸QIQJOJͷMPH@FSSPST@NBY@MFOͱQIQGQNDPOGͷMPH@MJNJUͰࢦఆ w ͜ΕΑΓ୹͍৔߹͸ϩά͕੾Γ٧ΊΒΕΔ w 1)1'1.ͰϩάΛग़͢ͱෆཁͳ઀಄͕ࣙೖΔͷͰEFDPSBUF@XPSLFST@PVUQVUͰΦϑʹ w Φϯͩͱ8"3/*/(<QPPMXXX>DIJMETBJEJOUPTUEFSSYYYͱ͍͏Α͏ʹແବʹ௕͍ log_limit = 16384 catch_workers_output = yes decorate_workers_output = no QIQJOJ QIQGQNDPOG
  14. 04 1)1ΞϓϦέʔγϣϯͷ,VCFSOFUFTԽ

  15. w ,VCFSOFUFTͷ࠷খ୯Ґ͸1PE w 1PEͷதʹͭҎ্ͷίϯςφ͕ೖΔ w 1PEͷઃܭ͸1)1Ͱ͸બ୒ࢶ͕͍͔ͭ͋͘Δ w "QBDIF w /(*/91)1'1.

    w Ұͭͷίϯςφʹ·ͱΊΔ w ෳ਺ͷίϯςφʹΘ͚Δ w /(*/9*OHSFTT 1)1ʹ͸1PEͷߏ੒ύλʔϯ͕͍͔ͭ͋͘Δ PHPΞϓϦέʔγϣϯͷKubernetesԽ
  16. w "QBDIF 1)1͸୯ҰίϯςφͰಈ͔ͤΔ w ಛʹཧ༝͕ͳ͚Ε͹൪ָ PHPΞϓϦέʔγϣϯͷKubernetesԽ "QBDIFΛར༻ͨ͠ύλʔϯ

  17. w /(*/9 '1.ΛҰͭͷίϯςφͰಈ͔ͤΔ w %PDLFSϕετϓϥΫςΟεʹҧ൓͍ͯ͠Δ w ίϯςφΞϓϦέʔγϣϯ w /(*/9ͱ'1.Λ؅ཧ͢Δ4VQFSWJTPSͱ͔ඞཁ PHPΞϓϦέʔγϣϯͷKubernetesԽ

    /(*/9ʴ'1.ͰಉҰίϯςφʹͨ͠ύλʔϯ
  18. w 1PE಺ͷίϯςφΛ/(*/9ͱ'1.ͰΘ͚ΒΕΔ w ιʔείʔυΛͲ͏΍ͬͯಉظ͢Δ͔ w ͦΕͧΕͷίϯςφʹόϯυϧ w JOJU$POUBJOFSͰιʔείʔυΛίϐʔ PHPΞϓϦέʔγϣϯͷKubernetesԽ /(*/9ʴ'1.Ͱผίϯςφʹͨ͠ύλʔϯ

  19. PHPΞϓϦέʔγϣϯͷKubernetesԽ /(*/9ʴ'1.Λผ1PEʹͨ͠ύλʔϯ w 1PEࣗମΛ/(*/9ͱ'1.ͰΘ͚ΒΕΔ w εέʔϧ΍σϓϩΠλΠϛϯάΛͣΒͤΔ w ιʔείʔυͷಉظํ๏͕ίϯςφʹόϯυϧͯ͠σϓϩΠλΠϛϯάΛἧ͑Δ͔͠ͳ͍

  20. PHPΞϓϦέʔγϣϯͷKubernetesԽ /(*/9*OHSFTT 1)1'1.Λར༻ͨ͠ύλʔϯ w /(*/9*OHSFTT͕'BTU$(*ʹରԠ͍ͯ͠Δ w IUUQTHJUIVCDPNLVCFSOFUFTJOHSFTTOHJOY w ͪ͜Β΋ιʔείʔυͷಉظ͸՝୊ w

    /(*/9*OHSFTTͷੜ੒͢Δ/(*/9ͷઃఆେ͖͍ w ϓϩμΫγϣϯͰͦͷઃఆΛอक͖͠ΕΔ͔ w ύλʔϯͷํ͕γϯϓϧ
  21. PHPΞϓϦέʔγϣϯͷKubernetesԽ ͍͔ͭ͘ͷύλʔϯʹରԠͨ͠1)1$IBSU IUUQTHJUIVCDPNDIBUXPSLDIBSUTUSFFNBTUFSQIQ

  22. PHPΞϓϦέʔγϣϯͷKubernetesԽ 1PEͷऴྃॲཧ ɹlifecycle: ɹɹpreStop: exec: command: - /bin/sh - -c

    - sleep 60; kill -QUIT 1; sleep 60 1PE 3VOOJOH 5FSNJOBUJOH 4FSWJDF 1SF4UPQ %FUBDI4FSWJDF 4*(5&3. 4*(,*-- UFSNJOBUJPO(SBDF1FSJPE4FDPOET EFGBMUT pOJTIQSFTUPQ UFSNJOBUFQPE w ίϯςφͷऴྃॲཧ͸4*(5&3.ͳͷͰQSF4UPQதʹ 4*(26*5Λૹͬͯ(SBDFq4IVUEPXO͢Δ w LJMM͸ඇಉظͳͷͰϦΫΤετͷॲཧ࣌ؒ଴ͭ w ऴྃॲཧதʹ4FSWJDF͔Β৽نϦΫΤετ͕ϧʔςΟϯ ά͞Εͳ͍Α͏ʹগ͠଴ͭඞ༻͕͋Δ
  23. w ,VCFSOFUFTͷϝτϦΫεऩूج൫ͷ1SPNFUIFVT΋ར༻Մೳ w "QBDIF&YQPSUFSɺ/(*/9&YQPSUFSɺ1)1'1.&YQPSUFSΛར༻͢Δ͜ͱͰϝτϦΫεΛऩू w 1)1Ͱ&YQPSUFSΛ࡞ΔͨΊͷϥΠϒϥϦ΋͋Δ w FOEDMPUIJOHQSPNFUIFVT@DMJFOU@QIQ w 01$BDIF΍"1$VͷϝτϦΫεͷऩूͱ͔Ͱར༻

    PHPΞϓϦέʔγϣϯͷKubernetesԽ &YQPSUFSΛ࢖ͬͨϝτϦΫεͷऩू
  24. 05 ,VCFSOFUFTΛ։ൃ؀ڥͱͯ͠࢖͑Δ͔

  25. KubernetesΛ։ൃ؀ڥͱͯ͠࢖͑Δ͔ 1IQ4UPSNͱ9EFCVHPO,VCFSOFUFT w 9EFCVH͔Β1IQ4UPSNʹͭͳ͕Ε͹0, w %PDLFSGPS.BDͷ৔߹͸EPDLFSGPSNBDMPDBMIPTUͰϗετʹͭͳ͕Δ w ϦϞʔτͷ,VCFSOFUFTΫϥελͷ৔߹͸1PE͔Β௨৴Մೳͳঢ়ଶʹ͢Δ w 9EFCVHͷೖͬͨ1PE͔Βϗετ΁ૄ௨Մೳʹ͢Ε͹0,

  26. w 1IQ4UPSN͸,VCFSOFUFTʹରԠ͍ͯ͠ͳ͍ w ΋͠ରԠ͢ΔͳΒEPDLFSFYFDΛϥοϓͨ͠QIQίϚϯυΛ༻ҙ͢ΔʢLVCFDUMͰ΋Մʣ w ͜ͷϑΝΠϧ΁ͷύεΛ1IQ4UPSNͷ$-**OUFSQSFUFSʹࢦఆ͢Δͱಈ࡞͢Δ w ͨͩ͠ɺύεͷϚοϐϯά͸ޮ͔ͳ͍ͷͰϩʔΧϧͱ,VCFSOFUFTͰಉҰͷύεʹ͢Δඞ༻͕͋Δ w Ϛ΢ϯτ͢ΔύεΛௐ੔͢ΔͳΓɺMJGFDZDMFQSF4UBSUͰTZNMJOLΛషΔͳΓ

    w ࡞੒ͨ͠ίϚϯυͰύεΛஔ͖׵͑ͯ΋ྑ͍͔΋ʁ KubernetesΛ։ൃ؀ڥͱͯ͠࢖͑Δ͔ 1IQ4UPSNͱ1)16OJUPO,VCFSOFUFT ɹ#!/bin/sh ɹCONTAINER_ID=$(docker ps -f name=php-fpm -q --latest) ɹdocker exec $CONTAINER_ID php "$@"
  27. KubernetesΛ։ൃ؀ڥͱͯ͠࢖͑Δ͔ ଞ؀ڥͱͷൺֱ 6OJU5FTU $PNQPTFS ॳճىಈ NT NT NT NT N

    NT NT N NT 7BHSBOU ,VCFSOFUFT EPDLFSDPNQPTF ճ໨ͷىಈ N N N
  28. 06 ·ͱΊ

  29. ·ͱΊ  ,VCFSOFUFTͰ΋ී௨ʹ1)1ಈ͘  ಛʹ΄͔ʹ͍͏͜ͱ͕ͳ͍͙Β͍ී௨ʹಈ͘  ֶͨͩ͠शίετ͸ߴ͍

  30. ಇ͘Λ΋ͬͱָ͘͠ɺ૑଄తʹ