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

Golangで学ぶHot deployの仕組み

Golangで学ぶHot deployの仕組み

Server::Starterの仕組みを学び、Golangでミニマムで
 実装してみた

5381bd68abe2b91239ca1600db2a890d?s=128

tsurubee

June 28, 2018
Tweet

Transcript

  1. !UTVSVCFF(.01FQBCP *OD ۝भΠϯϑϥަྲྀษڧձ ,JYT 7PM (PMBOHͰֶͿ )PUEFQMPZͷ࢓૊Έ

  2. (.0ϖύϘɹΠϯϑϥΤϯδχΞ !UTVSVCFF

  3. ೥݄(.0ϖύϘೖࣾ ΠϯϑϥΤϯδχΞྺϲ݄

  4. ໨࣍ w)PUEFQMPZͱ͸ʁͳͥඞཁ͔ʁ w)PUEFQMPZͷ࢓૊Έ w(PMBOHͰ࣮૷ͯ͠Έͨ

  5. ఻͍͑ͨ͜ͱ wμ΢ϯλΠϜθϩΛࢧ͑Δ)PUEFQMPZͷ࢓૊ΈͷҰྫ w(PMBOHΛ௨ͯ͡௿ϨΠϠʔͷٕज़ΛֶͿ͜ͱͰɺ
 ΤϯδχΞͱͯ͠ͷ෯͕޿͕ΔͷͰ͸ʁ wٕज़ॻಡΜͰ;Ή;Ή͢ΔΑΓ࣮૷ͯ͠Έͨํ͕ཧղ
 ͠΍͍͢

  6. )PUEFQMPZͱ͸ʁ ͳͥඞཁ͔ʁ

  7. )PUEFQMPZͱ͸ʁ wΞϓϦέʔγϣϯʹϩδοΫ΍ઃఆ౳ͷมߋΛՃ͑ͨࡍ ʹɺϦΫΤετͷॲཧΛଓ͚ଓ͚ͳ͕Βɺมߋͷ಺༰Λ൓ ө͢ΔͨΊͷखஈ

  8. )PUEFQMPZͱ͸ʁ wΞϓϦέʔγϣϯʹϩδοΫ΍ઃఆ౳ͷมߋΛՃ͑ͨࡍ ʹɺϦΫΤετͷॲཧΛଓ͚ଓ͚ͳ͕Βɺมߋͷ಺༰Λ൓ ө͢ΔͨΊͷखஈ ҰൠతͳσϓϩΠʢ/05)PUEFQMPZʣ 4PVSDFDPEF DIBOHF (PMBOHͷ৔߹ɺ όΠφϦͷ্ॻ͖

  9. )PUEFQMPZͱ͸ʁ wΞϓϦέʔγϣϯʹϩδοΫ΍ઃఆ౳ͷมߋΛՃ͑ͨࡍ ʹɺϦΫΤετͷॲཧΛଓ͚ଓ͚ͳ͕Βɺมߋͷ಺༰Λ൓ ө͢ΔͨΊͷखஈ ҰൠతͳσϓϩΠʢ/05)PUEFQMPZʣ 4PVSDFDPEF DIBOHF "QQMJDBUJPO 4UPQ (PMBOHͷ৔߹ɺ

    όΠφϦͷ্ॻ͖
  10. )PUEFQMPZͱ͸ʁ wΞϓϦέʔγϣϯʹϩδοΫ΍ઃఆ౳ͷมߋΛՃ͑ͨࡍ ʹɺϦΫΤετͷॲཧΛଓ͚ଓ͚ͳ͕Βɺมߋͷ಺༰Λ൓ ө͢ΔͨΊͷखஈ ҰൠతͳσϓϩΠʢ/05)PUEFQMPZʣ 4PVSDFDPEF DIBOHF "QQMJDBUJPO 4UPQ "QQMJDBUJPO

    4UBSU (PMBOHͷ৔߹ɺ όΠφϦͷ্ॻ͖ ࠶ىಈ
  11. )PUEFQMPZͱ͸ʁ wΞϓϦέʔγϣϯʹϩδοΫ΍ઃఆ౳ͷมߋΛՃ͑ͨࡍ ʹɺϦΫΤετͷॲཧΛଓ͚ଓ͚ͳ͕Βɺมߋͷ಺༰Λ൓ ө͢ΔͨΊͷखஈ ҰൠతͳσϓϩΠʢ/05)PUEFQMPZʣ 4PVSDFDPEF DIBOHF "QQMJDBUJPO 4UPQ "QQMJDBUJPO

    4UBSU (PMBOHͷ৔߹ɺ όΠφϦͷ্ॻ͖ ϦΫΤετΛॲཧ Ͱ͖ͳ͍͕࣌ؒൃੜ ࠶ىಈ
  12. )PUEFQMPZ͕ͳͥඞཁ͔ʁ wΘ͔ͣͳؒͰ΋Քಇ͍ͯ͠ΔαʔϏεΛࢭΊͨ͘ͳ͍ w)PUEFQMPZΛಋೖ͢Δ͜ͱͰɺγεςϜӡ༻ʹ͓͚Δ
 อकੑ͕޲্͠ɺਝ଎ͳ։ൃαΠΫϧ͕࣮ݱͰ͖Δ

  13. )PUEFQMPZͷ࢓૊Έ

  14. 4FSWFS4UBSUFS w؆୯ʹ)PUEFQMPZͰ͖ΔΑ͏ʹ͢ΔͨΊͷDQBONPEVMF w1FSMͰ࣮૷͞Ε͍ͯΔ͕ɺγάφϧͱ؀ڥม਺Λ࢖ͬͨ
 ൚༻ੑͷߴ͍࢓૊ΈͷͨΊɺ͞·͟·ͳݴޠͰར༻Մೳ

  15. 4FSWFS4UBSUFS w؆୯ʹ)PUEFQMPZͰ͖ΔΑ͏ʹ͢ΔͨΊͷDQBONPEVMF w1FSMͰ࣮૷͞Ε͍ͯΔ͕ɺγάφϧͱ؀ڥม਺Λ࢖ͬͨ
 ൚༻ੑͷߴ͍࢓૊ΈͷͨΊɺ͞·͟·ͳݴޠͰར༻Մೳ ಈ࡞Λݴ༿Ͱઆ໌͢Δͱɾɾ ʮ৽͍͠ϓϩηεΛىಈͯ͠৽͍͠ϦΫΤετΛͦͪΒʹ ྲྀͭͭ͠ɺݹ͍ϓϩηεͷϦΫΤετ͕׬ྃͨ͠Βਖ਼͘͠ ऴྃͤ͞Δʯ

  16. 4FSWFS4UBSUFS w؆୯ʹ)PUEFQMPZͰ͖ΔΑ͏ʹ͢ΔͨΊͷDQBONPEVMF w1FSMͰ࣮૷͞Ε͍ͯΔ͕ɺγάφϧͱ؀ڥม਺Λ࢖ͬͨ
 ൚༻ੑͷߴ͍࢓૊ΈͷͨΊɺ͞·͟·ͳݴޠͰར༻Մೳ ಈ࡞Λݴ༿Ͱઆ໌͢Δͱɾɾ ʮ৽͍͠ϓϩηεΛىಈͯ͠৽͍͠ϦΫΤετΛͦͪΒʹ ྲྀͭͭ͠ɺݹ͍ϓϩηεͷϦΫΤετ͕׬ྃͨ͠Βਖ਼͘͠ ऴྃͤ͞Δʯ (SBDFGVM3FTUBSUʢ༏խͳ࠶ىಈʣ

  17. )PUEFQMPZͷ࢓૊Έʢ؆қ൛ʣ TUBSU@TFSWFS 1BSFOU start_server —-port 8888 —- ./server TFSWFS $IJME

    TPDLFU GPSLFYFD TPDLFUΛڞ༗ BDDFQU TUBSU@TFSWFSɿ(SBDFGVMSFTUBSUͷͨΊͷϓϩάϥϜ TFSWFSɿ࣮ߦ͍ͨ͠ϓϩάϥϜ
  18. )PUEFQMPZͷ࢓૊Έʢ؆қ൛ʣ TUBSU@TFSWFS 1BSFOU start_server —-port 8888 —- ./server TFSWFS $IJME

    TPDLFU GPSLFYFD TPDLFUΛڞ༗ BDDFQU TUBSU@TFSWFSɿ(SBDFGVMSFTUBSUͷͨΊͷϓϩάϥϜ TFSWFSɿ࣮ߦ͍ͨ͠ϓϩάϥϜ ᶃϓϩάϥϜʹมߋʂ
  19. )PUEFQMPZͷ࢓૊Έʢ؆қ൛ʣ kill -HUP <master pid> TPDLFU BDDFQU TUBSU@TFSWFS 1BSFOU TFSWFS

    $IJME TUBSU@TFSWFSɿ(SBDFGVMSFTUBSUͷͨΊͷϓϩάϥϜ TFSWFSɿ࣮ߦ͍ͨ͠ϓϩάϥϜ ᶄ4*()61 ᶃϓϩάϥϜʹมߋʂ
  20. )PUEFQMPZͷ࢓૊Έʢ؆қ൛ʣ kill -HUP <master pid> TPDLFU GPSLFYFD TFSWFS $IJME ᶄ4*()61

    BDDFQU BDDFQU TUBSU@TFSWFS 1BSFOU TFSWFS $IJME ᶅ৽ϓϩάϥϜͰىಈ ᶃϓϩάϥϜʹมߋʂ
  21. )PUEFQMPZͷ࢓૊Έʢ؆қ൛ʣ kill -HUP <master pid> TPDLFU ऴྃ͢Δલʹʜ ᶃ৽نϦΫΤεड෇ ɹͷఀࢭ ᶄݱࡏॲཧதͷ

    ϦΫΤετͷ׬ྃ GPSLFYFD (SBDFGVM4IVUEPXO BDDFQU BDDFQU TUBSU@TFSWFS 1BSFOU TFSWFS $IJME ᶅ৽ϓϩάϥϜͰىಈ ᶄ4*()61 ᶆ4*(5&3. TFSWFS $IJME ᶃϓϩάϥϜʹมߋʂ
  22. )PUEFQMPZͷ࢓૊Έʢ؆қ൛ʣ TUBSU@TFSWFS 1BSFOU kill -HUP <master pid> TPDLFU GPSLFYFD TFSWFS

    $IJME BDDFQU ᶆ4*(5&3. ᶅ৽ϓϩάϥϜͰىಈ ᶄ4*()61 TFSWFS $IJME ᶃϓϩάϥϜʹมߋʂ ऴྃ͢Δલʹʜ ᶃ৽نϦΫΤεड෇ ɹͷఀࢭ ᶄݱࡏॲཧதͷ ϦΫΤετͷ׬ྃ (SBDFGVM4IVUEPXO
  23. ࣮૷ͯ͠Έͨ

  24. ϛχϚϜͰ࣮૷ͯ͠Έͨ IUUQTHJUIVCDPNUTVSVCFFHSBDFGVMSFTUBSUHP

  25. ࣮૷ϙΠϯτ̍ʙγάφϧϋϯυϥ func waitSignal(l net.Listener) { c := make(chan os.Signal, 1)

    signal.Notify(c, syscall.SIGTERM) go func() { sig := <-c switch sig { case syscall.SIGTERM: signal.Stop(c) l.Close() } }() } γάφϧͱ͸ϓϩηεؒ௨৴ͷҰछ ϓϩηεʹγάφϧΛૹ৴͢Δͱɺͦͷϓϩηεͷਖ਼ৗॲཧʹׂΓࠐΜͰɺ γάφϧݻ༗ͷॲཧΛ࣮ߦͰ͖Δ ͓खܰʹγάφϧͷϋϯυϦϯά͕ Ͱ͖Δ
  26. ࣮૷ϙΠϯτ̍ʙγάφϧϋϯυϥ func waitSignal(l net.Listener) { c := make(chan os.Signal, 1)

    signal.Notify(c, syscall.SIGTERM) go func() { sig := <-c switch sig { case syscall.SIGTERM: signal.Stop(c) l.Close() } }() } γάφϧͱ͸ϓϩηεؒ௨৴ͷҰछ ϓϩηεʹγάφϧΛૹ৴͢Δͱɺͦͷϓϩηεͷਖ਼ৗॲཧʹׂΓࠐΜͰɺ γάφϧݻ༗ͷॲཧΛ࣮ߦͰ͖Δ HPSPVUJOFͰγάφϧΛ଴ͪड͚Δ ͓खܰʹγάφϧͷϋϯυϦϯά͕ Ͱ͖Δ
  27. ࣮૷ϙΠϯτ̎ʙϑΝΠϧσΟεΫϦϓλ func forkExec(l *net.TCPListener) (*os.Process, error) { progName, _ :=

    exec.LookPath(os.Args[0]) pwd, _ := os.Getwd() f, _ := l.File() defer f.Close() files := []*os.File{os.Stdin, os.Stdout, os.Stderr, f} fdEnv := fmt.Sprintf("%s=%d", "FD_KEY", len(files)-1) return os.StartProcess(progName, os.Args, &os.ProcAttr{ Dir: pwd, Env: append(os.Environ(), fdEnv), Files: files, }) } ϑΝΠϧσΟεΫϓϦΫλ͸ɺϑΝΠϧ΍ιέοτͳͲΛந৅Խͨ͠࢓૊Έ ϓϩηεΛGPSLͨ͠ࡍ͸ɺ਌ϓϩηεͷϑΝΠϧσΟεΫϦϓλΛҾ͖ܧ͙
  28. ·ͱΊ w4FSWFS4UBSUFSͷ࢓૊ΈΛֶͼɺ(PMBOHͰϛχϚϜͰ
 ࣮૷ͯ͠Έͨ wγάφϧ΍ϑΝΠϧσΟεΫϦϓλͱ͍ͬͨ֓೦͸ٕज़ॻΛಡ ΜͰ;Ή;Ή͢ΔΑΓɺ࣮૷ͯ͠Έͨ΄͏͕ཧղ͠΍͔ͬͨ͢ w(PMBOH͸ଟ͘ͷ04ͷػೳΛ؆ܿͳίʔυͰѻ͑ΔͨΊɺ
 ࣗ෼ͷؔ৺ྖҬΛΑΓ௿͍ϨΠϠʔʹ޿͍͛ͯͨ͘Ίʹ
 దͨ͠ݴޠͩ

  29. ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂʂ