docker深入2-使用自定义的网络来配置zookeeper集群

2016/6/24

一、示例测试环境基于文档:docker深入2-熟悉v1.11和找不同.txt目标:熟悉自定义网络的应用----------------------------------------------------------------------------- n36.test -----------------------zk1                                        172.30.200.1zk2   -> docker network(net_env_test) ->   172.30.200.2zk3                                        172.30.200.3-------------------------------------------------------二、镜像和网络1、镜像[Jack@n36 ~]$ docker pull registry.aliyuncs.com/denverdino/zookeeper:3.4.82、自定义网络[Jack@n36 ~]$ docker network create --subnet 172.30.0.0/16 --gateway 172.30.0.1 --ip-range 172.30.0.0/17 net_env_testfe455726968a326cbae52a4260cfd1d013884f897f442fdaafa43f6fa2dffcc3注:解释一下这里的网络配置刚才我们创建的这个 network 所属的网段是:172.30.0.0/16可用于自动分配的ip范围是:172.30.0.0/17(172.30.0.1 - 172.30.127.254)这意味着,我们保留了一段IP用于手动分配:172.30.128.0/17(172.30.128.1 - 172.30.255.254)因此,这个也算是一个IP规划的小技巧,仅供参考。三、启动zookeeper集群,指定IP地址[Jack@n36 ~]$ docker run -d \--name zk1 \--net net_env_test \--ip 172.30.200.1 \-e SERVER_ID=1 \-e ADDITIONAL_ZOOKEEPER_1=server.1=172.30.200.1:2888:3888 \-e ADDITIONAL_ZOOKEEPER_2=server.2=172.30.200.2:2888:3888 \-e ADDITIONAL_ZOOKEEPER_3=server.3=172.30.200.3:2888:3888 \registry.aliyuncs.com/denverdino/zookeeper:3.4.8  [Jack@n36 ~]$ docker run -d \--name zk2 \--net net_env_test \--ip 172.30.200.2 \-e SERVER_ID=2 \-e ADDITIONAL_ZOOKEEPER_1=server.1=172.30.200.1:2888:3888 \-e ADDITIONAL_ZOOKEEPER_2=server.2=172.30.200.2:2888:3888 \-e ADDITIONAL_ZOOKEEPER_3=server.3=172.30.200.3:2888:3888 \registry.aliyuncs.com/denverdino/zookeeper:3.4.8[Jack@n36 ~]$ docker run -d \--name zk3 \--net net_env_test \--ip 172.30.200.3 \-e SERVER_ID=3 \-e ADDITIONAL_ZOOKEEPER_1=server.1=172.30.200.1:2888:3888 \-e ADDITIONAL_ZOOKEEPER_2=server.2=172.30.200.2:2888:3888 \-e ADDITIONAL_ZOOKEEPER_3=server.3=172.30.200.3:2888:3888 \registry.aliyuncs.com/denverdino/zookeeper:3.4.8[Jack@n36 ~]$ docker inspect --format='{
{range .NetworkSettings.Networks}}``.`IPAddress``end`' `docker ps -f name=zk[1-3] -q`172.30.200.3172.30.200.2172.30.200.1[Jack@n36 ~]$ docker ps -f name=zk[1-3]CONTAINER ID        IMAGE                                              COMMAND               CREATED             STATUS              PORTS                          NAMESc2feaffe2ce7        registry.aliyuncs.com/denverdino/zookeeper:3.4.8   "/opt/zookeeper.sh"   27 seconds ago      Up 26 seconds       2181/tcp, 2888/tcp, 3888/tcp   zk385e66c209e30        registry.aliyuncs.com/denverdino/zookeeper:3.4.8   "/opt/zookeeper.sh"   31 seconds ago      Up 30 seconds       2181/tcp, 2888/tcp, 3888/tcp   zk25950af6802fe        registry.aliyuncs.com/denverdino/zookeeper:3.4.8   "/opt/zookeeper.sh"   36 seconds ago      Up 35 seconds       2181/tcp, 2888/tcp, 3888/tcp   zk1四、测试1、连通性[Jack@n36 ~]$ docker exec -it zk1 bashbash-4.3# ping zk2PING zk2 (172.30.200.2): 56 data bytes64 bytes from 172.30.200.2: seq=0 ttl=64 time=0.173 ms^C--- zk2 ping statistics ---1 packets transmitted, 1 packets received, 0% packet lossround-trip min/avg/max = 0.173/0.173/0.173 msbash-4.3# ping zk3PING zk3 (172.30.200.3): 56 data bytes64 bytes from 172.30.200.3: seq=0 ttl=64 time=0.101 ms^C--- zk3 ping statistics ---1 packets transmitted, 1 packets received, 0% packet lossround-trip min/avg/max = 0.101/0.101/0.101 ms2、服务[Jack@n36 ~]$ docker exec -it zk1 bashbash-4.3# bin/zkCli.sh -server zk3:21812016-06-23 03:27:28,058 [myid:] - INFO  [main:Environment@100] - Client environment:zookeeper.version=3.4.8--1, built on 02/06/2016 03:18 GMT2016-06-23 03:27:28,063 [myid:] - INFO  [main:Environment@100] - Client environment:host.name=5950af6802fe2016-06-23 03:27:28,063 [myid:] - INFO  [main:Environment@100] - Client environment:java.version=1.8.0_77-internal2016-06-23 03:27:28,066 [myid:] - INFO  [main:Environment@100] - Client environment:java.vendor=Oracle Corporation2016-06-23 03:27:28,067 [myid:] - INFO  [main:Environment@100] - Client environment:java.home=/usr/lib/jvm/java-1.8-openjdk/jre2016-06-23 03:27:28,067 [myid:] - INFO  [main:Environment@100] - Client environment:java.class.path=/opt/zookeeper/bin/../build/classes:/opt/zookeeper/bin/../build/lib/*.jar:/opt/zookeeper/bin/../lib/slf4j-log4j12-1.6.1.jar:/opt/zookeeper/bin/../lib/slf4j-api-1.6.1.jar:/opt/zookeeper/bin/../lib/netty-3.7.0.Final.jar:/opt/zookeeper/bin/../lib/log4j-1.2.16.jar:/opt/zookeeper/bin/../lib/jline-0.9.94.jar:/opt/zookeeper/bin/../zookeeper-3.4.8.jar:/opt/zookeeper/bin/../src/java/lib/*.jar:/opt/zookeeper/bin/../conf:2016-06-23 03:27:28,067 [myid:] - INFO  [main:Environment@100] - Client environment:java.library.path=/usr/lib/jvm/java-1.8-openjdk/jre/lib/amd64/server:/usr/lib/jvm/java-1.8-openjdk/jre/lib/amd64:/usr/lib/jvm/java-1.8-openjdk/jre/../lib/amd64:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib2016-06-23 03:27:28,067 [myid:] - INFO  [main:Environment@100] - Client environment:java.io.tmpdir=/tmp2016-06-23 03:27:28,067 [myid:] - INFO  [main:Environment@100] - Client environment:java.compiler=
2016-06-23 03:27:28,068 [myid:] - INFO  [main:Environment@100] - Client environment:os.name=Linux2016-06-23 03:27:28,068 [myid:] - INFO  [main:Environment@100] - Client environment:os.arch=amd642016-06-23 03:27:28,068 [myid:] - INFO  [main:Environment@100] - Client environment:os.version=3.10.0-229.el7.x86_642016-06-23 03:27:28,068 [myid:] - INFO  [main:Environment@100] - Client environment:user.name=root2016-06-23 03:27:28,068 [myid:] - INFO  [main:Environment@100] - Client environment:user.home=/root2016-06-23 03:27:28,068 [myid:] - INFO  [main:Environment@100] - Client environment:user.dir=/opt/zookeeper2016-06-23 03:27:28,070 [myid:] - INFO  [main:ZooKeeper@438] - Initiating client connection, connectString=zk3:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@3eb07fd3Welcome to ZooKeeper!2016-06-23 03:27:28,109 [myid:] - INFO  [main-SendThread(zk3.net_env_test:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server zk3.net_env_test/172.30.200.3:2181. Will not attempt to authenticate using SASL (unknown error)JLine support is enabled2016-06-23 03:27:28,205 [myid:] - INFO  [main-SendThread(zk3.net_env_test:2181):ClientCnxn$SendThread@876] - Socket connection established to zk3.net_env_test/172.30.200.3:2181, initiating session2016-06-23 03:27:28,215 [myid:] - INFO  [main-SendThread(zk3.net_env_test:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server zk3.net_env_test/172.30.200.3:2181, sessionid = 0x3557b3d3ba80001, negotiated timeout = 30000WATCHER::WatchedEvent state:SyncConnected type:None path:null[zk: zk3:2181(CONNECTED) 0] [zk: zk3:2181(CONNECTED) 0] ls /[zookeeper][zk: zk3:2181(CONNECTED) 1] create /zk_test test_dataCreated /zk_test[zk: zk3:2181(CONNECTED) 2] ls /                         [zookeeper, zk_test][zk: zk3:2181(CONNECTED) 3] get /zk_testtest_datacZxid = 0x100000015ctime = Thu Jun 23 03:28:07 GMT 2016mZxid = 0x100000015mtime = Thu Jun 23 03:28:07 GMT 2016pZxid = 0x100000015cversion = 0dataVersion = 0aclVersion = 0ephemeralOwner = 0x0dataLength = 11numChildren = 0[zk: zk3:2181(CONNECTED) 7] quitQuitting...2016-06-23 03:28:25,427 [myid:] - INFO  [main:ZooKeeper@684] - Session: 0x3557b3d3ba80001 closedbash-4.3# ZYXW、参考1、学习Docker容器网络模型 - 搭建分布式Zookeeper集群https://yq.aliyun.com/articles/30328?spm=5176.100239.blogcont55866.14.KQScYn#https://github.com/denverdino/aliyungo/wiki/Zookeeper-cluster-with-Docker?spm=5176.100239.blogcont30328.19.av6ce8