Shantanu's Blog

Corporate Consultant

June 22, 2012


Create Auto-scaled load balancer

Create and configure load-balancer:
elb-create-lb MyLB --availability-zones us-east-1b --listener "protocol=TCP, lb-port=3306, instance-port=3306"

elb-configure-healthcheck MyLB --target "TCP:3306" --interval 30 --timeout 2 --healthy-threshold 6 --unhealthy-threshold 2

elb-register-instances-with-lb MyLB --instances i-29184c43

Create config parameter and auto-scaling group:
as-create-launch-config MyLC --image-id ami-41d00528 --instance-type m1.small --key severalnines --group quick-start-1 

as-create-auto-scaling-group MyAutoScalingGroup --launch-configuration MyLC --availability-zones us-east-1a  --min-size 2 --max-size 5 --load-balancers MyLB

Create policies and alarms:
as-put-scaling-policy MyScaleUpPolicy --auto-scaling-group MyAutoScalingGroup  --adjustment=1 --type ChangeInCapacity  --cooldown 300
mon-put-metric-alarm MyHighCPUAlarm  --comparison-operator  GreaterThanThreshold  --evaluation-periods  1 --metric-name  CPUUtilization  --namespace  "AWS/EC2"  --period  600  --statistic Average --threshold  80 --alarm-actions Policy_from_previous_step --dimensions "AutoScalingGroupName=MyAutoScalingGroup"

as-put-scaling-policy MyScaleDownPolicy --auto-scaling-group MyAutoScalingGroup  --adjustment=-1 --type ChangeInCapacity  --cooldown 300

mon-put-metric-alarm MyLowCPUAlarm  --comparison-operator  LessThanThreshold --evaluation-periods  1 --metric-name  CPUUtilization --namespace  "AWS/EC2"  --period  600  --statistic Average --threshold 40 --alarm-actions Policy_from_previous_step --dimensions "AutoScalingGroupName=MyAutoScalingGroup"

Describe scaling: 


associate domain:
## associate and dis-associate route 53 domain to LB
elb-associate-route53-hosted-zone useast --rr-name --hosted-zone-id Z36XR238KZYP2F --weight 100

elb-disassociate-route53-hosted-zone useast --rr-name --hosted-zone-id Z36XR238KZYP2F --weight 100

Pause auto-scale:

as-suspend-processes MyAutoScalingGroup
as-resume-processes  MyAutoScalingGroup

Delete config parameter and auto scale group:
as-delete-auto-scaling-group MyAutoScalingGroup  --force-delete

as-delete-launch-config  MyLC

Replace launch config:

as-create-launch-config app-server-launch-config-2 --image-id ami-c503e8ac --instance-type c1.medium --group web

as-update-auto-scaling-group app-server-as-group-1 --launch-configuration app-server-launch-config-2

Terminate instances:
as-update-auto-scaling-group app-server-as-group-1 --min-size 0

as-terminate-instance-in-auto-scaling-group i-12345abc --decrement-desired-capacity

as-terminate-instance-in-auto-scaling-group i-12345abc --no-decrement-desired-capacity


June 18, 2012


Script to install mysql

Here is the shell script that will install mysql version 5.5 on a new instance.
sh -xv /root/


# mysql data directory

mydate=`date '+%j%H%M%S'`

## make sure new EBS is formatted
#time mkfs /dev/xvdt

## Add mount point if any and mysql start command in /etc/rc.local

#/bin/mkdir -p /data
#/bin/mount /dev/xvdab /data
#/etc/init.d/iptables stop
#/etc/init.d/mysql start

## disable selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/sysconfig/selinux
/usr/sbin/setenforce 0
echo 0 > /selinux/enforce

## shut-down mysql if already running
mysqladmin shutdown

# remove old data directory
#rm -rf /var/lib/mysql/
rm -rf /root/download

## create required directories
# datadir
mkdir -p $my_data_dir
# pid directory
mkdir -p /var/run/mysql
# default socket directory
mkdir -p /var/lib/mysql
# download directory
mkdir /root/download
cd /root/download


# create my.cnf file
cat > /etc/my.cnf << heredoc




## logging


## date %j%H%M%Sserver-id=$mydate

## network and slave needs more bandwidth

## temporary tables optimization

## join queries use this buffer

## Query Cache
query_cache_type = 1
query_cache_size = 200M

## Performance Tunning

## temp dir
tmpdir = /mnt/

## InnoDB optimization

mysqld = /usr/bin/mysqld_safe
mysqladmin = /usr/bin/mysqladmin
user = multi_admin
password = multipass

socket = /tmp/mysql.sock2
port = 3307
pid-file = /mnt/data/hostname.pid2
datadir = /mnt/data/



# reset data-directory
mydir=`grep ^datadir /etc/my.cnf | awk -F'=' '{print $2}'`
rm -rf $mydir/*

# remove mysql
for package in `rpm -qa | grep -i mysql`
rpm -e $package

rpm -e mysql-libs-5.1.52-1.el6_0.1.x86_64

# install mysql
rpm -iUh /root/download/*

# install php
yum install -y php

# install mysql system files
mysql_install_db --datadir=$mydir

# restart mysql
/etc/init.d/mysql restart

Labels: , ,

June 15, 2012


Using Auto Scaling with load-balancer

We can increase / decrease instances based on a particular image that will have all the data built-in. Here are the 4 commands those we can execute to create the auto-scaling group. Auto Scaling and Load Balancing tools can be installed from the following links...

Load balancer can be easily created / disabled from the web interface. But I guess for auto-scaling we will need to install the command line tool mentioned above.

1) Create a load balancer:
elb-create-lb mysql_LoadBal --listener "1b-port=3306,instance-port=3306,protocol=TCP" --availability-zones us-east-1b

2) Create auto scaling group launch configuration:
as-create-launch-config mysql_Config --image-id ami-60da3d09 --instance-type m1.small

3) Create auto scaling group:
as-create-auto-scaling-group mysql_AutoScale --launch-configuration mysql_Config --availability-zones us-east-1b --min-size 1 --max-size 5 load-balancers mysql_LoadBal

4) Create Trigger:
as-create-or-update-trigger mysql_Trigger1 --auto-scaling-group mysql_AutoScale --namespace "AWS/EC2" --measure CPUUtilization --statistic Average --dimensions "AutoScalingGroupName=mysql_AutoScale" --units "Percent" --period 60 --lower-threshold 30 --upper-threshold 70 --lower-breach-increment"=-1" --upper-breach-increment "1" --breach-duration 120

When average CPU utilization in the group exceeds 70% over a two-minute interval, increase instances up-to maximum of 5. When load falls below 30% reduce the number of instances but keep at-least 1 instance running.

To check how it is working:
as-describe-scaling-activities mysql_AutoScale

i) In order to disable auto-scaling We will first have to remove the trigger:
as-delete-trigger mysql_Trigger1 --auto-scaling-group mysql_AutoScale

ii) Set minimum and maximum size of the group to 0 to force all its instances to terminate:
as-update-auto-scaling-group mysql_AutoScale --min-size 0 --max-size 0

iii) Remove auto-scaling group:
as-delete-auto-scaling-group mysql_AutoScale

iV) Remove load balancer:
elb-delete-lb mysql_LoadBal



EC2 Instance Metadata

Each EC2 instance can access run-time data about itself by making HTTP requests to the special address

wget -q

cat instance-id

You can also supply your own metadata when you launch an EC2 instance. This data is called "User Data". For e.g.

wget -q
cat user-data

You can have your custom AMI treat the user metadata as the URL of the script to be run when the instance starts.



EBS Raid

We can glue the EBS volumes together into a RAID.

mdadm --create /dev/md0 --level 0 --metadata=1.1 --raid-devices 2 /dev/sdh1 /dev/sdh2

Add the following line to /etc/mdadm.conf (creating if necessary)

DEVICES /dev/sdh1 /dev/sdh2

Run the following command to add additional configuration information to the file:

mdadm --detail --scan >> /etc/mdadm.conf

At this point the RAID volume (/dev/md0) has been created. It encapsulates the 2 volumes.

Create a file system:

mkfs /dev/md0

create a mount point and mount the volume on it:

mkdir /data
mount /dev/md0 /data


June 12, 2012


Check return code

While using "system" function to execute linux commands within PHP, we can use a variable to store the return value. We will continue only if the return value is 0 (success) or exit the script.

system("mysqldump -uroot db1 --ignore-table=db1.tbl_1 --ignore-table=db1.tbl_2 > /root/db1.sql", $retval);

if ($retval != '0') {
  exit ("dump db1.sql command failed");

June 08, 2012


Restarting slave

I have always ignored the following warning.

[Warning] Neither --relay-log nor --relay-log-index were used; so replication may break when this MySQL server acts as a slave and has his hostname changed!! Please use '--relay-log=slave-usw-relay-bin' to avoid this problem.

This made a big difference when I changed the AWS EBS and attached it to a new instance. Since the new instance had a new IP address, the relay-log file name was inconsistent. Therefore we must always specify log-bin and relay-log parameters in my.cnf


It is possible to recover slave if it has Failed to open the relay log. It is possible that MySQL saved it's relay logs in /var/run by default, which gets cleared out on boot.

To fix this, there are 2 ways.

you need to change the location MySQL uses for the logging by adding the following line to the [mysqld] section of /etc/my.cnf

relay-log = /var/lib/mysql/relay-bin

Then edit /var/lib/mysql/ to point to the first new relay log (leaving the master information the same.


Start the slave. Or if you already have the slave status at the time of stopping the slave. Note the following 2 parameters.

       Relay_Master_Log_File: mis-bin.000710
          Exec_Master_Log_Pos: 461352141

And then reset slave and start the replication by using the statement "CHANGE MASTER TO ..."


June 2001   July 2001   January 2003   May 2003   September 2003   October 2003   December 2003   January 2004   February 2004   March 2004   April 2004   May 2004   June 2004   July 2004   August 2004   September 2004   October 2004   November 2004   December 2004   January 2005   February 2005   March 2005   April 2005   May 2005   June 2005   July 2005   August 2005   September 2005   October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006   January 2007   February 2007   March 2007   April 2007   June 2007   July 2007   August 2007   September 2007   October 2007   November 2007   December 2007   January 2008   February 2008   March 2008   April 2008   July 2008   August 2008   September 2008   October 2008   November 2008   December 2008   January 2009   February 2009   March 2009   April 2009   May 2009   June 2009   July 2009   August 2009   September 2009   October 2009   November 2009   December 2009   January 2010   February 2010   March 2010   April 2010   May 2010   June 2010   July 2010   August 2010   September 2010   October 2010   November 2010   December 2010   January 2011   February 2011   March 2011   April 2011   May 2011   June 2011   July 2011   August 2011   September 2011   October 2011   November 2011   December 2011   January 2012   February 2012   March 2012   April 2012   May 2012   June 2012   July 2012   August 2012   October 2012   November 2012   December 2012   January 2013   February 2013   March 2013   April 2013   May 2013   June 2013   July 2013   September 2013   October 2013   January 2014   March 2014   April 2014   May 2014   July 2014   August 2014   September 2014   October 2014   November 2014   December 2014   January 2015   February 2015   March 2015   April 2015   May 2015   June 2015   July 2015   August 2015   September 2015   January 2016   February 2016   March 2016   April 2016   May 2016   June 2016   July 2016   August 2016   September 2016   October 2016   November 2016   December 2016   January 2017   February 2017   April 2017   May 2017  

This page is powered by Blogger. Isn't yours?