Pages

Saturday, January 10, 2015

Configuring Vagrant Multiple Machines

 centos7 base box was created as per the steps at http://anandbitra.blogspot.com/2015/01/creating-centos-7-vagrant-base-box.html

You can also add a box from publicly available catalog of Vagrant boxes at https://atlas.hashicorp.com/boxes/search

Modify Vagrantfile to configure CPUs, memory, network and mount shared folders. The below configuration will work for Virtualbox.

Open Git Bash
$ cd centos7-vagrant
$ vagrant init anandbitra/centos-7.1
$ vi Vagrantfile

Comment all the lines and add the below lines.

Vagrant.configure("2") do |config|
  config.vm.define "webserver" do |web_config|
     web_config.vm.box = "anandbitra/centos-7.1"
     web_config.vm.hostname = "webserver"
     web_config.vm.network "public_network", type: "dhcp"
     config.vm.provider "virtualbox" do |vb|
        vb.customize ["modifyvm", :id, "--usb", "off"]
        vb.customize ["modifyvm", :id, "--usbehci", "off"]
        vb.gui = false
        vb.cpus = 1
        vb.memory = 2048
     end
  end

  config.vm.define "dbserver" do |db_config|
     db_config.vm.box = "anandbitra/centos-7.1"
     db_config.vm.hostname = "dbserver"
     db_config.vm.network "public_network", type: "dhcp"
     config.vm.provider "virtualbox" do |vb|
        vb.customize ["modifyvm", :id, "--usb", "off"]
        vb.customize ["modifyvm", :id, "--usbehci", "off"]
        vb.gui = false
        vb.cpus = 1
        vb.memory = 1024
     end
  end
  config.vm.define "master-node-01" do |db_config|
     db_config.vm.box = "anandbitra/centos-7.1"
     db_config.vm.hostname = "master-node-01"
     # db_config.vm.network "public_network", type: "dhcp"
     db_config.vm.network "private_network", ip: "192.168.1.10"
     config.vm.provider "virtualbox" do |vb|
        vb.customize ["modifyvm", :id, "--usb", "off"]
        vb.customize ["modifyvm", :id, "--usbehci", "off"]
        vb.gui = false
        vb.cpus = 1
        vb.memory = 1024
     end
  end
  config.vm.define "core-node-01" do |db_config|
     db_config.vm.box = "anandbitra/centos-7.1"
     db_config.vm.hostname = "core-node-01"
     # db_config.vm.network "public_network", type: "dhcp"
     db_config.vm.network "private_network", ip: "192.168.1.11"
     config.vm.provider "virtualbox" do |vb|
        vb.customize ["modifyvm", :id, "--usb", "off"]
        vb.customize ["modifyvm", :id, "--usbehci", "off"]
        vb.gui = false
        vb.cpus = 1
        vb.memory = 1024
     end
  end
  config.vm.define "core-node-02" do |db_config|
     db_config.vm.box = "anandbitra/centos-7.1"
     db_config.vm.hostname = "core-node-02"
     # db_config.vm.network "public_network", type: "dhcp"
     db_config.vm.network "private_network", ip: "192.168.1.12"
     config.vm.provider "virtualbox" do |vb|
        vb.gui = false
        vb.cpus = 1
        vb.memory = 1024
        vb.customize ["modifyvm", :id, "--usb", "off"]
        vb.customize ["modifyvm", :id, "--usbehci", "off"]
        if !File.exists?(./data1.vdi)
          vb.customize ['createhd', '--filename', data1, '--variant', 'Fixed', '--size', 10 * 1024]
        end
        vb.customize ['storageattach', :id, '--storagectl', 'SATA Controller', '--port', 1, '--device', 0, '--type', 'hdd', '--medium', data1]
        if !File.exists?(./data2.vdi)
          vb.customize ['createhd', '--filename', data2, '--variant', 'Fixed', '--size', 10 * 1024]
        end
        vb.customize ['storageattach', :id, '--storagectl', 'SATA Controller', '--port', 2, '--device', 0, '--type', 'hdd', '--medium', data2]
     end
  end
end

Start all the VMs
$ vagrant up


Shutdown all the VMs
$ vagrant halt

Show information about Vagrant environments.
$ vagrant global-status

Start specific VM
$ vagrant up <name>

Shudown specific VM
$ vagrant halt <name>

Login to specific VM
$ vagrant ssh <name>

Destroy VM
$ vagrant destroy <id>

Saturday, January 3, 2015

Creating CentOS 7 Vagrant Base Box

Vagrant is a wrapper around virtualization software such as virtualbox, VMware or KVM. In development environment, you can quickly build virtual machines by configuring CPUs, memory, network, and storage.

Git provides a number of UNIX command line tools along with access to Git.
Download and install git from http://git-scm.com/download/win

Download and install Virtualbox from http://download.virtualbox.org/virtualbox/4.3.26/VirtualBox-4.3.26-98988-Win.exe

Download and install vagrant from https://dl.bintray.com/mitchellh/vagrant/vagrant_1.7.2.msi

Download CentOS 7 Minimal ISO at http://isoredirect.centos.org/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1503-01.iso

Create Virtual Machine

In Oracle VM VirtualBox Manager, click New
Name: centos7-vagrant
Type: Linux
Version: Red Hat (64 bit)

Memory size: 2048 MB
Hard drive: Create a virtual hard drive now
Hard drive file type: VMDK (Virtual Machine Disk)
Storage on physical hard drive: Dynamically allocated
File location and size: 30 GB
In Oracle VM VirtualBox Manager, select centos7-vagrant  and click Settings
General -> Advanced --> Shared Clipboard: Bidirectional
Storage --> Select Controller: IDE and Click Add CD/DVD device icon --> Choose Disk --> Select CentOS-7-x86_64-Minimal-1503-01.iso
Network --> Adapter 1 --> Check Enable Network Adapter --> Attached to: Bridged Adapter

Start the VM
Select Install CentOS 7
English (United States) and Click Continue
To switch from VM to laptop and VM, click Right Ctrl
Click INSTALLATION DESTINATION, select ATA VBOX HARDDISK 30 GiB, click Done
Click Begin Installation
Click ROOT PASSWORD, enter Root Password as vagrant, click Done
Click Reboot
In Settings window, highlight CentOS-7-x86_64-Minimal-1503-01.iso and click Remove floppy icon at bottom.

Create user vagrant and set password

Login as root
# useradd -c "Vagrant Admin" -G wheel -s /bin/bash vagrant
Set password as vagrant
# passwd vagrant

Modify Base Repository

# vi /etc/yum.repos.d/CentOS-Base.repo
Replace $releasever as 7 and $basearch as x86_64
[centosplus] 
enabled=1

Enable the network interface to activate at boot time and get dynamic IP

# vi /etc/sysconfig/network-scripts/ifcfg-enp0s3
TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
NAME=enp0s3
DEVICE=enp0s3
ONBOOT=yes

# ifup enp0s3

Disable SELINUX

# vi /etc/selinux/config
SELINUX=disabled

Update the sudoes file

# visudo
# Defaults    !visiblepw
Defaults:vagrant    !requiretty

Defaults       env_keep = "SSH_AUTH_SOCK"
Comment the below line
# %wheel       ALL=(ALL)            ALL
Uncomment the below line
%wheel         ALL=(ALL)               NOPASSWD:  ALL

Update the OpenSSH Configuration

# vi /etc/ssh/sshd_config
Port 22
PubkeyAuthentication yes
PermitEmptyPasswords no
PasswordAuthentication yes

Update packages and start ntpd

# yum -y udpate
# yum -y install ntp wget net-tools
# systemctl start ntpd.service
# systemctl enable ntpd.service
# systemctl is-enabled ntpd.service
# shutdown -r now

Add vagrant insecure key

Login as vagrant in VM or SSH into VM from Git Bash.
$ mkdir -p /home/vagrant/.ssh
$ chmod 0700 /home/vagrant/.ssh
$ wget --no-check-certificate https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub -O /home/vagrant/.ssh/authorized_keys
$ chmod 0600 /home/vagrant/.ssh/authorized_keys
$ chown -R vagrant:vagrant /home/vagrant/.ssh
$ exit

Installing Guest Tools

Login as root
Devices --> Insert Guest Addtions CD image...
# mount /dev/cdrom /mnt
# yum -y bzip2 gcc make kernel-devel
# yum groupinstall "X Window System"
# yum clean all
# /mnt/VBoxLinuxAdditions.run
# umount /mnt
# shutdown -h now

In Settings window, highlight VBoxGuestAdditions.iso and click Remove floppy icon at bottom.

Export and Compress package

Open Git Bash
$ mkdir vagrant
$ cd vagrant
$ vagrant package --output centos-7.1.box --base centos7-vagrant

This base box is uploaded at https://atlas.hashicorp.com/anandbitra/boxes/centos-7.1/versions/1.0.0/providers/virtualbox.box

Add vagrant base box

Open Git Bash
$ vagrant box add anandbitra/centos-7.1 centos-7.1.box

List vagrant boxes

Open Git Bash
$ vagrant box list

Remove vagrant base box

Open Git Bash
$ vagrant box remove anandbitra/centos-7.1