์ด๋ฒ ๊ธ์์๋ Vagrant๋ฅผ ์ด์ฉํด์ ansible์ ์ฌ์ฉํ ๋ ์ธ์ฆ ์์ ์ ์๋ตํ๊ธฐ ์ํด SSH ๊ณต๊ฐํค๋ฅผ ์๋์ผ๋ก ๋ฑ๋กํ๋ ์ค์ต๋๋ค. Vagrant๋ฅผ ์ด์ฉํด์ provisioningํ ๋ Ansible์ ๋ง์ด ์ฌ์ฉํ๊ฒ ๋ฉ๋๋ค. ๊ทธ๋ฐ๋ฐ ansible์ ssh๋ฅผ ํตํด ํต์ ํ๊ธฐ ๋๋ฌธ์ ansible hosts์ ์ง์ ๊ณ์ ์ ๋ณด๋ฅผ ๋ฑ๋กํด์ฃผ์ง ์๋ ์ด์ -k ์ต์ ์ ์ด์ฉํด์ผํ์ง๋ง... vagrant์์ ํ๊ฒฝ ์ธํ ์ ์ํด -k ์ต์ ์ ์ฌ์ฉํ ์ ์๋ค. ์ด๋ฐ ๋ถํธํจ์ ํด์ํ๊ธฐ ์ํด ๊ณต๊ฐํค๋ฅผ vagrant์์ ์ง์ ๋ฑ๋กํ์ฌ provisioning์ ํธํ๊ฒ ์งํํ ์ ์์ต๋๋ค.
Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant_API_Version = "2"
Vagrant.configure(Vagrant_API_Version) do |config|
#node01
config.vm.define:"node01" do |cfg|
config.vm.box = "centos/7"
cfg.vbguest.auto_update = false
cfg.vm.provider:virtualbox do |vb|
vb.name="CentOS-node01"
vb.customize ["modifyvm", :id, "--cpus",1]
vb.customize ["modifyvm", :id, "--memory",1024]
end
cfg.vm.host_name="node01"
cfg.vm.synced_folder ".", "/vagrant", disabled:true
cfg.vm.network "public_network", ip: "222.111.71.101"
cfg.vm.network "forwarded_port", guest: 22, host:29211, auto_correct: false, id: "ssh"
cfg.vm.provision "shell", path: "all_settings.sh"
end
#node02
config.vm.define:"node02" do |cfg|
config.vm.box = "centos/7"
cfg.vbguest.auto_update = false
cfg.vm.provider:virtualbox do |vb|
vb.name="CentOS-node02"
vb.customize ["modifyvm", :id, "--cpus",1]
vb.customize ["modifyvm", :id, "--memory",1024]
end
cfg.vm.host_name="node02"
cfg.vm.synced_folder ".", "/vagrant", disabled:true
cfg.vm.network "public_network", ip: "222.111.71.102"
cfg.vm.network "forwarded_port", guest: 22, host:29212, auto_correct: false, id: "ssh"
cfg.vm.provision "shell", path: "all_settings.sh"
end
#master
config.vm.define:"master" do |cfg|
config.vm.box = "centos/7"
cfg.vbguest.auto_update = false
cfg.vm.provider:virtualbox do |vb|
vb.name="CentOS-master"
vb.customize ["modifyvm", :id, "--cpus",1]
vb.customize ["modifyvm", :id, "--memory",2048]
end
cfg.vm.host_name="master"
cfg.vm.synced_folder ".", "/vagrant", disabled:true
cfg.vm.network "public_network", ip: "222.111.71.100"
cfg.vm.network "forwarded_port", guest: 22, host:29210, auto_correct: false, id: "ssh"
cfg.vm.provision "shell", path: "all_settings.sh"
cfg.vm.provision "shell", path: "master_init.sh"
cfg.vm.provision "shell", path: "nodes_ssh_key_settings.sh"
cfg.vm.provision "file", source: "puppet_settings.yml", destination: "puppet_settings.yml"
cfg.vm.provision "shell", inline: "ansible-playbook puppet_settings.yml"
end
end
master์์ node๋ก ํต์ ํ๋ ์์ ์ด ์๊ธฐ ๋๋ฌธ์ node๋ค์ ๋จผ์ provisioningํด์ค๋๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก ๋ชจ๋ ์๋ฒ์ all_settings.sh๋ฅผ ์คํํด์ฃผ๊ณ master์๋ ๋ฐ๋ก ์คํํด์ฃผ๋ ์์ ์ด ๋ ์ถ๊ฐ๋ฉ๋๋ค.
all_settings.sh
#! /usr/bin/env bash
# install useful packages
yum install net-tools vim -y
# edit hosts
cat << EOF > /etc/hosts
127.0.0.1 localhost
::1 localhost
222.111.71.100 master
222.111.71.101 node01
222.111.71.102 node02
EOF
# ssh password Authentication no to yes
sed -i -e 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config
systemctl restart sshd
ROOT_HOME=/root
VAGRANT_HOME=/home/vagrant
# create .ssh for root
mkdir -p $ROOT_HOME/.ssh
chmod 700 $ROOT_HOME/.ssh
touch $ROOT_HOME/.ssh/authorized_keys
chmod 644 $ROOT_HOME/.ssh/authorized_keys
hostname์ ์ด์ฉํ๊ธฐ ์ํด hostsํ์ผ์ ๋ชจ๋ ์์ ํด์ฃผ๊ณ ssh๋ฅผ Password๋ก ์ ๊ทผ๊ฐ๋ฅํ๋๋ก sshd_configํ์ผ์ ์์ ํด์ค๋๋ค. ์ดํ ๊ณต๊ฐํค๋ฅผ ๋ฑ๋กํ๊ธฐ ์ํด ๊ฒฝ๋ก๋ฅผ ์์ฑํด์ฃผ๊ณ ๊ถํ์ ๋ถ์ฌํฉ๋๋ค.
master_init.sh
#! /usr/bin/env bash
# install useful packages
yum install epel-release -y
yum install ansible -y
yum install sshpass -y
# edit ansible hosts
cat << EOF > /etc/ansible/hosts
[local]
master
[nodes]
node01
node02
EOF
master host์ ansible์ ์ค์นํ๊ณ password๋ฅผ ํตํด ์ ์ํ๊ธฐ ์ํด sshpass๋ฅผ ์ค์นํ๊ณ ansible hostsํ์ผ์ ์์ ํด์ค๋๋ค.
nodes_ssh_key_settings.sh
#! /usr/bin/env bash
ssh-keygen -t rsa -N '' -f /root/.ssh/id_rsa <<< n
declare -a server_hosts=("node01" "node02")
declare -a server_users=("vagrant")
key_val=$(cat /root/.ssh/id_rsa.pub)
for host in "${server_hosts[@]}"; do
sshpass -p vagrant ssh -o StrictHostKeyChecking=no root@$host "grep '$key_val' /root/.ssh/authorized_keys || echo '$key_val' >> /root/.ssh/authorized_keys"
for user in "${server_users[@]}"; do
sshpass -p vagrant ssh -o StrictHostKeyChecking=no $user@$host "grep '$key_val' /home/$user/.ssh/authorized_keys || echo '$key_val' >> /home/$user/.ssh/authorized_keys"
done
done
master node์ root ๊ณ์ ์์ nodes์ root์ vagrant ๊ณ์ ์ ๋น๋ฐ๋ฒํธ ์์ด ์ ์๊ฐ๋ฅํ๋๋ก ์ค์ ์ ๋๋ค. ansible playbook์ผ๋ก ์ค์ ํ์ผ๋ฉด ์ข์๊ฒ ์ง๋ง... ssh ํต์ ์ด ๊ฐ๋ฅํ์ง ์์ command๋ก ์คํํ๊ฒ ๋์์ต๋๋ค. ๊ทธ๋ฐ ๊ณผ์ ์์ ๋ฉฑ๋ฑ์ฑ์ ์งํค์ง ๋ชปํ์ฌ provisioning์ 1๋ฒ ์ด์ ์งํ๋๋ฉด ์ด๋ฏธ ์์ฑ๋ ๊ณต๊ฐํค๋ฅผ ๋ค์ ์์ฑํ๊ฑฐ๋ authorized_keys์ ๋๋ฒ ๋ฑ๋ก๋๋ ๊ฒฝ์ฐ๋ฅผ ๋ฐฉ์งํ๊ธฐ ์ํ ์ฒ๋ฆฌ๋ฅผ ํ์ต๋๋ค.
for๋ฌธ์ ์ด์ฉํ๊ณ server_hosts์ server_users๋ฅผ ์ถ๊ฐํด์ ๊ฐ๊ฐ ์๋ฒ์ ์ฌ์ฉ์๋ฅผ ๋๋ฆด ์ ์์ต๋๋ค. ๋น๋ฐ๋ฒํธ๋ ์ด๊ธฐ์ ๋ชจ๋ vagrant๋ก ์ค์ ๋ ๊ฒ์ด ์ ์ ์กฐ๊ฑด์ ๋๋ค.
-master node์์ ๊ณต๊ฐํค๋ฅผ scp๋ก ์ ์กํฉ๋๋ค.
-ssh-keygen์ overwriteํ๋ ๊ฒฝ์ฐ๋ฅผ ๋ฐฉ์งํ๊ธฐ ์ํด promt์ ๋ต์ n์ผ๋ก ์ค์ ํฉ๋๋ค.
-authorized-keys์ ๋ฑ๋ก ์ค๋ณต์ ๋ฐฉ์งํ๊ธฐ ์ํด grep ๋ช ๋ น์ด๋ก ํด๋น key๊ฐ ์๋์ง ํ์ธํฉ๋๋ค.
-๋ง์ง๋ง์ผ๋ก ์ ์กํ ๊ณต๊ฐํคํ์ผ์ ์ญ์ ํฉ๋๋ค.
๋๊ธ