“好記性不如爛筆頭。” —— 張溥 0x00 大綱 0x01 前言 部分內容翻譯自 ZooKeeper 3.6 Documentation,文末附原文章節,可對照理解。 0x02 獨立運行 在獨立模式下設置 ZooKeeper 服務很簡單。服務包含在單個 JAR 文件中,因此安裝包括創建配置。 下載 ...
“好記性不如爛筆頭。” —— 張溥
0x00 大綱
目錄0x01 前言
部分內容翻譯自 ZooKeeper 3.6 Documentation,文末附原文章節,可對照理解。
0x02 獨立運行
在獨立模式下設置 ZooKeeper 服務很簡單。服務包含在單個 JAR 文件中,因此安裝包括創建配置。
下載穩定的 ZooKeeper 版本後,將其解壓縮並 CD 到根目錄。
要啟動 ZooKeeper,您需要一個配置文件。這是一個示例,在 conf/zoo.cfg 中創建如下配置:
tickTime=2000
dataDir=/var/lib/zookeeper
clientPort=2181
這個文件名字可以隨便取,但為了方便這個討論,把它叫做 conf/zoo.cfg。更改 dataDir 的值以指定一個存在的目錄(初始值為空)。以下是每個欄位的含義:
-
tickTime:ZooKeeper 使用的基本時間單位(以毫秒為單位)。它用於執行心跳檢測,最小會話超時時間將是 tickTime 的兩倍。
-
dataDir:存儲記憶體中資料庫快照的位置,除非另有指定,否則事務日誌也將更新到資料庫中。
-
clientPort:偵聽客戶端連接的埠。
現在你已經創建了配置文件,你可以啟動 ZooKeeper:
bin/zkServer.sh start
(如果是 Windows 版本,則執行)
bin/zkServer.cmd
ZooKeeper 使用 log4j 記錄消息 - 更多詳細信息可在程式員指南的日誌記錄部分找到。您將看到輸出到控制台的日誌消息(預設)和/或日誌文件,具體取決於 log4j 配置。
此處概述的步驟在獨立模式下運行 ZooKeeper。沒有複製(集群),因此如果 ZooKeeper 進程出錯,服務將關閉。這對於大多數開發情況來說都很好,但是要在複製模式下運行 ZooKeeper,請參閱運行複製的 ZooKeeper。
0x03 集群運行
在獨立模式下運行 ZooKeeper 便於評估、開發和測試。但在生產環境中,您應該在複製(集群)模式下運行 ZooKeeper。同一應用程式中的複製伺服器組稱為 quorum ,在複製模式下, quorum 中的所有伺服器都具有同一配置文件的副本。
註意
對於複製模式,至少需要三台伺服器,強烈建議您擁有奇數台伺服器。如果只有兩台伺服器,則如果其中一臺伺服器發生故障,則沒有足夠的電腦來形成多數仲裁。兩台伺服器本質上不如一臺伺服器穩定,因為存在兩個單點故障。
複製模式所需的 conf/zoo.cfg 文件與獨立模式下使用的配置文件類似,但有一些不同之處。下麵是一個示例:
tickTime=2000
dataDir=/var/lib/zookeeper
clientPort=2181
initLimit=5
syncLimit=2
server.1=zoo1:2888:3888
server.2=zoo2:2888:3888
server.3=zoo3:2888:3888
新配置選項 initLimit 是 ZooKeeper 用來限制仲裁中的 ZooKeeper 伺服器必須連接到領導者的時長。配置選項 syncLimit 限制伺服器與領導者之間的超時時間。
對於這兩個超時,您可以使用 tickTime 指定時間單位。在此示例中,initLimit 的超時為 5 個時鐘周期,每個時鐘周期 2000 毫秒,即 10 秒。
伺服器配置 server.X 列出了組成 ZooKeeper 服務的伺服器。當伺服器啟動時,它通過在數據目錄中查找文件 myid 來知道它是哪個伺服器。該文件包含伺服器編號(ASCII字元集)。
最後,記下每個伺服器名稱後面的兩個埠號:“2888”和“3888”。各個節點使用這兩個埠連接到其他節點。這種連接是必要的,以便節點之間可以進行通信,例如,就更新順序達成一致。更具體地說,ZooKeeper 伺服器使用此埠將從者連接到領導。當出現新的領導時,從者使用此埠打開與領導的 TCP 連接。由於預設領導選舉也使用 TCP,因此我們目前需要另一個埠進行領導選舉。這是伺服器配置中的第二個埠。
註意
如果要在一臺電腦上測試多個伺服器,請將伺服器名稱指定為 localhost 並分配不同的埠(例如 2888:3888、2889:3889、2890:3890)。當然,單獨的 _dataDir_s 和不同的 _clientPort_s 也是必要的(在上述的複製示例中,在單個主機上運行,您將有三個不同的配置文件)。
請註意,在一臺電腦上設置多個服務不會產生任何冗餘。如果機器發生故障,所有 zookeeper 服務都將離線。完全冗餘要求每個服務都有自己的主機,它必須是完全獨立的物理伺服器。同一物理主機上的多個虛擬機仍然容易受到該宿主機故障的影響。
如果您的 ZooKeeper 電腦中有多個網路介面,您還可以指示 ZooKeeper 綁定所有介面,併在發生網路故障時自動切換到正常運行的介面。有關詳細信息,請參閱配置參數。
0x04 單機集群配置補充
將解壓後的 zookeeper 包複製三份,可分別重命名為 server1、server2、server3。
在 server1/conf/zoo.cfg 中創建如下配置:
tickTime=2000
dataDir=/server1/data
clientPort=2181
initLimit=5
syncLimit=2
server.1=localhost:2888:3888
server.2=localhost:2889:3889
server.3=localhost:2890:3890
在 server2/conf/zoo.cfg 中創建如下配置:
tickTime=2000
dataDir=/server2/data
clientPort=2182
initLimit=5
syncLimit=2
server.1=localhost:2888:3888
server.2=localhost:2889:3889
server.3=localhost:2890:3890
在 server3/conf/zoo.cfg 中創建如下配置:
tickTime=2000
dataDir=/server3/data
clientPort=2183
initLimit=5
syncLimit=2
server.1=localhost:2888:3888
server.2=localhost:2889:3889
server.3=localhost:2890:3890
在 /server1/data 中創建 myid 文件(沒有尾碼),文件內容為數字 1;
在 /server2/data 中創建 myid 文件(沒有尾碼),文件內容為數字 2;
在 /server3/data 中創建 myid 文件(沒有尾碼),文件內容為數字 3;
依次啟動三個 ZooKeeper 服務。
0x05 官方原文
Standalone Operation
Setting up a ZooKeeper server in standalone mode is straightforward. The server is contained in a single JAR file, so installation consists of creating a configuration.
Once you've downloaded a stable ZooKeeper release unpack it and cd to the root
To start ZooKeeper you need a configuration file. Here is a sample, create it in conf/zoo.cfg:
tickTime=2000 dataDir=/var/lib/zookeeper clientPort=2181
This file can be called anything, but for the sake of this discussion call it conf/zoo.cfg. Change the value of dataDir to specify an existing (empty to start with) directory. Here are the meanings for each of the fields:
tickTime : the basic time unit in milliseconds used by ZooKeeper. It is used to do heartbeats and the minimum session timeout will be twice the tickTime.
dataDir : the location to store the in-memory database snapshots and, unless specified otherwise, the transaction log of updates to the database.
clientPort : the port to listen for client connections
Now that you created the configuration file, you can start ZooKeeper:
bin/zkServer.sh start
ZooKeeper logs messages using log4j -- more detail available in the Logging section of the Programmer's Guide. You will see log messages coming to the console (default) and/or a log file depending on the log4j configuration.
The steps outlined here run ZooKeeper in standalone mode. There is no replication, so if ZooKeeper process fails, the service will go down. This is fine for most development situations, but to run ZooKeeper in replicated mode, please see Running Replicated ZooKeeper.
Running Replicated ZooKeeper
Running ZooKeeper in standalone mode is convenient for evaluation, some development, and testing. But in production, you should run ZooKeeper in replicated mode. A replicated group of servers in the same application is called a quorum, and in replicated mode, all servers in the quorum have copies of the same configuration file.
Note
For replicated mode, a minimum of three servers are required, and it is strongly recommended that you have an odd number of servers. If you only have two servers, then you are in a situation where if one of them fails, there are not enough machines to form a majority quorum. Two servers are inherently less stable than a single server, because there are two single points of failure.
The required conf/zoo.cfg file for replicated mode is similar to the one used in standalone mode, but with a few differences. Here is an example:
tickTime=2000 dataDir=/var/lib/zookeeper clientPort=2181 initLimit=5 syncLimit=2 server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888
The new entry, initLimit is timeouts ZooKeeper uses to limit the length of time the ZooKeeper servers in quorum have to connect to a leader. The entry syncLimit limits how far out of date a server can be from a leader.
With both of these timeouts, you specify the unit of time using tickTime. In this example, the timeout for initLimit is 5 ticks at 2000 milliseconds a tick, or 10 seconds.
The entries of the form server.X list the servers that make up the ZooKeeper service. When the server starts up, it knows which server it is by looking for the file myid in the data directory. That file has the contains the server number, in ASCII.
Finally, note the two port numbers after each server name: " 2888" and "3888". Peers use the former port to connect to other peers. Such a connection is necessary so that peers can communicate, for example, to agree upon the order of updates. More specifically, a ZooKeeper server uses this port to connect followers to the leader. When a new leader arises, a follower opens a TCP connection to the leader using this port. Because the default leader election also uses TCP, we currently require another port for leader election. This is the second port in the server entry.
Note
If you want to test multiple servers on a single machine, specify the servername as localhost with unique quorum & leader election ports (i.e. 2888:3888, 2889:3889, 2890:3890 in the example above) for each server.X in that server's config file. Of course separate _dataDir_s and distinct _clientPort_s are also necessary (in the above replicated example, running on a single localhost, you would still have three config files).
Please be aware that setting up multiple servers on a single machine will not create any redundancy. If something were to happen which caused the machine to die, all of the zookeeper servers would be offline. Full redundancy requires that each server have its own machine. It must be a completely separate physical server. Multiple virtual machines on the same physical host are still vulnerable to the complete failure of that host.
If you have multiple network interfaces in your ZooKeeper machines, you can also instruct ZooKeeper to bind on all of your interfaces and automatically switch to a healthy interface in case of a network failure. For details, see the Configuration Parameters.