Postgres and devops: testing 9.3 with Vagrant and Puppet - part two

November 04, 2013

In a previous blog post, I have gone through the steps of setting up a basic CentOS 6.4 64bit Linux distribution using Vagrant on your Ubuntu 12.04 LTS Linux. We will continue digging in the devops culture/movement and finally setup PostgreSQL 9.3 using Puppet in a local virtual machine.

This is what we are going to do now, together:

  1. install some “cool tools” for Ruby
  2. install the puppetlabs/postgresql module for Puppet
  3. instruct Vagrant to use Puppet as provisioner
  4. create a very simple Puppet manifest
  5. start up our machine
  6. test PostgreSQL 9.3

Installing the required tools

Ruby Programming LanguageThe most complicated part is the installation of the requirements for our Puppet modules. You can do without them, but I strongly encourage you to use them (or at least evaluate them and if you have enough Ruby experience, you might come with a ponderate decision).
The main reason is that, for example, if you want to use Puppet 3, you need at least Ruby 1.9.3 – which is not yet diffused.

We will use two tools for this purpose:

I strongly suggest you read the documentation of both projects. I have been also suggested by Marco Nenciarini to look at rbenv-installer too for installing rbenv, but I have not tried it yet. Otherwise, just follow my instructions for a quick solution.

In your user’s home directory, type:


git clone http://github.com/sstephenson/rbenv.git ~/.rbenv

Then, make sure you add ~/.rbenv/bin to your PATH variable, by editing your ~/.profile file as follows:


# rbenv
if [ -d "$HOME/.rbenv" ] ; then
PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
fi

For the sake of simplicity, log again with your user and type:


git clone http://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build

In order to install Ruby 2.0 in your user space, you can now type:


rbenv install 2.0.0-p247

This will take a while. Then, all you need to do is set this version as default version for your user:


rbenv global 2.0.0-p247

This is how I very that version 2.0.0-p247 is that one I will use from now on:


$ rbenv version
2.0.0-p247 (set by /home/gabriele/.rbenv/version)

The next step is to install librarian-puppet and puppet. This can be easily done as follows:


gem install librarian-puppet puppet
rbenv rehash

Installing the Puppet module for PostgreSQL

Puppet LogoThe previous section terminates all the preparatory steps you need to take when working with Vagrant and Puppet in a very reusable and gentle (as in “not invasive”) way.

It is now time to start your new project, which will get you to successfully install a basic CentOS system with PostgreSQL 9.3 in it.

You can decide to reuse the project directory you have created in my previous blog post about setting up a basic CentOS 6.4 64bit Linux distribution using Vagrant.

However, for the sake of simplicity, in this article we will start from scratch again, with a new directory.
We will call it vagrant-pg93, but you can choose a different name.

Create the directory and then enter it:


mkdir vagrant-pg93
cd vagrant-pg93

After that, create a file called Puppetfile, containing:


forge "http://forge.puppetlabs.com"

mod 'puppetlabs/postgresql'

librarian-puppet will use the repository of modules provided by Puppetlabs (defined by the forge command) and will install the ‘puppetlabs/postgresql’ module.

In order to do that, you need to type the following command:


librarian-puppet install --verbose

This command will create a modules directory and install the postgresql module as well as its dependencies, like for example apt. This is the content of the modules directory:


$ ls modules/
apt		concat		firewall	postgresql	stdlib

Use Puppet as Provisioner for Vagrant

VagrantIt is now time to create your configuration file for Vagrant (the so called Vagrantfile). As outlined in the first part of the article, Vagrant gives you the possibility to generate a basic configuration file which you can then modify. Just issue:


vagrant init

We encourage you once again to take some time to read the configuration file content, as well as the documentation of Vagrant. The changes below allow you to:

  • instantiate a minimal CentOS 64 bit Linux system
  • create a private network and assign the 192.168.33.10 IP address to your new created server (your hosting machine will act as a gateway for that network)
  • select Puppet as provisioner tool, by specifying the modules directory (the one we just generated through librarian-puppet).

This is the content of the file – after having stripped empty lines and comments from it:


VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = 'centos6-64'
  config.vm.box_url = 'http://puppet-vagrant-boxes.puppetlabs.com/centos-64-x64-vbox4210.box'
  config.vm.network :private_network, ip: "192.168.33.10"
  config.vm.provision :puppet do |puppet|
    puppet.module_path = "modules"
    puppet.manifests_path = "manifests"
    puppet.manifest_file  = "init.pp"
  end
end

A more detailed look at the Puppet section suggests that the manifest file for the server is called init.pp and resides in the manifests directory.

Create your simple Puppet manifest for PostgreSQL 9.3

We have selected the PostgreSQL manifest published by Puppetlabs, as it is very simple to setup a basic database server with version 9.3 of our most loved DBMS. However, one day, you will be able to create your own Puppet module.

For now, just create the manifests directory and, most importantly, the manifests/init.pp file, containing the following lines:


# Install PostgreSQL 9.3 server from the PGDG repository
class {'postgresql::globals':
  version => '9.3',
  manage_package_repo => true,
  encoding => 'UTF8',
  locale  => 'it_IT.utf8',
}->
class { 'postgresql::server':
  ensure => 'present',
  listen_addresses => '*',
  manage_firewall => true,
}

# Install contrib modules
class { 'postgresql::server::contrib':
  package_ensure => 'present',
}

That should be all in terms of configuration. The manifest is responsible, among other things, for instructing Puppet to:

  • use version 9.3
  • use the Community repository for packages (manage_package_repo)
  • define default encoding and locale (you can change the latter to en_AU.utf8 if you live in Australia, for example)
  • make sure that the PostgreSQL server is always up and running, even at startup, and opens TCP/IP traffic to every network interface (which means you can connect to PostgreSQL using pgAdmin or psql from the hosting machine)
  • install the contrib modules package (containing PostgreSQL extensions such as hstore or pg_stat_statements).

Start up your local PostgreSQL 9.3 server and test it

Now, cross your fingers and type:


vagrant up

It will take a few minutes for Vagrant to generate your virtual server through VirtualBox, then provision the resources requested by the manifest file we wrote.

Once finished, you can finally test that PostgreSQL is running, by typing:


vagrant ssh

Then, become the postgres system user with:


sudo -i -u postgres

And finally connect to the database:


psql

This way, you verified that the internal connection (within the virtual server) works correctly.

Final notes

Loving the ElephantSo far so good. We have been able to install PostgreSQL 9.3 and you can try it locally. In a future blog post, we will go through some more advanced configurations of your development Postgres box using Puppet.

But for now, I hope you are satisfied with this outcome.

If you want to shutdown your virtual server, you just need to type:


vagrant halt

If you want to remove the server, just type:


vagrant destroy

You can at any time regenerate it through:


vagrant up

These commands allow you to automate the generation of virtual machines, one of the key technical aspects of the devops movement.

Share this

More Blogs