Thursday, July 10, 2014

Vagrant basic plus multiple servers.

This article shows how to install ubuntu trusty server automatically on Virtual box by using "Vagrant" tool.(http://www.vagrantup.com/downloads.html)



First, we have to install virtual box(http://virtualbox.org) and vagrant.
I used mint Linux as a workstation. This distro is very gorgeous. Just personal taste.


 Vagrant version: (1.6.3).   Linux Mint 17 Qiana \n \l



oyj@oyj-ThinkPad-Edge-E535 ~ $ mkdir vt
oyj@oyj-ThinkPad-Edge-E535 ~ $ cd vt/
oyj@oyj-ThinkPad-Edge-E535 ~/vt $ ls
oyj@oyj-ThinkPad-Edge-E535 ~/vt $ vagrant init
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.


Find suitable image from below link. I shall use ubuntu server trusty.
Click popular link. There is ubuntu/trusty64 that is offcial image from Canonical.

https://vagrantcloud.com/discover/featured

Edit below line from "base" to "ubuntu/trusty64"


  config.vm.box = "ubuntu/trusty64"


oyj@oyj-ThinkPad-Edge-E535 ~/vt $ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu/trusty64'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: vt_default_1405043268291_38468
==> default: Fixed port collision for 22 => 2222. Now on port 2202.
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2202 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2202
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => /home/oyj/vt



Ubuntu server image shows up virtual box screen.


*Sshing to this server.


oyj@oyj-ThinkPad-Edge-E535 ~/vt $ vagrant ssh
Welcome to Ubuntu 14.04 LTS (GNU/Linux 3.13.0-24-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

 System information disabled due to load higher than 1.0

Last login: Wed Jul  9 04:13:03 2014 from 10.0.2.2





* Packaging.
Not to download image from "http://vagrantcloud.com" everytime that I want to install servers, I decided to package image.




oyj@oyj-ThinkPad-Edge-E535 ~/vt $ vagrant package
==> default: Attempting graceful shutdown of VM...
==> default: Clearing any previously set forwarded ports...
==> default: Exporting VM...
==> default: Compressing package to: /home/oyj/vt/package.box
oyj@oyj-ThinkPad-Edge-E535 ~/vt $ ls
Vagrantfile  package.box



* I want to make three servers for development. 1. web-apache, 2. tomcat7, 3. mysql server.
Here is Vagrantfile that I made. It is easy to understand. So I think there is no need to explain every lines. :)



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

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.define "apache" do |web|
    web.vm.box = "apache"
    web.vm.box_url = "file:///home/oyj/vt/package.box"
    web.vm.provision "shell", inline: "sudo apt-get update; sudo apt-get -y install apache2"
    web.vm.network "private_network", ip:"10.0.0.2"
    web.vm.hostname="apache"
  end  

  config.vm.define "tomcat7" do |was|
    was.vm.box = "tomcat7"
    was.vm.box_url = "file:///home/oyj/vt/package.box"
    was.vm.provision "shell", inline: "sudo apt-get update; sudo apt-get -y install tomcat7"
    was.vm.network "private_network", ip:"10.0.0.3"
    was.vm.hostname="tomcat7"
  end  
# To install mysql servers I used mysql_install.sh shell script
#
  config.vm.define "mysql" do |db|
    db.vm.box = "mysql"
    db.vm.box_url = "file:///home/oyj/vt/package.box"
    db.vm.provision "shell", :path =>  "mysql_install.sh"
    db.vm.network "private_network", ip:"10.0.0.4"
    db.vm.hostname="mysql"
  end  

end




* mysql_install.sh script
oyj@oyj-ThinkPad-Edge-E535 ~/vt $ cat mysql_install.sh
sudo debconf-set-selections <<< 'mysql-server-5.5 mysql-server/root_password password rootpass'
sudo debconf-set-selections <<< 'mysql-server-5.5 mysql-server/root_password_again password rootpass'
sudo apt-get update
sudo apt-get -y install mysql-server-5.5



* Now running vagrant up and connecting using ssh.

oyj@oyj-ThinkPad-Edge-E535 ~/vt $ vagrant up
Bringing machine 'apache' up with 'virtualbox' provider...
Bringing machine 'tomcat7' up with 'virtualbox' provider...
Bringing machine 'mysql' up with 'virtualbox' provider...
==> apache: Importing base box 'apache'...

...


* There is no problem :).

* sshing.
oyj@oyj-ThinkPad-Edge-E535 ~/vt $ vagrant ssh apache
Welcome to Ubuntu 14.04 LTS (GNU/Linux 3.13.0-24-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

  System information as of Thu Jul 10 22:27:13 EDT 2014

  System load:  0.82              Processes:           86
  Usage of /:   19.4% of 6.99GB   Users logged in:     0
  Memory usage: 29%               IP address for eth0: 10.0.2.15
  Swap usage:   0%

  Graph this data and manage this system at:
    https://landscape.canonical.com/

Last login: Wed Jul  9 04:13:03 2014 from 10.0.2.2


Same as mysql and tomcat7.



No comments:

Post a Comment