<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Linux Admin Zone</title>
	
	<link>http://linuxadminzone.com</link>
	<description>adding more reasons to celebrate open source</description>
	<lastBuildDate>Mon, 22 Apr 2013 06:05:11 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/LinuxAdminZone" /><feedburner:info uri="linuxadminzone" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId>LinuxAdminZone</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>redis master slave setup with persistency of slave updates master datadir</title>
		<link>http://feedproxy.google.com/~r/LinuxAdminZone/~3/S0BzSC3JYLM/</link>
		<comments>http://linuxadminzone.com/redis-master-slave-setup-with-persistency-of-slave-updates-master-datadir/#comments</comments>
		<pubDate>Mon, 22 Apr 2013 06:05:11 +0000</pubDate>
		<dc:creator>jagbir</dc:creator>
				<category><![CDATA[redis]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[persistency]]></category>

		<guid isPermaLink="false">http://linuxadminzone.com/?p=601</guid>
		<description><![CDATA[I am still testing this setup so please be aware to have adequate testing before trying to put it in production and share your valuable thoughts in comments if you have better idea for implementing same thing. As we know, Redis is an open source, BSD licensed, advanced key-value store with lots of features which [...]]]></description>
				<content:encoded><![CDATA[<p></p><p>I am still testing this setup so please be aware to have adequate testing before trying to put it in production and share your valuable thoughts in comments if you have better idea for implementing same thing. As we know, Redis is an open source, BSD licensed, advanced key-value store with lots of features which makes it preferred choice. Persistency (disk dump) is one of them. In a busy, loaded Redis server, persistency can have its own noticeable overhead and impact performance. </p>
<p><strong>How can we have data reliability/recoverability without having overhead of persistency in working redis server? </strong></p>
<p>We are talking about non-transient data here which is important for application. There can be lots of approaches. You can tune the disk dump frequency settings in redis.conf to have a fine balance of things, this will do the trick for Most of times.  You can also have a master slave setup where master do the work and only slave have disk dump enabled, this looks acceptable until one fine day where master server reboots or redis service restarted, and find nothing in its datadir. Consequently slave also get informed that no data exists so data will be wiped out from slave as well. Though you can recover data from old disk dumps from slave but this can be disastrous. One way I can think of to avoid such incident is to rsync data (dump) from slave to master datadir. There would be some latency and you can loss some data but in our case this looks like fine balance between performance and reliability. Here are the steps to implement this along with script to do the sync: </p>
<p><strong>Step 1. Turn off persistency in Redis master server</strong><br />
You can do this using config set commands so no need to restart redis service. Do not forget to update redis.conf also:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">$ redis-cli config <span style="color: #000000; font-weight: bold;">set</span> save <span style="color: #ff0000;">&quot;&quot;</span>
OK
$ <span style="color: #c20cb9; font-weight: bold;">vim</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>redis<span style="color: #000000; font-weight: bold;">/</span>redis.conf
 save <span style="color: #ff0000;">&quot;&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#save 900 1</span>
<span style="color: #666666; font-style: italic;">#save 300 10</span>
<span style="color: #666666; font-style: italic;">#save 60 10000</span></pre></td></tr></table></div>

<p><strong>Step 2. Make sure persistency (disk dump) enabled in slave server (by default they are enabled). Set up password less ssh access from master to slave server so rsync can work smoothly. You can follow this <a href="http://askubuntu.com/questions/46930/how-can-i-set-up-password-less-ssh-login" title="guide" target="_blank" rel="nofollow">guide</a> to have that in ubuntu.</strong></p><div class="wpInsert wpInsertInPostAd wpInsertMiddle" style="margin-left: 150px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-2792824039251059";
/* BoxAd-336x280 */
google_ad_slot = "2138368529";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<p><strong>Step 3. In Master server, put this script and update its variables to point to correct datadir etc. </strong></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">cat</span> redis-sync.sh 
<span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#j#</span>
<span style="color: #666666; font-style: italic;"># redis-sync.sh : sync redis db from slave to master since persistency in master is disabled.</span>
<span style="color: #666666; font-style: italic;">#j#</span>
&nbsp;
<span style="color: #007800;">LogFile</span>=<span style="color: #ff0000;">&quot;/var/log/redis/redis-sync.log&quot;</span>
<span style="color: #007800;">LogRotate</span>=<span style="color: #000000;">3</span>     <span style="color: #666666; font-style: italic;"># keep log for these number of days</span>
<span style="color: #007800;">LocalDataDir</span>=<span style="color: #ff0000;">&quot;/data/db/redis/&quot;</span>
<span style="color: #007800;">RemoteDataFile</span>=<span style="color: #ff0000;">&quot;/data/db/redis/dump.rdb&quot;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$#</span> <span style="color: #660033;">-ne</span> <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'Usage: redis-sync.sh &lt;serverip&gt; or logtrunc'</span>
  <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">0</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># it's a good idea to truncate self log file to avoid disk space alert due to it. </span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$1</span> == <span style="color: #ff0000;">&quot;logtrunc&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #007800;">LinesToKeep</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$LogRotate</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000;">24</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000;">60</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">bc</span><span style="color: #000000; font-weight: bold;">`</span>
  <span style="color: #c20cb9; font-weight: bold;">tail</span> <span style="color: #660033;">-n</span> <span style="color: #007800;">$LinesToKeep</span> <span style="color: #007800;">$LogFile</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #007800;">$LogFile</span>.new
  <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-f</span> <span style="color: #007800;">$LogFile</span>
  <span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #007800;">$LogFile</span>.new <span style="color: #007800;">$LogFile</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$(date +&quot;%b %d %R&quot;)</span> Log file <span style="color: #007800;">$LogFile</span> truncated to have last <span style="color: #007800;">$LogRotate</span> days of logs only.&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$LogFile</span>
  <span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #007800;">Instances</span>=<span style="color: #000000; font-weight: bold;">`</span>pgrep redis-sync <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">wc</span> -l<span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$Instances</span> <span style="color: #660033;">-gt</span> <span style="color: #000000;">2</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$(date +&quot;%b %d %R&quot;)</span> <span style="color: #007800;">$Instances</span> instance(s) of this script already running, exiting to let them complete.&quot;</span>  
 <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Generally redis db dump file is not readable by other user/group, hence give read permission to it</span>
<span style="color: #c20cb9; font-weight: bold;">ssh</span> ubuntu<span style="color: #000000; font-weight: bold;">@</span><span style="color: #007800;">$1</span> <span style="color: #ff0000;">&quot;sudo chmod +r <span style="color: #007800;">$RemoteDataFile</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$?</span> <span style="color: #660033;">-ne</span> <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
 <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$(date +&quot;%b %d %R&quot;)</span> Error: Communication failure or unable to make dump readable at $1.&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$LogFile</span>
 <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># sync dump from slave, redirect output to $LogFile for debugging else trash it</span>
rsync <span style="color: #660033;">-avz</span> ubuntu<span style="color: #000000; font-weight: bold;">@</span><span style="color: #007800;">$1</span>:<span style="color: #007800;">$RemoteDataFile</span> <span style="color: #007800;">$LocalDataDir</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$?</span> <span style="color: #660033;">-ne</span> <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
 <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$(date +&quot;%b %d %R&quot;)</span> Error: Unable to rsync <span style="color: #007800;">$RemoteDataFile</span> from $1 server!&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$LogFile</span>
 <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">touch</span> <span style="color: #007800;">$LocalDataDir</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$(date +&quot;%b %d %R&quot;)</span> OK: redis dump <span style="color: #007800;">$RemoteDataFile</span> syncd from $1.&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$LogFile</span></pre></td></tr></table></div>

<p>Supply slave server ip/hostname to script for sync, let&#8217;s do a test run, check log file for messages:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">$ .<span style="color: #000000; font-weight: bold;">/</span>redis-sync.sh 10.0.0.xxx
The authenticity of host <span style="color: #ff0000;">'10.0.0.xxx (10.0.0.xxx)'</span> can<span style="color: #ff0000;">'t be established.
ECDSA key fingerprint is d0:e7:0a:a1:56:32:ca:ec:f1:e3:90:7d:1b:56:0b:5b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '</span>10.0.0.xxx<span style="color: #ff0000;">' (ECDSA) to the list of known hosts.
&nbsp;
$ tailf /var/log/redis/redis-sync.log 
Apr 22 05:44 OK: redis dump /data/db/redis/dump.rdb syncd from 10.0.0.xxx.</span></pre></td></tr></table></div>

<p>Looks fine. You will get error message in log file in case script failed to work properly. </p>
<p><strong>Step 4. Put script in cron, also to avoid getting lots of disk space consumed by script log file, call it with logtrunc option to truncate that. You can set no. of days in script for which you want to have logs or can have automatic rotation.</strong></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">$ crontab <span style="color: #660033;">-e</span> 
<span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">/</span>root<span style="color: #000000; font-weight: bold;">/</span>custom-scripts<span style="color: #000000; font-weight: bold;">/</span>redis-sync.sh 10.0.0.xxx
&nbsp;
<span style="color: #666666; font-style: italic;"># truncate log file</span>
<span style="color: #000000;">0</span> <span style="color: #000000;">4</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">/</span>root<span style="color: #000000; font-weight: bold;">/</span>custom-scripts<span style="color: #000000; font-weight: bold;">/</span>redis-sync.sh logtrunc</pre></td></tr></table></div>

<p><strong>Step 5. You can disable auto starting of Redis service upon machine reboot, first call sync script to sync dump from slave and then start the service. Although this doesn&#8217;t have specific benefit since if master goes down, slave won&#8217;t do anything but in rare cases where data dir/dump in master got corrupted or get missed, it a good idea to take fresh from slave. </strong></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">$ update-rc.d redis-server disable
$ <span style="color: #c20cb9; font-weight: bold;">vim</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>rc.local
<span style="color: #000000; font-weight: bold;">/</span>root<span style="color: #000000; font-weight: bold;">/</span>custom-scripts<span style="color: #000000; font-weight: bold;">/</span>redis-sync.sh 10.0.0.147
<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>redis-server start</pre></td></tr></table></div>

<p>As mentioned earlier, this is just quick fix. Please share your thoughts on shortcomings/better approach, if any. </p>
<div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-2792824039251059";
/* LinksBottom-468x15 */
google_ad_slot = "9848270356";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LinuxAdminZone?a=S0BzSC3JYLM:T5Y7m8_ElEA:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LinuxAdminZone?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LinuxAdminZone?a=S0BzSC3JYLM:T5Y7m8_ElEA:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LinuxAdminZone?i=S0BzSC3JYLM:T5Y7m8_ElEA:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LinuxAdminZone?a=S0BzSC3JYLM:T5Y7m8_ElEA:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/LinuxAdminZone?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LinuxAdminZone?a=S0BzSC3JYLM:T5Y7m8_ElEA:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/LinuxAdminZone?i=S0BzSC3JYLM:T5Y7m8_ElEA:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/LinuxAdminZone/~4/S0BzSC3JYLM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://linuxadminzone.com/redis-master-slave-setup-with-persistency-of-slave-updates-master-datadir/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://linuxadminzone.com/redis-master-slave-setup-with-persistency-of-slave-updates-master-datadir/</feedburner:origLink></item>
		<item>
		<title>How to install redis with master slave config in ubuntu ec2 instance</title>
		<link>http://feedproxy.google.com/~r/LinuxAdminZone/~3/ly5jWccrSOw/</link>
		<comments>http://linuxadminzone.com/how-to-install-redis-with-master-slave-config-in-ubuntu-ec2-instance/#comments</comments>
		<pubDate>Wed, 24 Oct 2012 06:21:52 +0000</pubDate>
		<dc:creator>jagbir</dc:creator>
				<category><![CDATA[AWS]]></category>
		<category><![CDATA[ec2]]></category>
		<category><![CDATA[nosql]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[redis]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://linuxadminzone.com/?p=588</guid>
		<description><![CDATA[Redis is an open source, advanced key-value store. This guide provides you instructions/commands you can use to install and configure redis with master/slave config in two ec2 Ubuntu instances. Our intention is to use redis to store php sessions which needs to be shared among other instances. 1. Open ports in aws firewall: Redis works [...]]]></description>
				<content:encoded><![CDATA[<p></p><p><a href="http://redis.io">Redis</a> is an open source, advanced key-value store. This guide provides you instructions/commands you can use to install and configure redis with master/slave config in two ec2 Ubuntu instances. Our intention is to use redis to store php sessions which needs to be shared among other instances. </p>
<p><strong>1. Open ports in aws firewall:</strong><br />
Redis works with port 6379 which you need to open in aws firewall. You can do this by command line tools or Amazon Consle. IN case of Console, navigate to Security Groups, select security group in which instances are running and then select &#8220;inbound&#8221; tab to see/update rules. </p>
<p>Put port 6379 in port range text box and select the same security group (in which you are working right now) so that all instances in this security group can community using this port. Make sure to not open the port for world (by selecting 0.0.0.0/0 in source), this would be really bad practice from security point of view. </p>
<p>If you need to monitor redis from some other host/nagios etc. You need to open port for them as well either by specifying their IP address or their security group. </p>
<p><strong>2. Install using apt-get </strong><br />
You can setup Redis by installing using apt-get/aptitute or downloading manually. Both are easy ways but this installation through apt-get will be quick though it won&#8217;t fetch you latest/greatest stable build of Redis. </p>
<p>If you want to install latest build, then you should compile it and for that you can jump to step 3 right now.</p>
<p>A quick search for redis-server shows that the default Ubuntu repositories having very old version of Redis:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">apt-cache show</span> redis-server
Package: redis-server
Priority: optional
Section: universe<span style="color: #000000; font-weight: bold;">/</span>misc
Installed-Size: <span style="color: #000000;">511</span>
Maintainer: Ubuntu Developers <span style="color: #000000; font-weight: bold;">&lt;</span>ubuntu-devel-discuss<span style="color: #000000; font-weight: bold;">@</span>lists.ubuntu.com<span style="color: #000000; font-weight: bold;">&gt;</span>
Original-Maintainer: Chris Lamb <span style="color: #000000; font-weight: bold;">&lt;</span>lamby<span style="color: #000000; font-weight: bold;">@</span>debian.org<span style="color: #000000; font-weight: bold;">&gt;</span>
Architecture: amd64
Source: redis
Version: <span style="color: #000000;">2</span>:2.2.12-1build1
Depends: libc6 <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&gt;</span>= <span style="color: #000000;">2.7</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>, adduser
Filename: pool<span style="color: #000000; font-weight: bold;">/</span>universe<span style="color: #000000; font-weight: bold;">/</span>r<span style="color: #000000; font-weight: bold;">/</span>redis<span style="color: #000000; font-weight: bold;">/</span>redis-server_2.2.12-1build1_amd64.deb
...
...</pre></td></tr></table></div>

<p>Here you can see redis-server_2.2.12 while on Redis <a href="http://redis.io/download">site</a>, latest stable version is 2.4.17 as of writing this article. Obviously we should not install the old 2.2.12 release. </p>
<p>We can pick another repository which should have latest or at least near to latest version. One of such repository from where you can get the package is <a href="http://www.dotdeb.org/instructions/">dotdeb</a>. Here in this repositories I can see version 2.4.16 which is very near to latest release. </p>
<p>Add following in /etc/apt/sources.list file:</p><div class="wpInsert wpInsertInPostAd wpInsertMiddle" style="margin-left: 150px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-2792824039251059";
/* BoxAd-336x280 */
google_ad_slot = "2138368529";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">vim</span>  <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>apt<span style="color: #000000; font-weight: bold;">/</span>sources.list
	deb http:<span style="color: #000000; font-weight: bold;">//</span>packages.dotdeb.org squeeze all
	deb-src http:<span style="color: #000000; font-weight: bold;">//</span>packages.dotdeb.org squeeze all</pre></td></tr></table></div>

<p>Get and add dotdeb gnupg key after which run apt-get update and install redis-server:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>www.dotdeb.org<span style="color: #000000; font-weight: bold;">/</span>dotdeb.gpg; <span style="color: #c20cb9; font-weight: bold;">cat</span> dotdeb.gpg <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-key add</span> -
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get update</span>
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get install</span> redis-server</pre></td></tr></table></div>

<p><strong>3. Install using manual downloading it from redis site</strong><br />
This will enable you to get latest stable binaries of redis server. This process is neatly explained on Redis site itself <a href="http://redis.io/topics/quickstart">here</a>.</p>
<p>I&#8217;m here quickly putting commands with minor changes:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get install</span> build-essential
$ <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>src<span style="color: #000000; font-weight: bold;">/</span>
$ <span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>download.redis.io<span style="color: #000000; font-weight: bold;">/</span>redis-stable.tar.gz
$ <span style="color: #c20cb9; font-weight: bold;">tar</span> xvzf redis-stable.tar.gz
$ <span style="color: #7a0874; font-weight: bold;">cd</span> redis-stable
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">make</span>
$ <span style="color: #7a0874; font-weight: bold;">cd</span> src 
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">cp</span> redis-server <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">cp</span> redis-cli <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>
&nbsp;
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>redis <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>redis <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>log<span style="color: #000000; font-weight: bold;">/</span>redis
$ <span style="color: #7a0874; font-weight: bold;">cd</span> ..<span style="color: #000000; font-weight: bold;">/</span>utils; <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">cp</span> redis.conf.tpl <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>redis<span style="color: #000000; font-weight: bold;">/</span>redis.conf; <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">cp</span> redis_init_script <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>redis
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> update-rc.d redis defaults</pre></td></tr></table></div>

<p><strong>4. Update config to make master/slave</strong><br />
After following above commands in both of the instances, you will have Redis installed and running in them. Making Redis work in master/slave config is relatively very easy, all you need to do is to tell one of server to get updates from other. On the instance, where you want to make Redis slave, login in it and update redis.conf file. Essential parameters you need to check/update are shown below:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">vim</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>redis<span style="color: #000000; font-weight: bold;">/</span>redis.conf
daemonize <span style="color: #c20cb9; font-weight: bold;">yes</span>
pidfile <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>run<span style="color: #000000; font-weight: bold;">/</span>redis<span style="color: #000000; font-weight: bold;">/</span>redis-server.pid
port <span style="color: #000000;">6379</span>
<span style="color: #666666; font-style: italic;">#bind 127.0.0.1</span>
slaveof <span style="color: #000000;">10</span>.x.x.x <span style="color: #000000;">6379</span></pre></td></tr></table></div>

<p>You can see the last line <strong>slaveof <ip of master>
<port></strong> which specify from which server it should get updates using which port. It&#8217;s a good idea to specify internal IP of other instance for such internal communication for relatively better performance as well as save some costs. </p>
<p>You need to check/update config in both of the servers except on master, do not put that &#8220;slaveof&#8221; configuration line. </p>
<p>Start redis and check:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>redis start
$ redis-cli <span style="color: #c20cb9; font-weight: bold;">ping</span>
PONG</pre></td></tr></table></div>

<p>Redis is installed, configured and running. </p>
<p><strong>5. Install PHP client and update PHP config to use it</strong><br />
Our intention is to use Redis with PHP and for that you need to install Redis client for PHP. You can get more info about Redis clients <a href="http://redis.io/clients">here</a>.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get install</span> php5-redis
$ <span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>php5<span style="color: #000000; font-weight: bold;">/</span>conf.d<span style="color: #000000; font-weight: bold;">/</span>redis.ini
; configuration <span style="color: #000000; font-weight: bold;">for</span> php redis module
<span style="color: #007800;">extension</span>=redis.so
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> service apache2 reload
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> service apache2 reload
 <span style="color: #000000; font-weight: bold;">*</span> Reloading web server config apache2
   ...done.</pre></td></tr></table></div>

<p>You can see that PHP config is updated to use Redis extension. We reloaded Apache to apply updated PHP configs. You are good to go. Let me know if you face any issues or there&#8217;s any suggestions to improve the process. </p>
<div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-2792824039251059";
/* LinksBottom-468x15 */
google_ad_slot = "9848270356";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LinuxAdminZone?a=ly5jWccrSOw:e4BEk2P3rn8:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LinuxAdminZone?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LinuxAdminZone?a=ly5jWccrSOw:e4BEk2P3rn8:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LinuxAdminZone?i=ly5jWccrSOw:e4BEk2P3rn8:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LinuxAdminZone?a=ly5jWccrSOw:e4BEk2P3rn8:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/LinuxAdminZone?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LinuxAdminZone?a=ly5jWccrSOw:e4BEk2P3rn8:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/LinuxAdminZone?i=ly5jWccrSOw:e4BEk2P3rn8:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/LinuxAdminZone/~4/ly5jWccrSOw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://linuxadminzone.com/how-to-install-redis-with-master-slave-config-in-ubuntu-ec2-instance/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://linuxadminzone.com/how-to-install-redis-with-master-slave-config-in-ubuntu-ec2-instance/</feedburner:origLink></item>
		<item>
		<title>Quickly setup rvm, Ruby, RubyGems and Apache with Passenger in aws ec2 ubuntu instance</title>
		<link>http://feedproxy.google.com/~r/LinuxAdminZone/~3/oYT4PFpxCZE/</link>
		<comments>http://linuxadminzone.com/quickly-setup-rvm-ruby-rubygems-and-apache-with-passenger-in-aws-ec2-ubuntu-instance/#comments</comments>
		<pubDate>Mon, 08 Oct 2012 04:59:04 +0000</pubDate>
		<dc:creator>jagbir</dc:creator>
				<category><![CDATA[AWS]]></category>
		<category><![CDATA[ec2]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[passenger]]></category>
		<category><![CDATA[rvm]]></category>

		<guid isPermaLink="false">http://linuxadminzone.com/?p=567</guid>
		<description><![CDATA[While you can download Ruby directly from here or from repositories (though you may not find latest version in it). I preferred using the awesome Ruby version manager tool (rvm) for this purpose. Our setup was in requirement of latest Ruby, Gems and Passenger with Apache2 module and that setup seems to be straightforward but [...]]]></description>
				<content:encoded><![CDATA[<p></p><p>While you can download Ruby directly from <a href="http://www.ruby-lang.org/en/downloads/" target="_blank">here</a> or from repositories (though you may not find latest version in it). I preferred using the awesome Ruby version manager tool (<a href="https://rvm.io/rvm/install/" target="_blank">rvm</a>) for this purpose. Our setup was in requirement of  latest Ruby, Gems and Passenger with Apache2 module and that setup seems to be straightforward but I faced lots of smaller issues and hence thought of collecting and putting final commands here so that next time it would be a quick setup without all that digging. </p>
<p>These commands are tested successfully in Ubuntu 12.04 ec2 instance. </p>
<p><strong>Step 1. Install essential packages</strong><br />
I am using latest Ubuntu 12.04 AMI in a new small instance. We need to install required packages/libraries first otherwise you will be bumped up with hiccups and needs to install them one by one when got stuck so let&#8217;s install them at first place itself:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666;">$ </span><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get install</span> build-essential apache2 zlib1g-dev libcurl4-openssl-dev libssl-dev apache2-prefork-dev libapr1-dev libaprutil1-dev</pre></td></tr></table></div>

<p><strong>Step 2. Install RVM and latest stable Ruby</strong><br />
We will install RVM and specify that it should install latest stable Ruby as well.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> curl <span style="color: #660033;">-L</span> https:<span style="color: #000000; font-weight: bold;">//</span>get.rvm.io <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">bash</span> <span style="color: #660033;">-s</span> stable <span style="color: #660033;">--ruby</span>
$ <span style="color: #7a0874; font-weight: bold;">source</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>rvm<span style="color: #000000; font-weight: bold;">/</span>scripts<span style="color: #000000; font-weight: bold;">/</span>rvm</pre></td></tr></table></div>

<p>Please note that you need to run the last &#8216;source /usr/local/rvm/scripts/rvm&#8217; command once for all users where Ruby availability is needed otherwise other users won&#8217;t find Ruby like below example:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">su</span> exampleuser
$ ruby <span style="color: #660033;">--version</span>
The program <span style="color: #ff0000;">'ruby'</span> can be found <span style="color: #000000; font-weight: bold;">in</span> the following packages:
 <span style="color: #000000; font-weight: bold;">*</span> ruby1.8
 <span style="color: #000000; font-weight: bold;">*</span> ruby1.9.1
Try: <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get install</span> <span style="color: #000000; font-weight: bold;">&lt;</span>selected package<span style="color: #000000; font-weight: bold;">&gt;</span>
&nbsp;
$ <span style="color: #7a0874; font-weight: bold;">source</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>rvm<span style="color: #000000; font-weight: bold;">/</span>scripts<span style="color: #000000; font-weight: bold;">/</span>rvm
&nbsp;
$ ruby <span style="color: #660033;">--version</span>
ruby 1.9.3p194 <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">2012</span>-04-<span style="color: #000000;">20</span> revision <span style="color: #000000;">35410</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>x86_64-linux<span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></td></tr></table></div>

<p>Ruby is installed. Let&#8217;s proceed to setup Apache2 with Passenger now.</p><div class="wpInsert wpInsertInPostAd wpInsertMiddle" style="margin-left: 150px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-2792824039251059";
/* BoxAd-336x280 */
google_ad_slot = "2138368529";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<p><strong>Step3. Install Passenger and its apache module</strong><br />
We need to install OpenSSL extension/library for Ruby, I tried installing it with apt-get but that&#8217;s also fetching Ruby1.8 libraries which I don&#8217;t want to install, so I got it from Ruby source code, compiled and installed:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>rvm<span style="color: #000000; font-weight: bold;">/</span>src<span style="color: #000000; font-weight: bold;">/</span>ruby-1.9.3-p194<span style="color: #000000; font-weight: bold;">/</span>ext<span style="color: #000000; font-weight: bold;">/</span>openssl<span style="color: #000000; font-weight: bold;">/</span>
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> ruby extconf.rb
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span></pre></td></tr></table></div>

<p>Time to install passenger, module for apache and configure apache to use that module:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> gem <span style="color: #c20cb9; font-weight: bold;">install</span> passenger
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> passenger-install-apache2-module
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">vim</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>apache2<span style="color: #000000; font-weight: bold;">/</span>httpd.conf
   LoadModule passenger_module <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>rvm<span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span>ruby-1.9.3-p194<span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span>passenger-3.0.17<span style="color: #000000; font-weight: bold;">/</span>ext<span style="color: #000000; font-weight: bold;">/</span>apache2<span style="color: #000000; font-weight: bold;">/</span>mod_passenger.so
   PassengerRoot <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>rvm<span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span>ruby-1.9.3-p194<span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span>passenger-3.0.17
   PassengerRuby <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>rvm<span style="color: #000000; font-weight: bold;">/</span>wrappers<span style="color: #000000; font-weight: bold;">/</span>ruby-1.9.3-p194<span style="color: #000000; font-weight: bold;">/</span>ruby
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> service apache2 restart</pre></td></tr></table></div>

<p>Setup is done. </p>
<p>If you enjoyed this article, please share it with your friends. If there&#8217;s any issue/correction/suggestion, I&#8217;d be glad to hear that, please put it in comments below. </p>
<p><strong>Other helpful articles in this blow on AWS ec2: </strong></p>
<ul>
<li> <a href="http://linuxadminzone.com/quickly-setup-git-server-with-gitolite-gitweb-ssh-and-http-auth/" target="_blank">Quickly setup git server with gitolite, gitweb, ssh and http auth</a></li>
<li> <a href="http://linuxadminzone.com/create-secure-mongodb-replica-set-in-amazon-aws-ec2-instances/"  target="_blank"> Create secure mongodb replica-set in aws ec2 instance</a> </li>
<li> <a href="http://linuxadminzone.com/create-and-manage-raid0-raid10-using-ebs-volumes-in-aws-ec2-ubuntu-instance/"  target="_blank"> create and manage raid0, raid10 using ebs volumes in aws ec2 instance</a> </li>
<li> <a href="http://linuxadminzone.com/simple-upstart-job-in-ubuntu-to-ensure-your-processes-keep-running/"  target="_blank"> Simple upstart job in ubuntu to ensure your processes keep running</a> </li>
<li> <a href="http://linuxadminzone.com/install-and-configure-ftp-server-in-amazon-ec2-instance/"> Install and configure ftp server in aws ec2 </a> </li>
</ul>
<div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-2792824039251059";
/* LinksBottom-468x15 */
google_ad_slot = "9848270356";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LinuxAdminZone?a=oYT4PFpxCZE:ZSe1WCjNhXw:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LinuxAdminZone?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LinuxAdminZone?a=oYT4PFpxCZE:ZSe1WCjNhXw:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LinuxAdminZone?i=oYT4PFpxCZE:ZSe1WCjNhXw:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LinuxAdminZone?a=oYT4PFpxCZE:ZSe1WCjNhXw:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/LinuxAdminZone?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LinuxAdminZone?a=oYT4PFpxCZE:ZSe1WCjNhXw:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/LinuxAdminZone?i=oYT4PFpxCZE:ZSe1WCjNhXw:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/LinuxAdminZone/~4/oYT4PFpxCZE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://linuxadminzone.com/quickly-setup-rvm-ruby-rubygems-and-apache-with-passenger-in-aws-ec2-ubuntu-instance/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://linuxadminzone.com/quickly-setup-rvm-ruby-rubygems-and-apache-with-passenger-in-aws-ec2-ubuntu-instance/</feedburner:origLink></item>
		<item>
		<title>create secure mongodb replica-set in amazon aws ec2 instances</title>
		<link>http://feedproxy.google.com/~r/LinuxAdminZone/~3/ykv9J5Le5-8/</link>
		<comments>http://linuxadminzone.com/create-secure-mongodb-replica-set-in-amazon-aws-ec2-instances/#comments</comments>
		<pubDate>Mon, 10 Sep 2012 10:49:59 +0000</pubDate>
		<dc:creator>jagbir</dc:creator>
				<category><![CDATA[AWS]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[ec2]]></category>
		<category><![CDATA[mongodb]]></category>
		<category><![CDATA[replica-set]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://linuxadminzone.com/?p=548</guid>
		<description><![CDATA[Installation and configuration of MongoDB is relatively very easy. There are different ways you can run MongoDB, be it running single server, master-slave, replica-set or running in sharded/cluster way. By default, MongoDB installation doesn&#8217;t provide secure environment where clients should authenticate themselves before accesssing database. My setup consists 2 small instances running as normal MongoDB [...]]]></description>
				<content:encoded><![CDATA[<p></p><p>Installation and configuration of MongoDB is relatively very <a href="http://docs.mongodb.org/manual/tutorial/install-mongodb-on-debian-or-ubuntu-linux/" target="_blank">easy</a>. There are different ways you can run MongoDB, be it running single server, master-slave, <a href="http://docs.mongodb.org/manual/administration/replica-sets/" target="_blank">replica-set</a> or running in sharded/cluster way. By default, MongoDB installation doesn&#8217;t provide secure environment where clients should authenticate themselves before accesssing database. </p>
<p>My setup consists 2 small instances running as normal MongoDB servers and 1 micro instance running as arbiter. An arbiter is special member in replica-set which takes part in voting/selection of primary but doesn&#8217;t store any data in it. Its perfectly fine to host your arbiter server within an existing server running for some other service like load balancer (haproxy) etc because of its lighweight resource utilization. To make setup secure, clients must use username/password to connect to MongoDB and also members of replica-set must have shared key file.  </p>
<p>I have tested instructions written below in instances running Ubuntu 12.04 but I strongly reccommend to have adequate testing/checking before putting such setup in production. </p>
<p><strong>Step 1: Create instances and install MongoDB</strong></p>
<p>Create 3 instances of your choice (micro/small/medium etc). I have used 64-bit publicly available Ubuntu AMIs from <a href="http://cloud.ubuntu.com/ami/" target="_blank">here</a> for my instances. Let&#8217;s assume we got 3 instances with following internal IPs: </p>
<p>Instance1: 10.150.150.150<br />
Instance2: 10.150.150.151<br />
Instance3: 10.150.150.152</p>
<p>Time to configure them now, execute following commands in all 3 instances:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-key adv</span> <span style="color: #660033;">--keyserver</span> keyserver.ubuntu.com <span style="color: #660033;">--recv</span> 7F0CEB10
$ <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">tee</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>apt<span style="color: #000000; font-weight: bold;">/</span>sources.list.d<span style="color: #000000; font-weight: bold;">/</span>10gen.list
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get update</span>
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get install</span> mongodb-10gen</pre></td></tr></table></div>

<p>Here first we added 10gen key in our keys database and then added mongodb repository path. After that apt-get install mongodb-10gen will install latest stable release of mongodb in our server. </p>
<p>You can also store your mongodb database in separate EBS volume or with RAIDed EBS volumes, If interested, please refer this detailed article about it: <a href="http://linuxadminzone.com/create-and-manage-raid0-raid10-using-ebs-volumes-in-aws-ec2-ubuntu-instance/"> create and manage RAID0/RAID10 using EBS volumes in AWS EC2 Ubuntu instance </a></p>
<p><strong>Step 2: Configure replica-set name and initialize it</strong><br />
Open the mongodb config file <strong>/etc/mongodb.conf</strong> and jump to last line. Uncomment the line which starts with replSet and put a name for your replica-set, your file should look like this in all 3 instances:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">tail</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>mongodb.conf
<span style="color: #666666; font-style: italic;">#slave = true</span>
<span style="color: #666666; font-style: italic;">#source = master.example.com</span>
<span style="color: #666666; font-style: italic;"># Slave only: specify a single database to replicate</span>
<span style="color: #666666; font-style: italic;">#only = master.example.com</span>
<span style="color: #666666; font-style: italic;"># or</span>
<span style="color: #666666; font-style: italic;">#master = true</span>
<span style="color: #666666; font-style: italic;">#source = slave.example.com</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># in replica set configuration, specify the name of the replica set</span>
replSet = myreplica</pre></td></tr></table></div>

<p>Make sure to restart the mongodb service after any change in config file:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> service mongodb restart
mongodb stop<span style="color: #000000; font-weight: bold;">/</span>waiting
mongodb start<span style="color: #000000; font-weight: bold;">/</span>running, process <span style="color: #000000;">28129</span></pre></td></tr></table></div>

<p>Now in instance1 or instance2, start mongo shell by typing &#8216;mongo&#8217;. Initialize the replica-set and other 2 instances. Please remember that our 3rd instance is arbiter one so there&#8217;s different command to add that instance in replica-set:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666;">ubuntu@instance1:~$ </span>mongo
<span style="color: #000000; font-weight: bold;">&gt;</span> rs.initiate<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">&gt;</span> rs.add<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">&quot;10.150.150.151&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>; 
<span style="color: #000000; font-weight: bold;">&gt;</span> rs.addArb<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">&quot;10.150.150.152&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;  <span style="color: #666666; font-style: italic;">## its rs.addArb() to add arbiter</span></pre></td></tr></table></div>

<p>The output of these commands are not shown here because I already have working replica-set and just replaying the command for the purpose of this article. Anyhow, mongo should reply with &#8220;ok&#8221; upon a correct command and throws error otherwise.</p>
<p>Once you add servers in replica-set, the members of replica-set have voting among themselves where arbiter will vote and the server which have highest uptime will become primary (you can override that with priorty setting though). </p>
<p>After a while of initializing replica-set, the instance1 should become primary:</p><div class="wpInsert wpInsertInPostAd wpInsertMiddle" style="margin-left: 150px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-2792824039251059";
/* BoxAd-336x280 */
google_ad_slot = "2138368529";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">$ mongo
myreplica:PRIMARY<span style="color: #000000; font-weight: bold;">&gt;</span>  rs.conf<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;
<span style="color: #7a0874; font-weight: bold;">&#123;</span>
        <span style="color: #ff0000;">&quot;_id&quot;</span> : <span style="color: #ff0000;">&quot;myreplica&quot;</span>,
        <span style="color: #ff0000;">&quot;version&quot;</span> : <span style="color: #000000;">6</span>,
        <span style="color: #ff0000;">&quot;members&quot;</span> : <span style="color: #7a0874; font-weight: bold;">&#91;</span>
                <span style="color: #7a0874; font-weight: bold;">&#123;</span>
                        <span style="color: #ff0000;">&quot;_id&quot;</span> : <span style="color: #000000;">0</span>,
                        <span style="color: #ff0000;">&quot;host&quot;</span> : <span style="color: #ff0000;">&quot;10.150.150.150&quot;</span>,
                        <span style="color: #ff0000;">&quot;priority&quot;</span> : <span style="color: #000000;">2</span>
                <span style="color: #7a0874; font-weight: bold;">&#125;</span>,
                <span style="color: #7a0874; font-weight: bold;">&#123;</span>
                        <span style="color: #ff0000;">&quot;_id&quot;</span> : <span style="color: #000000;">1</span>,
                        <span style="color: #ff0000;">&quot;host&quot;</span> : <span style="color: #ff0000;">&quot;10.150.150.151&quot;</span>,
                        <span style="color: #ff0000;">&quot;priority&quot;</span> : <span style="color: #000000;">2</span>
                <span style="color: #7a0874; font-weight: bold;">&#125;</span>,
                <span style="color: #7a0874; font-weight: bold;">&#123;</span>
                        <span style="color: #ff0000;">&quot;_id&quot;</span> : <span style="color: #000000;">2</span>,
                        <span style="color: #ff0000;">&quot;host&quot;</span> : <span style="color: #ff0000;">&quot;10.150.150.152&quot;</span>,
                        <span style="color: #ff0000;">&quot;arbiterOnly&quot;</span> : <span style="color: #c20cb9; font-weight: bold;">true</span>
                <span style="color: #7a0874; font-weight: bold;">&#125;</span>
        <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
myreplica:PRIMARY<span style="color: #000000; font-weight: bold;">&gt;</span></pre></td></tr></table></div>

<p>You can use &#8220;rs.status()&#8221; for more detailed information about replica-set:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">myreplica:PRIMARY<span style="color: #000000; font-weight: bold;">&gt;</span> rs.status<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;
<span style="color: #7a0874; font-weight: bold;">&#123;</span>
        <span style="color: #ff0000;">&quot;set&quot;</span> : <span style="color: #ff0000;">&quot;myreplica&quot;</span>,
        <span style="color: #ff0000;">&quot;date&quot;</span> : ISODate<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">&quot;2012-09-10T09:48:25Z&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>,
        <span style="color: #ff0000;">&quot;myState&quot;</span> : <span style="color: #000000;">1</span>,
        <span style="color: #ff0000;">&quot;members&quot;</span> : <span style="color: #7a0874; font-weight: bold;">&#91;</span>
                <span style="color: #7a0874; font-weight: bold;">&#123;</span>
                        <span style="color: #ff0000;">&quot;_id&quot;</span> : <span style="color: #000000;">0</span>,
                        <span style="color: #ff0000;">&quot;name&quot;</span> : <span style="color: #ff0000;">&quot;10.150.150.150&quot;</span>,
                        <span style="color: #ff0000;">&quot;health&quot;</span> : <span style="color: #000000;">1</span>,
                        <span style="color: #ff0000;">&quot;state&quot;</span> : <span style="color: #000000;">1</span>,
                        <span style="color: #ff0000;">&quot;stateStr&quot;</span> : <span style="color: #ff0000;">&quot;PRIMARY&quot;</span>,
                        <span style="color: #ff0000;">&quot;uptime&quot;</span> : <span style="color: #000000;">269445</span>,
                        <span style="color: #ff0000;">&quot;optime&quot;</span> : Timestamp<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">1347270449000</span>, <span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>,
                        <span style="color: #ff0000;">&quot;optimeDate&quot;</span> : ISODate<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">&quot;2012-09-10T09:47:29Z&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>,
                        <span style="color: #ff0000;">&quot;self&quot;</span> : <span style="color: #c20cb9; font-weight: bold;">true</span>
                <span style="color: #7a0874; font-weight: bold;">&#125;</span>,
                <span style="color: #7a0874; font-weight: bold;">&#123;</span>
                        <span style="color: #ff0000;">&quot;_id&quot;</span> : <span style="color: #000000;">1</span>,
                        <span style="color: #ff0000;">&quot;name&quot;</span> : <span style="color: #ff0000;">&quot;10.150.150.151&quot;</span>,
                        <span style="color: #ff0000;">&quot;health&quot;</span> : <span style="color: #000000;">1</span>,
                        <span style="color: #ff0000;">&quot;state&quot;</span> : <span style="color: #000000;">2</span>,
                        <span style="color: #ff0000;">&quot;stateStr&quot;</span> : <span style="color: #ff0000;">&quot;SECONDARY&quot;</span>,
                        <span style="color: #ff0000;">&quot;uptime&quot;</span> : <span style="color: #000000;">269303</span>,
                        <span style="color: #ff0000;">&quot;optime&quot;</span> : Timestamp<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">1347270449000</span>, <span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>,
                        <span style="color: #ff0000;">&quot;optimeDate&quot;</span> : ISODate<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">&quot;2012-09-10T09:47:29Z&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>,
                        <span style="color: #ff0000;">&quot;lastHeartbeat&quot;</span> : ISODate<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">&quot;2012-09-10T09:48:23Z&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>,
                        <span style="color: #ff0000;">&quot;pingMs&quot;</span> : <span style="color: #000000;">0</span>
                <span style="color: #7a0874; font-weight: bold;">&#125;</span>,
                <span style="color: #7a0874; font-weight: bold;">&#123;</span>
                        <span style="color: #ff0000;">&quot;_id&quot;</span> : <span style="color: #000000;">2</span>,
                        <span style="color: #ff0000;">&quot;name&quot;</span> : <span style="color: #ff0000;">&quot;10.150.150.152&quot;</span>,
                        <span style="color: #ff0000;">&quot;health&quot;</span> : <span style="color: #000000;">1</span>,
                        <span style="color: #ff0000;">&quot;state&quot;</span> : <span style="color: #000000;">7</span>,
                        <span style="color: #ff0000;">&quot;stateStr&quot;</span> : <span style="color: #ff0000;">&quot;ARBITER&quot;</span>,
                        <span style="color: #ff0000;">&quot;uptime&quot;</span> : <span style="color: #000000;">269261</span>,
                        <span style="color: #ff0000;">&quot;lastHeartbeat&quot;</span> : ISODate<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">&quot;2012-09-10T09:48:24Z&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>,
                        <span style="color: #ff0000;">&quot;pingMs&quot;</span> : <span style="color: #000000;">0</span>
                <span style="color: #7a0874; font-weight: bold;">&#125;</span>
        <span style="color: #7a0874; font-weight: bold;">&#93;</span>,
        <span style="color: #ff0000;">&quot;ok&quot;</span> : <span style="color: #000000;">1</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>You can jump to other hosts instance2/instance3 and check status from there as well. Now the replica-set is operational, let&#8217;s make it secure. </p>
<p><strong>Step 3: Enable authentication and secure replica-set</strong><br />
Authentication is not enabled by default in MongoDB but its very easy to use. Open config file <strong>/etc/mongodb.conf</strong> and uncomment the <strong>auth=true</strong> line and comment out noauth line. These two lines should look like below:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">grep</span> auth <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>mongodb.conf
<span style="color: #666666; font-style: italic;">#noauth = true</span>
auth = <span style="color: #c20cb9; font-weight: bold;">true</span></pre></td></tr></table></div>

<p>Make this change in all instances and restart mongodb service.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> service mongodb restart
mongodb stop<span style="color: #000000; font-weight: bold;">/</span>waiting
mongodb start<span style="color: #000000; font-weight: bold;">/</span>running, process <span style="color: #000000;">28329</span></pre></td></tr></table></div>

<p>Go to mongo shell again and configure the administrative user:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">$ mongo
<span style="color: #000000; font-weight: bold;">&gt;</span> use admin; 
switched to db admin
<span style="color: #000000; font-weight: bold;">&gt;</span> db.addUser<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">&quot;mongoadmin&quot;</span>,<span style="color: #ff0000;">&quot;mongopass&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;</pre></td></tr></table></div>

<p>After this, you need to authenticate yourself to access details. Let&#8217;s try to get in mongo shell and show databases:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">$ mongo
<span style="color: #000000; font-weight: bold;">&gt;</span> show dbs; 
Mon Sep <span style="color: #000000;">10</span> <span style="color: #000000;">10</span>:02:09 uncaught exception: listDatabases failed:<span style="color: #7a0874; font-weight: bold;">&#123;</span> <span style="color: #ff0000;">&quot;errmsg&quot;</span> : <span style="color: #ff0000;">&quot;need to login&quot;</span>, <span style="color: #ff0000;">&quot;ok&quot;</span> : <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>So you need to supply your credentials:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&gt;</span> use admin;
<span style="color: #000000; font-weight: bold;">&gt;</span> db.auth<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">&quot;mongoadmin&quot;</span>,<span style="color: #ff0000;">&quot;mongopass&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>; 
<span style="color: #000000;">1</span>
myreplica:PRIMARY<span style="color: #000000; font-weight: bold;">&gt;</span></pre></td></tr></table></div>

<p>If you have some database, you can create username/password for that and instruct teams to use that user/pass to get connect. Let&#8217;s say you have a database name proddb, create a user/pass for it:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">myreplica:PRIMARY<span style="color: #000000; font-weight: bold;">&gt;</span> use proddb; 
myreplica:PRIMARY<span style="color: #000000; font-weight: bold;">&gt;</span> db.addUser<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">&quot;produ&quot;</span>,<span style="color: #ff0000;">&quot;mypass&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;
myreplica:PRIMARY<span style="color: #000000; font-weight: bold;">&gt;</span> db.addUser<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">&quot;produread&quot;</span>,<span style="color: #ff0000;">&quot;otherpass&quot;</span>, <span style="color: #c20cb9; font-weight: bold;">true</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;</pre></td></tr></table></div>

<p>Here, <strong>produ</strong> user will have read/write access to database <strong>proddb</strong> while <strong>produread</strong> will have only read-only access. </p>
<p><strong>Step 4: Secure replica-set members</strong><br />
To make your replica-set more secure, you can enable <strong>keyFile</strong> option where each server needs to have an identical key file which works like a password for their internal communication. Essentially its just a text file. </p>
<p>Repeat following commands in all instances.</p>
<p>Create a key file:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;myrandomtextforkeyfiletosecurereplicaset&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">tee</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>mongodb.key
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">vim</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>mongodb.conf
<span style="color: #007800;">keyFile</span>=<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>mongodb.key  <span style="color: #666666; font-style: italic;">## uncomment and set path of key file</span>
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> service mongodb restart</pre></td></tr></table></div>

<p>Now each member should have this identical file to remain a member of replica-set. Get more information about securing MongoDB from <a href="http://www.mongodb.org/display/DOCS/Security+and+Authentication" target="_blank">here</a>. </p>
<p>Have you tried securing your MongoDB installation? Is there anything you would like to recommend in this context or if there&#8217;s any comment/suggestion, please put it in comments below. </p>
<p>Helpful related articles:<br />
* <a href="http://linuxadminzone.com/create-and-manage-raid0-raid10-using-ebs-volumes-in-aws-ec2-ubuntu-instance/"> create and manage RAID0/RAID10 using EBS volumes in AWS EC2 Ubuntu instance </a><br />
* <a href="http://linuxadminzone.com/simple-and-efficient-mongodb-backup-using-script/"> Simple and efficient MongoDB Backup using script </a> </p>
<div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-2792824039251059";
/* LinksBottom-468x15 */
google_ad_slot = "9848270356";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LinuxAdminZone?a=ykv9J5Le5-8:4-C5YjUmAmc:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LinuxAdminZone?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LinuxAdminZone?a=ykv9J5Le5-8:4-C5YjUmAmc:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LinuxAdminZone?i=ykv9J5Le5-8:4-C5YjUmAmc:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LinuxAdminZone?a=ykv9J5Le5-8:4-C5YjUmAmc:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/LinuxAdminZone?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LinuxAdminZone?a=ykv9J5Le5-8:4-C5YjUmAmc:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/LinuxAdminZone?i=ykv9J5Le5-8:4-C5YjUmAmc:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/LinuxAdminZone/~4/ykv9J5Le5-8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://linuxadminzone.com/create-secure-mongodb-replica-set-in-amazon-aws-ec2-instances/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://linuxadminzone.com/create-secure-mongodb-replica-set-in-amazon-aws-ec2-instances/</feedburner:origLink></item>
		<item>
		<title>Simple upstart job in Ubuntu to ensure your processes keep running</title>
		<link>http://feedproxy.google.com/~r/LinuxAdminZone/~3/Wng-U0qWE64/</link>
		<comments>http://linuxadminzone.com/simple-upstart-job-in-ubuntu-to-ensure-your-processes-keep-running/#comments</comments>
		<pubDate>Mon, 27 Aug 2012 11:20:45 +0000</pubDate>
		<dc:creator>jagbir</dc:creator>
				<category><![CDATA[bash]]></category>
		<category><![CDATA[Monitoring]]></category>
		<category><![CDATA[monitoring]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[upstart]]></category>

		<guid isPermaLink="false">http://linuxadminzone.com/?p=544</guid>
		<description><![CDATA[This is small post explaining a simple concept in Ubuntu. One of my friend requested about a way to ensure his processes keep running. Of course there are multiple ways to do and I have documented this earlier with details my earlier post. Let&#8217;s review quickly: 1. Run processes through cron but it&#8217;s only helpful [...]]]></description>
				<content:encoded><![CDATA[<p></p><p>This is small post explaining a simple concept in Ubuntu. One of my friend requested about a way to ensure his processes keep running. Of course there are multiple ways to do and I have documented this earlier with details <a href="http://linuxadminzone.com/run-scripts-as-daemon-or-through-cron-continuously/" target="_blank">my earlier post</a>.</p>
<p>Let&#8217;s review quickly: </p>
<p>1. Run processes through cron but it&#8217;s only helpful if check frequency is per minute or higher.<br />
2. Use some process monitoring solution like <a href="http://mmonit.com/monit/" target="_blank">Monit</a>.<br />
3. Create custom daemon wrapper script which keep checking processes all the time.</p>
<p>Due to some path/env issue (I have not investigated that), my friend said his scripts are not running as expected under Monit or throug cron. Also cron is not an option here because he needs checking processes very frequently, let&#8217;s say every 5 seconds. This left us third option where we can create a custom wrapper script which should keep running like a daemon and keep an eye on processes. Now the question is how to ensure that our daemon keeps running all the time?</p>
<p>As described in my earlier blog post on Redhat based Linux, we can put entry in /etc/inittab which will ensure that our daemon will always be there in processlist but that sysV type has been discontinued in Ubuntu (12.04 I am talking about) and hence we need to create a simple process using <a href="http://upstart.ubuntu.com/getting-started.html" target="_blank">upstart</a>. </p>
<p>Let&#8217;s say our daemon script path is /root/daemon/mymonitor.sh </p><div class="wpInsert wpInsertInPostAd wpInsertMiddle" style="margin-left: 150px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-2792824039251059";
/* BoxAd-336x280 */
google_ad_slot = "2138368529";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<p>Create a config file for upstart job:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"> <span style="color: #666666; font-style: italic;">#!upstart</span>
description     <span style="color: #ff0000;">&quot;MyMon: monitoring my custom processes.&quot;</span>
&nbsp;
start on startup
stop on shutdown
&nbsp;
respawn
&nbsp;
<span style="color: #7a0874; font-weight: bold;">exec</span> start-stop-daemon <span style="color: #660033;">--start</span> <span style="color: #660033;">--make-pidfile</span> <span style="color: #660033;">--pidfile</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>run<span style="color: #000000; font-weight: bold;">/</span>mymon.pid <span style="color: #660033;">--exec</span> <span style="color: #000000; font-weight: bold;">/</span>root<span style="color: #000000; font-weight: bold;">/</span>daemon<span style="color: #000000; font-weight: bold;">/</span>mymon.sh <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>log<span style="color: #000000; font-weight: bold;">/</span>mymon.log <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">1</span></pre></td></tr></table></div>

<p>Here, &#8220;start on startup&#8221; and &#8220;stop on shutdown&#8221; implies the process should get started when system boots up and should be gracefully terminated upon shutdown. </p>
<p>respawn keyword ensures that this process should be running all the time. If its crashed/stopped, it will be respawned automatically. </p>
<p>exec is used here to execute our script, it gives PATH and optional arguments to our script. You can use &#8216;script&#8217; keyword instead to write down multiple script commands to be executed by /bin/sh. You can check full documention <a href="http://upstart.ubuntu.com/cookbook/" target="_blank">here</a>. </p>
<p>After saving the upstart config, time to start our job, check its pid and try stopping it:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> start mymon 
mymon start<span style="color: #000000; font-weight: bold;">/</span>running, process <span style="color: #000000;">20890</span>
&nbsp;
$ <span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>run<span style="color: #000000; font-weight: bold;">/</span>mymon.pid 
<span style="color: #000000;">20890</span>
&nbsp;
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> stop mymon
mymon stop<span style="color: #000000; font-weight: bold;">/</span>waiting</pre></td></tr></table></div>

<p>Do you also think its simple and efficient method for process monitoring? or if you have any other suggestion? would appreciate your comments. </p>
<p>Other helpful related articles:<br />
* <a href="http://linuxadminzone.com/run-scripts-as-daemon-or-through-cron-continuously/" target="_blank"> Run scripts as daemon or through cron continuously </a><br />
* <a href="http://linuxadminzone.com/bash-script-to-backup-essential-log-files-of-linux-server/" target="_blank"> Bash script to backup essential log files in Linux </a><br />
* <a href="http://linuxadminzone.com/top-5-most-useful-commands-tools-for-linux-administrators/" target="_blank">Top 5 most useful tools for Linux Admin</a></p>
<div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-2792824039251059";
/* LinksBottom-468x15 */
google_ad_slot = "9848270356";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LinuxAdminZone?a=Wng-U0qWE64:J8COYepGBgU:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LinuxAdminZone?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LinuxAdminZone?a=Wng-U0qWE64:J8COYepGBgU:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LinuxAdminZone?i=Wng-U0qWE64:J8COYepGBgU:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LinuxAdminZone?a=Wng-U0qWE64:J8COYepGBgU:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/LinuxAdminZone?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LinuxAdminZone?a=Wng-U0qWE64:J8COYepGBgU:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/LinuxAdminZone?i=Wng-U0qWE64:J8COYepGBgU:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/LinuxAdminZone/~4/Wng-U0qWE64" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://linuxadminzone.com/simple-upstart-job-in-ubuntu-to-ensure-your-processes-keep-running/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://linuxadminzone.com/simple-upstart-job-in-ubuntu-to-ensure-your-processes-keep-running/</feedburner:origLink></item>
	</channel>
</rss>
