zookeeperStarted.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <meta content="Apache Forrest" name="Generator">
  6. <meta name="Forrest-version" content="0.8">
  7. <meta name="Forrest-skin-name" content="pelt">
  8. <title>ZooKeeper Getting Started Guide</title>
  9. <link type="text/css" href="skin/basic.css" rel="stylesheet">
  10. <link media="screen" type="text/css" href="skin/screen.css" rel="stylesheet">
  11. <link media="print" type="text/css" href="skin/print.css" rel="stylesheet">
  12. <link type="text/css" href="skin/profile.css" rel="stylesheet">
  13. <script src="skin/getBlank.js" language="javascript" type="text/javascript"></script><script src="skin/getMenu.js" language="javascript" type="text/javascript"></script><script src="skin/fontsize.js" language="javascript" type="text/javascript"></script>
  14. <link rel="shortcut icon" href="images/favicon.ico">
  15. </head>
  16. <body onload="init()">
  17. <script type="text/javascript">ndeSetTextSize();</script>
  18. <div id="top">
  19. <!--+
  20. |breadtrail
  21. +-->
  22. <div class="breadtrail">
  23. <a href="http://www.apache.org/">Apache</a> &gt; <a href="http://hadoop.apache.org/">Hadoop</a> &gt; <a href="http://hadoop.apache.org/zookeeper/">ZooKeeper</a><script src="skin/breadcrumbs.js" language="JavaScript" type="text/javascript"></script>
  24. </div>
  25. <!--+
  26. |header
  27. +-->
  28. <div class="header">
  29. <!--+
  30. |start group logo
  31. +-->
  32. <div class="grouplogo">
  33. <a href="http://hadoop.apache.org/"><img class="logoImage" alt="Hadoop" src="images/hadoop-logo.jpg" title="Apache Hadoop"></a>
  34. </div>
  35. <!--+
  36. |end group logo
  37. +-->
  38. <!--+
  39. |start Project Logo
  40. +-->
  41. <div class="projectlogo">
  42. <a href="http://hadoop.apache.org/zookeeper/"><img class="logoImage" alt="ZooKeeper" src="images/zookeeper_small.gif" title="The Hadoop database"></a>
  43. </div>
  44. <!--+
  45. |end Project Logo
  46. +-->
  47. <!--+
  48. |start Search
  49. +-->
  50. <div class="searchbox">
  51. <form action="http://www.google.com/search" method="get" class="roundtopsmall">
  52. <input value="hadoop.apache.org" name="sitesearch" type="hidden"><input onFocus="getBlank (this, 'Search the site with google');" size="25" name="q" id="query" type="text" value="Search the site with google">&nbsp;
  53. <input name="Search" value="Search" type="submit">
  54. </form>
  55. </div>
  56. <!--+
  57. |end search
  58. +-->
  59. <!--+
  60. |start Tabs
  61. +-->
  62. <ul id="tabs">
  63. <li>
  64. <a class="unselected" href="http://hadoop.apache.org/zookeeper/">Project</a>
  65. </li>
  66. <li>
  67. <a class="unselected" href="http://wiki.apache.org/hadoop/ZooKeeper">Wiki</a>
  68. </li>
  69. <li class="current">
  70. <a class="selected" href="index.html">ZooKeeper Documentation</a>
  71. </li>
  72. </ul>
  73. <!--+
  74. |end Tabs
  75. +-->
  76. </div>
  77. </div>
  78. <div id="main">
  79. <div id="publishedStrip">
  80. <!--+
  81. |start Subtabs
  82. +-->
  83. <div id="level2tabs"></div>
  84. <!--+
  85. |end Endtabs
  86. +-->
  87. <script type="text/javascript"><!--
  88. document.write("Last Published: " + document.lastModified);
  89. // --></script>
  90. </div>
  91. <!--+
  92. |breadtrail
  93. +-->
  94. <div class="breadtrail">
  95. &nbsp;
  96. </div>
  97. <!--+
  98. |start Menu, mainarea
  99. +-->
  100. <!--+
  101. |start Menu
  102. +-->
  103. <div id="menu">
  104. <div onclick="SwitchMenu('menu_selected_1.1', 'skin/')" id="menu_selected_1.1Title" class="menutitle" style="background-image: url('skin/images/chapter_open.gif');">Overview</div>
  105. <div id="menu_selected_1.1" class="selectedmenuitemgroup" style="display: block;">
  106. <div class="menuitem">
  107. <a href="index.html">Welcome</a>
  108. </div>
  109. <div class="menuitem">
  110. <a href="zookeeperOver.html">Overview</a>
  111. </div>
  112. <div class="menupage">
  113. <div class="menupagetitle">Getting Started</div>
  114. </div>
  115. <div class="menuitem">
  116. <a href="releasenotes.html">Release Notes</a>
  117. </div>
  118. </div>
  119. <div onclick="SwitchMenu('menu_1.2', 'skin/')" id="menu_1.2Title" class="menutitle">Developer</div>
  120. <div id="menu_1.2" class="menuitemgroup">
  121. <div class="menuitem">
  122. <a href="api/index.html">API Docs</a>
  123. </div>
  124. <div class="menuitem">
  125. <a href="zookeeperProgrammers.html">Programmer's Guide</a>
  126. </div>
  127. <div class="menuitem">
  128. <a href="javaExample.html">Java Example</a>
  129. </div>
  130. <div class="menuitem">
  131. <a href="zookeeperTutorial.html">Barrier and Queue Tutorial</a>
  132. </div>
  133. <div class="menuitem">
  134. <a href="recipes.html">Recipes</a>
  135. </div>
  136. </div>
  137. <div onclick="SwitchMenu('menu_1.3', 'skin/')" id="menu_1.3Title" class="menutitle">Admin &amp; Ops</div>
  138. <div id="menu_1.3" class="menuitemgroup">
  139. <div class="menuitem">
  140. <a href="zookeeperAdmin.html">Administrator's Guide</a>
  141. </div>
  142. </div>
  143. <div onclick="SwitchMenu('menu_1.4', 'skin/')" id="menu_1.4Title" class="menutitle">Contributor</div>
  144. <div id="menu_1.4" class="menuitemgroup">
  145. <div class="menuitem">
  146. <a href="zookeeperInternals.html">ZooKeeper Internals</a>
  147. </div>
  148. </div>
  149. <div onclick="SwitchMenu('menu_1.5', 'skin/')" id="menu_1.5Title" class="menutitle">Informal Documentation</div>
  150. <div id="menu_1.5" class="menuitemgroup">
  151. <div class="menuitem">
  152. <a href="http://wiki.apache.org/hadoop/ZooKeeper">Wiki</a>
  153. </div>
  154. <div class="menuitem">
  155. <a href="http://wiki.apache.org/hadoop/ZooKeeper/FAQ">FAQ</a>
  156. </div>
  157. <div class="menuitem">
  158. <a href="http://hadoop.apache.org/zookeeper/mailing_lists.html">Mailing Lists</a>
  159. </div>
  160. <div class="menuitem">
  161. <a href="zookeeperOtherInfo.html">Other Info</a>
  162. </div>
  163. </div>
  164. <div id="credit"></div>
  165. <div id="roundbottom">
  166. <img style="display: none" class="corner" height="15" width="15" alt="" src="skin/images/rc-b-l-15-1body-2menu-3menu.png"></div>
  167. <!--+
  168. |alternative credits
  169. +-->
  170. <div id="credit2"></div>
  171. </div>
  172. <!--+
  173. |end Menu
  174. +-->
  175. <!--+
  176. |start content
  177. +-->
  178. <div id="content">
  179. <div title="Portable Document Format" class="pdflink">
  180. <a class="dida" href="zookeeperStarted.pdf"><img alt="PDF -icon" src="skin/images/pdfdoc.gif" class="skin"><br>
  181. PDF</a>
  182. </div>
  183. <h1>ZooKeeper Getting Started Guide</h1>
  184. <div id="minitoc-area">
  185. <ul class="minitoc">
  186. <li>
  187. <a href="#ch_GettingStarted">Getting Started: Coordinating Distributed Applications with
  188. ZooKeeper</a>
  189. <ul class="minitoc">
  190. <li>
  191. <a href="#sc_InstallingSingleMode">Installing and Running ZooKeeper in Single Server Mode</a>
  192. </li>
  193. <li>
  194. <a href="#sc_ConnectingToZooKeeper">Connecting to ZooKeeper</a>
  195. </li>
  196. <li>
  197. <a href="#sc_ProgrammingToZooKeeper">Programming to ZooKeeper</a>
  198. </li>
  199. <li>
  200. <a href="#sc_RunningReplicatedZooKeeper">Running Replicated ZooKeeper</a>
  201. </li>
  202. <li>
  203. <a href="#Other+Optimizations">Other Optimizations</a>
  204. </li>
  205. </ul>
  206. </li>
  207. </ul>
  208. </div>
  209. <a name="N10009"></a><a name="ch_GettingStarted"></a>
  210. <h2 class="h3">Getting Started: Coordinating Distributed Applications with
  211. ZooKeeper</h2>
  212. <div class="section">
  213. <p>This document contains information to get you started quickly with
  214. Zookeeper. It is aimed primarily at developers hoping to try it out, and
  215. contains simple installation instructions for a single ZooKeeper server, a
  216. few commands to verify that it is running, and a simple programming
  217. example. Finally, as a convenience, there are a few sections regarding
  218. more complicated installations, for example running replicated
  219. deployments, and optimizing the transaction log. However for the complete
  220. instructions for commercial deployments, please refer to the <a href="zookeeperAdmin.html">Zookeeper
  221. Administrator's Guide</a>.</p>
  222. <a name="N10016"></a><a name="sc_InstallingSingleMode"></a>
  223. <h3 class="h4">Installing and Running ZooKeeper in Single Server Mode</h3>
  224. <p>Setting up a ZooKeeper server in standalone mode is
  225. straightforward. The server is contained in a single JAR file, so
  226. installation consists of copying a JAR file and creating a
  227. configuration.</p>
  228. <div class="note">
  229. <div class="label">Note</div>
  230. <div class="content">
  231. <p>Zookeeper requires Java 1.5 or more recent.</p>
  232. </div>
  233. </div>
  234. <p>Once you have downloaded the ZooKeeper source, cd to the root of
  235. your ZooKeeper source, and run "ant jar". For example:</p>
  236. <pre class="code">
  237. $ cd ~/dev/zookeeper
  238. $ ~/dev/zookeeper/: ant jar
  239. </pre>
  240. <p>This should generate a JAR file called zookeeper.jar. To start
  241. Zookeeper, compile and run zookeeper.jar.</p>
  242. <p>To start ZooKeeper you need a configuration file. Here is a sample
  243. file:</p>
  244. <pre class="code">
  245. tickTime=2000
  246. dataDir=/var/zookeeper
  247. clientPort=2181
  248. </pre>
  249. <p>This file can be called anything, but for the sake of this
  250. discussion, call it <strong>zoo.cfg</strong>. Here are
  251. the meanings for each of the fields:</p>
  252. <dl>
  253. <dt>
  254. <term>
  255. <strong>tickTime</strong>
  256. </term>
  257. </dt>
  258. <dd>
  259. <p>the basic time unit in milliseconds used by ZooKeeper. It is
  260. used to do heartbeats and the minimum session timeout will be
  261. twice the tickTime.</p>
  262. </dd>
  263. </dl>
  264. <dl>
  265. <dt>
  266. <term>
  267. <strong>dataDir</strong>
  268. </term>
  269. </dt>
  270. <dd>
  271. <p>the location to store the in-memory database snapshots and,
  272. unless specified otherwise, the transaction log of updates to the
  273. database.</p>
  274. </dd>
  275. <dt>
  276. <term>
  277. <strong>clientPort</strong>
  278. </term>
  279. </dt>
  280. <dd>
  281. <p>the port to listen for client connections</p>
  282. </dd>
  283. </dl>
  284. <p>Now that you created the configuration file, you can start
  285. ZooKeeper:</p>
  286. <pre class="code">java -cp zookeeper-dev.jar:src/java/lib/log4j-1.2.15.jar:conf org.apache.zookeeper.server.quorum.QuorumPeerMain zoo.cfg</pre>
  287. <p>ZooKeeper logs messages using log4j -- more detail
  288. available in the
  289. <a href="zookeeperProgrammers.html#Logging">Logging</a>
  290. section of the Programmer's Guide. You will see log messages
  291. coming to the console and/or a log file depending on the log4j
  292. configuration.</p>
  293. <p>The steps outlined here run ZooKeeper in standalone mode. There is
  294. no replication, so if Zookeeper process fails, the service will go down.
  295. This is fine for most development situations, but to run Zookeeper in
  296. replicated mode, please see <a href="#sc_RunningReplicatedZooKeeper">Running Replicated
  297. Zookeeper</a>.</p>
  298. <p></p>
  299. <a name="N1006F"></a><a name="sc_ConnectingToZooKeeper"></a>
  300. <h3 class="h4">Connecting to ZooKeeper</h3>
  301. <p>Once ZooKeeper is running, you have several options for connection
  302. to it:</p>
  303. <ul>
  304. <li>
  305. <p>
  306. <strong>Java</strong>:
  307. Use <span class="codefrag computeroutput">java -cp
  308. zookeeper-dev.jar:src/java/lib/log4j-1.2.15.jar:conf
  309. org.apache.zookeeper.ZooKeeperMain
  310. 127.0.0.1:2181</span>
  311. </p>
  312. <p>This lets you perform simple, file-like operations.</p>
  313. </li>
  314. <li>
  315. <p>
  316. <strong>C</strong>: compile cli_mt
  317. (multi-threaded) or cli_st (single-threaded) by running
  318. <span class="codefrag command">make cli_mt</span> or <span class="codefrag command">make cli_st</span>
  319. in the c subdirectory in the ZooKeeper sources.</p>
  320. <p>You can run the program using <em>LD_LIBRARY_PATH=.
  321. cli_mt 127.0.0.1:2181</em> or <em>LD_LIBRARY_PATH=.
  322. cli_st 127.0.0.1:2181</em>. This will give you a simple shell
  323. to execute file system like operations on ZooKeeper.</p>
  324. </li>
  325. </ul>
  326. <a name="N100A3"></a><a name="sc_ProgrammingToZooKeeper"></a>
  327. <h3 class="h4">Programming to ZooKeeper</h3>
  328. <p>ZooKeeper has a Java bindings and C bindings. They are
  329. functionally equivalent. The C bindings exist in two variants: single
  330. threaded and multi-threaded. These differ only in how the messaging loop
  331. is done. For more information, see the <a href="zookeeperProgrammers.html#ch_programStructureWithExample.html">Programming
  332. Examples in the Zookeeper Programmer's Guide</a> for
  333. sample code using of the different APIs.</p>
  334. <a name="N100B1"></a><a name="sc_RunningReplicatedZooKeeper"></a>
  335. <h3 class="h4">Running Replicated ZooKeeper</h3>
  336. <p>Running ZooKeeper in standalone mode is convenient for evaluation,
  337. some development, and testing. But in production, you should run
  338. ZooKeeper in replicated mode. A replicated group of servers in the same
  339. application is called a <em>quorum</em>, and in replicated
  340. mode, all servers in the quorum have copies of the same configuration
  341. file. The file is similar to the one used in standalone mode, but with a
  342. few differences. Here is an example:</p>
  343. <pre class="code">
  344. tickTime=2000
  345. dataDir=/var/zookeeper
  346. clientPort=2181
  347. initLimit=5
  348. syncLimit=2
  349. server.1=zoo1:2888:3888
  350. server.2=zoo2:2888:3888
  351. server.3=zoo3:2888:3888
  352. </pre>
  353. <p>The new entry, <strong>initLimit</strong> is
  354. timeouts ZooKeeper uses to limit the length of time the Zookeeper
  355. servers in quorum have to connect to a leader. The entry <strong>syncLimit</strong> limits how far out of date a server can
  356. be from a leader.</p>
  357. <p>With both of these timeouts, you specify the unit of time using
  358. <strong>tickTime</strong>. In this example, the timeout
  359. for initLimit is 5 ticks at 2000 milleseconds a tick, or 10
  360. seconds.</p>
  361. <p>The entries of the form <em>server.X</em> list the
  362. servers that make up the ZooKeeper service. When the server starts up,
  363. it knows which server it is by looking for the file
  364. <em>myid</em> in the data directory. That file has the
  365. contains the server number, in ASCII.</p>
  366. <p>Finally, note the two port numbers after each server
  367. name: " 2888" and "3888". Peers use the former port to connect
  368. to other peers. Such a connection is necessary so that peers
  369. can communicate, for example, to agree upon the order of
  370. updates. More specifically, a ZooKeeper server uses this port
  371. to connect followers to the leader. When a new leader arises, a
  372. follower opens a TCP connection to the leader using this
  373. port. Because the default leader election also uses TCP, we
  374. currently require another port for leader election. This is the
  375. second port in the server entry.
  376. </p>
  377. <div class="note">
  378. <div class="label">Note</div>
  379. <div class="content">
  380. <p>If you want to test multiple servers on a single
  381. machine, specify the servername
  382. as <em>localhost</em> with unique quorum &amp;
  383. leader election ports (i.e. 2888:3888, 2889:3889, 2890:3890 in
  384. the example above) for each server.X in that server's config
  385. file. Of course separate <em>dataDir</em>s and
  386. distinct <em>clientPort</em>s are also necessary
  387. (in the above replicated example, running on a
  388. single <em>localhost</em>, you would still have
  389. three config files).</p>
  390. </div>
  391. </div>
  392. <a name="N100EE"></a><a name="Other+Optimizations"></a>
  393. <h3 class="h4">Other Optimizations</h3>
  394. <p>There are a couple of other configuration parameters that can
  395. greatly increase performance:</p>
  396. <ul>
  397. <li>
  398. <p>To get low latencies on updates it is important to
  399. have a dedicated transaction log directory. By default
  400. transaction logs are put in the same directory as the data
  401. snapshots and <em>myid</em> file. The dataLogDir
  402. parameters indicates a different directory to use for the
  403. transaction logs.</p>
  404. </li>
  405. <li>
  406. <p>
  407. <em>[tbd: what is the other config param?]</em>
  408. </p>
  409. </li>
  410. </ul>
  411. </div>
  412. <p align="right">
  413. <font size="-2"></font>
  414. </p>
  415. </div>
  416. <!--+
  417. |end content
  418. +-->
  419. <div class="clearboth">&nbsp;</div>
  420. </div>
  421. <div id="footer">
  422. <!--+
  423. |start bottomstrip
  424. +-->
  425. <div class="lastmodified">
  426. <script type="text/javascript"><!--
  427. document.write("Last Published: " + document.lastModified);
  428. // --></script>
  429. </div>
  430. <div class="copyright">
  431. Copyright &copy;
  432. 2008 <a href="http://www.apache.org/licenses/">The Apache Software Foundation.</a>
  433. </div>
  434. <!--+
  435. |end bottomstrip
  436. +-->
  437. </div>
  438. </body>
  439. </html>