728x90
반응형

0. 설치 환경

- VirtualBox 버전 6.1.26
- Vagrant 버전 2.2.15
- Vitual Studio Code (Editor) 

1. Vagrant 초기화

vagrant 를 구성하기 위해서 따로 폴더 또는 디렉토리를 만들었다.
만든 디렉토리에 가서 vagrant 명령어를 통해 초기 설정을 해준다.

[vagrant 명령어 간단 정리]
vagrant init : 프로비저닝을 위한 기초 파일 생성
vagrant up : Vagrantfile 을 읽어 들여 프로비저닝 진행
vagrant halt : 베이그런트에서 다루는 가상 머신 종료
vagrant destroy : 베이그런트에서 다루는 가상 머신 삭제
vagrant ssh : 베이그런트에서 다루는 가상 머신 접속
vagrant provision : 베이그런트에서 다루는 가상 머신에 변경된 설정 적용

# vagrant init
==> vagrant: A new version of Vagrant is available: 2.2.18 (installed version: 2.2.15)!
==> vagrant: To upgrade visit: https://www.vagrantup.com/downloads.html

A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

명령어를 실행하고 나면 Vagrantfile 이란 파일이 생성된 것을 확인할 수 있다.

2. Vagrant 가상 머신 실행하기

Vagrantfile 에 주석이 엄청 많은데 중간에 보면 config.vm.box 라는 부분을 확인할 수 있다.

(...)
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"                      #### 이 부분 !!!
(...)

기본값으로 base 란 값이 설정되어있는데 기본값을 vagrant up 을 해서 가상머신을 만들어봤다.

# vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'base' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Box file was not detected as metadata. Adding it directly...
==> default: Adding box 'base' (v0) for provider: virtualbox
    default: Downloading: base
An error occurred while downloading the remote file. The error
message, if any, is reproduced below. Please fix this error and try
again.

Couldn't open file /Users/jaynam/workspace/k8s_cluster/base

base 라는 Box 를 찾을 수 없다는 에러 메시지를 확인했다.
여기서 Box 는 가상 머신 이미지를 말한다. 그래서 가상 머신을 만들기 위한 이미지가 없는 것 같아서 난 에러였다.
가상 머신 이미지를 가져오려면 베이그런트 클라우드에서 가져와야 한다고 한다.

 

Vagrant Cloud by HashiCorp

Vagrant Cloud by HashiCorp

app.vagrantup.com

나는 클라우드에서 centos/7 라는 기본 centOS 7 버전 가상 머신 이미지를 사용하려고 한다.

그럼 가상 머신 이미지를 config.vm.box 의 값에 base 대신 centos/7 라고 적어주면 된다.

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

Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
end

주석을 다 지워주고 box 이미지를 변경했다.
그리고 테스트 해볼 겸 vagrant up 해봤다.

# vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' version '2004.01' is up to date...
==> default: Setting the name of the VM: k8s_cluster_default_1629366171289_77065
==> 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
    default: 
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default: 
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: No guest additions were detected on the base box for this VM! Guest
    default: additions are required for forwarded ports, shared folders, host only
    default: networking, and more. If SSH fails on this machine, please install
    default: the guest additions and repackage the box to continue.
    default: 
    default: This is not an error message; everything may continue to work properly,
    default: in which case you may ignore this message.
==> default: Rsyncing folder: /Users/jaynam/workspace/k8s_cluster/ => /vagrant

이렇게 베이그런트로 가상 머신을 만들었다.
virtualBox 를 통해서 만들어진 가상 머신을 확인할 수 있다.
가상 머신의 이름은 Vagrantfile 이 위치한 폴더 또는 디렉토리의 이름을 가져오는 것 같다.

vagrant ssh 명령어를 통해 가상 머신에 접속할 수 있다.

# vagrant ssh
[vagrant@k8s ~]$ cat /etc/redhat-release 
CentOS Linux release 7.8.2003 (Core)

3. Vagrant 으로 생성한 가상 머신 삭제하기

그럼 테스트로 만들어본 가상 머신을 지우려면 vagrant destroy 명령어를 통해 지울 수 있다.

# vagrant destroy

지우고나면 virtualbox 에서도 사라진 것을 확인 할 수 있다.
명령어를 사용하지 않고 virtualbox 에서 전원을 끄고 삭제해주는 방법도 있지만
vagrant destroy 라는 명령어를 통해서 지워주는 것을 권장한다고 한다.
뒤에 -f 옵션을 통해 강제로 삭제할 수도 있다.

# vagrant destroy -f

 

Vagarnt 로 VirtualBox 에서 가상 머신을 만들어보고 지우는 과정까지 간단하게 진행해봤다.
나중에 쿠버네티스 클러스터 테스트 환경을 Vagrant 와 VirtualBox 를 사용해서 구축해볼 예정이고
과정을 하나씩 정리해나갈 예정이다. 끝- 👍

728x90
반응형
복사했습니다!