Installing Zookeeper

Downloading & Installing

You can choose to install Zookeeper manually, or using Chocolatey. The Chocolatey way is easier, but the Chocolatey package for Zookeeper might be outdated.

With Chocolatey

If you do not have Chocolatey installed on your machine yet, do so following the instructions on https://chocolatey.org/. After that, open a command prompt as administrator and run choco install apache-zookeeper.

Without Chocolatey

Download the latest stable Zookeeper release in the 3.4 series. See https://zookeeper.apache.org/releases.html.

Extract the Zookeeper package, preferably to a path that is globally accessible (not user-restricted), for instance C:\tools\zookeeper-3.4.13.

Zookeeper requires Java. If you do not already have a JRE or JDK installed, download the latest JRE from https://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html.

Once you have it installed, make sure you have a JAVA_HOME system environment variable poiting to its location (for instance, C:\Program Files\Java\jdk1.8.0_112). Add it manually if it is not present (it must be a system environment variable, not a user one).

Setting Up

Navigate to <the folder where zookeeper is installed>\conf.

Make a copy of the file zoo_sample.cfg, rename it zoo.cfg. In this file, modify the dataDir entry, to point to an absolute existing directory. Backslashes need to be escaped. For instance:

dataDir=C:\\ProgramData\\zookeeper\\data

At this point, you should be ready to go. To run Zookeeper, navigate to the bin folder and launch zkServer.cmd. It should open a command prompt with zookeeper logs. To stop Zookeeper, just close the command prompt.

If the command prompt closes itself immediately after it opened, it means Zookeeper has failed to start. To see the error message, open a new command prompt, and run zkServer.cmd from there.

Zookeeper can also be used as a Windows service. If you want to do so, read on.

Running Zookeeper as a Windows Service

The use of an additional program, procrun, is required to create a Windows service for Zookeeper.

Download it from here, and extract the files to the location of your choice.

We need to manually add 2 system environment variables for the service:

  • ZOOKEEPER_SERVICE: The name that you want the service to have. For instance, Zookeeper.

  • ZOOKEEPER_HOME: The path to the root of your Zookeeper installation. In our example, it would be C:\tools\zookeeper-3.4.13.

Then, create a file named zkServerStop.cmd in %ZOOKEEPER_HOME%\bin, with the following contents:

@echo off
setlocal
TASKLIST /svc | findstr /c:"%ZOOKEEPER_SERVICE%" > %ZOOKEEPER_HOME%\zookeeper_svc.pid
FOR /F "tokens=2 delims= " %%G IN (%ZOOKEEPER_HOME%\zookeeper_svc.pid) DO (
        @set zkPID=%%G
)
taskkill /PID %zkPID% /T /F
del %ZOOKEEPER_HOME%/zookeeper_svc.pid
endlocal

Now, navigate to the directory where you extracted procrun earlier, and run the following command from a command prompt:

prunsrv.exe "//IS//%ZOOKEEPER_SERVICE%" ^
                --DisplayName="Zookeeper (%ZOOKEEPER_SERVICE%)" ^
                --Description="Zookeeper (%ZOOKEEPER_SERVICE%)" ^
                --Startup=auto --StartMode=exe ^
                --StartPath=%ZOOKEEPER_HOME% ^
                --StartImage=%ZOOKEEPER_HOME%\bin\zkServer.cmd ^
                --StopPath=%ZOOKEEPER_HOME%\ ^
                --StopImage=%ZOOKEEPER_HOME%\bin\zkServerStop.cmd ^
                --StopMode=exe --StopTimeout=5 ^
                --LogPath=%ZOOKEEPER_HOME% --LogPrefix=zookeeper-wrapper ^
                --PidFile=zookeeper.pid --LogLevel=Info --StdOutput=auto --StdError=auto

This will create a Windows service named %ZOOKEEPER_SERVICE%, and set it to auto-start. You should now see it in the Windows Services manager.

The service is now ready to go. Note that you might see the following error if you stop the service manually:

../_images/zookeeper-stop-error.jpg

This can happen because stopping the service calls a script that directly kills the zookeeper executable. To our knowledge, there is no cleaner method of stopping it, but it has no undesirable side effect other than this error message, which you will not see unless you stop the service manually anyway.

Credits: We made this guide thanks to this stackoverflow post.