Thursday, August 28, 2008

Cloud Computing: Setup Postgresql to use Amazon Elastic Block Store (EBS)

Cloud Computing: Setup Postgresql to use Amazon Elastic Block Store (EBS)

Prerequisites: Familiar with creating and launching instances using Amazon EC2, and familiar with editing config files in linux. Also make sure that Postgresql is installed on your instance, see installation steps at http://endurotracker.blogspot.com .

Overview

We will follow the same initial steps as outlined on Amazon’s developer website ( http://developer.amazonwebservices.com ). The latter steps will be specific to Postgresql.

1) Creating an Amazon EBS Volume

In this example, the user calls the CreateVolume API, specifying an 10 GB volume.

$ ec2-create-volume --size 10 --availability-zone us-east-1a

VOLUME vol-4d826724 858993459200 creating 2008-02-14T00:00:00+0000

$ ec2-describe-volumes vol-4d826724

VOLUME vol-4d826724 858993459200 available 2008-02-14T00:00:00+0000

2) Attach the Amazon EBS Volume to an Instance

In this example, the user calls the AttachVolume API to attach the volume vol-4d826724 to the instance i-6058a509 and expose it as the device /dev/sdh.

$ ec2-attach-volume vol-4d826724 -i i-6058a509 -d /dev/sdh

ATTACHMENT vol-4d826724 i-6058a509 /dev/sdh attaching 2008-02-14T00:15:00+0000

3) Describing Volumes and Instances

After creating Amazon EBS volumes and attaching them to instances, you can list them using the DescribeVolumes and the DescribeInstances functions.

To list all volumes owned by the user, including their status, the user invokes the DescribeVolumes function.

$ ec2-describe-volumes

VOLUME vol-4d826724 858993459200 in-use 2008-02-14T00:00:00+0000

ATTACHMENT vol-4d826724 i-6058a509 /dev/sdh attached 2008-02-14T00:00:17+0000

VOLUME vol-50957039 13958643712 available 2008-02-091T00:00:00+0000

VOLUME vol-6682670f 1073741824 in-use 2008-02-11T12:00:00+0000

ATTACHMENT vol-6682670f i-69a54000 /dev/sdh attached 2008-02-11T13:56:00+0000

The function returns the volume ID, capacity, status (in-use or available) and creation time of each volume. If the volume is attached, an attachment line shows the volume ID, the instance ID to which the volume is attached, the device name exposed to the instance, its status (attaching, attached, detaching, detached) and when it was attached.

The user can also view volumes that are attached to running instances by using the DescribeInstances function.

$ ec2-describe-instances

RESERVATION r-e112fc88 416161254515 default

INSTANCE i-3b887c52 ami-3fd13456 ec2-67-202-27-216.compute-1.amazonaws.com domU-12-31-38-00-35-94.compute-1.internalrunning gsg-keypair 0 m1.small 2007-11-26T13:20:35+0000 vol-4d826724

RESERVATION r-e612fc8f 416161254515 default

INSTANCE i-21b63c22 ami-3fd13456 ec2-67-202-18-227.compute-1.amazonaws.com domU-12-31-38-00-39-28.compute-1.internalrunning gsg-keypair 0 m1.small 2007-11-26T13:21:51+0000 vol-6682670f,vol-50957039

4) Create ext3 filesystem on persistence volume (Amazon EBS) and mount the volume

Command line: yes | mkfs -t ext3 /dev/sdh

Command line: mkdir /mnt/pgsql

Command Line: mount /dev/sdh /mnt/pgsql

5) Stop Postgresql if it is already running on your instance

Cmd line: service postgresql stop

6) Copy postgresql folders to /mnt/pgsql

On Fedora 8, the folder is /var/lib/pgsql, copy the contents of the folder to /mnt/pgsql.

7) Change the owner of /mnt/pgsql to postgres user, and initial db cluster

Command line: chown –R postgres /mnt/pgsql

su –postgres

initdb –D /mnt/pgsql/data

8) Edit postgresql startup script and change all entries from /var/lib/pgsql to /mnt/pgsql

command line: vi /etc/rc.d/init.d/postgresql

9) Configure postgres for local access (see previous postgres setup post) (you will need to edit /mnt/pgsql/data/pg_hba.conf and /mnt/pgsql/data/postgresql.conf)

10) Start postgresql

command line: service postgresql start

10) Enjoy !

Wednesday, August 27, 2008

DB Cloud Computing: Postgresql Setup on Amazon EC2 using Fedora 8

Postgresql Setup on Amazon EC2 using Fedora 8

Prerequisites: Familiar with creating and launching instances using Amazon EC2, and familiar with editing config files in linux.

Links to documentation on Amazon EC2 and how to create and launch instances:

http://developer.amazonwebservices.com/connect/kbcategory.jspa?categoryID=84

click on Technical documentation link.

Thanks goes to this article: http://www.postgresonline.com/journal/index.php?/archives/45-An-Almost-Idiots-Guide-to-PostgreSQL-YUM.html

There are some things missing from that article that I have addressed here.

1) Create Ami instance from pre-existing instance and launch instance.

Use ami-0abe5a63 for example, see url: http://www.nonhostile.com/mono-on-amazon-ec2.asp

url: http://developer.amazonwebservices.com/connect/entry.jspa?externalID=1616&categoryID=101

2) Logon to your instance using an SSH client (in windows you can use Putty)

3) Login as root

4) Edit /etc/yum/pluginconf.d/fastestmirror.conf and add to the end: exclude=postgresql*

(differs from the instructions at http://yum.pgsqlrpms.org/howtoyum.php to prevent your YUM update from getting postgresql from other sources)

5) Select the appropriate repository config file for your OS and choose 8.3 from here and navigating thru: http://yum.pgsqlrpms.org/reporpms/repoview/letter_p.group.html

Note the install file - should look something like http://yum.pgsqlrpms.org/reporpms/8.3/pgdg-fedora-8.3-4.noarch.rpm

6) Do a wget of the appropriate one: e.g.
wget http://yum.pgsqlrpms.org/reporpms/8.3/pgdg-fedora-8.3-4.noarch.rpm

7) Next install the rpm config file with:
rpm -ivh pgdg-fedora-8.4-1.noarch.rpm


8) Install the things that you want. These are the ones we tend to install

yum install postgresql
yum install postgresql-server
yum install postgis
yum install pgadmin3

Note:On Fedora 8, postgresql default location is /var/lib/pgsql/data

9) Create a user postgres

Add command prompt : useradd postgres

10) Set new user’s password

Add command prompt: passwd postgres

11) Initialize postgreSql

At command prompt:

chown postgres /var/lib/pgsql/data

su postgres

initdb –D /var/lib/pgsql/data

12) Configure postgresql for local access

From command line ( start up vi and add the 2 entries):

vi /var/lib/pgsql/data/pg_hba.conf
- add:local all all trust
host all all 127.0.0.1 255.255.255.255 trust
host all all 0.0.0.0 255.255.255.255 reject

13) To configure remote access, from command line, pass in range of external ips to allow access to:

vi /var/lib/pgsql/data/postgresql.conf

listen_addresses = ‘*’

vi /var/lib/pgsql/data/pg_hba.conf

host all all [start ip range] [end ip range] trust
14) Assuming you have the AMI tools installed on your desktop, from the command line authorize the default port of 5432, by doing the following:

c:\amitools\ec2-authorize default –p 5432

Note: Now you can use the Postgres Admin UI Tools (pgAdmin III) from the machines that you granted remote access to. PgAdmin III allows you to administer Postgresql via a GUI. You can get this tool by installing Postgresql on your Windows desktop(s). I recommend this since it makes administration much easier.

15) Restart Postgresql, from command line (logged in as root):

service postgresql restart

16) Create a db if you like, from command line:

su – postgres

createdb db_name

17) Create a postgresql username with password for new db.

From command line: (-U postgres , means username postgres)

createuser –P –U postgres

18) Restart postgresql

19) Test your connection

login as root, then from command line:

psql –U db_username db_name

Cloud Computing: Mono Setup on Amazon EC2

Mono Setup on Amazon EC2 with optional config for MonoRail

Prerequisites: Familiar with create and launching instances using Amazon EC2, and familiar with editing config files in linux.

Links to documentation on Amazon EC2 and how to create and launch instances:

http://developer.amazonwebservices.com/connect/kbcategory.jspa?categoryID=84

click on Technical documentation link.

For porting application to Mono see article:
http://www.mono-project.com/Guide:_Porting_ASP.NET_Applications

1) Create Ami instance from pre-existing instance and launch instance.

Use ami-0abe5a63, see url: http://www.nonhostile.com/mono-on-amazon-ec2.asp

url: http://developer.amazonwebservices.com/connect/entry.jspa?externalID=1616&categoryID=101

2) Update Apache on this instance. This instance is a Fedora 8 image with Apache

Edit /etc/httpd/conf/httpd.conf

Add these entries to the end to use test app from mono:

MonoServerPath default /opt/mono-1.9.1/bin/mod-mono-server2

Alias / “/opt/mono-1.9.1/lib/xsp/test”

AddMonoApplications default “/:/opt/mono-1.9.1/lib/xsp/test”

<Location />

MonoSetServerAlias default

SetHandler mono

<Location />

Here is other entry example (notice using MonoApplications instead of AddMonoApplications)

(not sure why examples differ), but both syntax work:

Alias hellomonoweb “/opt/mono-1.9.1/lib/xsp/hellomonoweb”

MonoApplications “/hellomonoweb:/opt/mono-1.9.1/lib/xsp/hellomonoweb”

<Location />

SetHandler mono

</Location>

Important to disable case sensitivity for files and file paths add this entry. This prevents issues like file not found due to differences between Linux and Windows handling of case sensitivity for files and file paths:

MonoSetEnv MONO_IOMAP=all

3) Optionally, if you are using MonoRail, a .Net implementation of Ruby on Rails ( see castleproject.org), add the following entry to /etc/httpd/conf.d/mod_mono.conf:

AddType applications/x-asp-net .rails