Pages

Saturday, December 26, 2015

Vagrant – Provisioning

Provisioning is some thing like providing. In Infrastructure ,a provisioning is some thing like preparing the Server or the Physical machine with all necessary configuration and installations before passing it to the Users. Vagrant provides a good support for the Provisioning the virtuals while configuration. In this article we will see how we can provision a virtual with necessary configuration.

We will be using a Sample Shell Script which will run after the creation of the virtual is done. The creation and running the script are done in sequence. The commands that use inside the script will be run inside the virtual once the creation is complete.

1) The Shell Script -  installPuppet.sh
This is the basic shell script which will install Puppet packages into the newly created virtuals. The script looks some thing like this,

[root@vx111a CentOsVagrant]# cat installPuppet.sh
#!/usr/bin/env bash
# This bootstraps Puppet on CentOS 6.x
# It has been tested on CentOS 6.3 64bit

set -e

REPO_URL="https://yum.puppetlabs.com/el/6/products/x86_64/puppetlabs-release-6-11.noarch.rpm"

if [ "$EUID" -ne "0" ]; then
  echo "This script must be run as root." >&2
  exit 1
fi

if which puppet > /dev/null 2>&1; then
  echo "Puppet is already installed"
  exit 0
fi

# Install puppet labs repo
echo "Configuring PuppetLabs repo..."
repo_path=$(mktemp)
wget --output-document=${repo_path} ${REPO_URL} 2>/dev/null
rpm -i ${repo_path} >/dev/null

# Install Puppet...
echo "Installing puppet"
yum install -y puppet > /dev/null

echo "Puppet installed!"

2) Now create the Vagrant file in a directory using the “vagrant init” command. Once file is created, change the contents like below,

[root@vx111a CentOsVagrant]# cat Vagrantfile
Vagrant.configure(2) do |config|
  config.vm.box = "CentOS"
  config.vm.box_url = "https://saleseng.s3.amazonaws.com/boxfiles/CentOS-6.3-x86_64-minimal.box"
  config.vm.host_name = "dev.puppetlabs.vm"
  config.vm.network "private_network", type: "dhcp"
  config.vm.network "forwarded_port", guest: 80, host: 8084
  config.vm.provision :shell, :path => "installPuppet.sh"

config.vm.provider :virtualbox do |vb|
     vb.name = "barhost"
end
end

If you check the above configuration, we see that we added some more parameters.

1) Vm Name

config.vm.provider :virtualbox do |vb|
     vb.name = "barhost"
     vb.cpus = 2
     vb.customize [“modifyvm”,:id,”- -memory”,”1024”
end

Using the above one, we will set the name of the virtual configured to “barhost”. We are also setting the number of Cpu’s that virtual should get by default.

A default memory of 1GB is allocated to the new Virtaul that is being created.

2) Port forwarding - if you have a web server inside the virtual machine listening on port 80, then it can be accessed from the host machine on port 8931, for example by typing localhost:8931 in your browser. This is some thing like Port ford warding available in Virtual and this can be obtained by adding this line

config.vm.network :forwarded_port, guest: 80, host: 8931, auto_correct: true

The auto_correct option set to true tells Vagrant to handle port collisions automatically. There are cases when you have multiple vagrant boxes running with the same port open on the host machine, in these cases Vagrant will automatically resolve the conflicting ports.

3) Public Network – Network is one area where Vagrant is very simple to configure. Vagrant does the network configuration by means of high level configuration settings. Vagrant does provide 2 types of networks

Private network means that each machine will get private ip address from the private address space and machines will be able to communicate with one to another via these addresses

public network allow access to the machine where as private networks will never allow the general public.

In the above configuration file we have defined a private network and dhcp is used to assign an IP address from the private network space.

We can also use an IP address to configure the network using public as,
config.vm.network "public_network", ip: "172.16.200.192"
 
4) The last line is the provisioning script location and telling the Vagrant to use the script to provision the virtual once configured.

config.vm.provision :shell, :path => "installPuppet.sh"

The script is located in the same directory and will be used to configure the virtuals with the steps that we defined in the script.

5) Now run the “vagrant up” command ,

[root@vx111a CentOsVagrant]# vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'CentOS'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: barhost
==> default: Fixed port collision for 22 => 2222. Now on port 2200.
==> default: Clearing any previously set network interfaces...
==> default: Available bridged network interfaces:
1) em1
2) vmnet8
3) virbr0
4) vmnet1
==> default: When choosing an interface, it is usually the one that is
==> default: being used to connect to the internet.
    default: Which interface should the network bridge to? 1
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: bridged
==> default: Forwarding ports...
    default: 80 => 8084 (adapter 1)
    default: 22 => 2200 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2200
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 4.1.18
    default: VirtualBox Version: 5.0
==> default: Setting hostname...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => /work/CentOsVagrant
==> default: Running provisioner: shell...
    default: Running: /tmp/vagrant-shell20151222-18898-u8qsod.sh
==> default: Puppet installed

Now  if we check the output , we can see the provisioning script is ran once the virtual is configured. The message “puppet installed” can be seen at last.


6) We can see the virtual with the name “barhost” available in Oracle virtual box


No comments :

Post a Comment