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

Vagrant (+Amazon EC2)

Vagrant (+Amazon EC2)

Vagrantの基礎とAmazon EC2との組み合わせ方

Ryutaro YOSHIBA

July 23, 2013
Tweet

More Decks by Ryutaro YOSHIBA

Other Decks in Technology

Transcript

  1. ΢ΥʔλʔϑΥʔϧ 7ࣈ ཁ݅ఆٛ ड͚ೖΕࢼݧ جຊઃܭ ૯߹ࢼݧ ৄࡉઃܭ ݁߹ࢼݧ ੡଄ɾ୯ମ ͕͔͔࣌ؒΓ͗͢

    ͯɺग़དྷͨࠒʹ͸ཁ ٻ͕มԽɻͦ΋ͦ΋ ཁ͕͍݅͋ͬͯͳ͍ ͜ͱ͕͜͜Ͱ෼͔Δ 13೥7݄23೔Ր༵೔
  2. ϑΟʔνϟʔ୯ҐͰ׬ྃ ίʔυϨϏϡʔ νΣοΫΠϯ Ϣχοτςετ ΧόϨοδ υΩϡϝϯτ ੑೳ ηΩϡϦςΟ σϓϩΠ ݁߹ςετ

    ड͚ೖΕςετ Ϋϩεϒϥ΢β ੩తղੳ ϑΟʔνϟʔ ίʔυϨϏϡʔ νΣοΫΠϯ Ϣχοτςετ ΧόϨοδ υΩϡϝϯτ ੑೳ ηΩϡϦςΟ σϓϩΠ ݁߹ςετ ड͚ೖΕςετ Ϋϩεϒϥ΢β ੩తղੳ ϑΟʔνϟʔ ίʔυϨϏϡʔ νΣοΫΠϯ Ϣχοτςετ ΧόϨοδ υΩϡϝϯτ ੑೳ ηΩϡϦςΟ σϓϩΠ ݁߹ςετ ड͚ೖΕςετ Ϋϩεϒϥ΢β ੩తղੳ ϑΟʔνϟʔ ίʔυϨϏϡʔ νΣοΫΠϯ Ϣχοτςετ ΧόϨοδ υΩϡϝϯτ ੑೳ ηΩϡϦςΟ σϓϩΠ ݁߹ςετ ड͚ೖΕςετ Ϋϩεϒϥ΢β ੩తղੳ ϑΟʔνϟʔ ίʔυϨϏϡʔ νΣοΫΠϯ Ϣχοτςετ ΧόϨοδ υΩϡϝϯτ ੑೳ ηΩϡϦςΟ σϓϩΠ ݁߹ςετ ड͚ೖΕςετ Ϋϩεϒϥ΢β ੩తղੳ ϑΟʔνϟʔ ίʔυϨϏϡʔ νΣοΫΠϯ Ϣχοτςετ ΧόϨοδ υΩϡϝϯτ ੑೳ ηΩϡϦςΟ σϓϩΠ ݁߹ςετ ड͚ೖΕςετ Ϋϩεϒϥ΢β ੩తղੳ ϑΟʔνϟʔ ίʔυϨϏϡʔ νΣοΫΠϯ Ϣχοτςετ ΧόϨοδ υΩϡϝϯτ ੑೳ ηΩϡϦςΟ σϓϩΠ ݁߹ςετ ड͚ೖΕςετ Ϋϩεϒϥ΢β ੩తղੳ ϑΟʔνϟʔ 13೥7݄23೔Ր༵೔
  3. Summit Developers 7BHSBOUͷಛ௃ • Ծ૝ϚγϯΛந৅Խ͢ΔϑϩϯτΤϯυ • ࡞ͬͯ • ࢼͯ͠ •

    ࣺͯͯ • ϓϩϏδϣχϯάػೳͱͷ૊Έ߹Θͤ • ϓϥάΠϯʹΑΔ֦ு 13೥7݄23೔Ր༵೔
  4. Summit Developers ͳΜͰ࢖͏ͷʁ • ૉૣ͘؀ڥΛ࡞Γ͍ͨ • ιϑτ΢ΣΞͷߏ੒ཁૉ͕૿͑ͨ • ؒҧ͑Δ •

    Զͷ؀ڥͰ͸ಈ͍͍ͯΔ໰୊ • ߏ੒؅ཧΛ༰қʹ • υΩϡϝϯτ͔Βίʔυ΁ ʢ*OGSBTUSVDUVSFBT$PEFʣ • ຊ࣭తͰͳ͍࡞ۀʹ࣌ؒΛ࢖Θͳ͍Α͏ʹ ϜμΛආ͚Δ 13೥7݄23೔Ր༵೔
  5. Summit Developers $IFGͷ$PPLCPPLͷ$* export VBOX_USER_HOME=/var/lib/jenkins/VirtualBox\ VMs && export VAGRANT_HOME=/var/lib/jenkins/.vagrant.d &&

    source /var/lib/jenkins/.rvm/scripts/rvm > /dev/null 2>&1 && cd ${WORKSPACE} && bundle install && bundle exec kitchen test 13೥7݄23೔Ր༵೔
  6. Summit Developers جຊతͳ༻ޠ • 7BHSBOUpMF • Ծ૝ϚγϯͷఆٛϑΝΠϧʢ3VCZʣ • #PY •

    Ծ૝ϚγϯͷςϯϓϨʔτ • 1MVHJO • 1SPWJEFS΍ͦ΋ͦ΋7BHSBOUͷ಺෦΋ϓϥάΠϯ Ͱ࡞ΒΕ͍ͯΔ • 1SPWJEFS • Ծ૝ϚγϯΛ࡞ΔΤϯδϯ෦෼ • 1SPWJTJPOFS • Ծ૝ϚγϯΛϓϩϏδϣχϯά͢ΔΤϯδϯ෦෼ 13೥7݄23೔Ր༵೔
  7. Summit Developers (FUUJOH4UBSUFE vagrant box add precise64 ¥ http://files.vagrantup.com/precise64.box vagrant

    init precise64 vagrant up Ծ૝ϚγϯΛىಈʢ7JSUVBM#PYʣ vagrant halt Ծ૝ϚγϯΛఀࢭ vagrant destroy Ծ૝ϚγϯΛഁغ 13೥7݄23೔Ր༵೔
  8. Summit Developers 1BDLFS { "builders": [{ "type": "amazon-ebs", "region": "ap-northeast-1",

    "source_ami": "ami-39b23d38", "instance_type": "m1.medium", "ssh_username": "ec2-user", "ssh_timeout": "5m", "ami_name": "amznlinux-{{.CreateTime}}" }], "provisioners": [{ "type": "shell", "script": "script.sh", "remote_path": "/tmp/script.sh" }] } ఆٛϑΝΠϧॻ͍ͯ packer build hoge.json Ϗϧυ͢Δ ࣗ෼༻".*ͷ࡞੒ྫ 13೥7݄23೔Ր༵೔
  9. Summit Developers 1SPWJEFS • 7BHSBOU·Ͱ͸7JSUVBM#PYͷΈ • 7BHSBOUҎ߱͸1SPWJEFSػೳʹΑΔ ந৅Խ • "NB[PO&$

    WBHSBOUBXT • 7.XBSFGVTJPO ༗ঈ • ,7. WBHSBOULWN • 0QFO4UBDL WBHSBOUPQFOTUBDL • 3BDL4QBDF WBHSBOUSBDLTQBDF • FUD 13೥7݄23೔Ր༵೔
  10. Summit Developers ίΞͷ֦ு plugin_load_proc = lambda do |directory| # We

    only care about directories next false if !directory.directory? # If there is a plugin file in the top-level directory, then load # that up. plugin_file = directory.join("plugin.rb") if plugin_file.file? puts "[INFO]loading monkey patch: #{plugin_file}" load(plugin_file) next true end end Vagrant.source_root.join(File.dirname(__FILE__) + "/patch/plugins/ communicators/").children(true).each do |directory| # Ignore non-directories next if !directory.directory? # Load from this directory, and exit if we successfully loaded a plugin puts directory next if plugin_load_proc.call(directory) end ϞϯΩʔύον 13೥7݄23೔Ր༵೔
  11. Summit Developers 7BHSBOUͰ&$Πϯελϯε Vagrant.configure("2") do |config| config.vm.box = "dummy" config.vm.box_url

    = "https://github.com/mitchellh/vagrant- aws/raw/master/dummy.box" config.vm.provider :aws do |aws, override| aws.access_key_id = ENV['AWS_ACCESS_KEY_ID'] aws.secret_access_key = ENV['AWS_SECRET_KEY'] aws.keypair_name = ENV['AWS_KEYPAIR_NAME'] override.ssh.username = ‘ec2-user’ override.ssh.private_key_path = ENV['AWS_PRIVATE_KEY_PATH'] aws.region = "ap-northeast-1" aws.ami = "ami-71b22570" aws.instance_type = "t1.micro" aws.ssh_username = ‘ec2-user’ aws.security_groups = ["web"] end end Ұ൪୯७ͳྫ 13೥7݄23೔Ր༵೔
  12. Summit Developers 71$΋͍͚Δ Vagrant.configure("2") do |config| config.vm.box = "dummy" config.vm.box_url

    = "https://github.com/mitchellh/vagrant-aws/raw/master/ dummy.box" config.vm.provider :aws do |aws, override| aws.access_key_id = ENV['AWS_ACCESS_KEY_ID'] aws.secret_access_key = ENV['AWS_SECRET_KEY'] aws.keypair_name = ENV['AWS_KEYPAIR_NAME'] override.ssh.username = ‘ec2-user’ override.ssh.private_key_path = ENV['AWS_PRIVATE_KEY_PATH'] #---- VPCݻ༗ͷઃఆ ----# aws.private_ip_address = "10.0.10.10" aws.subnet_id = "subnet-xxxxxxxx" aws.security_groups = ["sg-xxxxxxxx"] #---- VPCݻ༗ͷઃఆ͜͜·Ͱ ----# aws.region = "ap-northeast-1" aws.ami = "ami-71b22570" aws.instance_type = "t1.micro" aws.ssh_username = ‘ec2-user’ aws.security_groups = ["web"] end end ͨͩ͠71/઀ଓ͕ඞཁ 13೥7݄23೔Ր༵೔
  13. Summit Developers 4IFMMͰ1SPWJTJPOJOH Vagrant.configure("2") do |config| config.vm.box = "CentOS-6.4-x86_64-ja" config.vm.provision

    :shell, :path => "script.sh" end *OMJOF·ͨ͸֎෦εΫϦϓτܦ༝Ͱ؆୯ϓ ϩϏδϣχϯάʢҙ֎ͱΈΜͳ஌Βͳ͍ʣ 13೥7݄23೔Ր༵೔
  14. Summit Developers 4IFMMͰ1SPWJTJPOJOH #!/bin/sh yum install -y httpd php php-mbstring

    php-pdo php-mysql mysql- server /sbin/chkconfig --level 2345 mysqld on /sbin/chkconfig --level 2345 httpd on /etc/rc.d/init.d/httpd start /etc/rc.d/init.d/mysqld start cat < < END > /etc/sysconfig/iptables *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT END /etc/rc.d/init.d/iptables restart ͘͝ී௨ͷ4IFMM 13೥7݄23೔Ր༵೔
  15. Summit Developers $IFG4PMPͰ1SPWJTJPOJOH Vagrant.configure("2") do |config| config.vm.box = "CentOS-6.4-x86_64-ja" config.vm.network

    :private_network, ip: "192.168.33.10" config.vm.provision :chef_solo do |chef| chef.cookbooks_path = "./cookbooks" chef.roles_path = "./roles" chef.data_bags_path = "./data_bags" chef.add_role "web" chef.add_recipe "resolver" chef.add_recipe "myapp" chef.json = { :resolver => { :search => "localhost", :nameservers => ["8.8.8.8", "8.8.1.1"] } } end end ର৅Ϛγϯʹ$IFG$MJFOUͷ Πϯετʔϧ͕ඞཁ 13೥7݄23೔Ր༵೔
  16. Summit Developers $IFG4FSWFSͰ1SPWJTJPOJOH Vagrant.configure("2") do |config| config.vm.box = "CentOS-6.4-x86_64-ja" config.vm.network

    :private_network, ip: "192.168.33.80" config.vm.hostname = 'chef-test01' config.vm.provision :chef_client do |chef| chef.chef_server_url = "https://chef.meguro.ryuzee.com" chef.validation_client_name = "chef-validator" chef.validation_key_path = "#{ENV['HOME']}/.chef/chef- validator.pem" chef.client_key_path = "/etc/chef/client.pem" chef.environment ="development" chef.add_role "web" chef.add_recipe "analog" end end ର৅Ϛγϯʹ$IFG$MJFOUͷ Πϯετʔϧ͕ඞཁ 13೥7݄23೔Ր༵೔
  17. Summit Developers 1SPWJTJPOJOHͷར఺ • ߏங͢ΔԾ૝Ϛγϯ͕7JSUVBMCPYͩΖ͏͕ɺ "NB[PO&$ͩΖ͏͕ɺಉ͡εΫϦϓτΛ ࢖͑Δ • Կճ࣮ߦͯ͠΋ಉ݁͡ՌʹͳΔ Α͏ʹ͍ͨ͠

    • ΋ͬͱ΋ύοέʔδ͕ͳ͘ͳͬͨΓɺόʔ δϣϯ͕มΘͬͨΓ͢Δͱಈ͔ͳ͘ͳΔՄೳ ੑ΋͋ΔͷͰ$*͍ͨ͠ 13೥7݄23೔Ր༵೔
  18. Summit Developers 7BHSBOUͷߏ଄ͷ·ͱΊ Vagrant Core VirtualBox Amazon EC2 Vmware Fusion

    Vagrantfile ͦͷଞͷϓϥάΠϯ Shell Cookbook Manifest ੨͍Օॴ͕3VCZ Shell Chef Puppet 13೥7݄23೔Ր༵೔
  19. Summit Developers ૊Έ߹Θͤࣗ༝ Vagrant Core VirtualBox Amazon EC2 Vmware Fusion

    Vagrantfile ͦͷଞͷϓϥάΠϯ Shell Cookbook Manifest Shell Chef Puppet 13೥7݄23೔Ր༵೔
  20. Summit Developers ૊Έ߹Θͤࣗ༝ Vagrant Core VirtualBox Amazon EC2 Vmware Fusion

    Vagrantfile ͦͷଞͷϓϥάΠϯ Shell Cookbook Manifest Shell Chef Puppet 13೥7݄23೔Ր༵೔
  21. Summit Developers Ϣʔεέʔε • ϩʔΧϧ։ൃ؀ڥͱ·ͬͨ͘ಉ͡࢓ֻ͚Ͱςε τ؀ڥΛߏங͢Δ • ͦͷ··֬ೝ؀ڥͱ͓ͯ͠٬͞Μʹެ։͢Δ • &*1ͱηΩϡϦςΟάϧʔϓͷઃఆ

    • 3PVUFͰυϝΠϯ໊ϕʔεͰΞΫηε • ͍·ͷͱ͜Ζຊ൪͸ผͰߟ͑ͨํ͕ྑ͍͔΋ • %#ʹ"NB[PO3%4࢖ͬͨΓ 13೥7݄23೔Ր༵೔
  22. Summit Developers ৘ใݯ Create and Manage Virtualized Development Environments Mitchell

    Hashimoto Vagrant Up and Running http://docs.vagrantup.com/v2/ http://oreilly.com/go/vagrant 13೥7݄23೔Ր༵೔