Wednesday, October 26, 2011

How does PostgreSQL HA works in vFabric Data Director?

Databases go down due to various reasons. Some reasons are known and some unknown.
Common reasons are hardware failure, software failure, database unresponsive, etc. What is considered as a failure is actually one of the tasks. Various DBA's use a simple select statement as a test to make sure that the database is up and working. But what does one do if that simple select statement fails. I remembers years ago I worked on a module which will start paging  engineers in a sequence (and eventually their managers if the engineers failed to respond back in a certain expected way).  In this email/text age, scripts will start sending out emails and text messages.  What we are is basically in the Event->React-> Respond mode of operation.

However true HA needs to lower downtime which can  only be done by having the mode of operation as Event->Respond->React. To explain that when such an event happens, do an automated response first and then React to wake the engineers up :-)

How do you set this up in vFabric Data Director? This can be achieved by selecting the database properties, selecting the Database Configuration tab and set "High Availability" to "Enable". This is also refered as One-Click HA setting.

Of course this assumes that your virtual Data Cluster is set properly for providing the high availability services. How do you set it up properly? Well you need atleast two ESXi Hosts so if one host fails, the other can cover for it. Also vSphere HA property has been enabled in the Virtual Data Center Cluster. Note these settings are all "required" for vFabric Database setup and a "supported" setup does mandate atleast two ESXi Hosts in order for HA to work.

Now that we have gone over the setup requirements, lets go over the scenarios on how the application or user sees it.  A user is connected to the database using the connection string. Something happens and the database goes down and the connection drops. Chances are if you reconnect again immediately it may fail. However with certain time which is expected to be less than 5 minutes (which we call our Recovery Time Objective or RTO)  by default, if you try again you can connect to the database again.

So what happens in the background? Well if it was Magic, we would not tell you. But it is not really magic though it feels like that. Here is what will typically happen in the background.
For some reason the PostgreSQL fails to respond anymore it could be a "hung" situation or the PostgreSQL server has died. There is a small heartbeat monitor which figures out the status of the database. If it notices that the hung situation or no DB server process, it will try to restart the database. If the database cannot be restarted (because the whole VM appliance cannot respond anymore), it will in novice terms kill the virtual machine. The vCenter Server which has its own heartbeat on the VM appliance will see that the Virtual Machine has died (irrespective of the Database Monitor which may not be working if the whole host dies), the vCenter Server will restart the VM appliance on another server.

Since shared-storage is a requirement, the VM appliance will start on another host and it will feel like a reboot. Once the VM starts, the PostgreSQL server process will be restarted. At this point of time, the PostgreSQL server goes into recovery mode. The biggest question at this point of time typically is how long will the recovery mode take. Typically based on internal tests even with the heaviest workload on 8vCPU, the recovery time can finish within the checkpoint_timeout settings which means our Recovery Time Objective is guided by checkpoint_timeout + heartbeat latency + the time to restart the VM on another hosts.  Overall we try to fit that into our Recovery Time Objective of 5 minutes.

Great the virtual machine has restarted and the database has done its recovery and working again. Now what? Well dont forget in this cloud setup, the easiest thing is to use DHCP addresses. Unfortunately DHCP addresses are not guaranteed to be same after reboot . Plus rebooting on a different host makes it more complex to get the same IP. This IP address change can cause the Database connectivity to be lost to the actual end user.   In order to shield the end users from this complexity, we sort of implemented our own Database Name Server. However this can only work by modifying the clients which references the database using this "Virtual Hosts" format so that the clients can always find their intended database without really worrying about where it is running. A minor change in the PostgreSQL clients but a huge complexity reducer for end users to fix their IP addresses or domain names to the changed location.

Aha now this explains why vPostgres ships their own clients and libpq library which is API compatible with standard PostgreSQL libpq library.The libpq library is actually 100% compatible with standard PostgreSQL Libpq library. The only addition it has is the feature of Virtual Hosts which is critical for HA to work seemlessly without the users being concerned about the actual IP of the database. Without the change, HA will not work on the framework. Since it is 100% compatible, if an application works standard libpq it will work with vPostgres libpq. Similar changes are also done in the JDBC driver and ODBC Driver for vPostgres so HA is supported across all supported clients.

That said if you use standard libpq/psql and other standard clients and you know the IP Address of the vPostgres database and connect to it via that IP address (and not the virtual host string)  it will still work flawlessly. However if the database goes down and restarts with a new IP address then the client will have no ability to figure out the new IP address and will have to bug the Administrator to figure out the new IP address.

Though for folks familiar with vSphere terminology, HA is not FT - Fault Tolerant which is a different take on HA to further reduce downtime from minutes to seconds. More on that in future.

Thursday, October 13, 2011

Using PostgreSQL Server on Micro Cloud Foundry

With the recent news that PostgreSQL is now available in the Micro Cloud Foundry, I decided to take it for a test spin. I downloaded the Micro Cloud Foundry VM zip file which is about 1.0GB big. After downloading it I unzipped it on my MacBookPro and use VMware Fusion 4.0.2 to open the VM. As the VM booted up the console shows a message

Micro Cloud Foundry not configured

I selected the option 1 to configure the Micro Cloud. It asked me to configure my VM user password, Networking (DHCP or Static) and then asked me to enter my Cloud Foundry configuration token which was provided to me after I had created a pgtest.cloudfoundry.me domain  just before the download.

It took about 5 minutes to setup the cloud

After the setup: I got my micro cloud foundry setup with my local IP (looked like a bridge connection rather than NAT).

Then I installed the VMC tool on my Mac using  (Need Ruby)
(NOTE: Skip directly to ssh part if you donot want to install Ruby/vmc)

$ gem install vmc

$ vmc target http://api.pgtest.cloudfoundry.me

Got me connected to my micro cloud.
Then I did a
$ vmc register
to create my user account using a email id and password
Then I logged into the MicroCloud using
$ vmc login

Now when I do the following I see the PostgreSQL Service available with other databases also.

$ vmc services

============== System Services ==============

+------------+---------+---------------------------------------+
| Service    | Version | Description                           |
+------------+---------+---------------------------------------+
| mongodb    | 1.8     | MongoDB NoSQL store                   |
| mysql      | 5.1     | MySQL database service                |
| postgresql | 9.0     | PostgreSQL database service (vFabric) |
| rabbitmq   | 2.4     | RabbitMQ messaging service            |
| redis      | 2.2     | Redis key-value store service         |
+------------+---------+---------------------------------------+

=========== Provisioned Services ============
As you can see there are no provisioned services currently.


Here if you are like a Java/Spring developer you want to creating an application using Xin Li's post on "PostgreSQL for Micro Cloud Foundry- Spring Tutorial".

I am not interested in developing Java applications but I want access to the postgresql server directly.

Now comes the ssh part.

Currently the PostgreSQL server is not exposed externally from the Micro Cloud.
But on the console of Micro Cloud VM, you can configure the password of vcap user. Which means now you have ssh access to the Micro Cloud VM.

$ ssh vcap@mircrocloudip

$ cd /var/vcap/store/postgresql
$ vi postgresql.conf 

and edit listen_address to add your database client ip address out there.
For my demo setup I just opened it to all
listen_addresses='*'

Next assign a Postgres password for the "vcap" user
$ /var/vcap/packages/postgresql/bin/psql -d postgres
psql (9.0.4)
Type "help" for help.

postgres=# ALTER USER vcap WITH PASSWORD 'secret';
ALTER ROLE
postgres=#\q

Now I exit from Micro Cloud VM and using the console I restart the services.
Now the PostgreSQL service can be accessed from postgres client anywhere.

For example from a Macbook Pro

$ psql -h microcloudip -d postgres -U vcap
Password for user vcap:
psql (9.0.5, server 9.0.4)
Type "help" for help.

postgres=#


Try it out!

Friday, October 07, 2011

vFabric Data Director - All in a Box setup

vFabric Data Director 1.0 is available for download on the VMware download website. Generally the question we get a lot is on how to do a "small" setup for either a departmental setup or trial setup which is actually small enough to fit in a beefy workstation or a small server. Often time this helps people to evaluate the features of Data Director before doing the "standard" HA setup. Maybe it is only for tests/dev databases where one does not want to invest too much in the infrastructure setup itself..

So here is one such way of doing a "small" setup of vFabric Data Director for test/dev/qa databases.
(Note: This is going to be a long blog post)

What do you need?
  1. A beefy Workstation or departmental Server with lots of RAM and lots of Storage and atleast two network adapter (Even though only one need be connected to the departmental network)
  2. ESXi V5.0 installation CD (Software)
  3. vCenter  Virtual Appliance/Virtual Machine
  4. DHCP Server Virtual Machine
  5. vFabric Data Director Virtual Appliance Image
  6. Preferably atleast one static IP Address on your departmental network for DB Name Server or have Dynamic DNS on your departmental network
  7. License Keys - Use your VMware contacts.

Step I: Preparing the Workstation
On my test setup I had 12GB of RAM with 2x Quad Core x86_64 chips with 5 disks in it. Since this is my whole setup, the more memory I get, the merrier I am with the setup. It is recommended that you have some sort of Raid controller on the setup and have ability to create multiple LUN devices that will be exposed to the ESXi server. On my setup I had about one disk dedicated to ESXi and for other disks I created a RAID-5 LUN (RAID-10 is preferred but I did not have enough space on my demo setup).  If I had more Storage available I would do two setups with RAID-10 setup and a RAID-0 setup which can be used as backup datastore. The RAID preferences depends on individual needs on what to trade off (performance , availability, capacity).

Step II: Installing ESXi 5.0 
Using the ESXi V5.0 installation CD, I installed ESXi  on the first logical device and eventually setup the RAID protected device as a datastore that ESXi can use.

Step III: Install vCenter Virtual Machine
Here one can use the vCenter Virtual Appliance available also. On my demo setup I had used a vCenter Server based on Windows Server 2003 since that was available. Installation of vCenter Server itself can be its own blog entry and I will leave that to experts. For my purpose I had setup vCenter Server setup done in a virtual machine.

Step IV: Setting up vCenter Server for our task
This is where things are bit different for this special all in a box setup. The idea of setting up is to do vFabric Data Director Appliance which includes the hardware. Hence the idea is only the management console and the databases that it deploys are exposed outside (of course the ESXi also has to be visible outside to set this up)  and all other infrastructure related things are hidden within this appliance. This is where two network adapters will come in play. Lets go first with the steps and then a bit of explanation on why do it this way.
I am assuming that the ESXi Server and  vCenter VM has network connected to the live network adapter.
  • Connect to the vCenter through vSphere GUI or through the webclient.
  • Create a new DataCenter. 
  • Create a new Cluster
  • Add the ESXi Host to this cluster
  • Edit Settings for the Cluster to Enable
    • vSphere HA (even though we cannot do it on a single Host)
    • vSphere DRS
    • "VM and Application Monitoring" in VM Monitoring
  • Create a Distributed vSwitch as follows:
    • Go to networking in Inventory you will see your corporate network called probably "VM Network"
    • Add a new distributed vSwitch in the section where you have to add a physical Network Adapter, select the Network Adapter which is not plugged into the departmental network (Skip creating an automatic port group for the switch)
    • Now for the dvSwitch created, create two port Groups "vCenter Network" and "Internal Network"
  • For the ESXi host create a vmKernel port in "vCenter Network" portgroup with a static IP address 192.168.2.2
  • For the vCenter VM, create a new network adapter in the "vCenter Network" port group with a static IP of 192.168.2.1
  • Change the Managed IP of vCenter Server (Administration->vCenter Server Settings->Runtime Settings)  to 192.169.2.1
  • Make sure vCenter server can still access the ESXi server through the new "vCenter Network" portgroup
  • Setup DHCP Server Appliance such that its LAN network is on "Internal Network"
    • I had setup DHCP  Server such that its own IP is 192.168.1.1 and it does DHCP on the network from range 192.168.1.5 to 192.168.1.250 (for my demo setup)

Step V: Deploy vFabric Data Director OVA Template
  • Using the vSphere Client (connected to our vCenter Server) deploy the vFabric Data Director OVA template. 
  • The setup wizard will ask you to select the network for vCenter and the Management console. For the vCenter Network select the "vCenter Network" portgroup and for the Management Console, select the "VM Network" which is the live departmental network
  • If your setup is like mine, I do not have access to static IP adddress in the deparmental network so I just leave the next screen at its defaults to use DHCP and finish the deployment. 
Once the deployment finishes  there will be a new vApp called VMware Data Director.
  • Start the vAPP.  Once the vAPP starts, expand the "+" sign and select to the Management Server VM. 
  • Select the "Summary" tab of Management Server and wait till it shows an IP address.
  • Enter  that IP address in a browser and you should see message "This connection is untrusted" depending on your browse type, add it to your exceptions and then it should take you to License agreement screen. 

Step VI: Setup vFabric Data Director
  • Read and accept the License agreement to proceed. 
  • Next create an administrator account.
  • On the next screen since this is a small setup, I selected the Global User Management Mode
  • Setup the Branding as required on the next screen
  • Setup the SMTP server information if available (needed for user password resets) (You also need outgoing email id for a successful setup of SMTP )
  • On the next screen you have to setup vCenter Network Information. Since we dont have any DHCP on our "vCenter Network" portgroup, edit Network adapter settings and select "Static" with netmask information 255.255.255.0
  • Set static IP addresses 192.168.2.3 for Management server and 192.168.2.4 for DB Name Server
  • On the next screen change Internal Network to select "Internal Network" portgroup and leave DB Name Server Network as the "VM Network" which is the departmental network.
  • On the following screen, select the network settings of "Internal Network"  DHCP should be already selected. Also check Static and add the network mask as 255.255.255.0
  • For Management Server - Internal Network adapter, select static IP address and set it to 192.168.1.3
  • For DB Name Server - Internal Network adapter, select static IP address and set it to 192.168.1.4
  • Warning: This next bit is where you use your one static IP address or Dynamic DNS based FQDN requirement. We still have to provide DB Name Server - DB Name Service Network Adapter. If you have any influence on your IT, get a static IP address for this one. If you get the static IP, then click the Departmental "VM Network" setup and select static IP address with the associate Gateway, netmask and DNS Server setup. Then  set the static IP address for DB Name Server - DB Name Service Network Adapter with the static IP address that you get from your IT..
  • Next enter your Evaluation License keys for vFabric Data Director and vFabric Postgres
  • Finally verify information on the summary page and then click Finish
  • You should get a login screen if it sets up successfully
Note: The most tricky bit is getting the IP address from IT. If for some reasons you do not have a static IP address, fake a  fully qualified domain name for DHCP. Once setup and you get a login screen. Figure out the DHCP IP allocated to DB Name Server using vSphere Client (It is first IP address that shows in the Summary tab of DB Name Server VM). Enter using your administrator account credentials, go to "Administration" tab. Select Settings-> Networking setup. Select Edit Network Setup and step through the setup again and change your fakeFQDN with the DHCP IP address and press finish. Of course this is a hack and not recommended since DHCP IP addresses can change anytime if the lease is up or the system is rebooted and other network policies.

Step VII: Setting Up  an Resource Bundle in vFabric Data Director
For this we need a special Resource Pool in our Virtual Data Center
  • Using vSphere Client we create a resource Pool "Resource Bundle1" in the data center
  • Edit its settings such that it has reservations and limits matching for both CPU and memory
  • Also "Unlimited" should not be checked for both CPU and Memory. 
  • In my demo setup I set CPU reservations and limits to 4096MB and Memory reservations and limits to 4096 MB.
Enter the vFabric Management Console using your administrator credentials and go to "Manage & Monitor" tab.
  • Select "Resource Bundle" and create a new resource Bundle "ResourceBundle1".
  • If the setup is right the next screen should show you the CPU/Memory Resource Pool that we created "Resource Bundle1"  
  • Next select the RAID protected datastore and a size chunk off it for Database Storage. Select any alternative or the same datastore for "Backup Storage" with a sizeable chunk. In my demo I selected my Raid5 based datastore and 100GB sizes for both.
  • Next select the "VM Network" which is my departmental network through which uses will access the database. 
  • Click Finish to setup the Resource Bundle.


Step VII: Setup an Organization in vFabric Data Director
In "Manage&Monitor" select Organizations and create "+" a new organization called "DataDirectorOrg".
  • On the next screen you could select an new user or in my case I used "Choose an existing user" and used my administrator account to manage the Organization also.
  • Next I selected the resource Bundle I just created (need to select "Choose an existing Resource Bundle" to see the resource bundle) 
  • click Finish.
Once created there will be the new Org displayed. If you select it once, the link becomes active. If you then select the active link again it will open a New tab for the Organization for our next step.


Step VIII: Setup a Database Group
Next we have to setup a database group.
  • In the org tab select "Manage&Monitor" tab to see the list of databases (which is empty). 
  • Select the second tab "Database Groups"  to see the empty group list
  • Create ("+" ) a new database group "DBGroup1" where I selected half of my datastore resources assigned for this group leaving the rest of the entries at default.


Step IX: Create a database
  • Select and enter the database group we just created.
  • Create ("+") a new database "dbtest" with owner credentials "dba" and password.
  • Wait till deployment of the database succeeds and "dbtest" is running. 
  • Once running highlight it , right click to see the properties and get the UUID and Name. The client also needs the IP address of the DB Name server.
Here is an image of the distributed vSwitch from vSphere client on my demo box.





Step X: Connect to the database from a client
Download vPostgres Clients for your platform. Then using psql from the client connect to the database similar to the following example
psql -h {dd9fce1e-db46-4a08-99a1-e9023b8239fe}.129.55.555.55 -d dbtest1 -U dba

It should prompt for the dba password and now you are connected to the database and the setup is working. Check out my previous blog entry on how to use vPostgres Clients.

Finally now the setup of vFabric Data Director all in a box setup is working and tested.

Thursday, September 29, 2011

#PGWest 2011 - Using vFabric Postgres - A DB User's Perspective

Here are my slides from my #PGWest 2011 Presentation " Using vFabric Postgres - A DB User's Perspective" for vPostgres Databases as  deployed by vFabric Data Director.

Sunday, September 18, 2011

Next Stop: #PgWest 2011 - San Jose

Returned back from a great #pgopen in Chicago. It was nice to also see and meet again senior VMware executives, PostgreSQL community members and lot of folks (aka customers or potential customers ) who use PostgreSQL as key databases in their IT setup  in the conference.

Next stop now is #PgWest 2011 in San Jose starting on Septeber 27,2011. This year again #PgWest is in San Jose, California.

At this conference, I am presenting my first ever "Using vPostgres - A DB User perspective". The gist of this presentation is on how to use the vFabric Postgres client with the vFabric Postgres server deployed by vFabric Data Director. There are some small differences on how the client works compared to community PostgreSQL  and we will go over those in the session on exactly how it works, and see on how you use it for running and developing your own applications with it.

Also Alex Mirgorodskiy, VMware will be first time presenting in a PostgreSQL conference (that I am aware of at this point of time) on "vFabric Postgres Database Internals".  In this session Alex will go over the subtle tansparent changes which makes vFabric Postgres so easy to deploy, manage and perform in the vFabric Data Director world. Not to steal his thunder but this will cover the new Elastic Database Memory in detail.

Bill Hodak, VMware will present  about  vFabric Data Director itself on how it leverages the features of PostgreSQL and provide a management framework over it to provide enterprise framework and provide self-service features making it easy for even folks who donot know a lot about PostgreSQL database itself to deploy, tune, monitor, backup, restore, clone the database instances.

And to give an overview of VMware's commitment to PostgreSQL Community, Charles Fan, Sr.VP - VMware R&D will be presenting the keynote at #PgWest 2011.

Overall pretty excited about the conference and  to meet lot of new people in the bay area.