物联网架构成长之路(27)-Docker练习之Zookeeper安装

时间:2022-11-15 15:20:05

0. 前言

  准备了解一下消息队列MQ,对比了一些开源的中间件,最后选择Kafka作为以后用到的消息队列,消息队列的应用场景及Kafka与其他消息队列的优缺点这里就不细说了,具体的可以参考其他博客说明。不过Kafka依赖Zookeeper,因此先练习一些用Docker构建Zookeeper。

1. 安装ZooKeeper

  使用Kafka前,要安装ZooKeeper。这里利用最近刚学的Docker,我构建成一个ZooKeeper Image,供以后使用。

 FROM openjdk:8-jdk-alpine

 RUN apk add --no-cache bash && rm -rf /var/cache/apk/* && /bin/bash
RUN wget http://mirrors.aliyun.com/apache/zookeeper/zookeeper-3.4.13/zookeeper-3.4.13.tar.gz && \
tar -zxvf zookeeper-3.4.13.tar.gz && \
rm -rf zookeeper-3.4.13.tar.gz && \
mv zookeeper-3.4.13 zookeeper && \
cd /zookeeper && rm -rf contrib dist-maven docs recipes src *.txt *.md *.xml && \
cp /zookeeper/conf/zoo_sample.cfg /zookeeper/conf/zoo.cfg && \
sed -i "s#dataDir=/tmp/zookeeper#dataDir=/data#g" /zookeeper/conf/zoo.cfg
ENV PATH /zookeeper/bin:$PATH EXPOSE 2181 CMD ["zkServer.sh", "start-foreground"]

  构建、运行

 docker build -t zookeeper:3.4. .
docker run -d -p : zookeeper:3.4.

  挂载 -v /my_data:/data

物联网架构成长之路(27)-Docker练习之Zookeeper安装

物联网架构成长之路(27)-Docker练习之Zookeeper安装

2. Zookeeper Cluster模式

  具体的Cluster模式,可以参考我之前的博客 https://www.cnblogs.com/wunaozai/p/8249657.html
  现在是测试3个node的Zookeeper节点
  zoo.cfg

 tickTime=
initLimit=
syncLimit=
dataDir=/data
clientPort=
server.=zoo1::
server.=zoo2::
server.=zoo3::

  docker-compose.yml

 version: ''
services:
zoo1:
image: zookeeper:3.4.
volumes:
- /root/workspace/docker/kafka/zookeeper/zoo.cfg:/zookeeper/conf/zoo.cfg
- /root/workspace/docker/kafka/zookeeper/myid1:/data/myid
zoo2:
image: zookeeper:3.4.
volumes:
- /root/workspace/docker/kafka/zookeeper/zoo.cfg:/zookeeper/conf/zoo.cfg
- /root/workspace/docker/kafka/zookeeper/myid2:/data/myid
zoo3:
image: zookeeper:3.4.
volumes:
- /root/workspace/docker/kafka/zookeeper/zoo.cfg:/zookeeper/conf/zoo.cfg
- /root/workspace/docker/kafka/zookeeper/myid3:/data/myid

  准备运行3个节点的Zookeeper。要现在目录准备zoo.cfg文件,在该配置文件最后面,要配置server.id=server:port,然后还要为每个Zookeeper节点准备一个myid文件

 echo "" > myid1
echo "" > myid2
echo "" > myid3
docker-componse up

  启动后,我们进入任意一台主机,然后create path data,这样这份数据都会同步到各个节点上。

3. zkui Zookeeper可视化Web客户端

  我使用这个Zookeeper可视化客户端,https://github.com/DeemOpen/zkui 在源代码之上,

mvn package -Dmaven.test.skip=true

  利用Dockerfile构建zkui Image
  Dockerfile

 FROM openjdk:8-jdk-alpine

 MAINTAINER wunaozai <i@wunaozai.com>

 WORKDIR /var/app
ADD zkui-*.jar /var/app/zkui.jar
ADD config.cfg /var/app/config.cfg
ADD bootstrap.sh /var/app/bootstrap.sh EXPOSE 9090 ENTRYPOINT ["/var/app/bootstrap.sh"]

  bootstrap.sh

 #!/bin/sh

 ZK_SERVER=${ZK_SERVER:-"localhost:2181"}

 USER_SET=${USER_SET:-"{\"users\": [{ \"username\":\"admin\" , \"password\":\"manager\",\"role\": \"ADMIN\" \},{ \"username\":\"appconfig\" , \"password\":\"appconfig\",\"role\": \"USER\" \}]\}"}
LOGIN_MESSAGE=${LOGIN_MESSAGE:-"Please login using admin/manager or appconfig/appconfig."} sed -i "s/^zkServer=.*$/zkServer=$ZK_SERVER/" /var/app/config.cfg sed -i "s/^userSet = .*$/userSet = $USER_SET/" /var/app/config.cfg
sed -i "s/^loginMessage=.*$/loginMessage=$LOGIN_MESSAGE/" /var/app/config.cfg echo "Starting zkui with server $ZK_SERVER" exec java -jar /var/app/zkui.jar

  config.cfg

 #Server Port
serverPort=9090
#Comma seperated list of all the zookeeper servers
zkServer=localhost:2181,localhost:2181
#Http path of the repository. Ignore if you dont intent to upload files from repository.
scmRepo=http://myserver.com/@rev1=
#Path appended to the repo url. Ignore if you dont intent to upload files from repository.
scmRepoPath=//appconfig.txt
#if set to true then userSet is used for authentication, else ldap authentication is used.
ldapAuth=false
ldapDomain=mycompany,mydomain
#ldap authentication url. Ignore if using file based authentication.
ldapUrl=ldap://<ldap_host>:<ldap_port>/dc=mycom,dc=com
#Specific roles for ldap authenticated users. Ignore if using file based authentication.
ldapRoleSet={"users": [{ "username":"domain\\user1" , "role": "ADMIN" }]}
userSet = {"users": [{ "username":"admin" , "password":"manager","role": "ADMIN" },{ "username":"appconfig" , "password":"appconfig","role": "USER" }]}
#Set to prod in production and dev in local. Setting to dev will clear history each time.
env=prod
jdbcClass=org.h2.Driver
jdbcUrl=jdbc:h2:zkui
jdbcUser=root
jdbcPwd=manager
#If you want to use mysql db to store history then comment the h2 db section.
#jdbcClass=com.mysql.jdbc.Driver
#jdbcUrl=jdbc:mysql://localhost:3306/zkui
#jdbcUser=root
#jdbcPwd=manager
loginMessage=Please login using admin/manager or appconfig/appconfig.
#session timeout 5 mins/300 secs.
sessionTimeout=300
#Default 5 seconds to keep short lived zk sessions. If you have large data then the read will take more than 30 seconds so increase this accordingly.
#A bigger zkSessionTimeout means the connection will be held longer and resource consumption will be high.
zkSessionTimeout=5
#Block PWD exposure over rest call.
blockPwdOverRest=false
#ignore rest of the props below if https=false.
https=false
keystoreFile=/home/user/keystore.jks
keystorePwd=password
keystoreManagerPwd=password
# The default ACL to use for all creation of nodes. If left blank, then all nodes will be universally accessible
# Permissions are based on single character flags: c (Create), r (read), w (write), d (delete), a (admin), * (all)
# For example defaultAcl={"acls": [{"scheme":"ip", "id":"192.168.1.192", "perms":"*"}, {"scheme":"ip", id":"192.168.1.0/24", "perms":"r"}]
defaultAcl=
# Set X-Forwarded-For to true if zkui is behind a proxy
X-Forwarded-For=false

4. Zookeeper与zkui结合

  用Zookeeper自带的zkCli.sh -server 127.0.0.1 可以正常的对Zookeeper数据进行增删改查了。但是有时候测试的时候,还是要Web的客户端比较方便,前期调试开发都是比较友好的。

  docker-compose.yml

 version: '3'
services:
zoo:
image: registry.cn-shenzhen.aliyuncs.com/wunaozai/zookeeper
ports:
- 2181:2181
zkui:
image: registry.cn-shenzhen.aliyuncs.com/wunaozai/zkui
environment:
- ZK_SERVER=zoo:2181
ports:
- 8080:9090

物联网架构成长之路(27)-Docker练习之Zookeeper安装

  docker-compose.yml

 version: '3'
services:
zoo1:
image: registry.cn-shenzhen.aliyuncs.com/wunaozai/zookeeper
volumes:
- /home/lmx/workspace/docker/zookeeper/zoo.cfg:/zookeeper/conf/zoo.cfg
- /home/lmx/workspace/docker/zookeeper/myid1:/data/myid
zoo2:
image: registry.cn-shenzhen.aliyuncs.com/wunaozai/zookeeper
volumes:
- /home/lmx/workspace/docker/zookeeper/zoo.cfg:/zookeeper/conf/zoo.cfg
- /home/lmx/workspace/docker/zookeeper/myid2:/data/myid
zoo3:
image: registry.cn-shenzhen.aliyuncs.com/wunaozai/zookeeper
volumes:
- /home/lmx/workspace/docker/zookeeper/zoo.cfg:/zookeeper/conf/zoo.cfg
- /home/lmx/workspace/docker/zookeeper/myid3:/data/myid
zkui:
image: registry.cn-shenzhen.aliyuncs.com/wunaozai/zkui
environment:
- ZK_SERVER=zoo1:2181,zoo2:2181,zoo3:2181
ports:
- 8080:9090

物联网架构成长之路(27)-Docker练习之Zookeeper安装

参考资料:

  https://www.cnblogs.com/wunaozai/p/8249657.html
  http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_singleAndDevSetup
  https://hub.docker.com/r/library/openjdk/
  https://hub.docker.com/r/wurstmeister/zookeeper

本文地址: https://www.cnblogs.com/wunaozai/p/9978795.html