本文介绍: 编写 Dockerfile构建⼀个Docker镜像(不能包含 MySQL 服务端程序),完成以下需求镜像包含⼀个 shell 脚本容器启动后每隔 30s 收集 MySQL 数据库当前连接数,将数据同时输出⾄ /data/log ⽂件(⽇志可以持久保存)及标准输出数据库IP、端⼝、⽤户及密码可以容器启动时通过e 指定环境变量修改要求容器启动后可以使⽤ docker logs container_namedocker execicontainer_name tail

说明:上周四面试时候,一公司发过来的面试题需要提前做一下,然后对这两个面试题,进行一个整理汇总

1.编写Dockerfile

1.1 编写脚本

#!/bin/bash
# Author:zfl
while true
do
    MYSQL_USER=$MYSQL_USER
    MYSQL_PASSWD=$MYSQL_PASSWD
    MYSQL_IP=$MYSQL_IP
    MYSQL_PORT=$MYSQL_PORT
    TIMESTAMP=`date +"%Y-%m-%d %H:%M:%S"`
    connect_counts=`mysql -u${MYSQL_USER} -p${MYSQL_PASSWD} -h${MYSQL_IP} -P${MYSQL_PORT} -e  'show status'|grep Threads_connected|awk '{print $2}'`
    echo "[$TIMESTAMP] Number of active DB connections is $connect_counts." >> /data/log
    echo "[$TIMESTAMP] Number of active DB connections is $connect_counts." >> /dev/stdout
    sleep 30
done

1.2 准备需要mysql安装rpm

# 因为没有service所以只需要安装客户端
root@k8s-node02(192.168.1.12)/data/mysql_connect/mysql-server-8.0.25>ll
total 52M
 mysql-community-client-8.0.25-1.el7.x86_64.rpm
 mysql-community-libs-compat-8.0.25-1.el7.x86_64.rpm
 mysql-community-libs-8.0.25-1.el7.x86_64.rpm
 mysql-community-client-plugins-8.0.25-1.el7.x86_64.rpm
 mysql-community-common-8.0.25-1.el7.x86_64.rpm
root@k8s-node02(192.168.1.12)/data/mysql_connect/mysql-server-8.0.25>

1.3 编写dockerfile

root@k8s-node02(192.168.1.12)/data/mysql_connect>cat Dockerfile 
FROM centos:7.9
LABEL NAME="ZFL"
ADD  mysql-server-8.0.25.tar.gz /root
COPY mysql_connect.sh /root
RUN  yum install /root/mysql-server-8.0.25/*.rpm -y
RUN  chmod +x /root/mysql_connect.sh
ENV TimeZone=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TimeZone /etc/localtime && echo $TimeZone > /etc/timezone
ENV MYSQL_USER "root"
ENV MYSQL_PASSWD "" 
ENV MYSQL_IP ""
ENV MYSQL_PORT "3306"
CMD ["/root/mysql_connect.sh"]

root@k8s-node02(192.168.1.12)/data/mysql_connect>

1.4构造镜像

root@k8s-node02(192.168.1.12)/data/mysql_connect>docker build -t mysql_connect:v3 ./
Sending build context to Docker daemon  54.02MB


root@k8s-node02(192.168.1.12)/data/mysql_connect>di
REPOSITORY                                                        TAG                 IMAGE ID            CREATED             SIZE
mysql_connect                                                     v3                  2c08ff6e43b0        6 seconds ago       548MB

1.5 运行mysql_connect服务

测试

​ 在192.168.1.11 安装了mysql的服务

 docker run -itd  --name mysql_conn 
 -e MYSQL_USER="root"  
 -e MYSQL_PASSWD="AAAaaa111." 
 -e MYSQL_IP="192.168.1.11" 
 -e MYSQL_PORT="3306"    
 -v /data/log:/data/log 
 mysql_connect:v3
root@k8s-node02(192.168.1.12)/data/mysql_connect>docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
13202a213321        mysql_connect:v3    "/root/mysql_connect…"   2 seconds ago       Up 1 second                             mysql_conn
root@k8s-node02(192.168.1.12)/data/mysql_connect>docker logs -f mysql_conn
mysql: [Warning] Using a password on the command line interface can be insecure.
[2023-11-28 15:44:26] Number of active DB connections is 1.

root@k8s-node02(192.168.1.12)/data/mysql_connect> docker exec -i -t mysql_conn tail -f /data/log 
[2023-11-28 15:44:26] Number of active DB connections is 1.
[2023-11-28 15:44:56] Number of active DB connections is 1.
[2023-11-28 15:45:26] Number of active DB connections is 1.
[2023-11-28 15:45:56] Number of active DB connections is 1.

###持久存储
root@k8s-node02(192.168.1.12)/data>tail -f 10 log
[2023-11-28 15:44:26] Number of active DB connections is 1.
[2023-11-28 15:44:56] Number of active DB connections is 1.
[2023-11-28 15:45:26] Number of active DB connections is 1.
[2023-11-28 15:45:56] Number of active DB connections is 1.
[2023-11-28 15:46:26] Number of active DB connections is 1.
[2023-11-28 15:46:56] Number of active DB connections is 1.
[2023-11-28 15:47:26] Number of active DB connections is 1.
[2023-11-28 15:47:56] Number of active DB connections is 1.
[2023-11-28 15:48:26] Number of active DB connections is 1.

2.编写Docker-compose

编写 dockercompose.yml 脚本,使⽤题⽬1中构建镜像及 MySQL 镜像启动服务

2.1 编写docker-compose

version: "3"
services:
  mysql:
    container_name: mysql
    image: mysql:latest
    restart: always 
    ports:
      - 3306:3306          
    environment:
      MYSQL_ROOT_PASSWORD: AAAaaa111.
    volumes:
      - /root/docker-compose/mysql/conf/:/etc/mysql/
      - /root/docker-compose/mysql/mysql_data/:/var/lib/mysql/
    networks:
      - test_net
###############
  mysql_conn:
    container_name: mysql_conn1
    image: mysql_connect:v3
    restart: always
    environment:
      MYSQL_USER: "root"
      MYSQL_PASSWD: "AAAaaa111."
      MYSQL_IP: "192.168.1.11"
      MYSQL_PORT: "3306"
    volumes:
      - /data/log:/data/log
    networks:
      - test_net
#########################################
networks:
  test_net:
    name: test_net
    driver: bridge
    ipam:
      config:
      - subnet: "172.200.0.0/16"

# 执行docker-copmose
root@k8s-node02(192.168.1.12)~/mysql>up -d
[+] Running 2/2
 ✔ Container mysql_conn1  Started                                                                                                                                       0.8s 
 ✔ Container mysql        Started                                                                                                                                       0.7s 
root@k8s-node02(192.168.1.12)~/mysql>dps
NAME                IMAGE               COMMAND                  SERVICE             CREATED             STATUS              PORTS
mysql               mysql:5.6.38        "docker-entrypoint.s…"   mysql               7 seconds ago       Up 6 seconds        0.0.0.0:3306->3306/tcp
mysql_conn1         mysql_connect:v3    "/root/mysql_connect…"   mysql_conn          7 seconds ago       Up 6 seconds        
root@k8s-node02(192.168.1.12)~/mysql>

2.2 测试

root@k8s-node02(192.168.1.12)/data>cat log 
[2023-11-28 16:17:31] Number of active DB connections is 1.
[2023-11-28 16:18:01] Number of active DB connections is 1.
[2023-11-28 16:18:31] Number of active DB connections is 1.
root@k8s-node02(192.168.1.12)/data>

root@k8s-node02(192.168.1.12)/data>docker logs -f   mysql_conn1
mysql: [Warning] Using a password on the command line interface can be insecure.
[2023-11-28 16:17:31] Number of active DB connections is 1.
root@k8s-node02(192.168.1.12)/data> docker exec -i -t mysql_conn1 tail -f /data/log 
[2023-11-28 16:17:31] Number of active DB connections is 1.
[2023-11-28 16:18:01] Number of active DB connections is 1.
[2023-11-28 16:18:31] Number of active DB connections is 1.
[2023-11-28 16:19:01] Number of active DB connections is 1.
[2023-11-28 16:19:31] Number of active DB connections is 1.
[2023-11-28 16:20:01] Number of active DB connections is 1.
[2023-11-28 16:20:31] Number of active DB connections is 1.
[2023-11-28 16:21:02] Number of active DB connections is 1.

原文地址:https://blog.csdn.net/xiaolong1155/article/details/134794669

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_39990.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注