k8s集群部署mysql(docker自创建镜像)

江枫思渺然 提交于 2020-08-06 17:13:49

前言

公司要在k8s集群中使用mysql服务,作为对开发环境的测试数据存储.目前这个服务已经在k8s集群中稳定运行.话不多说.直接开搞

环境说明:k8s集群版本:1.17

mysql版本:mysql-5.7.20

1创建dockerfille编写

FROM centos:7
  
RUN ping -c 1 www.baidu.com
RUN yum remove -y libnuma.so.1
RUN yum install -y vim net-tools cmake numactl bison bison-devel libaio-devel gcc gcc-c++ git  ncurses-devel
RUN groupadd mysql && useradd mysql -g mysql && mkdir /home/mysql/mydata/ && mkdir /home/mysql/mysql_logs  && mkdir /home/mysql/scripts && mkdir /home/mysql/cnf
ADD ./mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz /home/mysql/
RUN chown mysql:mysql -R /home/mysql/ && cd /home/mysql/ &&  \
mv mysql-5.7.20-linux-glibc2.12-x86_64 mysql-5.7
ENV PATH /home/mysql/mysql-5.7/bin:$PATH

COPY ./scripts /home/mysql/scripts/
RUN  /bin/sh /home/mysql/scripts/create_mycnf.sh && rm -rf /home/mysql/scripts/create_mycnf.sh

RUN chown mysql:mysql -R /home/mysql/
RUN /bin/sh /home/mysql/scripts/init_password.sh

USER mysql
CMD ["sh","/home/mysql/scripts/start.sh"] 

注:如要更改镜像中的安装路径,可按需修改相关路径.

各位现在可能看这个dockerfile有点懵,没关系,咱们接着往下看

2创建相关脚本文件

创建my.cnf 

[root@master01 mysql-5.7]# cat scripts/create_mycnf.sh 
#!/bin/bash

myCnf='/home/mysql/cnf/my.cnf'

if [ -f ${myCnf} ];then
    echo 'cnf文件已存在..'
    exit
fi

cat > /home/mysql/cnf/my.cnf <<EOF
[mysqld]
basedir=/home/mysql/mysql-5.7
datadir=/home/mysql/mydata
character_set_server=utf8
init_connect='SET NAMES utf8'
port=3306
lower_case_table_names=1
max_connections=1000
sql_mode=STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

log-error=/home/mysql/mysql_logs/mysql.log
skip-name-resolve
[clinet]
default-character-set=utf8
EOF

注意:如修改dockerfiel中的mysql安装路径.my.cnf中的相关配置也要做出对应修改

初始化mysql

[root@master01 mysql-5.7]# cat scripts/init_password.sh 
#!/bin/bash
initPass(){
    #初始化mysql,并获取初始化密码 并启动mysql
    mysqld --defaults-file=/home/mysql/cnf/my.cnf --user=mysql --basedir=/home/mysql/mysql-5.7/ --datadir=/home/mysql/mydata  --initialize


    init_pass=`cat /home/mysql/mysql_logs/mysql.log |grep root@localhost: |awk '{print $11}'`
    echo 初始化默认密码: ${init_pass}
    cat >/home/mysql/init_password <<EOF
    ${init_pass}
EOF
}

num=`ls /home/mysql/mydata |wc -l`

if [ $num -eq 0 ];then
    initPass
fi

注意:如路径修改,修改做出对应调整

mysql启动脚本

[root@master01 mysql-5.7]# cat scripts/start.sh 
#!/bin/bash
my_cnf=/home/mysql/cnf/my.cnf
mysql_bin=/home/mysql/mysql-5.7/bin

User='mysql'

if [ `whoami`  !=  $User ];then
    echo 'please use $User user'
    exit
fi


ps -ef |grep mysqld |grep -v grep
if [ $? == 0 ];then
    echo  "MySQL is running..."
    exit
else
    echo -e  "\033[32mMySQL is starting. please wait...\033[0m"
    cd $mysql_bin
    $mysql_bin/mysqld --defaults-file=$my_cnf &
fi
tail -f /dev/null

生成镜像

docker build -t mysql-5.7:v1 .

----
build过程中出现问题,请自行解决.一般都是路径问题.生成镜像后,最好本地运行一下docker镜像.

运行容器测试

我这边没有开放端口,也没有外挂存储目录.只是简短的对容器进行一个测试.是否可以运行.并且我这边文章的么目标也不打算用docker运行.主要目的还是将mysql运行到k8s中.如果有读者打算通过docker运行记得将mysql的数据文件外挂出来.确保容器重启后数据不会丢失

 可以看到相关的文件已经创建,密码也初始化成功.我们可以连接mysql进行最后测试

最后上传镜像

2配置k8s环境mysql

mysql-5.7.yaml

[root@master01 mysql-5.7]# cat mysql-5.7.yaml 
apiVersion: apps/v1
kind: StatefulSet  #配置为statefulset
metadata:
  name: mysql-5-7
  #namespace: release
spec:
  serviceName: mysql-5-7
  selector:
    matchLabels:
      app: mysql-5-7
  replicas: 1
  template:
    metadata:
      labels:
        app: mysql-5-7
    spec:
      imagePullSecrets:
      - name: regsecret
      containers:
      - name: mysql-5-7
        image: 10.10.10.55:5000/public/mysql-5.7:v2   #这里替换你的私有仓库地址
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 3306
        volumeMounts:  #文件外挂.我这里用的flusterfs,你们替换成自己相关的路径
        - name: mysql-data
          mountPath: /home/mysql/mydata
        - name: mysql-logs
          mountPath: /home/mysql/mysql_logs
        - name: mysql-conf
          mountPath: /home/mysql/cnf/
        - name: host-time
          mountPath: /etc/localtime
      restartPolicy: Always
      volumes:
      - name: mysql-logs
        glusterfs:
          endpoints: glusterfs-r4
          path: /mysql-5.7
      - name: mysql-data
        glusterfs:
          endpoints: glusterfs-r4
          path: xxx/mysql-5.7/mydata
      - name: mysql-conf
        glusterfs:
          endpoints: glusterfs-r4
          path: xxx/mysql-5.7/cnf
      - name: host-time
        hostPath:
          path: /etc/localtime

mysql-5.7-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: mysql-5-7
spec:
  type: ClusterIP
  clusterIP: 10.99.49.169
  ports:
  - name: mysql-5-7
    port: 3306
    protocol: TCP
    targetPort: 3306
  selector:
    app: mysql-5-7

traefik-ingress.yaml

apiVersion: traefik.containo.us/v1alpha1
kind: IngressRouteTCP
metadata:
  name: mysql
spec:
  entryPoints:
    - mysql
  routes:
  - match: HostSNI(`*`)
    services:
    - name: mysql-5-7
      port: 3306

这里我没有nginx-ingress, 搞了一天没有找到ingress实现tcp连接,我这边果断用了traefik

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!