因为个人学习需要,为软件系统的虚拟容器化,以下将mysql制作为docker镜像,并记录下详细步骤。
欢迎大家学习交流和转载,同时写作不易,如果各位觉得不错,请点赞支持。
备注:以下代码和文章,欢迎复制和转载,请在开头备注清楚“原始网址和作者”,谢谢!
一、实验环境
1.本地电脑:
windows 10 x64 / git version 2.21.0.windows.1
2. 本地docker 构建环境:
CentOS 7.4 x64 / Docker Engine - Community 18.09.6 x64
3. 阿里云构建环境:
代码库:https://code.aliyun.com/03_docker_base/mysql-single
镜像库:https://cr.console.aliyun.com/repository/cn-hangzhou/kevin_docker_registry/docker_base_mysql-single/details4.
mysql版本:mysql-cluster-8.0.18-rc-linux-glibc2.12-x86_64.tar.gz
二、编写代码
本地编写dockerfile代码,目录层次如下
1. 目录层次,
dockerfile的代码目录,如下
----|----/03_docker_base/mysql-single/
|----soft/ # 放COPY命令,需要复制的小文件,大文件建议通过-v和VOLUMN方式挂载共享卷方式获得。
|----mysql.conf # mysql的配置参数文件
|----mysql.sh # mysql的启动脚本文件
|----mysql-single-centos.Dockerfile # docker镜像的源代码
|----mysql-single.sh # 个人笔记,记录一下当时操作的命令和步骤
构建环境下面的共享卷的资源目录,如下
----|----/nfs_data/dockerfile/mysql-single/soft/
|----mysql-cluster-8.0.18-rc-linux-glibc2.12-x86_64.tar
2. 代码介绍,如下:
提醒:
<a>最初制作mysql镜像的时候,由于是挂载共享卷,所以共享卷的文件只能被CMD或ENTRYPOINT命令访问,RUN是构建时刻,所以访问不了volumn共享卷的文件。
<b>创建的dockerfile/mysql.sh/mysql.conf 等文件,一定要在linux下面创建和编辑,防止产生特殊字符和非法编码。编码都默认使用UTF-8 BOM。
<c>执行测试,可以使用命令 -v host_path:docker_path 进行挂载共享卷,并且dockerfile文件中也要对应写VOLUMN ["docker_path"]
1 FROM centos:centos7 2 MAINTAINER tian.dong <tiandong19860806@126.com> 3 4 RUN 5 # yum update -y -q && 6 yum -y -q install libaio -y && 7 yum -y -q install numactl -y && 8 yum clean all 9 10 # step 1: set enviroment 11 ENV DOCKER_DATA /nfs_data 12 ENV DOCKER_PASS pass 13 ENV MYSQL_BASEDIR /opt/install/mysql 14 ENV MYSQL_DATADIR /opt/data/mysql 15 ENV MYSQL_LOGDIR /opt/log/mysql 16 ENV MYSQL_CONF /opt/install/mysql/conf 17 ENV SOFT_MYSQL /nfs_data/dockerfile/mysql-single/soft/mysql-cluster-8.0.18-rc-linux-glibc2.12-x86_64.tar.gz 18 19 RUN 20 echo '' >> /etc/profile && 21 echo 'PATH=$PATH:$MYSQL_BASEDIR/bin:$MYSQL_BASEDIR/lib' >> /etc/profile 22 23 # VOLUME docker 方式需要写;K8S 方式不需要写,可通过PV/PVC实现 24 VOLUME ["$DOCKER_DATA"] 25 26 27 # step 2: make directory 28 RUN mkdir -p ${MYSQL_CONF} && 29 groupadd mysql && 30 useradd -g mysql mysql && 31 echo ${DOCKER_PASS} | passwd --stdin mysql 32 33 # USER mysql:mysql 34 35 COPY soft/mysql.conf ${MYSQL_CONF}/ 36 COPY soft/mysql.sh /usr/local/bin/ 37 # COPY soft/mysql-cluster-8.0.18-rc-linux-glibc2.12-x86_64.tar.gz /opt/install/ 38 # RUN wget -O /opt/install/mysql-cluster-8.0.18-rc-linux-glibc2.12-x86_64.tar.gz -c 39 # https://downloads.mysql.com/archives/get/p/14/file/mysql-cluster-8.0.18-rc-linux-glibc2.12-x86_64.tar.gz 40 41 # COPY soft/mysql-cluster-8.0.18-rc-linux-glibc2.12-x86_64.tar.gz /opt/install/ 42 # 使用RUN,不用CMD的原因,执行顺序 RUN (build) < CMD (starting) < ENTRYPOINT (started) 43 # 并且,在RUN命令中,所有VOLUMN或docker -v path:path的外部挂载点,都是处于尚未就绪的状态;只有ENTRYPOINT时刻,才会挂载外部存储介质完毕。 44 RUN 45 chmod +x /usr/local/bin/mysql.sh && 46 chmod -R 777 ${DOCKER_DATA} && 47 mkdir -p ${MYSQL_BASEDIR} && 48 mkdir -p ${MYSQL_DATADIR} && 49 mkdir -p ${MYSQL_LOGDIR} && 50 mkdir -p ${MYSQL_CONF} && 51 mkdir -p ${MYSQL_LOGDIR}/{error,pid,bin,relayidx,relaylog,slow}/ && 52 touch ${MYSQL_LOGDIR}/error/error.log && 53 # tar -zxf ${SOFT_MYSQL} -C ${MYSQL_BASEDIR} && 54 # mv ${MYSQL_BASEDIR}/mysql-cluster-8.0.18-rc-linux-glibc2.12-x86_64/* ${MYSQL_BASEDIR}/ && 55 touch ${MYSQL_LOGDIR}/pid/pid.log && 56 touch ${MYSQL_LOGDIR}/bin/bin.log && 57 touch ${MYSQL_LOGDIR}/relaylog/relay.log && 58 touch ${MYSQL_LOGDIR}/relayidx/relay.index && 59 touch ${MYSQL_LOGDIR}/slow/slow.log && 60 chmod -R 744 ${MYSQL_BASEDIR} && 61 chmod -R 744 ${MYSQL_DATADIR} && 62 chmod -R 744 ${MYSQL_LOGDIR} && 63 chmod -R 644 ${MYSQL_CONF}/mysql.conf && 64 chmod -R 644 ${MYSQL_LOGDIR}/error/error.log && 65 chmod -R 644 ${MYSQL_LOGDIR}/pid/pid.log && 66 chown -R mysql:mysql ${MYSQL_BASEDIR} && 67 chown -R mysql:mysql ${MYSQL_DATADIR} && 68 chown -R mysql:mysql ${MYSQL_LOGDIR} && 69 chown -R mysql:mysql ${MYSQL_CONF}/mysql.conf && 70 chown -R mysql:mysql ${MYSQL_LOGDIR}/error/error.log && 71 chown -R mysql:mysql ${MYSQL_LOGDIR}/pid/pid.log && 72 chown -R mysql:mysql ${MYSQL_LOGDIR}/relaylog/relay.log && 73 chown -R mysql:mysql ${MYSQL_LOGDIR}/relayidx/relay.index && 74 chown -R mysql:mysql ${MYSQL_LOGDIR}/slow/slow.log && 75 # RUN su mysql && 76 rm -rf ${SOFT_MYSQL} && 77 rm -rf ${MYSQL_BASEDIR}/mysql-cluster-8.0.18-rc-linux-glibc2.12-x86_64 78 79 # open port 80 EXPOSE 13306 33060 81 82 # step 3: install mysql 83 # start application 84 ENTRYPOINT ["/usr/local/bin/mysql.sh"]
三、本地测试
操作环境:本地VM=CentOS 7.4 x64,并且安装有docker软件
1. 构建镜像,
# 首先把本地写好的dockerfile所有文件,复制到下面的linux目录。注意文件要按照linux的编码UTF-8 BOM。
1 root >> cd /nfs_data/dockerfile/mysql-single 2 3 root >> docker build -f ./mysql-single.Dockerfile . -t registry.cn-hangzhou.aliyuncs.com/kevin_docker_registry/docker_base_mysql-single:1.0.0 4 5 # 查看镜像 6 root >> docker images
2. 测试镜像
1 root >> docker run -it -p 3306:13306 -p 3060:33060 -v /nfs_data:/nfs_data registry.cn-hangzhou.aliyuncs.com/kevin_docker_registry/docker_base_mysql-single:1.0.0 /bin/bash 2 3 # 如下root@localhost: ;B>l:0tq0Xht 4 5 # 就是mysql数据库的初始管理员账号和密码,用户可以使用如下命令,在本机登陆mysql数据库之后修改mysql的root账号密码 6 7 # 本机登陆mysql控制台 8 root >> mysql -u root -p 9 # 然后输入默认的root密码,注意不是VM机器的密码。 10 11 # 修改账号密码(方式1:命令行 链接密码) 12 mysql >> alter user 'root'@'localhost' IDENTIFIED BY 'pass'; 13 mysql >> alter user 'app_user'@'%' IDENTIFIED BY 'pass'; 14 15 # 修改账号密码(方式2:Nactive工具 链接密码) 16 mysql >> alter user 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'pass'; 17 mysql >> alter user 'app_user'@'%' IDENTIFIED WITH mysql_native_password BY 'pass'; 18 19 # 创建新账号 20 mysql >> create user 'app_user'@'%' identified by 'pass';
四、发布镜像
1. 如果想把docker镜像发布到互联网,可以参考我之前的一篇文章,有详细介绍阿里的git代码库和docker镜像库的创建和使用。
https://www.cnblogs.com/itshare/p/11067054.html
2. 另外,如果只是每次从本地手动发布,只需要登陆链接aliyun的docker仓库地,使用如下命令,即可:
1 # 查询本地镜像 2 root >> docker images | grep mysql 3 4 # 登录远程镜像仓库 5 root >> docker login --username=kevin@1522315893340281 registry.cn-hangzhou.aliyuncs.com/kevin_docker_registry/docker_base_mysql-single 6 7 # 上传镜像,到远程镜像仓库 8 # 格式 = docker pull {镜像的仓库}:{镜像的版本} 9 root >> docker pull registry.cn-hangzhou.aliyuncs.com/kevin_docker_registry/docker_base_mysql-single:1.0.0
3. 查看阿里云的上传后的镜像,地址如下
# 镜像版本和状态
# 构建历史和结果
# 远程仓库的镜像,测试如下
# 远程仓库的镜像,详细如下
4. 客户端,登陆MYSQL,如下:
连接信息,IP为Docker容器的宿主VM的IP,端口为VM的映射端口。比如 192.168.XXX.YYY:3306
==================
Update:
最后,将上面制作的docker镜像,部署到K8S环境中,方法如下:
# 注意:PV/PVC,需要提前创建好
1 kubectl apply -f - << EOF 2 apiVersion: extensions/v1beta1 3 kind: Deployment 4 metadata: 5 name: mysql-single 6 namespace: database-system 7 spec: 8 selector: 9 matchLabels: 10 app: mysql-single 11 replicas: 1 12 template: 13 metadata: 14 labels: 15 app: mysql-single 16 spec: 17 containers: 18 - name: mysql-single 19 image: registry.cn-hangzhou.aliyuncs.com/kevin_docker_registry/docker_base_mysql-single:1.0.0 20 volumeMounts: 21 - mountPath: /opt/soft # docker inner path 22 name: soft-storage 23 - mountPath: /opt/log # docker inner path 24 subPath: database-system 25 name: log-storage 26 - mountPath: /opt/app # docker inner path 27 subPath: database-system 28 name: app-storage 29 - mountPath: /opt/data # docker inner path 30 subPath: database-system 31 name: data-storage 32 env: 33 - name: DOCKER_DATA_ROOT 34 value: "/opt/data/mysql-single" 35 - name: DOCKER_APP_ROOT 36 value: "/opt/app/mysql-single" 37 - name: DOCKER_LOG_ROOT 38 value: "/opt/log/mysql-single" 39 - name: DOCKER_CODE_ROOT 40 value: "/opt/soft" 41 volumes: 42 - name: soft-storage 43 persistentVolumeClaim: 44 claimName: pvc2soft.database-system 45 - name: log-storage 46 persistentVolumeClaim: 47 claimName: pvc2log.database-system 48 - name: app-storage 49 persistentVolumeClaim: 50 claimName: pvc2app.database-system 51 - name: data-storage 52 persistentVolumeClaim: 53 claimName: pvc2data.database-system 54 --- 55 apiVersion: v1 56 kind: Service 57 metadata: 58 name: mysql-single 59 namespace: database-system 60 spec: 61 type: LoadBalancer 62 ports: 63 - name: "mysql-console" 64 port: 13306 65 targetPort: 13306 66 protocol: TCP 67 - name: "mysql-manager" 68 port: 33060 69 targetPort: 33060 70 protocol: TCP 71 selector: 72 app: mysql-single
# 显示部署结果,如下:
1 [root@CNT7XDCK11D01 ~]# kubectl get all -n database-system -o wide 2 NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES 3 pod/mysql-single-697dfccfd-5hdw2 1/1 Running 0 77m 172.16.216.3 192.168.3.97 <none> <none> 4 5 NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR 6 service/mysql-single LoadBalancer 10.3.184.205 10.5.0.16 13306:41252/TCP,33060:47401/TCP 77m app=mysql-single 7 8 NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR 9 deployment.apps/mysql-single 1/1 1 1 77m mysql-single registry.cn-hangzhou.aliyuncs.com/kevin_docker_registry/docker_base_mysql-single:1.0.0 app=mysql-single 10 11 NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR 12 replicaset.apps/mysql-single-697dfccfd 1 1 1 77m mysql-single registry.cn-hangzhou.aliyuncs.com/kevin_docker_registry/docker_base_mysql-single:1.0.0 app=mysql-single,pod-template-hash=697dfccfd
# 最后,在客户端设置Mysql Nactive工具的连接信息,如下
成功登陆mysql,截图如下: