Vagrant란?

행복한 콩🌳·2023년 4월 19일

Vagrant 란?

✅ Vagrant란?
Vagrant는 가상화 환경을 관리하고 프로비저닝하는 도구입니다. 개발자들이 동일한 개발 환경을 구축하고, 다른 운영 체제나 호스트에서 동일한 개발 환경을 실행할 수 있게 해줍니다.

Vagrant는 VirtualBox, VMware 등과 같은 가상화 소프트웨어와 함께 사용할 수 있습니다.
Vagrantfile이라는 파일을 작성하여 가상 머신의 설정 및 프로비저닝을 관리하며, 이 파일은 버전 관리 시스템에 저장하여 다른 개발자와 공유할 수 있습니다.

Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://vagrantcloud.com/search.
config.vm.box = "ubuntu/jammy64"
# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
# config.vm.box_check_update = false
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# NOTE: This will enable public access to the opened port
# config.vm.network "forwarded_port", guest: 80, host: 8080
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine and only allow access
# via 127.0.0.1 to disable public access
# config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
# Create a private network, which allows host-only access to the machine
# using a specific IP.
# config.vm.network "private_network", ip: "192.168.33.10"
# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network "public_network"
# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
// 공유를 생성할 폴더 위치를 지정해줌 첫번째 인자(기준은 vagrantfile 위치)
config.vm.synced_folder "../../../Dropbox", "/dropbox/"
# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
config.vm.provider "virtualbox" do |vb|
# Display the VirtualBox GUI when booting the machine
vb.gui = false
# Customize the amount of memory on the VM:
vb.memory = "2048"
# Assign vpus to the VM
vb.cpus = "4"
end
# View the documentation for the provider you are using for more
# information on available options.
# Enable provisioning with a shell script. Additional provisioners such as
# Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
# documentation for more information about their specific syntax and use.
config.vm.provision "shell", inline: <<-SHELL
    apt-get update
    ln -s /dropbox ~/
  SHELL
end

각 부분의 의미

Vagrant.configure("2")

  • Vagrantfile의 버전 명시, Vagrantfile의 맨 처음에 위치해야함

do |config|

  • Vagrant 설정 정의하는 블록 열어줌, config는 이 블록 안에서 Vagrant 설정을 지정하는 데 사용되는 변수
  • do 뒤의 수직 막대 (|)는 블록에서 사용할 변수를 정의하는데 사용

config.vm.box = "ubuntu/jammy64"

config.vm.box_check_update = false

  • Vagrant가 가상 머신에 할당된 Box 이미지를 업데이트할 때 자동으로 확인하는 것을 비활성화하는 설정

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

  • 가상 머신에 대한 포트 포워딩을 설정하는 라인
  • guest 속성에는 가상 머신 안의 포트 번호를 지정하고, host 속성에는 호스트 컴퓨터에서의 포트 번호를 지정
  • ex, 호스트 컴퓨터에서 브라우저를 열어 http://localhost:8080으로 접속시 가상 머신 안의 80번 포트로 접속

config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  • 호스트와 게스트 간에 포트 포워딩을 설정하는 데 사용됩니다. 이 경우에는 게스트의 80번 포트를 호스트의 8080번 포트로 포워딩
  • 추가로 host_ip 옵션을 사용하여 호스트의 IP 주소를 지정하여 게스트의 포트에 대한 외부 접근을 비활성화 할 수 있음

config.vm.network "private_network", ip: "192.168.33.10"

  • Vagrantfile에서 가상 머신에 네트워크를 추가하는 데 사용
  • private_network는 호스트 컴퓨터와 가상 머신 간에만 통신할 수 있는 전용 네트워크를 만듦
  • 가상 머신에 고정 IP 주소를 할당할 수 있으며, 이를 통해 호스트와 가상 머신 간의 통신이 쉽게 가능

config.vm.network "public_network"

  • Vagrant에서 사용 중인 가상 머신을 호스트의 공용 네트워크 인터페이스에 연결하도록 구성
  • 외부에서 가상 머신에 직접 연결할 수 있음

config.vm.synced_folder "../../../Dropbox", "/dropbox/"

  • 공유를 생성할 폴더 위치를 지정해줌 첫번째 인자(기준은 vagrantfile 위치)

config.vm.provider "virtualbox" do |vb|

  • Vagrantfile에서 VirtualBox 공급자(provider)를 설정하는 부분
  • 가상화 솔루션을 사용하여 생성된 가상 머신의 설정을 구성할 수 있음. vb 변수를 사용하여 가상 머신의 속성, 예를 들어 GUI 여부, 메모리 및 CPU 수 등을 지정할 수 있음

vb.gui = false

  • vb.gui = false는 VirtualBox GUI를 사용하지 않도록 설정하는 Vagrantfile의 구성 요소

vb.memory = "2048"

  • VM 메모리 설정

vb.cpus = "4"

  • VM의 CPU 설정

Vagrant 주요 명령어

vagrant --help

vagrant plugin install vagrant-vbguest
vagrant init
vagrant validate
vagrant up
vagrant ssh-config
vagrant ssh
vagrant halt
vagrant destroy

vagrant global-status
profile
매일매일 조금씩 모여 숲이 되자🐣

0개의 댓글