How to use Vagrant to deploy containers on vSphere with VMware Photon!

You already have a large vSphere environment and your developers are quickly eating up space on their Laptops or worse, they are going out to a Public cloud on their corporate card. Why not enable them to use their favorite tools on vSphere? This post is not about using Vagrant or Vagrant on vSphere. This post is primarily focused on showcasing the power of VMware Photon as a Container host for agile application development. With that said most of this post should also work with other flavors or Linux that Vagrant supports but no guarantees there.

Download and install the latest version of Vagrant. I am using Vagrant on CentOS 6.6 x64 . This will become my developer machine.

Next, we need to install and configure the Vagrant vSphere Provider by typing vagrant plugin install vagrant-vsphere and press enter see link for more info on this plugin

vagrant plugin install vagrant-vsphere

You will also need a Photon template available in vCenter before we continue on the Developer Machine. See the following guide to create your Photon Template in vSphere.

Boot up the template and login as root user and create a vagrant account by typing useradd vagrant

Now set the password of the vagrant account by typing passwd vagrant

I set my password to vagrant

Now type visudo then press i and scroll down and add the following line then press esc key then hold shift and press ZZ to save it

vagrant ALL=(ALL) NOPASSWD:ALL

Now we need to add the Vagrant key to the user account

mkdir -p /home/vagrant/.ssh

Now get the keys using the following

Note: It is one single line, make sure not to have a hard return when you copy and paste or this won’t work

wget --no-check-certificate https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub -O /home/vagrant/.ssh/authorized_keys

Now lets reset permission on the keys, copy and paste each line below the screenshot and press enter.

Be sure to enter each line and press enter separately

chmod 0700 /home/vagrant/.ssh
chmod 0600 /home/vagrant/.ssh/authorized_keys
chown -R vagrant /home/vagrant/.ssh

Type shutdown now and press enter to shutdown the template

shutdown now

Login to vCenter and convert the Photon VM to template

Back on the CentOS instance where we installed Vagrant earlier we need to create a dummy box.

Find where the example box is by typing the following

find / -name example_box

Change to the directory listed

Now you can create the dummy box with the following command

tar cvzf dummy.box ./metadata.json

Next we will create a new folder under /root for storing the box file we created and other vagrant configuration files

mkdir -p ~/vagrant-vms/example_box

Now move the dummy box with the following command

mv dummy.box ~/vagrant-vms/example_box

Now change to the new directory you just created

cd ~/vagrant-vms/

Now we need to create a vagrant file that vagrant will use to start the Photon VM on vSphere

touch Vagrantfile

Edit the VagrantFile with your vCenter Settings using the vi editor

vi Vagrantfile

Click i to change to insert mode and past the following into the file, then pres ESC key and then Hold Shift and hit ZZ to save it

Make sure to change the settings for your vSphere environment

Vagrant.configure("2") do |config|
 config.vm.box = 'vsphere'
 config.vm.box_url = './example_box/dummy.box'
 config.vm.provider :vsphere do |vsphere|
 # The host we're going to connect to
 vsphere.host = 'vcsa-01a.corp.local'
 # The host for the new VM
 vsphere.compute_resource_name = 'Cluster Site A'
 # The resource pool for the new VM
 vsphere.resource_pool_name = 'VagrantVMs'
 # The template we're going to clone
 vsphere.template_name = 'Photon'
 # The name of the new machine
 vsphere.name = 'Photon-Vagrant'
 # vSphere login
 vsphere.user = 'administrator@corp.local' 
 # vSphere password
 vsphere.password = 'VMware1!'
 # If you don't have SSL configured correctly, set this to 'true'
 vsphere.insecure = true
 end
end

Install the Vagrant Photon Guest Plugin by typing vagrant plugin install vagrant-guests-photon and press enter

vagrant plugin install vagrant-guests-photon

Now type vagrant up –provider=vsphere and press enter

Note you will get an rsync error, just ignore this.

Now type vagrant ssh to connect to the new box

vagrant ssh

Now let’s pull and start a container by typing sudo docker run -d -p 80:80 vmwarecna/nginx

sudo docker run -d -p 80:80 vmwarecna/nginx

now type ifconfig to get the IP address of the Photon Box

Open that address in a web browser

Enjoy making your developers happy and saving a ton of money that would have been spent elsewhere!!

Remember sharing is caring!

3 Replies to “How to use Vagrant to deploy containers on vSphere with VMware Photon!”

  1. Didrou run into any issues with nokogiri? I get the following issue running “vagrant up –provider=vsphere”

    PS C:\HashiCorp\Vagrant\vagrant-vms> vagrant up –provider=vsphere
    C:/HashiCorp/Vagrant/embedded/gems/gems/nokogiri-1.6.3.1-x86-mingw32/lib/nokogiri.rb:29:in `require’: cannot load such file — nokogiri/nokogiri (LoadError)
    from C:/HashiCorp/Vagrant/embedded/gems/gems/nokogiri-1.6.3.1-x86-mingw32/lib/nokogiri.rb:29:in `rescue in ‘
    from C:/HashiCorp/Vagrant/embedded/gems/gems/nokogiri-1.6.3.1-x86-mingw32/lib/nokogiri.rb:25:in `’
    from C:/HashiCorp/Vagrant/home/gems/gems/rbvmomi-1.8.2/lib/rbvmomi/trivial_soap.rb:4:in `require’
    from C:/HashiCorp/Vagrant/home/gems/gems/rbvmomi-1.8.2/lib/rbvmomi/trivial_soap.rb:4:in `’
    from C:/HashiCorp/Vagrant/home/gems/gems/rbvmomi-1.8.2/lib/rbvmomi/connection.rb:4:in `require’
    from C:/HashiCorp/Vagrant/home/gems/gems/rbvmomi-1.8.2/lib/rbvmomi/connection.rb:4:in `’
    from C:/HashiCorp/Vagrant/home/gems/gems/rbvmomi-1.8.2/lib/rbvmomi.rb:11:in `require’
    from C:/HashiCorp/Vagrant/home/gems/gems/rbvmomi-1.8.2/lib/rbvmomi.rb:11:in `’
    from C:/HashiCorp/Vagrant/home/gems/gems/vagrant-vsphere-1.6.0/lib/vSphere/action/connect_vsphere.rb:1:in `require’
    from C:/HashiCorp/Vagrant/home/gems/gems/vagrant-vsphere-1.6.0/lib/vSphere/action/connect_vsphere.rb:1:in `’
    from C:/HashiCorp/Vagrant/home/gems/gems/vagrant-vsphere-1.6.0/lib/vSphere/action.rb:163:in `block in action_get_state’
    from C:/HashiCorp/Vagrant/home/gems/gems/vagrant-vsphere-1.6.0/lib/vSphere/action.rb:160:in `tap’
    from C:/HashiCorp/Vagrant/home/gems/gems/vagrant-vsphere-1.6.0/lib/vSphere/action.rb:160:in `action_get_state’
    from C:/HashiCorp/Vagrant/home/gems/gems/vagrant-vsphere-1.6.0/lib/vSphere/provider.rb:12:in `action’
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/lib/vagrant/machine.rb:187:in `block in action’
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/lib/vagrant/environment.rb:561:in `lock’
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/lib/vagrant/machine.rb:185:in `call’
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/lib/vagrant/machine.rb:185:in `action’
    from C:/HashiCorp/Vagrant/home/gems/gems/vagrant-vsphere-1.6.0/lib/vSphere/provider.rb:22:in `state’
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/lib/vagrant/machine.rb:501:in `state’
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/lib/vagrant/machine.rb:144:in `initialize’
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/lib/vagrant/vagrantfile.rb:79:in `new’
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/lib/vagrant/vagrantfile.rb:79:in `machine’
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/lib/vagrant/environment.rb:663:in `machine’
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/lib/vagrant/plugin/v2/command.rb:177:in `block in with_target_vms’
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/lib/vagrant/plugin/v2/command.rb:201:in `call’
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/lib/vagrant/plugin/v2/command.rb:201:in `block in with_target_vms’
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/lib/vagrant/plugin/v2/command.rb:183:in `each’
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/lib/vagrant/plugin/v2/command.rb:183:in `with_target_vms’
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/plugins/commands/up/command.rb:89:in `block in execute’
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/lib/vagrant/environment.rb:278:in `block (2 levels) in batch’
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/lib/vagrant/environment.rb:276:in `tap’
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/lib/vagrant/environment.rb:276:in `block in batch’
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/lib/vagrant/environment.rb:275:in `synchronize’
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/lib/vagrant/environment.rb:275:in `batch’
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/plugins/commands/up/command.rb:88:in `execute’
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/lib/vagrant/cli.rb:42:in `execute’
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/lib/vagrant/environment.rb:302:in `cli’
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/bin/vagrant:174:in `’
    PS C:\HashiCorp\Vagrant\vagrant-vms>

    Any help would be appreciated, many thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.