zookeeper安装
节点设置: zookeeper节点172.16.23.120,172.16.23.121,172.16.23.122
[root@master zookeeper]# cat /etc/ansible/hosts |tail -20 ## db-[99:101]-node.example.com [all] 172.16.23.120 172.16.23.121 172.16.23.122 172.16.23.131 [master] 172.16.23.120 [nodes] 172.16.23.121 172.16.23.122 [cluster] 172.16.23.120 172.16.23.121 172.16.23.122
1.安装zookeeper依赖环境java,从oracle官网下载,建议jdk8,最新jdk15安装会有报错,安装完成后配置环境变量
[root@node2 zookeeper]# cat /etc/profile.d/zookeeper.sh export JAVA_HOME=/usr/local/jdk export CLASSPATH=$JAVA_HOME/lib/ export JRE_HOME=$JAVA_HOME/jre export PATH=$PATH:$JAVA_HOME/bin export ZOOKEEPER_HOME=/usr/local/zookeeper export PATH=$PATH:$ZOOKEEPER_HOME/bin
2.验证java环境:
[root@master zookeeper]# ansible cluster -m shell -a "java -version" 172.16.23.120 | CHANGED | rc=0 >> java version "1.8.0_261" Java(TM) SE Runtime Environment (build 1.8.0_261-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.261-b12, mixed mode) 172.16.23.121 | CHANGED | rc=0 >> java version "1.8.0_261" Java(TM) SE Runtime Environment (build 1.8.0_261-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.261-b12, mixed mode) 172.16.23.122 | CHANGED | rc=0 >> java version "1.8.0_261" Java(TM) SE Runtime Environment (build 1.8.0_261-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.261-b12, mixed mode)
3.下载zookeeper:wget http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz
4.修改配置文件:
[root@master zookeeper]# cat conf/zoo.cfg |egrep -v "^#|^$" tickTime=2000 initLimit=10 syncLimit=5 dataDir=/usr/local/zookeeper/data dataLogDir=/usr/local/zookeeper/logs clientPort=2181 server.1=172.16.23.120:2888:3888 server.2=172.16.23.121:2888:3888 server.3=172.16.23.122:2888:3888
将配置文件同步到其他节点:
# ansible nodes -m copy -a "src=/usr/local/zookeeper/conf/zoo.cfg dest=/usr/local/zookeeper/conf/zoo.cfg"
然后在各节点创建节点id:
# ansible 172.16.23.120 -m shell -a "echo 1 > /usr/local/zookeeper/data/myid" # ansible 172.16.23.121 -m shell -a "echo 2 > /usr/local/zookeeper/data/myid" # ansible 172.16.23.122 -m shell -a "echo 3 > /usr/local/zookeeper/data/myid"
然后验证一下:
[root@master zookeeper]# ansible cluster -m shell -a "cat /usr/local/zookeeper/data/myid" 172.16.23.120 | CHANGED | rc=0 >> 1 172.16.23.121 | CHANGED | rc=0 >> 2 172.16.23.122 | CHANGED | rc=0 >> 3
将zookeeper应用作为自启动:
[root@master zookeeper]# cat /usr/lib/systemd/system/zookeeper.service [Unit] Description=zookeeper After=network.target [Service] Type=forking Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/jdk/bin" WorkingDirectory=/usr/local/zookeeper/bin ExecStart=/usr/local/zookeeper/bin/zkServer.sh start ExecStop=/usr/local/zookeeper/bin/zkServer.sh stop Restart=on-failure [Install] WantedBy=multi-user.target
然后分发到其他节点:
ansible nodes -m copy -a "src=/usr/lib/systemd/system/zookeeper.service dest=/usr/lib/systemd/system/zookeeper.service" ansible cluster -m shell -a "systemctl daemon-reload" ansible cluster -m shell -a "systemctl enable zookeeper" ansible cluster -m shell -a "systemctl status zookeeper" ansible cluster -m shell -a "systemctl start zookeeper" ansible cluster -m shell -a "systemctl status zookeeper"
查询节点状态,角色:
[root@master zookeeper]# ./bin/zkServer.sh status ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg Mode: leader
如果出现如下状态:
[root@node1 zookeeper]# ./bin/zkServer.sh status ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg Error contacting service. It is probably not running.
查询zookeeper的端口2181是否正常,2888,3888端口是否正常,查询应用目录下面的zookeeper.out日志文件,如果2181端口存在,而2888,3888不存在,那么表示节点之间不能正常通信,很明显的一个就是防火墙问题:
[root@node1 zookeeper]# ./bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Error contacting service. It is probably not running.
[root@node1 zookeeper]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since 二 2020-10-06 20:52:07 CST; 1h 45min ago
Docs: man:firewalld(1)
Main PID: 984 (firewalld)
CGroup: /system.slice/firewalld.service
└─984 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
10月 06 20:52:01 node1 systemd[1]: Starting firewalld - dynamic firewall daemon...
10月 06 20:52:07 node1 systemd[1]: Started firewalld - dynamic firewall daemon.
[root@node1 zookeeper]# systemctl stop firewalld
[root@node1 zookeeper]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@node1 zookeeper]# ./bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Mode: follower
关闭防火墙就能正常通信,也就能正常获取角色
[root@master zookeeper]# ansible cluster -m shell -a "cd /usr/local/zookeeper;./bin/zkServer.sh status" 172.16.23.120 | CHANGED | rc=0 >> Mode: leaderZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg 172.16.23.121 | CHANGED | rc=0 >> Mode: followerZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg 172.16.23.122 | CHANGED | rc=0 >> Mode: followerZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
以上zookeeper就正常安装完成