vagrant 설치 및 활용

개발툴|2016. 6. 30. 11:57

https://docs.docker.com/toolbox/toolbox_install_windows/virtual machines 의 라이프 사이클을 관리


vagrant를 사용하지 않는 상황에서는 버츄얼 머신 툴을 이용하여 VM을 생성하고 해당 VM에 OS를 설치해야 하는 반복적이고 지루한 작업들을 해야 한다.

허나 vagrant는 이런 반복적인 작업들을 자동화 할 수 있게 제공해 주고 있고 VM의 생성과 시작 그리고 삭제를 편리하게 해준다.


대게 vagrant를 사용하는 가장 큰 이유는 각 개발자간 일치하지 않는 작업 환경을 동일하게 구성하여 일관성을 유지하고 환경 구성으로 인한 작업 시간을 최소화 하기 위해서 사용이 되는데 나는 다음의 3가지 목표를 위해서 vagrant를 선택하게 되었다.


목표

 - IE 버전별 가상 머신 관리

 - python 테스트를 위한 환경 구성

 - 새로운 기술에 대한 환경 구성 및 테스트


위의 목표를 제시하게 된 계기는

내 로컬 머신에는 IE8이 설치되어 있는 상황인데 QA 테스트 시 IE 버전에 따른 버그들이 존재하여 디버깅하는데 어려움이 있었다.


python 테스트 환경이 필요하게 된 이유는

운영 환경에서 필요한 유틸들을 python을 이용하여 개발을 진행하고 있는데 로컬 머신이 윈도우이다 보니 테스트 코드를 실행하는데 어려움들이 존재하더라. (예를 들어 리눅스에 의존적인 명령어 사용 시 ssh, scp, tar, ps)


마지막으로 새로운 기술에 대한 환경 구성을 할 때 우리 팀에서 관리하고 있는 장비에 서버들을 설치하고 이를 테스트 하는 방식으로 진행하였다.

이렇게 되니 업무와는 상관 없는 설치 파일들과 테스트 코드들이 존재하게 되고 이로 인해 서버 관리가 제대로 되지 않는 상황이 발생된다.


Virtualbox version : 4.3.30

Vagrant version : 1.8.4


1.

vagrant 설치

https://www.vagrantup.com/downloads.html 접속 하여 OS 맞는 실행 파일 다운로드


2.

virtualbox 설치

https://docs.docker.com/toolbox/toolbox_install_windows/


3.

box 다운로드

기본 설정을 가진 VM 템플릿 이미지 (OS, 메모리, 파일시스템)

http://www.vagrantbox.es/ 접속하여 다운로드 받을 box의 URL을 복사한 후 아래 명령어 실행


기본적으로 box는 C 드라이브에 다운로드 되기에 여유 공간이 없다면 다음과 같이 환경 변수를 추가해 주면 된다.



vagrant box add win7-ie10 http://aka.ms/vagrant-win7-ie10


E:\vagrant>vagrant box add win7-ie10 http://aka.ms/vagrant-win7-ie10

==> box: Box file was not detected as metadata. Adding it directly...

==> box: Adding box 'win7-ie10' (v0) for provider:

    box: Downloading: http://aka.ms/vagrant-win7-ie10

    box: Progress: 0% (Rate: 742k/s, Estimated time remaining: 1:26:25)))


다운로드 받은 box 리스트를 확인

E:\vagrant>vagrant box list

centos6.6       (virtualbox, 0)

modernIE/w7-ie9 (virtualbox, 0.0.2)

ubuntu14.04     (virtualbox, 0)

win7-ie10       (virtualbox, 0)

win7-ie8        (virtualbox, 0)

4.

init 처리

다운로드 받은 box를 실행하게 하는 설정 파일을 생성하도록 한다.

적절한 폴더 안에서 아래 명령어를 실행하면 Vagrantfile이 만들어진다.

vagrant init win7-ie10


Vagrantfile은 VM 상세 설정 파일이다. (포트 포워딩, sync folder 등)


5.

가상 머신 실행

Vagrantfile 파일이 존재하는 디렉토리에서 아래 명령어를 실행하면 가상 머신이 동작한다.

vagrant up


실행 시 아래와 같은 오류가 발생되면 윈도우 path에서 cygwin64를 삭제하면 된다.

The executable 'cygpath' Vagrant is trying to run was not

found in the %PATH% variable. This is an error. Please verify

this software is installed and on the path.


가상 머신이 실행되면 box의 이미지 파일을 가상 머신에서 실행 가능하게 하기 위해서 설치 작업이 진행되는데 이때에도 기본적으로 C 드라이브에 파일이 생성된다. 이를 해결하기 위해서는 

C:\Users\사용자이름\.VirtualBox\VirtualBox.xml 파일에서 default machine folder 를 수정해 주도록 하자.



설치 오류 경험담


노트북에 vagrant 환경 구축을 시도했다.

vagrant up 이후 default: SSH auth method: private key 에서 멈추는 현상 발생

몇 분동안 멈춰있다가 타임 아웃 오류 로그가 찍힌다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
D:\vagrant\kube1>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu14.04'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: kube1_default_1506500237545_11901
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (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
Timed out while waiting for the machine to boot. This means that
Vagrant was unable to communicate with the guest machine within
the configured ("config.vm.boot_timeout" value) time period.
 
If you look above, you should be able to see the error(s) that
Vagrant had when attempting to connect to the machine. These errors
are usually good hints as to what may be wrong.
 
If you're using a custom box, make sure that networking is properly
working and you're able to connect to the machine. It is a common
problem that networking isn't setup properly in these boxes.
Verify that authentication configurations are also setup properly,
as well.
 
If the box appears to be booting properly, you may want to increase
the timeout ("config.vm.boot_timeout") value.
cs


이 문제의 원인은 전혀 예상하지 못한 곳에서 찾았다.

노트북 BIOS의 Virtualization 설정이 Disabled 되어있어서 였다.


아놔~~ 이런 어이 상실...

https://superuser.com/questions/396270/should-i-enable-intel-virtualization-and-vt-d

If you use Virtualbox, you should enable both. VT-d does direct I/O. And I believe Intel Virtualization is the same as VT-x.

Enabled로 모두 바꿔주고 vagrant up 하니 VM이 정상 기동된다.



명령어


vagrant halt : 중지

vagrant destroy : 중지 후 디렉토리 clean

vagrant reload : Vagrantfile 의 설정 변경을 적용하기 위한 명령어

vagrant share : 외부에서 vm에서 구동되고 있는 웹 서버에 접속이 가능하게 함

vagrant -v : 버전 확인



Vagrantfile 설정


[포트 포워딩]

guest 머신에서 동작하는 아파치 서버의 웹 페이지를 host 머신에서 접속하기 위한 설정

아래와 같이 포트 포워딩 설정을 추가하면 호스트 머신에서 http://localhost:8080 으로 연결하여 페이지를 확인할 수 있다.

config.vm.network :forwarded_port, guest: 80, host: 8080


[폴더 동기화]

호스트 머신의 common_source_folder 폴더를 가상 머신의 vagrant_data 경로에 링크

config.vm.synced_folder "E:\\vagrant\\common_source_folder", "/vagrant_data"


[provision]

vagrant up 할 때 아래의 shell 실행

config.vm.provision "shell", inline: <<-SHELL

apt-get update

apt-get install -y apache2

SHELL


[원격지에서 ssh 연결]

내 로컬 컴퓨터에 vagrant 환경을 구축하여 우분투 VM을 기동하였다.

기본적으로 로컬 컴퓨터에서 우분투 VM에 ssh 접속이 가능하지만 다른 컴퓨터에서는 접속이 불가능하다.

이를 위해서는 다음과 같은 설정 추가가 필요하다.

Vagrantfile 내에 아래 설정을 추가해 주면 다른 컴퓨터에서도 우분투 VM에 접속할 수 있다.

config.vm.network :forwarded_port, guest: 22, host: 2222, host_ip: "0.0.0.0", id: "ssh", auto_correct: true

https://stackoverflow.com/questions/19204358/vagrant-forwarding-ssh-from-remote-server



기타


vm 머신에서 host 장비로의 접속은 기본적으로 10.0.2.2 아이피로 접근할 수 있다.

즉, host 머신에 20010 톰켓 서버가 구동되어 있고 vm 머신의 IE에서 host 머신의 웹 애플리케이션에 접속하고 싶을 때에는 http://10.0.2.2:20010/ 와 같이 접근하면 된다.


[root 패스워드 설정]

# sudo passwd root


[vagrant-vbguest install]

윈도우 command창에서 다음의 명령어 실행

> vagrant plugin install vagrant-vbguest
https://github.com/dotless-de/vagrant-vbguest


[box 이미지 검색]

https://atlas.hashicorp.com/boxes/search 사이트에서 box 이미지 검색 후 다운로드 할 수 있다.

ex) vagrant box add bento/ubuntu-16.04



https://prezi.com/e1xobwhzv6ds/vagrant-and-remote-debugging-on-android/

댓글()