镜像构建交付和使用Rancher进行容器创建管理(2)

可使用docker内置的volume功能,将配置文件和Zookeeper的datadir映射至宿主机进行管理。
如在rancher中:

Docker微服务-镜像构建交付和使用rancher进行容器创建管理

所以,宿主机必须存在:
1) zookeeper-config/,目录为Zookeeper所有配置文件,包含集群的选举节点IP和选举端口,主要zoo.cfg文件中。

ll zookeeper-config/ total 16 -rw-r--r-- 1 root root 535 Jan 4 17:43 configuration.xsl -rw-r--r-- 1 root root 2161 Jan 4 17:43 log4j.properties -rw-r--r-- 1 root root 1012 Jan 4 17:43 zoo.cfg -rw-r--r-- 1 root root 922 Jan 4 17:43 zoo_sample.cf

2) zookeeper-data01/,目录对应zookeeper集群中的datadir,有存放一个myid文件,Zookeeper集群启动后产生的数据也会持久化到此宿主机目录下。Zookeeper集群有3个znode节点,所以datadir有:
zookeeper-data02/
zookeeper-data03/

ll /data/docker/ drwxr-xr-x 3 root root 48 Jan 5 17:25 ms-zookeeper3.4.10 drwxr-xr-x 2 root root 92 Jan 5 16:54 zookeeper-config drwxr-xr-x 2 root root 18 Jan 4 17:59 zookeeper-data01 drwxr-xr-x 2 root root 18 Jan 4 18:02 zookeeper-data02 drwxr-xr-x 2 root root 18 Jan 4 18:02 zookeeper-data03

2.镜像构建

2.1 下载zookeeper-3.4.10.tar.gz包
wget https://mirrors.cnnic.cn/apache/zookeeper/zookeeper-3.4.10/zookeeper-3.4.10.tar.gz
创建zookeeper-3.4.10构建build目录

mkdir -p /data/docker/ms-zookeeper3.4.10/ -rw-r--r-- 1 root root 207 Jan 4 18:58 dockerfile drwxr-xr-x 10 root root 4096 Mar 23 2017 zookeeper-3.4.10

2.2 编写dockfile
vim /data/docker/ms-zookeeper3.4.10/dockerfile

FROM ms/jdk1.8:v1 MAINTAINER zookeeper3.4.10 "name@alaxiaoyou.com" ADD zookeeper-3.4.10/ /usr/local/zookeeper/ WORKDIR /usr/local/zookeeper ENTRYPOINT ./bin/zkServer.sh start && tail -f zookeeper.out

其中zookeeper-3.4.10目录添加至镜像中,然后交付。但只是一个Zookeeper运行的模板,具体配置可通过volume,用宿主机的配置覆盖镜像中的配置。

2.3 构建,镜像

cd /data/docker/ms-zookeeper3.4.10 docker build -t ms/zookeeper3.4.10:v1 . docker images | grep zook ms/zookeeper3.4.10 v1 cb1bea8cf3d5 23 hours ago 567MB registry.cn-hangzhou.aliyuncs.com/xmbaby-tmp/tmp-zookeeper-ms v1 cb1bea8cf3d5 23 hours ago 567MB

镜像运行,本地docker本身命令行启动Zookeeper容器

[root@rancher-qa docker]# docker run --name zookeeper01 -it -v /data/docker/zookeeper-config/:/usr/local/zookeeper/conf/ -v /data/docker/zookeeper-data01/:/tmp/zookeeper/ ms/zookeeper3.4.10 /bin/bash

以上docker命令行启动卷映射目录正常,宿主机目录能映射到容器中。但rancher中用UI界面进行卷映射时,发现宿主机映射的目录在容器中并未映射到。待解决。

Docker微服务-镜像构建交付和使用rancher进行容器创建管理


此问题是由于,rancher有两台主机,而容器都运行在未进行设置映射目录的宿主机中。此问题困扰近一周时间,<_>

2.4 登录阿里云容器镜像服务
创建镜像仓库名称tmp-zookeeper-ms

Docker微服务-镜像构建交付和使用rancher进行容器创建管理

2.5 zookeeper镜像push到阿里云Docker Registry
切换到rancher/server
登录到阿里云Docker Registry
登录Docker Registry -> 打tag -> push到Docker Registry

[root@rancher-qa ms-zookeeper3.4.10]# docker login --username=name@alaxiaoyou.com registry.cn-hangzhou.aliyuncs.com Password: Login Succeeded [root@rancher-qa ms-zookeeper3.4.10]# [root@rancher-qa ms-zookeeper3.4.10]# docker tag cb1bea8cf3d5 registry.cn-hangzhou.aliyuncs.com/xmbaby-tmp/tmp-zookeeper-ms:v1 [root@rancher-qa ms-zookeeper3.4.10]# [root@rancher-qa ms-zookeeper3.4.10]# docker push registry.cn-hangzhou.aliyuncs.com/xmbaby-tmp/tmp-zookeeper-ms:v1 The push refers to a repository [registry.cn-hangzhou.aliyuncs.com/xmbaby-tmp/tmp-zookeeper-ms] ed1b06ecb437: Pushed 96f6833b8de8: Mounted from xmbaby-tmp/ms-jdk1.8 071d8bd76517: Mounted from xmbaby-tmp/ms-jdk1.8 v1: digest: sha256:c55b36fb410d7db9409e3ca4fb27e0d8d2d05e1970d99270ff9d5a6d8ee25e0b size: 954 [root@rancher-qa ms-zookeeper3.4.10]# docker images | grep zook registry.cn-hangzhou.aliyuncs.com/xmbaby-tmp/tmp-zookeeper-ms latest cb1bea8cf3d5 8 days ago 567MB registry.cn-hangzhou.aliyuncs.com/xmbaby-tmp/tmp-zookeeper-ms v1 cb1bea8cf3d5 8 days ago 567MB ms/zookeeper3.4.10 latest cb1bea8cf3d5 8 days ago 567MB ms/zookeeper3.4.10 v1 cb1bea8cf3d5 8 days ago 567MB

2.6查看

Docker微服务-镜像构建交付和使用rancher进行容器创建管理

至此,tmp-zookeeper-ms镜像交付完成。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/76c900b84e720f5a30e284909548b750.html