“好記性不如爛筆頭,” —— 張溥
0x00 大綱
目錄- 0x00 大綱
- 0x01 前言
- 0x02 獨立運行
- 0x03 集群運行
- 0x04 單機集群配置補充
- 0x05 官方原文
- Standalone Operation
- Running Replicated ZooKeeper
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=2181This 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 startZooKeeper 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:3888The 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.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/539993.html
標籤:其他
