用docker部署zookeeper笔记

1)下载

wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u141-b15/336fa29ff2bb4ef291e347e091f7f4a7/jdk-8u141-linux-x64.tar.gz
wget https://www-eu.apache.org/dist/zookeeper/stable/zookeeper-$ZOOKEEPER_VERSION.tar.gz

  

2)Dockfile编写

FROM centos

RUN yum -y install tar yum-utils hostname net-tools  
RUN mkdir /opt/java && mkdir /opt/zookeeper 
COPY jdk-8u141-linux-x64.tar.gz /opt/java

RUN tar zxf /opt/java/jdk-8u141-linux-x64.tar.gz -C /opt/java &&\ 
    JAVA_HOME=/opt/java/jdk1.8.0_141 &&\ 
    sed -i "/^PATH/i export JAVA_HOME=$JAVA_HOME" /root/.bash_profile &&\ 
    sed -i "s%^PATH.*$%&:$JAVA_HOME/bin%g" /root/.bash_profile &&\ 
    source /root/.bash_profile

ENV ZOOKEEPER_VERSION=3.4.12 
COPY zookeeper-"$ZOOKEEPER_VERSION".tar.gz /opt/zookeeper
RUN tar zxf /opt/zookeeper/zookeeper*.tar.gz -C /opt/zookeeper
COPY start.sh /opt/zookeeper/
RUN chmod +x /opt/zookeeper/start.sh
EXPOSE 2181
ENTRYPOINT ["sh", "/opt/zookeeper/start.sh", "3.4.12"]

 

3)zookeeper启动脚本start.sh

#!/user/bin/sh

ZOOKEEPER_VERSION=$1
source /root/.bash_profile
cp /opt/zookeeper/zookeeper-"$ZOOKEEPER_VERSION"/conf/zoo_sample.cfg /opt/zookeeper/zookeeper-"$ZOOKEEPER_VERSION"/conf/zoo.cfg
echo server.1=zk1:2888:3888 >> /opt/zookeeper/zookeeper-"$ZOOKEEPER_VERSION"/conf/zoo.cfg
echo server.2=zk2:2888:3888 >> /opt/zookeeper/zookeeper-"$ZOOKEEPER_VERSION"/conf/zoo.cfg
echo server.3=zk3:2888:3888 >> /opt/zookeeper/zookeeper-"$ZOOKEEPER_VERSION"/conf/zoo.cfg
mkdir -p /tmp/zookeeper
echo $2 > /tmp/zookeeper/myid
/opt/zookeeper/zookeeper-"$ZOOKEEPER_VERSION"/bin/zkServer.sh start-foreground

4)主机配置hosts 修改/etc/hosts末尾添加, 启动的容器使用主机的hosts

  172.17.0.2  zk1
  172.17.0.3  zk2
  172.17.0.4  zk3

5)  

生成 image:
docker build -t myzookeeper 启动三个容器: docker run -it --name zk1 --hostname zk1 $(cat /etc/hosts|awk -F ' ' '{if(NR>2){print "--add-host "$2":"$1}}') -d myzookeeper 1 docker run -it --name zk2 --hostname zk2 $(cat /etc/hosts|awk -F ' ' '{if(NR>2){print "--add-host "$2":"$1}}') -d myzookeeper 2 docker run -it --name zk3 --hostname zk3 $(cat /etc/hosts|awk -F ' ' '{if(NR>2){print "--add-host "$2":"$1}}') -d myzookeeper 3

 6)查看所有的容器主机名和IP 

docker inspect -f '{{.Name}} - {{.NetworkSettings.IPAddress }}' $(docker ps -aq)

7)查看zookeeper状态

[root@localhost zk_docker_file]# telnet 172.17.0.2 2181
Trying 172.17.0.2...
Connected to 172.17.0.2.
Escape character is '^]'.
stat
Zookeeper version: 3.4.12-e5259e437540f349646870ea94dc2658c4e44b3b, built on 03/27/2018 03:55 GMT
Clients:
/172.17.0.1:40332[0](queued=0,recved=1,sent=0)

Latency min/avg/max: 0/0/0
Received: 2
Sent: 1
Connections: 1
Outstanding: 0
Zxid: 0x0
Mode: follower
Node count: 4

 

8)用客户端连接

./zookeeper-3.4.12/bin/zkCli.sh -server  172.17.0.3:2181

9)zookeeper的c++使用

https://github.com/yandaren/zk_cpp

  

posted @ 2018-12-04 17:14  buger(八哥)  阅读(1430)  评论(0编辑  收藏  举报