Vagrant? Provisioning?
Vagrant๋ Provisioning์ ์ํ ํด์ ๋๋ค. ๊ทธ๋ผ Provisioning์ด ๋ญ๊น์? ์ ๊ฐ ์ ์ํ๋ Provisioning์ ์์คํ ์ ๋ฏธ๋ฆฌ ์ฝ๋ํํ์ฌ ์ธ์ ๋ ์ง ์ฆ์ ์์คํ ์ ๊ตฌ์ฑํ ์ ์๋๋ก ํ๋ ๊ฒ ์ ๋๋ค. ์๋ฅผ ๋ค์ด Virtual box๋ฅผ ์ฌ์ฉํด์ ๊ฐ์๋จธ์ ์ ์์ฑํ๋๋ฐ ํ์ํ ๋ฉ๋ชจ๋ฆฌ, cpu, ๋คํธ์ํฌ๋ฅผ ์ ์ํ๊ณ ์์คํ ์ ํ์ํ ํจํค์ง๋ฅผ ์ค์น ํน์ ํ๊ฒฝ์ค์ ์ ๋ฏธ๋ฆฌ ์ฝ๋ํํ์ฌ ์๋ฒ๋ฅผ ์์ฑํ ์ค๋นํ ์ ์์ต๋๋ค.
Vagrant๋ฅผ ์ฌ์ฉํ๋ฉด ์ฃผ๊ธฐ์ ์ผ๋ก ์๋ฒ๋ฅผ ๋ฐฐํฌํ๊ณ ์๋กญ๊ฒ ํ๊ฒฝ์ ์ ์ฉํ ํ์๊ฐ ์์ ๋ ์ ์ฉํ๊ฒ ์ฌ์ฉ๋ ์ ์์ต๋๋ค. ๊ฐ๊ฐ ๋ค๋ฅธ ์๋น์ค์ ํ ์คํธํ๊ฒฝ์ ๊ตฌ์ถํ๊ฑฐ๋, ํ์ต์ด๋ ๋จ๊ธฐ์ ์ผ๋ก ์ฌ์ฉ์ ํ์ํ ์ ์์ต๋๋ค.
Vagrant ์ค์น
Vagrant ํํ์ด์ง๋ฅผ ์ด์ฉํด์ ์ด์์ฒด์ ์ ๋ง๊ฒ ์ค์นํ๋ค.
Virtual box
์ค์ต์ ์ํด 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์ ๊ฐ์๋จธ์ ์ ์ญ์ ํฉ๋๋ค.
๋๊ธ