1. 程式人生 > >ZooKeeper註冊中心安裝(單節點)

ZooKeeper註冊中心安裝(單節點)

機器 file ade recommend cfg pro 應用程序 /etc inpu

1.下載zookeeper

下載地址:https://archive.apache.org/dist/zookeeper/,這裏我們使用3.4.6版本。

[winner-0715@localhost software]$ wget https://archive.apache.org/dist/zookeeper/zookeeper-3.4.6/zookeeper-3.4.6.tar.gz

2.解壓 zookeeper 安裝包

[winner-0715@localhost software]$ tar -zxvf zookeeper-3.4.6.tar.gz

3.在zookeeper-3.4.6 目錄下創建data數據目錄和logs日誌目錄:

[winner-0715@localhost software]$ cd zookeeper-3.4.6
[winner-0715@localhost zookeeper-3.4.6]$ mkdir data
[winner-0715@localhost zookeeper-3.4.6]$ mkdir logs

4.將 zookeeper-3.4.6/conf 目錄下的 zoo_sample.cfg 文件拷貝一份,命名為為zoo.cfg

[winner-0715@localhost zookeeper-3.4.6]$ cd conf/
[winner-0715@localhost conf]$ ll
total 
12 -rw-rw-r--. 1 winner-0715 winner-0715 535 Feb 20 2014 configuration.xsl -rw-rw-r--. 1 winner-0715 winner-0715 2161 Feb 20 2014 log4j.properties -rw-rw-r--. 1 winner-0715 winner-0715 922 Feb 20 2014 zoo_sample.cfg [winner-0715@localhost conf]$ cp zoo_sample.cfg zoo.cfg [winner-0715@localhost conf]$ ll total
16 -rw-rw-r--. 1 winner-0715 winner-0715 535 Feb 20 2014 configuration.xsl -rw-rw-r--. 1 winner-0715 winner-0715 2161 Feb 20 2014 log4j.properties -rw-rw-r--. 1 winner-0715 winner-0715 922 Dec 30 11:52 zoo.cfg -rw-rw-r--. 1 winner-0715 winner-0715 922 Feb 20 2014 zoo_sample.cfg

5.修改 zoo.cfg 配置文件:

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
dataDir=/home/winner-0715/software/zookeeper-3.4.6/data
dataLogDir=/home/winner-0715/software/zookeeper-3.4.6/logs
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
server.1=192.168.41.128:2888:3888

其中,
2888 端口號是 zookeeper 服務之間通信的端口。
3888 是 zookeeper 與其他應用程序通信的端口。
edu-provider-01 是在 hosts 中已映射了 IP 的主機名。
initLimit:這個配置項是用來配置 Zookeeper 接受客戶端(這裏所說的客戶端不是用戶連接 Zookeeper 服務器的客戶端,而是 Zookeeper 服務器集群中連接到Leader 的 Follower 服務器)初始化連接時最長能忍受多少個心跳時間間隔數。
當已經超過 10 個心跳的時間(也就是 tickTime)長度後 Zookeeper 服務器還沒有收到客戶端的返回信息,那麽表明這個客戶端連接失敗。總的時間長度就是5*2000=10 秒。
syncLimit:這個配置項標識 Leader 與 Follower 之間發送消息,請求和應答時間長度,最長不能超過多少個 tickTime 的時間長度,總的時間長度就是 2*2000=4秒。
server.A=B:C:D:其中 A 是一個數字,表示這個是第幾號服務器; B 是這個服務器的 IP 地址或/etc/hosts 文件中映射了 IP 的主機名; C 表示的是這個服務器與集群中的 Leader 服務器交換信息的端口; D 表示的是萬一集群中的 Leader 服務
器掛了,需要一個端口來重新進行選舉,選出一個新的 Leader,而這個端口就是用來執行選舉時服務器相互通信的端口。如果是偽集群的配置方式,由於 B 都是
一樣,所以不同的 Zookeeper 實例通信端口號不能一樣,所以要給它們分配不同的端口號

6.在 dataDir=/home/winner-0715/software/zookeeper-3.4.6/data下創建 myid 文件
編輯 myid 文件,並在對應的 IP 的機器上輸入對應的編號。如在 zookeeper 上, myid文件內容就是 1。 如果只在單點上進行安裝配置, 那麽只有一個 server.1。

[winner-0715@localhost data]$ vim myid
[winner-0715@localhost data]$ ll
total 4
-rw-rw-r--. 1 winner-0715 winner-0715 2 Dec 30 12:11 myid
[winner-0715@localhost data]$ cat myid 
1

7.配置zookeeper的環境變量。

[winner-0715@localhost ~]$ vim .bash_profile 
[winner-0715@localhost ~]$ cat .bash_profile 
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH

#zookeeper env
export ZOOKEEPER_HOME=/home/winner-0715/software/zookeeper-3.4.6
export PATH=$ZOOKEEPER_HOME/bin:$PATH

.bash_profile在用戶目錄下。

8. 在防火墻中打開要用到的端口 2181、 2888、 3888
切換到 root 用戶權限,執行以下命令:

[root@localhost winner-0715]#chkconfig iptables on
[root@localhost winner-0715]#service iptables start
[root@localhost winner-0715]#vi /etc/sysconfig/iptables
[root@localhost winner-0715]# cat /etc/sysconfig/iptables
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 2181 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 2888 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3888 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

看下22端口就知道怎麽改了!

重啟防火墻:

[root@localhost winner-0715]# service iptables restart
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Unloading modules:                               [  OK  ]
iptables: Applying firewall rules:                         [  OK  ]

查看防火墻端口狀態:

[root@localhost winner-0715]# service iptables status
Table: filter
1Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
2    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
3    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
4    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22 
5    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:2181 
6    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:2888 
7    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:3888 
8    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         
1    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         

9.啟動並測試 zookeeper(要用一般用戶啟動,不要用 root) :

(1)因為配置了環境變量,所以不需要切換到zookeeper的bin目錄下就可以執行啟動命令。

[winner-0715@localhost ~]$ zkServer.sh start
JMX enabled by default
Using config: /home/winner-0715/software/zookeeper-3.4.6/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

(2)輸入 jps 命令查看進程:

[winner-0715@localhost ~]$ jps
4954 Jps
4924 QuorumPeerMain

其中, QuorumPeerMain zookeeper 進程,啟動正常

(3) 查看狀態:

[winner-0715@localhost ~]$ zkServer.sh status
JMX enabled by default
Using config: /home/winner-0715/software/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: standalone

(4) 查看 zookeeper 服務輸出信息:
由於服務信息輸出文件在/home/winner-0715/zookeeper-3.4.6/bin/zookeeper.out

$ tail -500f zookeeper.out 

10.停止 zookeeper 進程:

[winner-0715@localhost bin]$ ./zkServer.sh stop
JMX enabled by default
Using config: /home/winner-0715/software/zookeeper-3.4.6/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED

11.配置zookeeper開機啟動

編輯/etc/rc.local 文件,加入:

su winner-0715 -c ‘/home/winner-0715/zookeeper-3.4.6/bin/zkServer.sh start‘

-c:call,調用後面的腳本的意思。

ZooKeeper註冊中心安裝(單節點)