[Vagrant] Hello, Vagrant

Jay Park·2021년 12월 1일
0

Vagrant

목록 보기
2/3

Vagrant 프로젝트 시작하기

Vagrant 프로젝트 구성의 첫 단계는 Vagrantfile 생성하는 것이다.

  • Project Root Directory 지정
  • 프로젝트에 필요한 머신과 자원을 기술 + 설치할 SW + 어떻게 접근할 것인지 지정
$ mkdir vagrant_getting_started
$ cd vagrant_getting_started
$ vagrant init hashicorp/bionic64

Vagrant는 가상 머신을 바닥부터 만드는 대신에 box 라고 부르는 베이스 이미지 를 빠르게 클론(복제)하여 사용한다. Vagrantfile 을 생성한 후 가장 먼저 하는 일이 Vagrant 환경에서 사용할 box를 지정하는 것이다.

Vagrant 파일 없이 box를 추가하고 싶은 경우는 vagrant add box 명령어를 사용한다.

박스들은 글로벌하게 저장된다. 각 프로젝트는 박스를 초기 이미지로 사용하여 복제를 하게 되고 실제 베이스 이미지를 변경하지 않는다.

$ vagrant box add hashicorp/bionic64

위 명령어에서 네임스페이스를 사용하고 있음을 알 수 있다. 박스 이름은 usernameboxname 두 부분으로 나뉜다. (URL이나 Local File Path를 사용해서도 박스를 지정할 수 있다고 한다.)

박스를 추가하였으니 프로젝트가 추가된 박스를 베이스로 사용하도록 구성할 필요가 있다.

Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/bionic64"
end

추가로 버전과 URL도 명시할 수 있다.

Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/bionic64"
  config.vm.box_version = "1.0.282"
  config.vm.box_url = "https://vagrantcloud.com/hashicorp/bionic64"
end

Boot and Environment (환경 부팅하기)

프로젝트 초기화하고 사용할 박스를 지정하였으니 첫번째 Vagrant 환경을 부팅을 시킬 시간이다.

가상 머신 띄우기

$ vagrant up

SSH로 머신에 들어가기

$ vagrant ssh
Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 4.15.0-58-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Tue Jun 16 21:57:57 UTC 2020

  System load:  0.44              Processes:           91
  Usage of /:   2.5% of 61.80GB   Users logged in:     0
  Memory usage: 11%               IP address for eth0: 10.0.2.15
  Swap usage:   0%

 * MicroK8s gets a native Windows installer and command-line integration.

     https://ubuntu.com/blog/microk8s-installers-windows-and-macos

0 packages can be updated.
0 updates are security updates.

vagrant@vagrant:~$

CTRL-D를 누르거나 logout 명령어로 빠져나올 수 있다.

vagrant@vagrant:~$ logout
Connection to 127.0.0.1 closed.

머신 제거하기

$ vagrant box list
hashicorp/bionic64  (virtualbox, 1.0.282)

$ vagrant box remove hashicorp/bionic64
Removing box 'hashicorp/bionic64' (v1.0.282) with provider 'virtualbox'...

로컬과 게스트 파일 싱크하기

SSH 터미널안에서 vi로 편집하는게 썩 편리하지는 않다. Vagrant는 로컬 파일을 자동으로 게스트 머신과 동기화를 시켜주므로 이 방법을 사용하여 로컬에서 편집하고 가상 개발 환경에서 파일을 실행할 수 있다.

*로컬 호스트 머신 --> 원격 게스트 머신

Vagrant는 Vagrantfile이 위치한 루트 디렉토리를 게스트 머신의 /vagrant 디렉토리에 마운트한다.
(synced folder)

vagrant@vagrant:~$ ls /vagrant
Vagrantfile

게스트 머신에서 파일을 생성한 후 호스트 머신에서도 동일하게 보이는지 테스트 해보자.

vagrant@vagrant:~$ touch /vagrant/foo

vagrant@vagrant:~$ exit
logout
Connection to 127.0.0.1 closed.

# ls
Vagrantfile foo

가상 머신 프로비전닝

이제 웹서버를 띄워보자.

HTML Directory 생성

$ mkdir html

간단하게 index.html 문서를 생성한다.

<!DOCTYPE html>
<html>
  <body>
    <h1>Getting started with Vagrant!</h1>
  </body>
</html>

프로비저닝 스크립트 작성하기

쉘스크립트를 이용하여 아파치를 셋업해 보자. 아래의 내용을 bootstrap.sh로 저장한다.

#!/usr/bin/env bash

apt-get update
apt-get install -y apache2
if ! [ -L /var/www ]; then
  rm -rf /var/www
  ln -fs /vagrant /var/www
fi

Vagrant 구성

Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/bionic64"
  config.vm.provision :shell, path: "bootstrap.sh"
end

웹서버 프로비전

$ vagrant reload --provision

여기서 두 번째 난관을 만나게 된다.

일단 vagrant-proxyconf를 먼저 시도해 본다.

# vagrant plugin install vagrant-proxyconf
Installing the 'vagrant-proxyconf' plugin. This can take a few minutes...
ERROR:  SSL verification error at depth 0: unable to get local issuer certificate (20)
ERROR:  You must add ~~회사 인증서~~ to your local trusted store
Vagrant failed to load a configured plugin source. This can be caused
by a variety of issues including: transient connectivity issues, proxy
filtering rejecting access to a configured plugin source, or a configured
plugin source not responding correctly. Please review the error message
below to help resolve the issue:

  SSL_connect returned=1 errno=0 state=error: certificate verify failed (unable to get local issuer certificate) (https://gems.hashicorp.com/specs.4.8.gz)

Source: https://gems.hashicorp.com/

아래 내용 참고해서 인증서 설치하면 정상적으로 플러그인이 설치가 된다.

# workaround of above https problem -- vagrant 설치폴더
$sudo -E echo ./xxxxxxx.crt >> /C/HashiCorp/Vagrant/embedded/cacert.pem

다음 Vagrantfile에 아래의 내용을 추가하자.

Vagrant.configure("2") do |config|
  if Vagrant.has_plugin?("vagrant-proxyconf")
    config.proxy.http     = "http://192.168.0.2:3128/"
    config.proxy.https    = "http://192.168.0.2:3128/"
    config.proxy.no_proxy = "localhost,127.0.0.1,.example.com"
  end
  # ... other stuff
end

이제 프락시 타고 프로비저닝이 정상적으로 잘 진행된다.

    default: VirtualBox Version: 6.1
==> default: Configuring proxy for Apt...
==> default: Configuring proxy environment variables...
==> default: Configuring proxy for Git...
==> default: Mounting shared folders...
    default: /vagrant => D:/Workspace/hello-vagrant
==> default: Running provisioner: shell...
    default: Running: C:/Users/JAESEO~1.PAR/AppData/Local/Temp/vagrant-shell20211201-10976-1er1iq4.sh
    default: Get:1 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
    default: Get:2 http://archive.ubuntu.com/ubuntu bionic InRelease [242 kB]
    default: Get:3 http://security.ubuntu.com/ubuntu bionic-security/main i386 Packages [1,080 kB]
    default: Get:4 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
    default: Get:5 http://archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]

프로비저닝이 진행되면 웹서버가 실행되어 동작하나 아직 호스트의 브라우저에서는 접근이 되지 않는다. 일단 게스트의 SSH 터미널에서 확인을 먼저 해보자.

vagrant@vagrant:~$ wget -qO- 127.0.0.1
<!DOCTYPE html>
<html>
  <body>
    <h1>Getting started with Vagrant!</h1>
  </body>
</html>

Defining a Forwarded Port

Host PC 에서 VM 내의 웹서버에 접근하기 위해서는 포트 포워딩 설정이 필요하다.

Vagrant.configure("2") do |config|
  config.vm.network "forwarded_port", guest: 80, host: 8080
end

위의 설정을 추가한 후 vagrant reload --provision 명령을 실행하면 포트 포워딩 설정이 이루어지고 이후에 Host PC의 브라우저에서 localhost:8080 으로 접근하면 웹 페이지가 출력된다.

[참고]

Local Windows PC에 GitHub Actions Self Hosted Runner 설치 중에 사내 Proxy 서버로 인한 문제 해결 과정을 추가로 작성하였다.

E: The repository 'https://cli.github.com/packages stable Release' does not have a Release file.

https://ubuntu.com/server/docs/security-trust-store

$ sudo apt-get install -y ca-certificates
$ sudo cp local-ca.crt /usr/local/share/ca-certificates
$ sudo update-ca-certificates

npm ERR! code UNABLE_TO_VERIFY_LEAF_SIGNATURE

$ npm config set cafile /vagrant/local-ca.crt 

gcloud's default CA certificates failed to verify your connection, which can happen if you are behind a proxy or firewall.

$ gcloud config set core/custom_ca_certs_file /vagrant/local-ca.crt
profile
Jaytiger

0개의 댓글