๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
DevOps/IaC ์ž๋™ํ™”

[Vagrant] ์‚ฌ์šฉ๋ฒ• ๋ฐ CentOS Provisioning ๊ธฐ๋ณธ ๊ตฌ์ถ•, Vagrant๋ž€?

by ๐ŸŒปโ™š 2020. 12. 19.

Vagrant? Provisioning?

Vagrant๋Š” Provisioning์„ ์œ„ํ•œ ํˆด์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ Provisioning์ด ๋ญ˜๊นŒ์š”? ์ œ๊ฐ€ ์ •์˜ํ•˜๋Š” Provisioning์€ ์‹œ์Šคํ…œ์„ ๋ฏธ๋ฆฌ ์ฝ”๋“œํ™”ํ•˜์—ฌ ์–ธ์ œ๋“ ์ง€ ์ฆ‰์‹œ ์‹œ์Šคํ…œ์„ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด Virtual box๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฐ€์ƒ๋จธ์‹ ์„ ์ƒ์„ฑํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ๋ฉ”๋ชจ๋ฆฌ, cpu, ๋„คํŠธ์›Œํฌ๋ฅผ ์ •์˜ํ•˜๊ณ  ์‹œ์Šคํ…œ์— ํ•„์š”ํ•œ ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ ํ˜น์€ ํ™˜๊ฒฝ์„ค์ •์„ ๋ฏธ๋ฆฌ ์ฝ”๋“œํ™”ํ•˜์—ฌ ์„œ๋ฒ„๋ฅผ ์ƒ์„ฑํ•  ์ค€๋น„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

Vagrant๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ฃผ๊ธฐ์ ์œผ๋กœ ์„œ๋ฒ„๋ฅผ ๋ฐฐํฌํ•˜๊ณ  ์ƒˆ๋กญ๊ฒŒ ํ™˜๊ฒฝ์€ ์ ์šฉํ•  ํ•„์š”๊ฐ€ ์žˆ์„ ๋•Œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ๊ฐ ๋‹ค๋ฅธ ์„œ๋น„์Šค์˜ ํ…Œ์ŠคํŠธํ™˜๊ฒฝ์„ ๊ตฌ์ถ•ํ•˜๊ฑฐ๋‚˜, ํ•™์Šต์ด๋‚˜ ๋‹จ๊ธฐ์ ์œผ๋กœ ์‚ฌ์šฉ์— ํ•„์š”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

Vagrant ์„ค์น˜

 

Vagrant by HashiCorp

Vagrant enables users to create and configure lightweight, reproducible, and portable development environments.

www.vagrantup.com

Vagrant ํ™ˆํŽ˜์ด์ง€๋ฅผ ์ด์šฉํ•ด์„œ ์šด์˜์ฒด์ œ์— ๋งž๊ฒŒ ์„ค์น˜ํ•œ๋‹ค.

 

Virtual box

 

Oracle VM VirtualBox

Welcome to VirtualBox.org! News Flash Important November 16th, 2020We're hiring! Looking for a new challenge? We're hiring a VirtualBox senior developer in 3D area (Europe/Russia/India). Important October 27th, 2020We're hiring! Looking for a new challenge

www.virtualbox.org

์‹ค์Šต์„ ์œ„ํ•ด Virtual box๋ฅผ ์„ค์น˜ํ•ด์ค๋‹ˆ๋‹ค.

 

 

Vagrant ์‹ค์Šต

vagrant init

vagrant init

vagrant init ๋ช…๋ น์„ ์‹คํ–‰ํ•˜๋ฉด 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 = "base"

  # 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.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # 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 = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # 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
  #   apt-get install -y apache2
  # SHELL
end

ํ•ด๋‹น Vagrantfile์„ ์ˆ˜์ •ํ•˜์—ฌ provisioning์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

 

vagrant up

 

๊ทธ๋Ÿผ 2๊ฐœ์˜ CentOS ์„œ๋ฒ„๋ฅผ provisioningํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

vagrant up

 

Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant_API_Version = "2"
Vagrant.configure(Vagrant_API_Version) do |config|
  #test01
  config.vm.define:"test01" do |cfg|
    config.vm.box = "centos/7"
    cfg.vbguest.auto_update = false
    cfg.vm.provider:virtualbox do |vb|
      vb.name="CentOS-test01"
      vb.customize ["modifyvm", :id, "--cpus",1]
      vb.customize ["modifyvm", :id, "--memory",1024]
    end
    cfg.vm.host_name="test01"
    cfg.vm.synced_folder ".", "/vagrant", disabled:true
    cfg.vm.network "public_network", ip: "222.111.71.201"
    cfg.vm.network "forwarded_port", guest: 22, host:39211, auto_correct: false, id: "ssh"
  end
  #test02
  config.vm.define:"test02" do |cfg|
    config.vm.box = "centos/7"
    cfg.vbguest.auto_update = false
    cfg.vm.provider:virtualbox do |vb|
      vb.name="CentOS-test02"
      vb.customize ["modifyvm", :id, "--cpus",1]
      vb.customize ["modifyvm", :id, "--memory",1024]
    end
    cfg.vm.host_name="test02"
    cfg.vm.synced_folder ".", "/vagrant", disabled:true
    cfg.vm.network "public_network", ip: "222.111.71.202"
    cfg.vm.network "forwarded_port", guest: 22, host:39212, auto_correct: false, id: "ssh"
  end
end

์„œ๋ฒ„์˜ ์‹œ์Šคํ…œ ํ™˜๊ฒฝ์„ ์ง์ ‘ ์ฝ”๋“œํ™”ํ•˜์—ฌ ์„œ๋ฒ„๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฝ”๋“œ์˜ ๋‚ด์šฉ์€ ruby๋กœ ๋˜์–ด ์žˆ๊ณ  ๊ฐ€์‹œ์„ฑ์ด ์ข‹์•„ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

config.vm.define vagrant ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ• ๋•Œ ์ด์šฉ๋  tag์ž…๋‹ˆ๋‹ค.
config.vm.box provisioningํ•  ์šด์˜์ฒด์ œ๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์šด์˜์ฒด์ œ์˜ ์ •๋ณด๋“ค์€ app.vagrantup.com/boxes/search์—์„œ ํ™•์ธ ๊ฐ€๋Šฅ
cfg.vbguest.auto_update No package kernel-devel-3.10.0-1127.el7.x86_64 available.
kernal ๊ด€๋ จ๋œ ์˜ค๋ฅ˜ ๋ฐœ์ƒํ•œ๋‹ค๋ฉด ํ•ด๋‹น ๊ฐ’์„ false๋กœ ์ง€์ •ํ•ด์ค๋‹ˆ๋‹ค.
vb.name ๊ฐ€์ƒ ๋จธ์‹ ์˜ ์ด๋ฆ„์„ ์ง€์ •
vb.customize ๊ฐ€์ƒ๋จธ์‹ ์˜ ์ŠคํŽ™์„ ์ง€์ •
cfg.vm.host_name hostname ์ง€์ •
cfg.vm.synced_folder host์™€ ์‚ฌ์šฉํ•˜๊ฒŒ๋  ๊ณต์œ ํด๋”๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
cfg.vm.network ๋„คํŠธ์›Œํฌ์— ๋Œ€ํ•œ ์„ค์ •

์ •์˜ํ•œ ์ฝ”๋“œ ๋‚ด์šฉ๋Œ€๋กœ provisioning์ด ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค. ์ค‘๊ฐ„์— ๋„คํŠธ์›Œํฌ์— ๋Œ€ํ•œ ์„ค์ •์ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

Virtualbox๋ฅผ ํ™•์ธํ•ด๋ณด๋ฉด provisioning์„ ์ง„ํ–‰ํ•œ ๊ฐ€์ƒ๋จธ์‹ ์ด ์˜ฌ๋ผ์˜จ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

vagrant provision

vagrant provision

ํ•ด๋‹น ๋ช…๋ น์„ ์‚ฌ์šฉํ•ด์„œ ์„œ๋ฒ„์˜ ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ์ ์šฉํ•˜์—ฌ provisioning์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

 

init.sh

#! /bin/bash

# install useful packages
yum install net-tools -y
yum install vim -y

ํŒŒ์ผ์„ ํ•˜๋‚˜ ์ƒ์„ฑํ•ด์„œ ๊ฐ host์— ๋ฐ˜์˜ํ•  ํŒŒ์ผ์„ ์ƒ์„ฑํ•ด์ค๋‹ˆ๋‹ค.

 

Vagrantfile

#test01
  config.vm.define:"test01" do |cfg|
    config.vm.box = "centos/7"
    cfg.vbguest.auto_update = false
    cfg.vm.provider:virtualbox do |vb|
      vb.name="CentOS-test01"
      vb.customize ["modifyvm", :id, "--cpus",1]
      vb.customize ["modifyvm", :id, "--memory",1024]
    end
    cfg.vm.host_name="test01"
    cfg.vm.synced_folder ".", "/vagrant", disabled:true
    cfg.vm.network "public_network", ip: "222.111.71.201"
    cfg.vm.network "forwarded_port", guest: 22, host:39211, auto_correct: false, id: "ssh"
    cfg.vm.provision "shell", path: "init.sh"
  end

cfg.vm.provision ์˜ต์…˜์„ ์ถ”๊ฐ€ํ•ด์„œ ์ƒ์„ฑํ•œ init.sh๋ฅผ ์‹คํ–‰ํ•ด์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

 

vagrant provision test01

test01 tag๋ฅผ ์ด์šฉํ•ด์„œ ํ•ด๋‹น host๋งŒ provisioning์„ ์ง„ํ–‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

vagrant ssh

vagrant ssh test01

ํ•ด๋‹น ์„œ๋ฒ„๋กœ ๋ถ™์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ root, vagrant 2๊ฐœ ๊ณ„์ •์ด ์žˆ๊ณ  ๋ชจ๋‘ 'vagrant'๋ผ๋Š” ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

vagrant halt

vagrant halt

Vagrantfile์— ์ •์˜๋˜์–ด ์žˆ๋Š” ๋ชจ๋“  host๋ฅผ ์ข…๋ฃŒ์‹œํ‚ต๋‹ˆ๋‹ค.

 

 

vagrant destroy

vagrant destroy

Vagrantfile์— ์ •์˜๋˜์–ด ์žˆ๋Š” ๋ชจ๋“  host์˜ ๊ฐ€์ƒ๋จธ์‹ ์„ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.

๋Œ“๊ธ€