1. ๋ก์ปฌ PC์์ Vagrant๋ก CentOS ๊ฐ์ํ๊ฒฝ ๋์ฐ๊ธฐ
์ค์น ํ๊ฒฝ
Macbook Pro Intel (2019)
CentOS 7.9
์ค์น ์คํฌ๋ฆฝํธ
#!/usr/bin/env bash
directory="$HOME/workspace/VM/centos7"
ssh_key_file="$HOME/.ssh/id_rsa"
# Check if Homebrew is installed
if ! [ -x "$(command -v brew)" ]; then
echo '>> Homebrew is not installed.' >&2
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
# Check if VirtualBox is installed
if ! [ -x "$(command -v vboxmanage)" ]; then
echo '>> VirtualBox is not installed.' >&2
brew install --cask virtualbox
fi
# Check if VirtualBox Extension Pack is installed
if ! vboxmanage list extpacks | grep "Oracle VM VirtualBox Extension Pack"; then
echo '>> VirtualBox Extension Pack is not installed.'
rm Oracle_VM_VirtualBox_Extension_Pack-$(VBoxManage -v | cut -d r -f 1).vbox-extpack
fi
# Check if Vagrant is installed
if ! [ -x "$(command -v vagrant)" ]; then
echo '>> Vagrant is not installed.' >&2
brew install vagrant
fi
# Check if Ansible is installed
if ! [ -x "$(command -v ansible)" ]; then
echo '>> Ansible is not installed.' >&2
brew install ansible
fi
# Create directory for CentOS VM
if [ ! -d "$directory" ]
then
mkdir -p "$directory"
fi
cd "$directory"
# Check if the required Vagrant plugins are installed
if ! vagrant plugin list | grep -q vagrant-vbguest
then
echo ">> vagrant-vbguest plugin is not installed."
vagrant plugin install vagrant-vbguest
fi
if ! vagrant plugin list | grep -q vagrant-disksize
then
echo ">> vagrant-disksize plugin is not installed."
vagrant plugin install vagrant-disksize
fi
# Initialize Vagrantfile
vagrant init
# Edit Vagrantfile
cat << EOF > Vagrantfile
ENV["LC_ALL"] = "en_US.UTF-8"
Vagrant.configure("2") do |centos|
# All servers will run cent 7
centos.vm.box = "centos/7"
centos.vm.box_check_update = false
centos.disksize.size = "60GB"
# Create the cent1 Server
N = 1
(1..N).each do |i|
hostname = "cent7-#{i}"
centos.vm.define hostname do |host1|
host1.vm.hostname = hostname
host1.vm.network "private_network", ip: "192.168.56.#{10 + i}"
host1.vbguest.auto_update = false
host1.vm.provider "virtualbox" do |v|
v.name = hostname
v.memory = "2048"
v.cpus = "2"
v.linked_clone = "true"
v.gui = "false"
v.customize ['modifyvm', :id, '--graphicscontroller', 'vmsvga']
v.customize ['modifyvm', :id, '--vram', '20']
end
end
end
# Provision with Ansible playbook
centos.vm.provision "ansible" do |ansible|
ansible.playbook = "init.yml"
end
end
EOF
# Edit Ansible playbook
cat << EOF > init.yml
- name: init.yml
hosts: all
gather_facts: no
become: yes
tasks:
- name: Create users
user:
name: "{{ item }}"
shell: /bin/bash
home: "/home/{{ item }}"
generate_ssh_key: true
password_lock: yes
with_items:
- irteam
- irteamsu
- centos
- name: Add sudoers.d file
copy:
content: |
%{{item}} ALL=(ALL) NOPASSWD: ALL
dest: "/etc/sudoers.d/{{item}}"
owner: root
group: root
mode: 0440
validate: "/usr/sbin/visudo -c -f '%s'"
with_items:
- irteam
- irteamsu
- centos
- name: Add SSH key
authorized_key:
user: "{{ item }}"
state: present
key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
with_items:
- irteam
- irteamsu
- centos
- vagrant
- root
- name: Restart SSH service
ansible.builtin.systemd:
state: restarted
name: sshd.service
EOF
# Start Vagrant VM
vagrant up
# Generate SSH key
if [[ ! -f "$ssh_key_file" ]]; then
echo ">> Generating new SSH key..."
ssh-keygen
fi
cat ~/.ssh/id_rsa.pub
cd "$directory"
# SSH into Vagrant VM
vagrant SSH
vagrant ssh -c "cat ~/.ssh/authorized_keys"
vagrant ssh -c 'exit'
# Print public SSH key
cat "$ssh_key_file"
# Provision Vagrant VM
vagrant provision
# Connect
ssh-keyscan -H 192.168.56.11 >> ~/.ssh/known_hosts
ssh -o StrictHostKeyChecking=no centos@192.168.56.11
๊ฒฐ๊ณผ
์ฑ๊ณต!
2. Ubuntu ์ธ์คํด์ค์์ Vagrant๋ก CentOS ๊ฐ์ํ๊ฒฝ ๋์ฐ๊ธฐ
์ค์น ํ๊ฒฝ
NHN Cloud
Ubuntu 20.04 LTS
CentOS 7.9
์ค์น ์คํฌ๋ฆฝํธ
#!/usr/bin/env bash
directory="$HOME/workspace/VM/centos7"
ssh_key_file="$HOME/.ssh/id_rsa"
# Check if wget is installed
if ! command -v wget &> /dev/null
then
echo ">> wget is not installed."
sudo apt-get update
sudo apt-get install -y wget
fi
# Download and add VirtualBox public key
# Add VB repo to package manager
# Update package list
sudo apt-get update
# Install VirtualBox
sudo apt-get install -y virtualbox-6.1
# Clean up
#sudo apt-get autoremove
#sudo apt-get autoclean
# Check if VirtualBox Extension Pack is installed
if ! vboxmanage list extpacks | grep -q "Oracle VM VirtualBox Extension Pack"; then
echo '>> VirtualBox Extension Pack is not installed.'
wget https://download.virtualbox.org/virtualbox/6.1.42/Oracle_VM_VirtualBox_Extension_Pack-6.1.42.vbox-extpack
rm "Oracle_VM_VirtualBox_Extension_Pack-6.1.42.vbox-extpack"
fi
# Check if Vagrant is installed
if ! [ -x "$(command -v vagrant)" ]; then
echo '>> Vagrant is not installed.' >&2
wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
sudo apt update && sudo apt install -y vagrant
fi
# Check if Ansible is installed
if ! [ -x "$(command -v ansible)" ]; then
echo '>> Ansible is not installed.' >&2
sudo apt install -y ansible
fi
# Create directory for CentOS VM
if [ ! -d "$directory" ]
then
mkdir -p "$directory"
fi
cd "$directory"
# Check if the required Vagrant plugins are installed
if ! vagrant plugin list | grep -q vagrant-vbguest
then
echo ">> vagrant-vbguest plugin is not installed."
vagrant plugin install vagrant-vbguest
fi
if ! vagrant plugin list | grep -q vagrant-disksize
then
echo ">> vagrant-disksize plugin is not installed."
vagrant plugin install vagrant-disksize
fi
# Initialize Vagrantfile
vagrant init
# Edit Vagrantfile
cat << EOF > Vagrantfile
ENV["LC_ALL"] = "en_US.UTF-8"
Vagrant.configure("2") do |centos|
# All servers will run cent 7
centos.vm.box = "centos/7"
centos.vm.box_check_update = false
centos.disksize.size = "60GB"
# Create the cent1 Server
N = 1
(1..N).each do |i|
hostname = "cent7-#{i}"
centos.vm.define hostname do |host1|
host1.vm.hostname = hostname
host1.vm.network "private_network", ip: "192.168.56.#{10 + i}"
host1.vbguest.auto_update = false
host1.vm.provider "virtualbox" do |v|
v.name = hostname
v.memory = "2048"
v.cpus = "2"
v.linked_clone = "true"
v.gui = "false"
v.customize ['modifyvm', :id, '--graphicscontroller', 'vmsvga']
v.customize ['modifyvm', :id, '--vram', '20']
end
end
end
# Provision with Ansible playbook
centos.vm.provision "ansible" do |ansible|
ansible.playbook = "init.yml"
end
end
EOF
# Edit Ansible playbook
cat << EOF > init.yml
- name: init.yml
hosts: all
gather_facts: no
become: yes
tasks:
- name: Create users
user:
name: "{{ item }}"
shell: /bin/bash
home: "/home/{{ item }}"
generate_ssh_key: true
password_lock: yes
with_items:
- irteam
- irteamsu
- centos
- name: Add sudoers.d file
copy:
content: |
%{{item}} ALL=(ALL) NOPASSWD: ALL
dest: "/etc/sudoers.d/{{item}}"
owner: root
group: root
mode: 0440
validate: "/usr/sbin/visudo -c -f '%s'"
with_items:
- irteam
- irteamsu
- centos
- name: Add SSH key
authorized_key:
user: "{{ item }}"
state: present
key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
with_items:
- irteam
- irteamsu
- centos
- vagrant
- root
- name: Restart SSH service
ansible.builtin.systemd:
state: restarted
name: sshd.service
EOF
# Start Vagrant VM
vagrant up
# Generate SSH key
if [[ ! -f "$ssh_key_file" ]]; then
echo ">> Generating new SSH key..."
ssh-keygen
fi
cat ~/.ssh/id_rsa.pub
cd "$directory"
# SSH into Vagrant VM
vagrant SSH
vagrant ssh -c "cat ~/.ssh/authorized_keys"
vagrant ssh -c 'exit'
# Print public SSH key
cat "$ssh_key_file"
# Provision Vagrant VM
vagrant provision
# Connect
#ssh centos@192.168.56.11
ssh-keyscan -H 192.168.56.11 >> ~/.ssh/known_hosts
ssh -o StrictHostKeyChecking=no centos@192.168.56.11
๊ฒฐ๊ณผ
์คํจ..
์๋ฌ ์ํฉ
NHN ํด๋ผ์ฐ๋ ubuntu ์ธ์คํด์ค์์ vagrant๋ก centos ๊ฐ์ํ๊ฒฝ์ ๋์ฐ๋ ค๊ณ ํจ
vagrant up ๋จ๊ณ์์ ์๋ฌ๊ฐ ๋ฐ์ํด ๋ค์ ๋จ๊ณ๋ก ๋์ด๊ฐ์ง ๋ชป ํจ
There was an error while executing `VBoxManage`, a CLI used by Vagrant
for controlling VirtualBox. The command and stderr is shown below.
Command: ["startvm", "d7c9b9b1-a0d1-4ff2-b119-3a33f10a1540", "--type", "gui"]
Stderr: VBoxManage: error: The virtual machine 'cent7-1' has terminated unexpectedly during startup because of signal 6
VBoxManage: error: Details: code NS_ERROR_FAILURE (0x80004005), component MachineWrap, interface IMachine
์๋ํด๋ณธ ๋ฐฉ๋ฒ
ํธ์คํธ ์ฌ๋ถํ
- ์คํจ
virtualbox์ virtualbox extension pack ๋ฒ์ ๋ง์ถ๊ธฐ - ์คํจ
VM headless ๋ชจ๋๋ก ์์ - ์๋ ์
๊ฒฐ๋ก
์ค์น๋ฅผ ์งํํ๊ณ ์๋ ubuntu ์์ฒด๊ฐ ์ธ์คํด์ค ํ๊ฒฝ์ด์ด์ ๊ทธ ์์ ๋ ๊ฐ์ํ๊ฒฝ(VM)์ ๋์ฐ๋๊ฒ ๋ถ๊ฐ๋ฅํ ๊ฒ์ผ๋ก ํ๋จ..
vagrant ๋์ ํธ์คํธ OS ์์ ํ์ดํผ๋ฐ์ด์ ๋ฅผ ๋์ฐ์ง ์๋ ๊ตฌ์กฐ์ธ docker๋ก ์งํํ๊ธฐ๋ก ๊ฒฐ์ !