前言
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux或Windows操作系统的机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。
云计算、大数据,移动技术的快速发展,加之企业业务需求的不断变化,导致企业架构要随时更改以适合业务需求,跟上技术更新的步伐。毫无疑问,这些重担都将压在企业开发人员身上;团队之间如何高效协调,快速交付产品,快速部署应用,以及满足企业业务需求,是开发人员亟需解决的问题。Docker技术恰好可以帮助开发人员解决这些问题。
前段时间刚刚测试了容器版本3.1.4,然后马上就发布了4.0了,这里制作一下4.0的容器版本,体验容器制作的乐趣。
一、准备容器制作环境
1. Dockerfile
[root@oceanbase1 oceanbase]# cat Dockerfile FROM centos:7.9.2009 COPY boot /root/boot/ ENV VERSION=4.0.0.0-100000272022110114 RUN yum-config-manager --add-repo https://mirrors.aliyun.com/oceanbase/OceanBase.repo && \ yum install -y --nogpgcheck ob-deploy obclient ob-sysbench wget libaio && \ rm -rf /usr/obd/mirror/remote/* && \ rm -rf /u01/mysql /u01/obclient/bin/mysqld* /u01/obclient/bin/aria* /u01/obclient/bin/maria* && \ yum clean all RUN mkdir /root/pkg && \ cd /root/pkg && \ yum install --downloadonly --downloaddir=. oceanbase-ce-${VERSION}.el7 oceanbase-ce-libs-${VERSION}.el7 obagent && \ rm -rf /usr/obd/mirror/remote/* && \ obd mirror clone /root/pkg/*.rpm && \ obd mirror list local && \ rm -rf /root/pkg/* && \ yum clean all ENV PATH=/root/boot:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin WORKDIR /root CMD ["_boot"]
2. boot
- _boot为容器启动后执行的脚本,里边包含了安装数据库及启动保持运行状态
- boot-mini-tmp.yaml boot-tmp.yaml obagent.yaml 为配置文件
- _env 环境变量设置文件
- init_tenant_user.sql 执行SQL文件
- ob-mysql 登录脚本
[root@oceanbase1 oceanbase]# ls boot/ _boot boot-mini-tmp.yaml boot-tmp.yaml _env init_tenant_user.sql obagent.yaml ob-mysql [root@oceanbase1 oceanbase]# cat boot/_boot #!/bin/bash CWD=$(cd `dirname $0`;pwd) cd "${CWD}" source _env STAMP="$(date +%s)" function is_true() { value=$1 # convert value to upper case string(can work in bash 4.x) value=${value^^} if [ "x${value}" == "xNO" ] || [ "x${value}" == "xFALSE" ] || [ "x${value}" == "x0" ]; then return 1 fi return 0 } # return 0 if mini_mode is nil or 'no'/'false'/0 # 0 means true and 1 for false in bash function is_mini_mode() { if test -z ${MINI_MODE} then return 1 fi return `is_true ${MINI_MODE}` } function exit_while_error() { if test -z ${EXIT_WHILE_ERROR} then return 0 fi return `is_true ${EXIT_WHILE_ERROR}` } function remove_disk_check_logic_in_obd() { # make sure obd copy the plugin code obd cluster list start_check_file='/root/.obd/plugins/oceanbase/3.1.0/start_check.py' sed -i "s/critical('(%s) %s not enough disk space\. (Avail/alert('(%s) %s not enough disk space\. (Avail/g" $start_check_file sed -i "s/critical(EC_OBSERVER_NOT_ENOUGH_DISK_4_CLOG/alert(EC_OBSERVER_NOT_ENOUGH_DISK_4_CLOG/g" $start_check_file } # We should decide whether the observer's data exists and # whether the obd has the information of the cluster if [ -f "$HOME/.obd/cluster/${OB_CLUSTER_NAME}/config.yaml" ]; then echo "find obd deploy information, skip configuring..." echo "start ob cluster ..." obd cluster start $OB_CLUSTER_NAME else # nothing here, bootstrap echo "generate boot.yaml ..." TMPFILE="boot.${STAMP}.yaml" if is_mini_mode then echo "oceanbase-ce docker in mini mode" cp -f boot-mini-tmp.yaml $TMPFILE else cp -f boot-tmp.yaml $TMPFILE fi cat obagent.yaml >> $TMPFILE sed -i "s|@OB_HOME_PATH@|${OB_HOME_PATH}|g" $TMPFILE sed -i "s|@OB_MYSQL_PORT@|${OB_MYSQL_PORT}|g" $TMPFILE sed -i "s|@OB_RPC_PORT@|${OB_RPC_PORT}|g" $TMPFILE sed -i "s|@OB_ROOT_PASSWORD@|${OB_ROOT_PASSWORD}|g" $TMPFILE [ "${OB_DATA_DIR}" ] && echo " data_dir: ${OB_DATA_DIR}" >> $TMPFILE [ "${OB_REDO_DIR}" ] && echo " redo_dir: ${OB_REDO_DIR}" >> $TMPFILE echo "create boot dirs and deploy ob cluster ..." mkdir -p $OB_HOME_PATH #obd mirror clone /root/pkg/*.rpm \ #&& obd mirror list local remove_disk_check_logic_in_obd obd cluster autodeploy "${OB_CLUSTER_NAME}" -c $TMPFILE \ && obd cluster tenant create "${OB_CLUSTER_NAME}" -n ${OB_TENANT_NAME} \ && obclient -h127.1 -uroot@${OB_TENANT_NAME} -A -P${OB_MYSQL_PORT} < init_tenant_user.sql \ && mv -f $TMPFILE ${OB_HOME_PATH}/boot.yaml \ && echo "deploy success!" fi if [ $? -eq 0 ]; then echo "boot success!" else echo "boot failed!" if exit_while_error then exit 1 else echo "Please check the log file ${OB_HOME_PATH}/log/observer.log" fi fi exec /sbin/init
二、 容器制作
- docker build
[root@oceanbase1 oceanbase]# docker build -t oceanbase/oceanbase-ce:test . Sending build context to Docker daemon 59.9kB Step 1/8 : FROM centos:7.9.2009 ---> eeb6ee3f44bd Step 2/8 : COPY boot /root/boot/ ---> 7b129b57d9d2 Step 3/8 : ENV VERSION=4.0.0.0-100000272022110114 ---> Running in b1eec03fa3d4 Removing intermediate container b1eec03fa3d4 ---> 288c489c3885 Step 4/8 : RUN yum-config-manager --add-repo https://mirrors.aliyun.com/oceanbase/OceanBase.repo && yum install -y --nogpgcheck ob-deploy obclient ob-sysbench wget libaio && rm -rf /usr/obd/mirror/remote/* && rm -rf /u01/mysql /u01/obclient/bin/mysqld* /u01/obclient/bin/aria* /u01/obclient/bin/maria* && yum clean all ---> Running in c643c4eeaf91 Loaded plugins: fastestmirror, ovl adding repo from: https://mirrors.aliyun.com/oceanbase/OceanBase.repo grabbing file https://mirrors.aliyun.com/oceanbase/OceanBase.repo to /etc/yum.repos.d/OceanBase.repo repo saved to /etc/yum.repos.d/OceanBase.repo Loaded plugins: fastestmirror, ovl Determining fastest mirrors * base: mirrors.huaweicloud.com * extras: mirrors.huaweicloud.com * updates: mirrors.huaweicloud.com Resolving Dependencies --> Running transaction check ---> Package libaio.x86_64 0:0.3.109-13.el7 will be installed ---> Package ob-deploy.x86_64 0:1.6.0-41.el7 will be installed ---> Package ob-sysbench.x86_64 0:1.0.20-3.el7 will be installed ---> Package obclient.x86_64 0:2.2.0-6.el7 will be installed --> Processing Dependency: libobclient >= 2.0.0 for package: obclient-2.2.0-6.el7.x86_64 ---> Package wget.x86_64 0:1.14-18.el7_6.1 will be installed --> Running transaction check ---> Package libobclient.x86_64 0:2.2.0-5.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: libaio x86_64 0.3.109-13.el7 base 24 k ob-deploy x86_64 1.6.0-41.el7 oceanbase.community.stable 34 M ob-sysbench x86_64 1.0.20-3.el7 oceanbase.development-kit 346 k obclient x86_64 2.2.0-6.el7 oceanbase.community.stable 5.8 M wget x86_64 1.14-18.el7_6.1 base 547 k Installing for dependencies: libobclient x86_64 2.2.0-5.el7 oceanbase.community.stable 1.3 M Transaction Summary ================================================================================ Install 5 Packages (+1 Dependent package) Total download size: 42 M Installed size: 115 M Downloading packages: -------------------------------------------------------------------------------- Total 839 kB/s | 42 MB 00:50 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : libobclient-2.2.0-5.el7.x86_64 1/6 Installing : obclient-2.2.0-6.el7.x86_64 2/6 Installing : ob-sysbench-1.0.20-3.el7.x86_64 3/6 Please execute 'sysbench --help' for more information. Installing : libaio-0.3.109-13.el7.x86_64 4/6 Installing : wget-1.14-18.el7_6.1.x86_64 5/6 install-info: No such file or directory for /usr/share/info/wget.info.gz Installing : ob-deploy-1.6.0-41.el7.x86_64 6/6 Installation of obd finished successfully Please source /etc/profile.d/obd.sh to enable it Verifying : ob-deploy-1.6.0-41.el7.x86_64 1/6 Verifying : obclient-2.2.0-6.el7.x86_64 2/6 Verifying : ob-sysbench-1.0.20-3.el7.x86_64 3/6 Verifying : libobclient-2.2.0-5.el7.x86_64 4/6 Verifying : wget-1.14-18.el7_6.1.x86_64 5/6 Verifying : libaio-0.3.109-13.el7.x86_64 6/6 Installed: libaio.x86_64 0:0.3.109-13.el7 ob-deploy.x86_64 0:1.6.0-41.el7 ob-sysbench.x86_64 0:1.0.20-3.el7 obclient.x86_64 0:2.2.0-6.el7 wget.x86_64 0:1.14-18.el7_6.1 Dependency Installed: libobclient.x86_64 0:2.2.0-5.el7 Complete! Loaded plugins: fastestmirror, ovl Cleaning repos: base extras oceanbase.community.stable oceanbase.development-kit : updates Cleaning up list of fastest mirrors Removing intermediate container c643c4eeaf91 ---> 35c0ede713a8 Step 5/8 : RUN mkdir /root/pkg && cd /root/pkg && yum install --downloadonly --downloaddir=. oceanbase-ce-${VERSION}.el7 oceanbase-ce-libs-${VERSION}.el7 obagent && rm -rf /usr/obd/mirror/remote/* && obd mirror clone /root/pkg/*.rpm && obd mirror list local && rm -rf /root/pkg/* && yum clean all ---> Running in 620cfce5977a Loaded plugins: fastestmirror, ovl Determining fastest mirrors * base: mirrors.tuna.tsinghua.edu.cn * extras: mirrors.bfsu.edu.cn * updates: mirrors.tuna.tsinghua.edu.cn Resolving Dependencies --> Running transaction check ---> Package obagent.x86_64 0:1.2.0-4.el7 will be installed ---> Package oceanbase-ce.x86_64 0:4.0.0.0-100000272022110114.el7 will be installed ---> Package oceanbase-ce-libs.x86_64 0:4.0.0.0-100000272022110114.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: obagent x86_64 1.2.0-4.el7 oceanbase.community.stable 6.8 M oceanbase-ce x86_64 4.0.0.0-100000272022110114.el7 oceanbase.community.stable 64 M oceanbase-ce-libs x86_64 4.0.0.0-100000272022110114.el7 oceanbase.community.stable 155 k Transaction Summary ================================================================================ Install 3 Packages Total download size: 71 M Installed size: 323 M Background downloading packages, then exiting: warning: /root/pkg/obagent-1.2.0-4.el7.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID e9b4a7aa: NOKEY Public key for obagent-1.2.0-4.el7.x86_64.rpm is not installed -------------------------------------------------------------------------------- Total 738 kB/s | 71 MB 01:38 exiting because "Download Only" specified name: obagent version: 1.2.0 release:4.el7 arch: x86_64 md5: 0e8f5ee68c337ea28514c9f3f820ea546227fa7e add /root/pkg/obagent-1.2.0-4.el7.x86_64.rpm to local mirror name: oceanbase-ce version: 4.0.0.0 release:100000272022110114.el7 arch: x86_64 md5: 42611dc51ca9bb28f36e60e4406ceea4a74914c7 add /root/pkg/oceanbase-ce-4.0.0.0-100000272022110114.el7.x86_64.rpm to local mirror name: oceanbase-ce-libs version: 4.0.0.0 release:100000272022110114.el7 arch: x86_64 md5: 188919f8128394bf9b62e3989220ded05f1d14da add /root/pkg/oceanbase-ce-libs-4.0.0.0-100000272022110114.el7.x86_64.rpm to local mirror +----------------------------------------------------------------------------------------------------------+ | local Package List | +-------------------+---------+------------------------+--------+------------------------------------------+ | name | version | release | arch | md5 | +-------------------+---------+------------------------+--------+------------------------------------------+ | obagent | 1.2.0 | 4.el7 | x86_64 | 0e8f5ee68c337ea28514c9f3f820ea546227fa7e | | oceanbase-ce | 4.0.0.0 | 100000272022110114.el7 | x86_64 | 42611dc51ca9bb28f36e60e4406ceea4a74914c7 | | oceanbase-ce-libs | 4.0.0.0 | 100000272022110114.el7 | x86_64 | 188919f8128394bf9b62e3989220ded05f1d14da | +-------------------+---------+------------------------+--------+------------------------------------------+ Loaded plugins: fastestmirror, ovl Cleaning repos: base extras oceanbase.community.stable oceanbase.development-kit : updates Cleaning up list of fastest mirrors Removing intermediate container 620cfce5977a ---> e06e193106ed Step 6/8 : ENV PATH=/root/boot:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin ---> Running in 73a5733d1607 Removing intermediate container 73a5733d1607 ---> 70c1a0400752 Step 7/8 : WORKDIR /root ---> Running in 5438b10bddb6 Removing intermediate container 5438b10bddb6 ---> b05b213b9854 Step 8/8 : CMD ["_boot"] ---> Running in fe35aded5746 Removing intermediate container fe35aded5746 ---> 7daceecff241 Successfully built 7daceecff241 Successfully tagged oceanbase/oceanbase-ce:test
三、运行测试
1. docker image ls
[root@oceanbase1 boot]# docker image ls oceanbase/oceanbase-ce test 7daceecff241 9 minutes ago 447MB
2. 运行启动
[root@oceanbase1 oceanbase]# docker run -p 2882:2882 --name oceanbase -e MINI_MODE=1 -d oceanbase/oceanbase-ce:latest 6534eaf1d439ae5d500411c4ca3be9ef513c8f9ddefaa3401093d73a5df24446
3. 查看日志
[root@oceanbase1 oceanbase]# docker logs -f oceanbase generate boot.yaml ... oceanbase-ce docker in mini mode create boot dirs and deploy ob cluster ... Local deploy is empty Package oceanbase-ce-4.0.0.0-100000272022110114.el7 is available. Package obagent-1.2.0-4.el7 is available. install oceanbase-ce-4.0.0.0 for local ok install obagent-1.2.0 for local ok Cluster param config check ok Open ssh connection ok Generate observer configuration ok Generate obagent configuration ok install oceanbase-ce-4.0.0.0 for local ok install obagent-1.2.0 for local ok +--------------------------------------------------------------------------------------------+ | Packages | +--------------+---------+------------------------+------------------------------------------+ | Repository | Version | Release | Md5 | +--------------+---------+------------------------+------------------------------------------+ | oceanbase-ce | 4.0.0.0 | 100000272022110114.el7 | 42611dc51ca9bb28f36e60e4406ceea4a74914c7 | | obagent | 1.2.0 | 4.el7 | 0e8f5ee68c337ea28514c9f3f820ea546227fa7e | +--------------+---------+------------------------+------------------------------------------+ Repository integrity check ok Parameter check ok Open ssh connection ok Cluster status check ok Initializes observer work home ok Initializes obagent work home ok Remote oceanbase-ce-4.0.0.0-100000272022110114.el7-42611dc51ca9bb28f36e60e4406ceea4a74914c7 repository install ok Remote oceanbase-ce-4.0.0.0-100000272022110114.el7-42611dc51ca9bb28f36e60e4406ceea4a74914c7 repository lib check !! Remote obagent-1.2.0-4.el7-0e8f5ee68c337ea28514c9f3f820ea546227fa7e repository install ok Remote obagent-1.2.0-4.el7-0e8f5ee68c337ea28514c9f3f820ea546227fa7e repository lib check ok Try to get lib-repository Package oceanbase-ce-libs-4.0.0.0-100000272022110114.el7 is available. install oceanbase-ce-libs-4.0.0.0 for local ok Remote oceanbase-ce-libs-4.0.0.0-100000272022110114.el7-188919f8128394bf9b62e3989220ded05f1d14da repository install ok Remote oceanbase-ce-4.0.0.0-100000272022110114.el7-42611dc51ca9bb28f36e60e4406ceea4a74914c7 repository lib check ok obcluster deployed Get local repositories ok Search plugins ok Open ssh connection ok Load cluster param plugin ok Check before start observer ok [WARN] OBD-1007: (127.0.0.1) The recommended number of open files is 655350 (Current value: 65536) [WARN] (127.0.0.1) clog and data use the same disk (/) Check before start obagent ok Start observer ok observer program health check ok Connect to observer ok Initialize cluster ok Start obagent ok obagent program health check ok Wait for observer init ok +---------------------------------------------+ | observer | +-----------+---------+------+-------+--------+ | ip | version | port | zone | status | +-----------+---------+------+-------+--------+ | 127.0.0.1 | 4.0.0.0 | 2881 | zone1 | ACTIVE | +-----------+---------+------+-------+--------+ obclient -h127.0.0.1 -P2881 -uroot -Doceanbase +------------------------------------------------+ | obagent | +------------+-------------+------------+--------+ | ip | server_port | pprof_port | status | +------------+-------------+------------+--------+ | 172.17.0.2 | 8088 | 8089 | active | +------------+-------------+------------+--------+ obcluster running Get local repositories and plugins ok Open ssh connection ok Connect to observer ok Create tenant test ok deploy success! boot success!
4. 查看版本
[root@oceanbase1 oceanbase]# docker exec -it oceanbase ob-mysql sys login as root@sys Command is: obclient -h127.1 -uroot@sys -A -Doceanbase -P2881 Welcome to the OceanBase. Commands end with ; or \g. Your OceanBase connection id is 3221487621 Server version: OceanBase_CE 4.0.0.0 (r100000272022110114-6af7f9ae79cd0ecbafd4b1b88e2886ccdba0c3be) (Built Nov 1 2022 14:57:18) Copyright (c) 2000, 2018, OB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. obclient [oceanbase]> select version); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your OceanBase version for the right syntax to use near ')' at line 1 obclient [oceanbase]> select version(); +------------------------------+ | version() | +------------------------------+ | 5.7.25-OceanBase_CE-v4.0.0.0 | +------------------------------+ 1 row in set (0.002 sec) obclient [oceanbase]>
四、容器基础命令
1. 查看所有容器
$ docker ps [参数] #参数说明 列出所有正在运行的容器 -a 列出所有容器 -n=? 显示最近创建的容器 -q 只显示容器的编号
2. 退出
$ exit #直接停止并退出 Ctrl+P+Q #不停止退出
3. 删除
$ docker rm 容器id #删除指定id的容器,不能删除正在运行的 $ docker rm -f $(docker ps -aq) #强制删除所有的容器 $ docker ps -a -q |xargs docker rm #管道删除所有容器
4. 启动和停止
$ docker start 容器id #启动 $ docker restart 容器id #重启 $ docker stop 容器id #停止 $ docker kill 容器id #强制停止 $ docker pause 容器id #暂停 $ docker unpause 容器id #取消暂停
总结
容器版本制作还是挺方便迅速的,download比想象的要快很多,上次可能是我网络的原因导致build巨慢,建议官方也发布deb版本的安装包,采用Ubuntu的基础镜像可以减少image大小。