2011년 5월 21일 토요일

Vagant

Vagant

  • 개요: 가상머신을 쉽게 사용하게 해주는 도구
  • 공식 사이트 Vagrant
  • 지식 : UNIX, Ruby
  • 도구 : Vagrant , VirtualBox / VMware / EC2
  • 확인
vagrant —version
Vagrant 1.6.2

가상머신

  • Box (템플릿) 을 구하자.
  • 가상머신 초기화
  • 가상머신 부팅.
  • Box 입수
    vargrant box add precise32 \
    http://files.vagrantup.com/precise32.box
  • 리스트를 보자.
    vagrant box list
  • Box를 지워보자
    vagrant box remove precise32
  • Box는 템플릿으로 이것을 가지고 여러개의 가상 머신으로 확장이 가능하다.
  • 다시말하면 1 Box로 여러개의 다른 가상 머신을 만들어 사용할 수 있다.

가상머신을 만들어 보자.

  • CentOS64를 이용해 보자.
  • 우선 템플릿을 받는다.
  • 초기화를 한다.
> vagrant box init centos64
vagrant init centos64
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
  • Vagrantfile을 정보를 확인해 보자.
    less Vagrantfile
  • Vagrantfile 이 있는 폴더에서 실행한다.
    vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos64'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: myCentOSVM_default_1400596418688_49507
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => /Users/mscho/Simon/vagrant/myCentOSVM

가상 머신의 정지, 재기동, 소거

  • status 상태를 본다.
  • suspend 잠자기에서 깨어난다.
  • halt 중지 시킨다.
  • up 기동 시킨다.
  • reload 재시동 시킨다.
  • destory 가상머신을 지운다.

status

vagrant status
Current machine states:

default                   running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.

suspend

> vagrant suspend
==> default: Saving VM state and suspending execution...

resume

> vagrant resume
==> default: Resuming suspended VM...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection refused. Retrying...
==> default: Machine booted and ready!

destroy

vagrant destroy
    default: Are you sure you want to destroy the 'default' VM? [y/N] Y
==> default: Forcing shutdown of VM...
==> default: Destroying VM and associated drives...

가상 머신을 접속해 보자.

  • 부팅한 이후에 ssh로 접속해 보자.
    vagrant up
    vagrant ssh
  • http 서버를 설치해 보자.
    sudo yum -y install httpd
  • http 서버를 동작 시키자.
    sudo service httpd start
    sudo chkconfig httpd on
  • 방화벽을 꺼보자.
    sudo service iptables stop
    sudo chkconfig iptables off

웹페이지를 만들고 접속해 보자.

cd /var/www/html
sudo vi index.html
<h1> hello world. </h1>
  • 네트워크 설정을 위해서 빠져 나가자.
    exit
  • Vagrantfile 을 편집해서 네트워크를 설정하자.
    emacs Vagrantfile
    config.vm.network “private_network”, ip: “192.168.33.10”
  • 저장한후 reload하자.
    vagrant reload

공유 폴더를 사용해 보자.

  • 호스트의 Vagrantfile이 있는 폴더가 공유 폴더가 된다.
  • 가상머신은 /vagrant 폴더가 공유 폴더가 된다.
  • 예제로 호스트에서 index.html을 수정해서 가상 머신의 웹서비스로 보자.

가상 머신에서 공유 폴더 설정

vagrant ssh
sudo rm -fr /var/www/html
sudo ln -fs /vagrant /var/www/html
exit

호스트에서 index.html을 만들고, 브라우져로 가상 머신에 접속해 보자.

emacs index.html
<h1> hello world from OS X </h1>

Provisioning 을 사용해 보자.

  • 정의 : Vargant up이후 자동적으로 실행되는 처리를 말한다.
  • 새로운 provision용 vm을 만들자
    mkdir myOS
    cd myOS
    vagrant init centos64
  • provision 설정을 해 보자
    emacs VagrantFile
    config.vm.provision :shell, :inline => “echo hello world”
  • 설정한 vm을 동작시켜 보자.
vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos64'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: myCentOSVM2_default_1400599326027_6547
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => /Users/mscho/Simon/vagrant/myCentOSVM2
==> default: Running provisioner: shell...
    default: Running: inline script
==> default: hello world
  • 마지막에 hello world가 나타나는 것을 알수 있다.

좀더 세밀한 프로비전을 해 보자.

  • Vagrantfile을 열어서 설정을 바꿔 준다.
    config.vm.provision :shell, :path => “provision.sh”
sudo yum -y install httpd
sudo service httpd start
sudo chkconfig httpd on
sudo service iptables stop
sudo chkconfig iptables off
  • 설정한 프로비전을 적용해 보자.
    vagrant provision
  • 프로비전에 맞게 웹서비스가 올라가 있는지 확인해 보자.
    vagrant ssh
    sudo service httpd status
    httpd (pid 2121) is running…

자신만의 Box를 만들어 보자.

  • 동작하는 vm으로 만들어 보자.
    vagrant package
  • 만들어진 box를 확인해 보자.
    ls
    Vagrantfile package.box provision.sh
  • 만들어진 box를 새로운 템플릿으로 추가해 보자.
    vagrant box add my_box package.box
    ==> box: Adding box 'my_box' (v0) for provider: 
       box: Downloading: file:///Users/Simon/vagrant/myCentOSVM2/package.box
    ==> box: Successfully added box 'my_box' (v0) for 'virtualbox'!
    
  • 추가된 템플릿을 확인해 보자.
    ls ~/.vagrant.d/boxes/
    centos64 my_box precise32
  • 새로 추가된 템플릿으로 vm을 동작시켜 보자.
    cd ..
    mkdir myBox
    cd myBox
    vagrant init my_box
    vagrant up
    vagrant ssh
    sudo service httpd status
    httpd (pid 1391) is running…

plugin을 사용해 보자.

  • 여러가지 plugin이 있다.
    vagrant plugin install sahara
Installing the 'sahara' plugin. This can take a few minutes...
Building nokogiri using packaged libraries.
Building libxml2-2.8.0 for nokogiri with the following patches applied:
    - 0001-Fix-parser-local-buffers-size-problems.patch
    - 0002-Fix-entities-local-buffers-size-problems.patch
    - 0003-Fix-an-error-in-previous-commit.patch
    - 0004-Fix-potential-out-of-bound-access.patch
    - 0005-Detect-excessive-entities-expansion-upon-replacement.patch
    - 0006-Do-not-fetch-external-parsed-entities.patch
    - 0007-Enforce-XML_PARSER_EOF-state-handling-through-the-pa.patch
    - 0008-Improve-handling-of-xmlStopParser.patch
    - 0009-Fix-a-couple-of-return-without-value.patch
    - 0010-Keep-non-significant-blanks-node-in-HTML-parser.patch
    - 0011-Do-not-fetch-external-parameter-entities.patch
************************************************************************
IMPORTANT!  Nokogiri builds and uses a packaged version of libxml2.

If this is a concern for you and you want to use the system library
instead, abort this installation process and reinstall nokogiri as
follows:

    gem install nokogiri -- --use-system-libraries

If you are using Bundler, tell it to use the option:

    bundle config build.nokogiri --use-system-libraries
    bundle install

However, note that nokogiri does not necessarily support all versions
of libxml2.

For example, libxml2-2.9.0 and higher are currently known to be broken
and thus unsupported by nokogiri, due to compatibility problems and
XPath optimization bugs.
************************************************************************
************************************************************************
Building libxslt-1.1.28 for nokogiri with the following patches applied:
    - 0001-Adding-doc-update-related-to-1.1.28.patch
    - 0002-Fix-a-couple-of-places-where-f-printf-parameters-wer.patch
    - 0003-Initialize-pseudo-random-number-generator-with-curre.patch
    - 0004-EXSLT-function-str-replace-is-broken-as-is.patch
    - 0006-Fix-str-padding-to-work-with-UTF-8-strings.patch
    - 0007-Separate-function-for-predicate-matching-in-patterns.patch
    - 0008-Fix-direct-pattern-matching.patch
    - 0009-Fix-certain-patterns-with-predicates.patch
    - 0010-Fix-handling-of-UTF-8-strings-in-EXSLT-crypto-module.patch
    - 0013-Memory-leak-in-xsltCompileIdKeyPattern-error-path.patch
    - 0014-Fix-for-bug-436589.patch
    - 0015-Fix-mkdir-for-mingw.patch
************************************************************************
IMPORTANT!  Nokogiri builds and uses a packaged version of libxslt.

If this is a concern for you and you want to use the system library
instead, abort this installation process and reinstall nokogiri as
follows:

    gem install nokogiri -- --use-system-libraries

If you are using Bundler, tell it to use the option:

    bundle config build.nokogiri --use-system-libraries
    bundle install
************************************************************************
Installed the plugin 'sahara (0.0.16)'!
vagrant plugin -h
Usage: vagrant plugin <command> [<args>]

Available subcommands:
     install
     license
     list
     uninstall
     update

For help on any individual command run `vagrant plugin COMMAND -h`
vagrant plugin list
sahara (0.0.16)
vagrant-login (1.0.1, system)
vagrant-share (1.0.1, system)
$ vagrant plugin uninstall sahara
Uninstalling the 'sahara' plugin...
$ vagrant plugin list      
vagrant-login (1.0.1, system)
vagrant-share (1.0.1, system)

sahara를 사용해 보자.

  • sahara 플러그 인은 sanabox를 지원해 준다.
  • sandbox는 vm에 프로그램을 설치하거나 변경했는데, 이것을 되돌릴때 사용
vagrant sandbox on
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
vagrant ssh
touch test
ls
test
exit
  • 되돌려 보자.
vagrant sandbox rollback
vagrant ssh
ls
exit
  • 샌드 박스 커밋을 사용해서 확정하여 관리해 보자.
  • vm동작중에는 시간이 많이 걸리므로 잠재운다.
vagrant suspend
==> default: Saving VM state and suspending execution...
vagrant sandbox commit
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
  • sandbox를 중지해 보자.
sandbox status
[default] Sandbox mode is on
vagrant sandbox off
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
vagrant sandbox status
[default] Sandbox mode is off vagrant sandbox off

댓글 없음:

댓글 쓰기