profile for Gajendra D Ambi on Stack Exchange, a network of free, community-driven Q&A sites

Saturday, March 31, 2018

Deploying multi node VMs with vagrant

So we all know how to deploy/provision a single node VM from here.
What if you want to deploy more than one VM? A 3 tier web app may be! for your development team or you have nothing else to do and you are bored.


My setup:

windows 10
babun shell emulator
vagrant 2.x

Goal:

Deploy a 3 tier web app

Process:


1
2
3
4
5
{ vagrant }  » mkdir 3tier
{ vagrant }  » cd 3tier/
{ 3tier }  » ls
{ 3tier }  » touch Vagrantfile
{ 3tier }  » cat Vagrantfile
line
1. created a project directory
2. got inside the directory
4. created an empty Vagrantfile
5. listing the contents of the Vagrantfile shows nothing which means it is empty
Using the nano or vi editor I will add the following
Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/trusty64"
end

from the following link
https://app.vagrantup.com/ubuntu/boxes/trusty64 to the Vagrantfile
because that is what we need to deploy a bare minimum vagrant VM.
Now let us provision the VM.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
{ 3tier }  » cat Vagrantfile
# my 3 tier web app
Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/trusty64"
end
###

{ 3tier }  » vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu/trusty64'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'ubuntu/trusty64' is up to date...
==> default: Setting the name of the VM: 3tier_default_1522504177366_47355
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    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.3.36
    default: VirtualBox Version: 5.2
==> default: Mounting shared folders...
    default: /vagrant => D:/vagrant/3tier
{ 3tier }  » vagrant ssh
Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 3.13.0-143-generic x86_64)

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

  System information as of Sat Mar 31 13:50:11 UTC 2018

  System load:  0.33              Processes:           82
  Usage of /:   3.6% of 39.34GB   Users logged in:     0
  Memory usage: 25%               IP address for eth0: 10.0.2.15
  Swap usage:   0%

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

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

0 packages can be updated.
0 updates are security updates.

New release '16.04.4 LTS' available.
Run 'do-release-upgrade' to upgrade to it.


vagrant@vagrant-ubuntu-trusty-64:~$ hostname
vagrant-ubuntu-trusty-64
vagrant@vagrant-ubuntu-trusty-64:~$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:bd:94:b9 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:febd:94b9/64 scope link
       valid_lft forever preferred_lft forever
vagrant@vagrant-ubuntu-trusty-64:~$

line
3-5 contents of the Vagrantfile
8. provision the vagrant VM which is mentioned int he Vagrantfile
9. virtualbox is the default virtualization provider unless mentioned otherwise in the vagrantfile
45. vagrant ssh gets you connected to the newly provisioned vagrant VM since there is only one now.
70. hostname of the vagrant vm
72-84. the new VM's networking information.

Now let us destroy this VM and deploy 3 of these.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
vagrant@vagrant-ubuntu-trusty-64:~$ exit
logout
Connection to 127.0.0.1 closed.
{ 3tier }  » ls
Vagrantfile
{ 3tier }  » vagrant status
Current machine states:

default                   running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.
{ 3tier }  » vagrant destroy
    default: Are you sure you want to destroy the 'default' VM? [y/N] y
==> default: Forcing shutdown of VM...
==> default: Destroying VM and associated drives...
{ 3tier }  »
line
1-3. exiting out of the vm
6. checking the vagrant vm status
15. destroying the current vagrant VM.

Now I clear out the Vagrantfile and populate it with the following (without the line numbers)

1
2
3
4
5
6
7
8
9
Vagrant.configure("2") do |config|

  config.vm.define "web" do |web|    
    web.vm.box = "ubuntu/trusty64"
    web.vm.hostname = "web.local"
    web.vm.network "private_network", ip: "192.168.9.2"
  end
  
end

line
1. Start of vagrant file configuration
9. End of vagrant file configuration

3-7. configuration for the VM named web
4. box name
5. hostname for the web vm
6. networking configuration for the web vm

Let us deploy and check it out.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
{ 3tier }  » ls
Vagrantfile
{ 3tier }  » cat Vagrantfile
Vagrant.configure("2") do |config|

  config.vm.define "web" do |web|
    web.vm.box = "ubuntu/trusty64"
    web.vm.hostname = "web.local"
    web.vm.network "private_network", ip: "192.168.9.2"
  end

end{ 3tier }  » vagrant up
Bringing machine 'web' up with 'virtualbox' provider...
==> web: Importing base box 'ubuntu/trusty64'...
==> web: Matching MAC address for NAT networking...
==> web: Checking if box 'ubuntu/trusty64' is up to date...
==> web: Setting the name of the VM: 3tier_web_1522508097183_4245
==> web: Clearing any previously set forwarded ports...
==> web: Clearing any previously set network interfaces...
==> web: Preparing network interfaces based on configuration...
    web: Adapter 1: nat
    web: Adapter 2: hostonly
==> web: Forwarding ports...
    web: 22 (guest) => 2222 (host) (adapter 1)
==> web: Booting VM...
==> web: Waiting for machine to boot. This may take a few minutes...
    web: SSH address: 127.0.0.1:2222
    web: SSH username: vagrant
    web: SSH auth method: private key
    web:
    web: Vagrant insecure key detected. Vagrant will automatically replace
    web: this with a newly generated keypair for better security.
    web:
    web: Inserting generated public key within guest...
    web: Removing insecure key from the guest if it's present...
    web: Key inserted! Disconnecting and reconnecting using new SSH key...
==> web: Machine booted and ready!
==> web: Checking for guest additions in VM...
    web: The guest additions on this VM do not match the installed version of
    web: VirtualBox! In most cases this is fine, but in rare cases it can
    web: prevent things such as shared folders from working properly. If you see
    web: shared folder errors, please make sure the guest additions within the
    web: virtual machine match the version of VirtualBox you have installed on
    web: your host and reload your VM.
    web:
    web: Guest Additions Version: 4.3.36
    web: VirtualBox Version: 5.2
==> web: Setting hostname...
==> web: Configuring and enabling network interfaces...
==> web: Mounting shared folders...
    web: /vagrant => D:/vagrant/3tier
{ 3tier }  » vagrant ssh
Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 3.13.0-143-generic x86_64)

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

  System information as of Sat Mar 31 14:55:30 UTC 2018

  System load:  0.46              Processes:           82
  Usage of /:   3.6% of 39.34GB   Users logged in:     0
  Memory usage: 25%               IP address for eth0: 10.0.2.15
  Swap usage:   0%

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

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

0 packages can be updated.
0 updates are security updates.

New release '16.04.4 LTS' available.
Run 'do-release-upgrade' to upgrade to it.


vagrant@web:~$ hostname
web
vagrant@web:~$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:bd:94:b9 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:febd:94b9/64 scope link
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:9f:45:5c brd ff:ff:ff:ff:ff:ff
    inet 192.168.9.2/24 brd 192.168.9.255 scope global eth1
       valid_lft forever preferred_lft forever
vagrant@web:~$

line
4-12. configuration of our Vagrantfile
12-51. provisioning of the vagrant box
52. ssh to our newly deployed vagrant box
77-78. checking the hostname of the vagrant box (web vm)
79-95. networking configuration of the web vagrant box.
94. the ip address that we specified in the vagrantfile is seen here.

Now let us do it for 3 VMs. Web, app and db vm. Here how my new VM looks.

Vagrant.configure("2") do |config|

  config.vm.define "web" do |web|    
    web.vm.box = "ubuntu/trusty64"
    web.vm.hostname = "web.local"
    web.vm.network "private_network", ip: "192.168.9.2"
  end
  
  config.vm.define "app" do |app|    
    app.vm.box = "ubuntu/trusty64"
    app.vm.hostname = "app.local"
    app.vm.network "private_network", ip: "192.168.9.3"
  end
  
  config.vm.define "db" do |db|    
    db.vm.box = "ubuntu/trusty64"
    db.vm.hostname = "db.local"
    db.vm.network "private_network", ip: "192.168.9.4"
  end
  
end

Now I ran the Vagrant up and it deployed all 3 VMs. Below is me checking them out.

{ 3tier }  » vagrant status
Current machine states:

web                       running (virtualbox)
app                       running (virtualbox)
db                        running (virtualbox)

This environment represents multiple VMs. The VMs are all listed
above with their current state. For more information about a specific
VM, run `vagrant status NAME`.
{ 3tier }  » ssh vagrant

{ 3tier }  » vagrant ssh
This command requires a specific VM name to target in a multi-VM environment.
{ 3tier }  » vagrant ssh web
Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 3.13.0-143-generic x86_64)

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

  System information as of Sat Mar 31 15:10:01 UTC 2018

  System load:  0.56              Processes:           84
  Usage of /:   3.6% of 39.34GB   Users logged in:     0
  Memory usage: 25%               IP address for eth0: 10.0.2.15
  Swap usage:   0%

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

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

0 packages can be updated.
0 updates are security updates.

New release '16.04.4 LTS' available.
Run 'do-release-upgrade' to upgrade to it.


vagrant@web:~$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:bd:94:b9 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:febd:94b9/64 scope link
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:8d:f5:db brd ff:ff:ff:ff:ff:ff
    inet 192.168.9.2/24 brd 192.168.9.255 scope global eth1
       valid_lft forever preferred_lft forever
vagrant@web:~$ exit
logout
Connection to 127.0.0.1 closed.
{ 3tier }  » vagrant ssh app
Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 3.13.0-143-generic x86_64)

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

  System information as of Sat Mar 31 15:10:52 UTC 2018

  System load:  0.46              Processes:           80
  Usage of /:   3.6% of 39.34GB   Users logged in:     0
  Memory usage: 25%               IP address for eth0: 10.0.2.15
  Swap usage:   0%

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

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

0 packages can be updated.
0 updates are security updates.

New release '16.04.4 LTS' available.
Run 'do-release-upgrade' to upgrade to it.


vagrant@app:~$ exit
logout
Connection to 127.0.0.1 closed.
{ 3tier }  » vagrant ssh db
Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 3.13.0-143-generic x86_64)

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

  System information as of Sat Mar 31 15:11:44 UTC 2018

  System load:  0.52              Processes:           84
  Usage of /:   3.6% of 39.34GB   Users logged in:     0
  Memory usage: 25%               IP address for eth0: 10.0.2.15
  Swap usage:   0%

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

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

0 packages can be updated.
0 updates are security updates.

New release '16.04.4 LTS' available.
Run 'do-release-upgrade' to upgrade to it.


vagrant@db:~$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:bd:94:b9 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:febd:94b9/64 scope link
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:30:09:9e brd ff:ff:ff:ff:ff:ff
    inet 192.168.9.4/24 brd 192.168.9.255 scope global eth1
       valid_lft forever preferred_lft forever
vagrant@db:~$ hostname
db



No comments:

Post a Comment