Vagrant

deploy in virtual box

ubuntu 16.04

mkdir test
cd test
vagrant init ubuntu/xenial64
vagrant up --provider virtualbox

ubuntu 14.04

#only one time
vagrant box add ubuntu14.04 https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box

#everytime
mkdir test
cd test
vagrant init ubuntu14.04 
vagrant up

ubuntu 12.04

mkdir test
cd test
# download the configuration file 
vagrant init precise64 http://files.vagrantup.com/precise64.box
vagrant up
# the first time will be necessary download the operative system of the machines

# to login inside
vagrant ssh 

# to destroy the environment
vagrant destroy

# see the status
vagrant status

reload the machine

vagrant reload

halts the machine, and then starts the machine again with the new configuration. It skips the initial step to clone the box, since the machine is already created.

take at look inside the first Vagrantfile

cat Vagrantfile | grep -v "#"

VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "precise64"
  config.vm.box_url = "http://files.vagrantup.com/precise64.box"
end

change network configuration

https://docs.vagrantup.com/v2/networking/public_network.html

Vagrant.configure("2") do |config|
  config.vm.network "public_network"
end

shared folder

basic configuration
Vagrant::Config.run do |config|
# ...
  config.vm.share_folder "v-root", "/foo", "."
end
  • "v-root" : is the name of the default shared folder , you need to put to override the configuration
  • "/foo" : destination folder on the virtual machine
  • "." : is the directory where take the source
another directory
Vagrant::Config.run do |config|
  # ...
  config.vm.share_folder "data", "/data", "./data"
end
note on the shared folder

Shared folders incur a heavy performance penalty within the virtual machine when there is heavy I/O, so they should only be used for source files. Any compilation step, database files, and so on should be done outside the shared folder filesystem inside the guest filesystem itself.

networking

forward port

Vagrant.configure("2") do |config|
  config.vm.network "forwarded_port", guest: 80, host: 8080
end

forward port 80 on the guest to port 8080 on the host. This means from my machine in my browser i write http://localhost:8080 and I will connect with the web server (for example) listen on 80 of the virtual machine.

states

suspend
$ vagrant suspend
[default] Saving VM state and suspending execution...
$ vagrant status
Current machine states:
default
saved (virtualbox)

Suspending the guest machine will save the current running state of the machine and
stop it. The machine can then later be resumed from this exact suspended state.
With suspending, it is more like freezing time

to resume

vagrant up 
#or 
vagrant resume.

halt

$ vagrant halt
[default] Attempting graceful shutdown of VM...
$ vagrant status
Current machine states:
default
poweroff (virtualbox)

Halting the guest machine will shut it down, like a normal computer. The machine can
then later be resumed via a normal boot up process, as if hitting the power button
This allows the guest to execute a proper shutdown
sequence, cleaning up all resources and safely shutting down.
If Vagrant is unable to gracefully shut down the machine, it will forcefully shut it down.

to resume

vagrant up

destroy

$ vagrant destroy
Are you sure you want to destroy the 'default' VM? [y/N] y
[default] Forcing shutdown of VM...
[default] Destroying VM and associated drives...
Working with the Vagrant Machine
|
31
$ vagrant status
Current machine states:
default
not created (virtualbox)

if you don't wont ask

vagrant destroy --force

deploy in ec2

First Run

source guide http://www.iheavy.com/2014/01/16/how-to-deploy-on-amazon-ec2-with-vagrant/

code tested ubuntu desktop 12.04

full install in ubuntu 14.04 server

remove all the previous versions 
download the last package from here http://www.vagrantup.com/downloads.html
dpkg -i vagrant*.deb
apt-get install vagrant
apt-get install build-essential
vagrant plugin install vagrant-aws
install
sudo vagrant plugin install vagrant-aws

we need to do like sudo because it needs to install gems
configure

install dummy box

vagrant box add dummy https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box

create the working directory

 mkdir testaws
$ cd testaws
$ vagrant init

this will create a Vagrantfile where put the info to edit

configure the bash variable
at the end of ~/.bashrc add

export AWS_ACCESS_KEY=AKXXXXXXXXXXXXXX
export AWS_SECRET_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

and after
source ~/.bashrc

it is convenient delete the content of the vagrant file and replace with this

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu_aws"
  config.vm.box_url = "https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box"
  config.vm.provider :aws do |aws, override|

    aws.keypair_name = "namekey"
    override.ssh.private_key_path = "/home/giuseppe/pem/namekey.pem"
    override.ssh.username = "ubuntu"

    aws.block_device_mapping = [{ 'DeviceName' => '/dev/sda1', 'Ebs.VolumeSize' => 100 }]

    aws.instance_type = "t1.micro"
    aws.region = "us-west-2"
    aws.ami = "ami-e32556d3"

end
  • you must create before the keypair and download the key
  • you need also to choose a region and and choose an aws.ami for ubuntu this is a very good locator http://cloud-images.ubuntu.com/locator/ec2/ if you want use t1.micro instance type must be ebs
  • this config file assign the default security group is in this security group there is not the ssh from your location the state of creation will be always (or you can specify in the options to use a private ip that can be in the security group of the vpc but I have not yet tested this)
==> default: Waiting for SSH to become available...
deploy the machine in amazon
vagrant up --provider=aws

Bringing machine 'default' up with 'aws' provider...
[fog][WARNING] Unable to load the 'unf' gem. Your AWS strings may not be properly encoded.
==> default: HandleBoxUrl middleware is deprecated. Use HandleBox instead.
==> default: This is a bug with the provider. Please contact the creator
==> default: of the provider you use to fix this.
==> default: Warning! The AWS provider doesn't support any of the Vagrant
==> default: high-level network configurations (`config.vm.network`). They
==> default: will be silently ignored.
==> default: Launching an instance with the following settings...
==> default:  -- Type: t1.micro
==> default:  -- AMI: ami-e32556d3
==> default:  -- Region: us-west-2
==> default:  -- Keypair: namekey
==> default:  -- Block Device Mapping: []
==> default:  -- Terminate On Shutdown: false
==> default:  -- Monitoring: false
==> default:  -- EBS optimized: false
==> default: Waiting for instance to become "ready"...
==> default: Waiting for SSH to become available...
==> default: Machine is booted and ready for use!
==> default: Rsyncing folder: /home/giuseppe/vagrant/testaws/ => /vagrant
login
vagrant ssh

advanced use

real parameters

The parameter of configuration can be read here https://github.com/mitchellh/vagrant-aws/blob/master/README.md the official documents but to read the real parameters and field available in your config take a look to this file

~/.vagrant.d/gems/gems/vagrant-aws-0.4.1/lib/vagrant-aws/config.rb

some useful code

for add tags and for vpc

    aws.tags = {
     'Name' => 'Test Vagrant machine',
     'type' => 'software',
    }

    ## vpc configuration ###
    aws.elastic_ip = true
    aws.subnet_id = "subnet-123456a89" 
    aws.security_groups = "sg-1a23d56f"

it is not necessary the vpc id because there is the subnet_id , of course you need to setup the subnet and security group before.
Vagrant try always to access using the public ip so the security group rule must be open for my location

deploy in rackspace

A simple VagrantFile

machinename="mymachinename"
usernameinrackspace = "mypersonaleusername"
apikeyinrackspace = "******"

# Ubuntu 14.04 LTS (Trusty Tahr)
imagechoosen = "e19a734c-c7e6-443a-830c-242209c4d65d"

#2GB Standard Instance
flavorchoosen = "4"

# 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.box = "dummy"    
  config.vm.hostname = machinename
  config.ssh.port ="22"

  config.ssh.private_key_path = "~/.ssh/id_rsa"

  config.vm.provider :rackspace do |rs|
    rs.username        = usernameinrackspace
    rs.api_key         = apikeyinrackspace
    rs.flavor          = flavorchoosen
    rs.image           = imagechoosen
    rs.public_key_path = "~/.ssh/id_rsa.pub"
    rs.rackspace_region = :ord
    rs.server_name = machinename
  end
end

To execute the command below you need before , to setup the credential in the VagrantFile

You can obtain a list of image available with this command

giuseppe@peppelaptop> vagrant rackspace images list
==> default: Image ID                             Image Name
==> default: 9c6765df-7fe4-4fd9-8308-a78faf399651 Ubuntu 10.04 LTS (Lucid Lynx)
==> default: 2f85a777-9ffd-4b49-a60e-1155ceb93a5e Ubuntu 12.04 LTS (Precise Pangolin)
==> default: ea322e55-0a03-48d6-b812-d9cf77fd05e7 Ubuntu 12.04 LTS (Precise Pangolin) (PVHVM)
==> default: e19a734c-c7e6-443a-830c-242209c4d65d Ubuntu 14.04 LTS (Trusty Tahr)
==> default: cc6e0096-84f9-4beb-a21e-d80a11a769d8 Ubuntu 14.04 LTS (Trusty Tahr) (PVHVM)

To obtain a list of Flavor available

giuseppe@peppelaptop> vagrant rackspace flavors
==> default: Flavor ID                            Flavor Name
==> default: 2                                    512MB Standard Instance
==> default: 3                                    1GB Standard Instance
==> default: 4                                    2GB Standard Instance
==> default: 5                                    4GB Standard Instance
==> default: 6                                    8GB Standard Instance
==> default: 7                                    15GB Standard Instance
==> default: 8                                    30GB Standard Instance
==> default: performance1-1                       1 GB Performance
==> default: performance1-2                       2 GB Performance
==> default: performance1-4                       4 GB Performance
==> default: performance1-8                       8 GB Performance
==> default: performance2-120                     120 GB Performance
==> default: performance2-15                      15 GB Performance
==> default: performance2-30                      30 GB Performance
==> default: performance2-60                      60 GB Performance
==> default: performance2-90                      90 GB Performance

after that you can run

vagrant up --provider=rackspace

Troubleshouting

if you have ssl problems probably you have a proxy in the middle.

SSL certificate problem: self signed certificate in certificate chain
More details here: http://curl.haxx.se/docs/sslcerts.html

curl performs SSL certificate verification by default, using a "bundle"
 of Certificate Authority (CA) public keys (CA certs). If the default
 bundle file isn't adequate, you can specify an alternate file
 using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
 the bundle, the certificate verification probably failed due to a
 problem with the certificate (it might be expired, or the name might
 not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
 the -k (or --insecure) option.

you need to run the command with the insecure option
vagrant box add ubuntu14.04 https://cloud-images.ubuntu.com/vagrant
/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box --insecure
Salvo diversa indicazione, il contenuto di questa pagina è sotto licenza Creative Commons Attribution-ShareAlike 3.0 License