Vagrant with multiple machines and ports

Posted on Wed 06 January 2016 by Pavlo Khmel

Vagrant environment:

vagrant_env

1. Prepare host system

1.1. Install Unubtu 14.04 x64 on host system.

Download page: http://www.ubuntu.com/download/desktop

Install it.

1.2. Install VirtualBox 5.0.6.

Download page https://www.virtualbox.org/wiki/Linux_Downloads

Install:

sudo dpkg -i virtualbox-5.0_5.0.6-103037~Ubuntu~trusty_amd64.deb

1.3. Generating public/private rsa key pair

ssh-keygen
mkdir .vagrant.d
cp -a .ssh/id_rsa .vagrant.d/

1.4. Install SSH server

sudo apt-get install openssh-server
sudo /etc/init.d/ssh restart

1.5. Install Git

sudo apt-get install git

2. Install VM guest

2.1. Create file config_vm.sh in your home directory:

#!/bin/bash
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
sed -i 's/ONBOOT=no/ONBOOT=yes/g' /etc/sysconfig/network-scripts/ifcfg-eth0
sed -i 's/^\(Defaults.*requiretty\)/#\1/' /etc/sudoers
chkconfig iptables off
rm -f /etc/udev/rules.d/70-persistent-net.rules
useradd vagrant
mkdir -m 0700 -p /home/vagrant/.ssh
mv /root/id_rsa.pub /home/vagrant/.ssh/authorized_keys
chmod 600 /home/vagrant/.ssh/authorized_keys
chown -R vagrant:vagrant /home/vagrant/.ssh
echo "vagrant ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
# install Guest Additions
mount -r /dev/cdrom /media/
yum -y install kernel-devel-`uname -r` kernel-header-`uname -r` gcc perl
/media/VBoxLinuxAdditions.run

2.2. Download CentoOS 6.7 x64 minimal

Download page: https://wiki.centos.org/Download

File: CentOS-6.7-x86_64-minimal.iso

2.3. Create new virtual machine

  • Name: centos6
  • Type: Linux
  • Version: Red Hat (64-bit)
  • Other option by default.

Start machine and use ISO CentOS-6.7-x86_64-minimal.iso.
- Install options by default.
- Set root password: vagrant

2.4. Prepare VM for vagrant

Run virtual box centos6 and login.
Use user: root and password vagrant

IMPORTANT: Insert Guest Additions

Get IP from host system

ip a | grep "inet " | grep -v '127.0.0.1'

For example you got 192.168.1.2

IMPORTANT (reminder): Insert Guest Additions before run config_vm.sh

VirtualBox window > Devices > Insert Guest Additions CD image

Run these commands on guest system (replace nidlar with your username):

ifup eth0
scp khmel@192.168.1.2:/home/khmel/.ssh/id_rsa.pub ./
scp khmel@192.168.1.2:/home/khmel/config_vm.sh ./
bash config_vm.sh
shutdown -h now

3. Configure Vagrant on host system

3.1. Download and install Vagrand

Download page: http://www.vagrantup.com/downloads

Install

sudo dpkg -i vagrant_1.7.4_x86_64.deb

3.2. Create vagrant box

Create directory for new vagrant environment and copy files:

mkdir vagrant-homework

Create vagrant-homework/Vagrantfile

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

Vagrant.configure(2) do |config|
  config.vm.define "appsrv" do |appsrv_config|
    appsrv_config.vm.box = "appsrv"
    appsrv_config.ssh.private_key_path = '~/.vagrant.d/id_rsa'
    appsrv_config.vm.network "private_network", :ip => '10.10.10.2'
    appsrv_config.vm.network "forwarded_port", guest: 22, host: 3333, id: "ssh"
    appsrv_config.vm.network "forwarded_port", guest: 80, host: 8080
  end
  config.vm.define "monsrv" do |monsrv_config|
    monsrv_config.vm.box = "monsrv"
    monsrv_config.ssh.private_key_path = '~/.vagrant.d/id_rsa'
    monsrv_config.vm.network "private_network", :ip => '10.10.10.3'
    monsrv_config.vm.network "forwarded_port", guest: 22, host: 4444, id: "ssh"
    monsrv_config.vm.network "forwarded_port", guest: 80, host: 8181
    monsrv_config.vm.network "forwarded_port", guest: 443, host: 4433
  end
end

Package VM and add vagrant box and add id_rsa file:

vagrant package --output centos6.box --base centos6
vagrant box add appsrv centos6.box
vagrant box add monsrv centos6.box
cp .ssh/id_rsa .vagrant.d/

Start vagrant environment:

cd vagrant-homework
vagrant up

Test ssh connection:

ssh -i ~/.vagrant.d/id_rsa vagrant@127.0.0.1 -p 3333
ssh -i ~/.vagrant.d/id_rsa vagrant@127.0.0.1 -p 4444