05/07/2016 - VAGRANT
Bu örneğimizde tek bir Vagrantfile ile iki ayrı kutu yaratacağız. Kutulara apache sunucusu yüklenmiş olacak ve ayrıca ana bilgisayarın işletim sisteminden ulaşılabilir halde olacak. Her iki kutunun kendilerine ait Virtual Box girdileri olacak.
Sisteminizde vagrant ve Oracle VM yazılımlarının kurulu olduğunu varsayıyorum. Bununla birlikte ubuntu/trusty64
kutusunun sisteminize vagrant box add ubuntu/trusty64
komutu ile eklendiğini de varsayıyorum. Eğer emin değilseniz ls -l ~/.vagrant.d/boxes/
komutu ile kontrol edebilirsiniz.
mkdir dual-machine
$ cd dual-machine/
Bu işlemi dual-machine
klasöründeyken yapacaksınız. Dosyayı web-server.sh
olarak adlandırın.
#!/usr/bin/env bash
echo -e "-- Setting global variables\n"
APACHE_CONFIG=/etc/apache2/apache2.conf
DOCUMENT_ROOT=/var/www/html
LOCALHOST=localhost
echo -e "-- Updating packages list\n"
apt-get update -y -qq
echo -e "-- Installing Apache web server\n"
apt-get install -y apache2 > /dev/null 2>&1
echo -e "-- Adding ServerName to Apache config\n"
grep -q "ServerName ${LOCALHOST}" "${APACHE_CONFIG}" || echo "ServerName ${LOCALHOST}" >> "${APACHE_CONFIG}"
echo -e "-- Restarting Apache web server\n"
service apache2 restart
echo -e "-- Creating a dummy index.html file\n"
cat > ${DOCUMENT_ROOT}/index.html <<EOD
<html>
<head>
<title>${HOSTNAME}</title>
</head>
<body>
<h1>${HOSTNAME}</h1>
<p>Hi sir, I am going to serve you today!</p>
</body>
</html>
EOD
Bu işlemi dual-machine
klasöründeyken yapacaksınız. Dosyayı Vagrantfile
olarak adlandırın.
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
# Type of the box
config.vm.box = "ubuntu/trusty64"
# Configs for web server 1
config.vm.define :webserver1 do |webserver1_config|
# Configs for virtual machine entry in Oracle VM VirtualBox
webserver1_config.vm.provider :virtualbox do |vb_config|
vb_config.name = "Vagrant Web Server 1 - Ubuntu 14.04"
end
# Terminal name
webserver1_config.vm.hostname = "webserver1"
# IP address to access from host machine
webserver1_config.vm.network "private_network", ip: "192.168.50.10"
# Script to run while setting up the machine
webserver1_config.vm.provision :shell, path: "web-server.sh"
# Shared folder between host and virtual machine with "faster" nfs option and writing permissions enabled
webserver1_config.vm.synced_folder "webserver1", "/var/www/html", create: true, nfs: true, mount_options: ["actimeo=2"]
end
# Configs for web server 2
config.vm.define :webserver2 do |webserver2_config|
# Configs for virtual machine entry in Oracle VM VirtualBox
webserver2_config.vm.provider :virtualbox do |vb_config|
vb_config.name = "Vagrant Web Server 2 - Ubuntu 14.04"
end
# Terminal name
webserver2_config.vm.hostname = "webserver2"
# IP address to access from host machine
webserver2_config.vm.network "private_network", ip: "192.168.50.20"
# Script to run while setting up the machine
webserver2_config.vm.provision :shell, path: "web-server.sh"
# Shared folder between host and virtual machine with "faster" nfs option and writing permissions enabled
webserver2_config.vm.synced_folder "webserver2", "/var/www/html", create: true, nfs: true, mount_options: ["actimeo=2"]
end
end
Eğer "bootstrap.sh" dosyasının içeriğinde değişiklik yaparsanız, vagrant reload --provision
komutunu kullanmanız gerekecek. Bu işlem sırasında, admin şifresi sorulacak ve oraya işletim sisteminizin şifresini gireceksiniz. Eğer şifre girmeyi iptal etmek istiyorsanız nfs adresini okuyabilirsiniz.
$ vagrant up --provision
Bringing machine 'webserver1' up with 'virtualbox' provider...
Bringing machine 'webserver2' up with 'virtualbox' provider...
==> webserver1: ......
==> webserver1: ......
==> webserver1: ......
==> webserver1: -- Setting global variables
==> webserver1: -- Updating packages list
==> webserver1: -- Installing Apache web server
==> webserver1: -- Adding ServerName to Apache config
==> webserver1: -- Restarting Apache web server
==> webserver1: * Restarting web server apache2
==> webserver1: ...done.
==> webserver1: -- Creating a dummy index.html file
==> webserver2: ......
==> webserver2: ......
==> webserver2: ......
==> webserver2: -- Setting global variables
==> webserver2: -- Updating packages list
==> webserver2: -- Installing Apache web server
==> webserver2: -- Adding ServerName to Apache config
==> webserver2: -- Restarting Apache web server
==> webserver2: * Restarting web server apache2
==> webserver2: ...done.
==> webserver2: -- Creating a dummy index.html file
# Server 1
$ vagrant ssh webserver1
vagrant@webserver1:~$
# Server 2
$ vagrant ssh webserver2
vagrant@webserver2:~$
Sadece http://192.168.50.10/
veya http://192.168.50.20/
giderek test edebilirsiniz.