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

Gohperに逆らうとどうなるのか

 Gohperに逆らうとどうなるのか

Go Conference Spring 2016

Seiji Takahashi

April 23, 2016
Tweet

More Decks by Seiji Takahashi

Other Decks in Technology

Transcript

  1. ࣗݾ঺հ • ߴڮ੣ೋ / ͪ·͖Μ • Github: timakin • Twitter:

    @__timakin__ • Gmail: [email protected] • hatena: timakin.log • ϓϥΠϕʔτͰGoͰ࡞ͬͨCLIπʔϧ • timakin/ts - teckstack (ςοΫܥͷ৘ใऩूCLI) • timakin/octop - octopatrol (github tracking CLI)
  2. LXC(Linux Containers)ͷ֓ཁ • ίϯςφԾ૝Խʹඞཁͳchroot΍cgroupɺnamespaceͷ෼ׂ౳Λ ѻ͏APIΛఏڙ͢Δٕज़Ͱ͢ɻ • Ծ૝ϚγϯΛ࡞ΔͷͰ͸ͳ͘ɺϗετOSͷϦιʔεΛ෼ׂɺ؅ཧ ͢Δ͜ͱͰִ཭ۭؒΛ࡞Γग़͠·͢ɻϋʔυ΢ΣΞͷγϛϡϨʔγϣ ϯ͕ඞཁͳ͍ͨΊɺԾ૝ԽʹΑΔΦʔόʔϔου͕গͳ͍Ͱ͢ɻ •

    v0.8ҎલͷDocker͸LXCܦ༝ͰҰ෦Ϧιʔε؅ཧΛ͍ͯ͠·ͨ͠ɻ • Docker͸Ϧιʔε෼ׂ΋ͦ͏Ͱ͕͢ɺΞϓϦέʔγϣϯͷσϓϩ Π؀ڥ΍Πϝʔδͷόʔδϣχϯάʹओ؟Λஔ͍͍ͯΔ఺Ͱɺࠩҟ ͕͋Δ΋ͷͩͱ͍͏ҹ৅Ͱ͢ɻ
  3. go-lxcͷ֓ཁ • Go Bindings for LXC (Linux Containers) • LXD

    (REST APIΛఏڙ͢ΔσʔϞϯΛඋ͓͑ͯ ΓɺωοτϫʔΫܦ༝Ͱͷૢ࡞͕Մೳͳίϯ ςφٕज़ɻLXCͷਐԽ൛తͳ΋ͷ)ʹ΋ಋೖ͞ Ε͍ͯ·͢ɻ • ଞʹ΋ruby-lxc΍python2-lxc౳͋Γ·͕͢ɺ ։ൃ͸ࢭ·ͬͯ·͢…
  4. go-lxcͷAPIͷྫ • ίϯςφͷجຊૢ࡞ • ৽ن࡞੒ɿfunc NewContainer • ίϯςφىಈɿfunc (*Container) Start

    • ίϯςφ಺ͰͷίϚϯυ࣮ߦɿfunc (*Container) Execute • ίϯςφഁغɿfunc (*Container) Destroy • ίϯςφఀࢭɿfunc (*Container) Shutdown • ίϯςφҰཡɿfunc Containers
  5. go-lxcͷίʔυϦʔσΟϯά // go-lxc/container.go func (c *Container) Start() error { if

    err := c.makeSure(isNotRunning); err != nil { return err } c.mu.Lock() defer c.mu.Unlock() if !bool(C.go_lxc_start(c.container, 0, nil)) { return ErrStartFailed } return nil }
  6. go-lxcͷίʔυϦʔσΟϯά // lxc/src/lxc/lxccontainer.c struct lxc_container *lxc_container_new(const char *name, const char

    *configpath) { struct lxc_container *c; ... c->start = lxcapi_start; ... } static bool lxcapi_start(struct lxc_container *c, int useinit, char * const argv[]) { bool ret; current_config = c ? c->lxc_conf : NULL; ret = do_lxcapi_start(c, useinit, argv); current_config = NULL; return ret; }
  7. Dockerϓν࣮૷աఔɿ࣮૷ • e.g. pull -> run, execͷྲྀΕ • Docker HubͷAPIΛୟ͖ɺimages

    endpointΛୟ͍ͯɺඞཁͳsession tokenΛऔಘ͢Δ • session token෇͖ͰRegistry APIΛୟ͍ͯɺ֘౰λάͷimage_idΛऔಘ͢ Δ • Imageͷancestry(diffཤྺ)Λऔಘ͢Δ • layer(ImageͷόΠφϦσʔλ)Λऔಘ͠ɺಛఆσΟϨΫτϦ഑Լʹల։͢Δ • ετϨʔδͷঢ়ଶͷࠩ෼(ancestry)ΛϨΠϠͱͯ͠ॏͶ߹ΘͤΔ͜ͱͰɺΠ ϝʔδΛܗ੒͢ΔɻʢϢχΦϯϑΝΠϧγεςϜʣ • snapshotΛ೚ҙͷσΟϨΫτϦʹอଘͭͭ͠ɺͦͷΠϝʔδΛݩʹLXCΛ ىಈɺίϚϯυ࣮ߦ
  8. Dockerϓν࣮૷աఔɿ࣮૷ • e.g. pull -> run, execͷྲྀΕ • Docker HubͷAPIΛୟ͖ɺimages

    endpointΛୟ͍ͯɺඞཁͳsession tokenΛऔಘ͢Δ • session token෇͖ͰRegistry APIΛୟ͍ͯɺ֘౰λάͷimage_idΛऔಘ͢ Δ • Imageͷancestry(diffཤྺ)Λऔಘ͢Δ • layer(ImageͷόΠφϦσʔλ)Λऔಘ͠ɺಛఆσΟϨΫτϦ഑Լʹల։͢Δ • ετϨʔδͷঢ়ଶͷࠩ෼(ancestry)ΛϨΠϠͱͯ͠ॏͶ߹ΘͤΔ͜ͱͰɺΠ ϝʔδΛܗ੒͢ΔɻʢϢχΦϯϑΝΠϧγεςϜʣ • snapshotΛ೚ҙͷσΟϨΫτϦʹอଘͭͭ͠ɺͦͷΠϝʔδΛݩʹLXCΛ ىಈɺίϚϯυ࣮ߦʢ͕͜͜LXCΛ࢖͏৔ॴʣ
  9. ͨͱ͑͹container_start static VALUE container_start(int argc, VALUE *argv, VALUE self) {

    int ret; VALUE rb_use_init, rb_daemonize, rb_close_fds, rb_args, rb_opts; struct start_without_gvl_args args; args.use_init = 0; args.daemonize = 1; args.close_fds = 0; args.args = NULL; rb_args = Qnil; rb_scan_args(argc, argv, "01", &rb_opts); if (!NIL_P(rb_opts)) { Check_Type(rb_opts, T_HASH); rb_use_init = rb_hash_aref(rb_opts, SYMBOL("use_init")); if (!NIL_P(rb_use_init)) ͓͏ɺͦ͏ͩͳɻ
  10. ݁ՌɺGolang͡Όͳ͔͔ͬͨΒ… • GolangͳΒ
 func (*Container) IPAddress΍
 func (*Container) CPUStatsͳͲɺ
 ίϯςφΛ؅ཧ͢Δʹ౰ͨͬͯݟ͍ͨ৘ใ͕


    ࣗ༝ʹݟΕ·͢ɻ • Ұํlxc/lxc-ruby͸…
 ϝϞϦ΋ωοτϫʔΫ؅ཧ΋configͷΈ…
 ΋͏ͪΐͬͱ࿑ಇͯ͠ʂ