์ค์น
- VirtualBox ์ค์น
- Vagrant ์ค์น
brew install vagrant ansible
# GUIํด (์ ํ์ฌํญ)
brew install vagrant-manager
์ฌ์ฉ ๋ฐฉ๋ฒ
์ด๊ธฐ์์
1. vagrant ๊ด๋ จ ํ์ผ์ด ๋ง๋ค์ด์ง ์์ ํด๋ ๊ตฌ์ฑ ๋ฐ ์ด๋
mkdir -p /Users/nhn/workspace/VM/centos7
cd /Users/nhn/workspace/VM/centos7
2. ํ๋ฌ๊ทธ์ธ ์ค์น
vagrant plugin install vagrant-vbguest vagrant-disksize
3. ์ด๊ธฐ ํ์ผ ์์ฑ
vagrant init
- vi 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
centos.vm.provision "ansible" do |ansible|
ansible.playbook = "init.yml"
end
# centos.vm.provision "shell" do |shell|
# shell.path = "extend.sh"
# end
end
centos.vm.box
- ์์ฑํ ๋ฐ์ค ์ด๋ฏธ์ง๋ช
- url์์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ ๊ณตํ๋ ์ด๋ฏธ์ง ์ ํ๊ฐ๋ฅ
v.linked_clone
- ํด๋น ์ต์ ์ ์ค ๊ฒฝ์ฐ ๋ฐ์ค์ ๋ํ ๋ชจ๋ ์ด๋ฏธ์ง ํ์ผ์ ๋ง๋๋๊ฒ ์๋๋ผ ์ฐจ์ด์ ๋ง ์ถ๊ฐ๋ก ์์ฑํจ
- ์ฉ๋๊ด๋ฆฌ์ ๋์๋จ
centos.vm.provision
- box๋ก๋ ํ ํ๋ก๋น์ ๋์ ํ ํ์ผ์ ๋ด์ฉ ์ถ๊ฐ ๊ฐ๋ฅ
- vi init.yml
- name: init.yml
hosts: all
gather_facts: no
become: yes
tasks:
- name: ์ฌ์ฉ์ ์ด๋ฆ ์์ฑ
user:
name: "{{ item }}"
shell: /bin/bash
home: "/home/{{ item }}"
generate_ssh_key: true
password_lock: yes
with_items:
- irteam
- irteamsu
- centos
- name: sudoers.d ์ถ๊ฐ
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: ssh๋ด์ฉ ์ถ๊ฐ
authorized_key:
user: "{{ item }}"
state: present
key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
with_items:
- irteam
- irteamsu
- centos
- vagrant
- root
- name: Make sure a service unit is restarting
ansible.builtin.systemd:
state: restarted
name: sshd.service
ssh ๊ด๋ จ ์ค๋ฅ ๋ฐ์
fatal: [cent7-1]: FAILED! => {"msg": "An unhandled exception occurred while running the lookup plugin 'file'. Error was a <class 'ansible.errors.AnsibleError'>, original message: could not locate file in lookup: ~/.ssh/id_rsa.pub. could not locate file in lookup: ~/.ssh/id_rsa.pub"}
ํด๊ฒฐ๋ฐฉ๋ฒ
1. sshํค ์์ฑ
ssh-keygen
2. sshํค๊ฐ ์กด์ฌํ๋์ง ํ์ธ
cat ~/.ssh/id_rsa.pub
3. Vagrantfile ํ์ผ์ด ์์นํ ๊ณณ์ผ๋ก ์ด๋
cd /Users/nhn/workspace/VM/centos7
4. ๋ฒ ์ด๊ทธ๋ฐํธ ๊ธฐ์กด ๋ช
๋ น์ด๋ฅผ ํตํด ssh ์ ๊ทผ
vagrant ssh
5. ์ธ์ฆํค ๋ฑ๋ก์ฌ๋ถ ํ์ธ
cat ~/.ssh/authorized_keys
exit
์๋์ฒ๋ผ ssh-rsa๋ก ์์ํ๋ ๋ด์ฉ์ด ๋ฌ๋ค๋ฉด ํค๊ฐ ์๋ ๊ฒ์ ๋๋ค!
6. ์ธ์ฆํค ํ์ธ
cat ~/.ssh/id_rsa.pub
7. ํค๋ฅผ ์์ฑํ๊ณ vagrant provision์ ์ํ
vagrant provision
8. ssh ํค๋ฅผ ํตํด ์ ๊ทผ
ssh centos@192.168.56.11
- ํ๋ก๋น์ ๋๊น์ง ์ ์์ ์ผ๋ก ์งํ๋๋ฉด ์์ ๋ช
๋ น์ด๋ก ์ด๋ ๊ฒฝ๋ก์์๋ ํฐ๋ฏธ๋์์ ๋ฐ๋ก VM์ ์ ๊ทผ ๊ฐ๋ฅ!
- authorized_key ํค๋ฅผ ๋ฑ๋กํ๊ธฐ ๋๋ฌธ์ ssh๋ฅผ ํตํ ํจ์ค์๋ ๋ฏธ์ ๋ ฅ ์ ๊ทผ์ด ๊ฐ๋ฅํด์ง
- ip๋ Vagrantfile์ ์ ์๋ ๊ฒ์ ๋ฐ๋ผ 192.168.56.11๋ถํฐ ์์ฐจ์ ์ผ๋ก ๋ถ์ฌ๋จ
๋ช ๋ น์ด
# ์๋์ ๋ช
๋ น์ด๋ ๋ชจ๋ ์ปจํธ๋กคํ๋ ค๋ VM์ ์ค์ ์ด ์ ์๋ Vagrantfile์ด ์์นํ ๊ฒฝ๋ก์์ ์ํํด์ผ ์ ์์๋ํจ.
# VM๊ธฐ๋
vagrant up
# ์์ฑํ VM์ ssh ์ ๊ทผ, vagrant๊ณ์ ์ ์ ๊ทผํ๊ฒ๋จ.
vagrant ssh
# ์์ฑ ์คํญ์ ๊ฑด๋๋ฐ๊ณ ํ๋ก๋น์ ๋๋ง ์งํ.
vagrant provision
# VM ์ข
๋ฃ
vagrant halt
# ์์ฑํ VM ์ญ์ . f๋ ๊ฐ์ ์ต์
.
vagrant destroy -f
์ฐธ๊ณ ๋งํฌ