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

0. 前言

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

 

1. 安装ZooKeeper

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

 1 FROM openjdk:8-jdk-alpine
 2 
 3 RUN apk add --no-cache bash && rm -rf /var/cache/apk/* && /bin/bash
 4 RUN wget http://mirrors.aliyun.com/apache/zookeeper/zookeeper-3.4.13/zookeeper-3.4.13.tar.gz && \
 5     tar -zxvf zookeeper-3.4.13.tar.gz && \
 6     rm -rf zookeeper-3.4.13.tar.gz && \
 7     mv zookeeper-3.4.13 zookeeper && \
 8     cd /zookeeper && rm -rf contrib dist-maven docs recipes src *.txt *.md *.xml && \
 9     cp /zookeeper/conf/zoo_sample.cfg /zookeeper/conf/zoo.cfg && \
10     sed -i "s#dataDir=/tmp/zookeeper#dataDir=/data#g" /zookeeper/conf/zoo.cfg
11 ENV PATH /zookeeper/bin:$PATH
12 
13 EXPOSE 2181
14 
15 CMD ["zkServer.sh", "start-foreground"]

  构建、运行

1 docker build -t zookeeper:3.4.13 .
2 docker run -d -p 2181:2181 zookeeper:3.4.13

  挂载 -v /my_data:/data


2. Zookeeper Cluster模式

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

1 tickTime=2000
2 initLimit=10
3 syncLimit=5
4 dataDir=/data
5 clientPort=2181
6 server.1=zoo1:2888:3888
7 server.2=zoo2:2888:3888
8 server.3=zoo3:2888:3888

  docker-compose.yml

 1 version: '3'
 2 services:
 3     zoo1:
 4         image: zookeeper:3.4.13
 5         volumes:
 6             - /root/workspace/docker/kafka/zookeeper/zoo.cfg:/zookeeper/conf/zoo.cfg
 7             - /root/workspace/docker/kafka/zookeeper/myid1:/data/myid
 8     zoo2:
 9         image: zookeeper:3.4.13
10         volumes:
11             - /root/workspace/docker/kafka/zookeeper/zoo.cfg:/zookeeper/conf/zoo.cfg
12             - /root/workspace/docker/kafka/zookeeper/myid2:/data/myid
13     zoo3:
14         image: zookeeper:3.4.13
15         volumes:
16             - /root/workspace/docker/kafka/zookeeper/zoo.cfg:/zookeeper/conf/zoo.cfg
17             - /root/workspace/docker/kafka/zookeeper/myid3:/data/myid

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

1 echo "1" > myid1
2 echo "2" > myid2
3 echo "3" > myid3
4 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

 1 FROM openjdk:8-jdk-alpine
 2 
 3 MAINTAINER wunaozai <i@wunaozai.com>
 4 
 5 WORKDIR /var/app
 6 ADD zkui-*.jar /var/app/zkui.jar
 7 ADD config.cfg /var/app/config.cfg
 8 ADD bootstrap.sh /var/app/bootstrap.sh
 9 
10 EXPOSE 9090
11 
12 ENTRYPOINT ["/var/app/bootstrap.sh"]

  bootstrap.sh

 1 #!/bin/sh
 2 
 3 ZK_SERVER=${ZK_SERVER:-"localhost:2181"}
 4 
 5 USER_SET=${USER_SET:-"{\"users\": [{ \"username\":\"admin\" , \"password\":\"manager\",\"role\": \"ADMIN\" \},{ \"username\":\"appconfig\" , \"password\":\"appconfig\",\"role\": \"USER\" \}]\}"}
 6 LOGIN_MESSAGE=${LOGIN_MESSAGE:-"Please login using admin/manager or appconfig/appconfig."}
 7 
 8 sed -i "s/^zkServer=.*$/zkServer=$ZK_SERVER/" /var/app/config.cfg
 9 
10 sed -i "s/^userSet = .*$/userSet = $USER_SET/" /var/app/config.cfg
11 sed -i "s/^loginMessage=.*$/loginMessage=$LOGIN_MESSAGE/" /var/app/config.cfg
12 
13 echo "Starting zkui with server $ZK_SERVER"
14 
15 exec java -jar /var/app/zkui.jar

  config.cfg

 1 #Server Port
 2 serverPort=9090
 3 #Comma seperated list of all the zookeeper servers
 4 zkServer=localhost:2181,localhost:2181
 5 #Http path of the repository. Ignore if you dont intent to upload files from repository.
 6 scmRepo=http://myserver.com/@rev1=
 7 #Path appended to the repo url. Ignore if you dont intent to upload files from repository.
 8 scmRepoPath=//appconfig.txt
 9 #if set to true then userSet is used for authentication, else ldap authentication is used.
10 ldapAuth=false
11 ldapDomain=mycompany,mydomain
12 #ldap authentication url. Ignore if using file based authentication.
13 ldapUrl=ldap://<ldap_host>:<ldap_port>/dc=mycom,dc=com
14 #Specific roles for ldap authenticated users. Ignore if using file based authentication.
15 ldapRoleSet={"users": [{ "username":"domain\\user1" , "role": "ADMIN" }]}
16 userSet = {"users": [{ "username":"admin" , "password":"manager","role": "ADMIN" },{ "username":"appconfig" , "password":"appconfig","role": "USER" }]}
17 #Set to prod in production and dev in local. Setting to dev will clear history each time.
18 env=prod
19 jdbcClass=org.h2.Driver
20 jdbcUrl=jdbc:h2:zkui
21 jdbcUser=root
22 jdbcPwd=manager
23 #If you want to use mysql db to store history then comment the h2 db section.
24 #jdbcClass=com.mysql.jdbc.Driver
25 #jdbcUrl=jdbc:mysql://localhost:3306/zkui
26 #jdbcUser=root
27 #jdbcPwd=manager
28 loginMessage=Please login using admin/manager or appconfig/appconfig.
29 #session timeout 5 mins/300 secs.
30 sessionTimeout=300
31 #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.
32 #A bigger zkSessionTimeout means the connection will be held longer and resource consumption will be high.
33 zkSessionTimeout=5
34 #Block PWD exposure over rest call.
35 blockPwdOverRest=false
36 #ignore rest of the props below if https=false.
37 https=false
38 keystoreFile=/home/user/keystore.jks
39 keystorePwd=password
40 keystoreManagerPwd=password
41 # The default ACL to use for all creation of nodes. If left blank, then all nodes will be universally accessible
42 # Permissions are based on single character flags: c (Create), r (read), w (write), d (delete), a (admin), * (all)
43 # For example defaultAcl={"acls": [{"scheme":"ip", "id":"192.168.1.192", "perms":"*"}, {"scheme":"ip", id":"192.168.1.0/24", "perms":"r"}]
44 defaultAcl=
45 # Set X-Forwarded-For to true if zkui is behind a proxy
46 X-Forwarded-For=false


4. Zookeeper与zkui结合

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

  docker-compose.yml

 1 version: '3'
 2 services:
 3     zoo:
 4         image: registry.cn-shenzhen.aliyuncs.com/wunaozai/zookeeper
 5         ports:
 6             - 2181:2181
 7     zkui:
 8         image: registry.cn-shenzhen.aliyuncs.com/wunaozai/zkui
 9         environment:
10             - ZK_SERVER=zoo:2181
11         ports:
12             - 8080:9090

  docker-compose.yml

 1 version: '3'
 2 services:
 3     zoo1:
 4         image: registry.cn-shenzhen.aliyuncs.com/wunaozai/zookeeper
 5         volumes:
 6             - /home/lmx/workspace/docker/zookeeper/zoo.cfg:/zookeeper/conf/zoo.cfg
 7             - /home/lmx/workspace/docker/zookeeper/myid1:/data/myid
 8     zoo2:
 9         image: registry.cn-shenzhen.aliyuncs.com/wunaozai/zookeeper
10         volumes:
11             - /home/lmx/workspace/docker/zookeeper/zoo.cfg:/zookeeper/conf/zoo.cfg
12             - /home/lmx/workspace/docker/zookeeper/myid2:/data/myid
13     zoo3:
14         image: registry.cn-shenzhen.aliyuncs.com/wunaozai/zookeeper
15         volumes:
16             - /home/lmx/workspace/docker/zookeeper/zoo.cfg:/zookeeper/conf/zoo.cfg
17             - /home/lmx/workspace/docker/zookeeper/myid3:/data/myid
18     zkui:
19         image: registry.cn-shenzhen.aliyuncs.com/wunaozai/zkui
20         environment:
21             - ZK_SERVER=zoo1:2181,zoo2:2181,zoo3:2181
22         ports:
23             - 8080:9090

 

 

参考资料:

  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

posted @ 2018-11-20 08:41  无脑仔的小明  阅读(1291)  评论(0编辑  收藏  举报