MySQL容器化改造,K8S编排,helm-Chart改造

  • Post author:
  • Post category:mysql


以下记录编译的mysql容器化-K8S编排-helm—chart舵手改造。

此文章全为学习、生产建议使用官方chart部署。

文章分为两大部分、

第一部分是写如何将mysql容器化操作、第二部分为那官网镜像如何改造使用。


第一部分


一、镜像准备

1、dockerfile部分

FROM registry.det.baidu.com/library/baidu/base-centos:0.1.1
WORKDIR /home/sock/mysql/
RUN groupadd mysql && useradd  -g mysql mysql 
ADD mysql-5.7.21.tar.gz /home/sock
ADD entrypoint.sh /
ENV PATH="/home/sock/mysql/bin/:${PATH}"
EXPOSE 3306
#ENTRYPOINT [ "entrypoint.sh" ]
CMD  [ "/entrypoint.sh" ]

2、entrypoint.sh部分

#!/bin/bash
sock(){
        init
        sleep 5
        pass
        start
        sleep 5
        user
}
DIRECTORY=/home/sock/mysql/data
dir=/home/sock
sock=/home/sock/mysql/tmp/mysql.sock
#NEW_PASSWORD=Baidu@1234.

########init-passwd#######
pass(){
echo $(tail -1 /home/mysql_passwd.txt |awk -F " " '{print $NF}')  > /home/mysql_passwd.txt
}

########init-sock#########
init(){
if [ "`ls -A $DIRECTORY`" = "" ]; then
echo "$DIRECTORY is indeed empty"
$dir/mysql/bin/mysqld --initialize --user=mysql --basedir=/$dir/mysql --datadir=$dir/mysql/data/ &> /home/mysql_passwd.txt
else
echo "$DIRECTORY is not empty"
fi
}
########init-user#########
user(){
mima=$(cat /home/mysql_passwd.txt)
auth=`$dir/mysql/bin/mysql  -uroot -p$NEW_PASSWORD -P 3306 -S $sock  -e "SELECT * FROM mysql.user WHERE user='root'\G"|grep Host|grep %|wc -l`
if [ $auth -eq 1 ]; then
echo "-uroot -p$NEW_PASSWORD % "
tail -f $dir/mysql/log/mysql.log
else
$dir/mysql/bin/mysql --connect-expired-password -uroot -p${mima} -e  "alter user 'root'@'localhost' identified by '$NEW_PASSWORD';" -S $sock
$dir/mysql/bin/mysql -uroot -p$NEW_PASSWORD -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '$NEW_PASSWORD' WITH GRANT OPTION;" -S  $sock
tail -f $dir/mysql/log/mysql.log
fi
}
start(){
#######sock#####
counter=$(ps -C mysqld --no-heading|wc -l)
if [  "${counter}" = "0" ]; then
$dir/mysql/bin/mysqld_safe --defaults-file=$dir/mysql/etc/my.cnf --user=mysql &

else
echo mysql-up 
fi 
}

sock
exec "$@"

3、run起mysql服务

NEW_PASSWORD=“Pwd@1234.” ##root密码

docker run  -itd  -e NEW_PASSWORD="Pwd@1234."  -p 8496:3306  --name mysql-5.7.21 registry.det.baidu.com/library/mysql:5.7.21

4、验证

mysql -uroot -pPwd@1234. -h10.61.187.40 -P 8496

二、k8s编排

1、mysql-server.yaml svc部分

apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  type: NodePort
  selector:
    app: mysql
  ports:
    - port: 3306
      targetPort: 3306
      nodePort: 8385

2、mysql-secret.yaml secret 部分

apiVersion: v1
data:
  mysql-root-passwd: 1qaz2wsx
kind: Secret
metadata:
  name: mysql-root-passwd
  namespace: default
type: Opaque

3、mysql-cm.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-cnf
data:
  my.cnf: |
    [client]
    port = 3306
    socket = /home/sock/mysql/tmp/mysql.sock
    default-character-set = utf8mb4
    
    [mysql]
    prompt="MySQL [\d]> "
    no-auto-rehash
    
    [mysqld]
    port = 3306
    socket = /home/sock/mysql/tmp/mysql.sock
    
    basedir = /home/sock/mysql/
    datadir = /home/sock/mysql/data
    pid-file = /home/sock/mysql/mysql.pid
    user = mysql
    bind-address = 0.0.0.0
    server-id=1
    general_log=On
    log_timestamps=SYSTEM
    general_log_file=/home/sock/mysql/log/mysql.log
    log_error = /home/sock/mysql/log/mysql-error.log
    slow_query_log = on
    slow_query_log_file = /home/sock/mysql/log/mysql-slow.log
    long_query_time=1
    slow_query_log = 1
    log_queries_not_using_indexes = ON
    
    log-bin=master-bin
    log-slave-updates
    innodb_flush_log_at_trx_commit=2
    master_info_repository=table
    relay_log_info_repository=TABLE
    
    
    
    binlog_cache_size=4M
    max_binlog_cache_size=4096M
    max_binlog_size=1024M
    binlog_format = mixed
    expire_logs_days = 7
    init-connect = 'SET NAMES utf8mb4'
    character-set-server = utf8mb4
    
    skip-name-resolve
    back_log = 300
    
    max_connections = 42961
    max_connect_errors = 6000
    open_files_limit = 65535
    table_open_cache = 1024
    max_allowed_packet = 500M
    binlog_cache_size = 1M
    max_heap_table_size = 8M
    tmp_table_size = 128M
    
    read_buffer_size = 2M
    read_rnd_buffer_size = 8M
    sort_buffer_size = 8M
    join_buffer_size = 8M
    key_buffer_size = 256M
    
    thread_cache_size = 64
    
    query_cache_type = 1
    query_cache_size = 64M
    query_cache_limit = 2M
    
    ft_min_word_len = 4
    
    performance_schema = 0
    explicit_defaults_for_timestamp
    
    
    skip-external-locking
    
    default_storage_engine = InnoDB
    innodb_file_per_table = 1
    innodb_open_files = 500
    innodb_buffer_pool_size = 1024M
    innodb_write_io_threads = 4
    innodb_read_io_threads = 4
    innodb_thread_concurrency = 0
    innodb_purge_threads = 1
    innodb_flush_log_at_trx_commit = 2
    innodb_log_buffer_size = 2M
    innodb_log_file_size = 32M
    innodb_log_files_in_group = 3
    innodb_max_dirty_pages_pct = 90
    innodb_lock_wait_timeout = 120
    
    bulk_insert_buffer_size = 8M
    myisam_sort_buffer_size = 64M
    myisam_max_sort_file_size = 10G
    myisam_repair_threads = 1
    
    interactive_timeout = 28800
    wait_timeout = 28800
    
    [mysqldump]
    quick
    max_allowed_packet = 500M
    
    [myisamchk]
    key_buffer_size = 256M
    sort_buffer_size = 8M
    read_buffer = 4M
    write_buffer = 4M

4、 mysql-statefulsets.yaml statefulsets部分

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  serviceName: "mysql"
  replicas: 1 
  template:
    metadata:
      labels:
        app: mysql 
    spec:
      terminationGracePeriodSeconds: 10
      containers:
      - name: mysql
        image: registry.det.baidu.com/library/mysql:5.7.21
        imagePullPolicy: IfNotPresent
        command: ['sh', '-c', '/entrypoint.sh' ]
        env:
        - name: PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-root-passwd
              key: mysql-root-passwd
        ports:
        - containerPort: 3306
          name: mysql
        readinessProbe:
          tcpSocket:
            port: 3306
          initialDelaySeconds: 5
          periodSeconds: 10
        livenessProbe:
          exec:
            command:
            - sh
            - -c
            - mysqladmin ping -u root -p${NEW_PASSWORD} -S /home/sock/mysql/tmp/mysql.sock
          initialDelaySeconds: 15
          periodSeconds: 20
        volumeMounts:
        - mountPath: /home/sock/mysql/data
          name: data
        - name: mysql-conf
          mountPath: /home/sock/mysql/etc/my.cnf
          subPath: my.cnf
      volumes:
      - name: mysql-conf
        configMap:
          name: mysql-cnf
  volumeClaimTemplates:
    - metadata:
        name: data
        labels:
         app: mysql
      spec:
        accessModes:
          - "ReadWriteOnce"
        resources:
          requests:
            storage: "300Gi"
        storageClassName: "local-path"


第二部分


一、chart方式使用官网镜像

Charts地址:https://github.com/jinjianming/chart/tree/master/stable/mysql-5.7.30

镜像:

docker pull mysql:5.7.30


部署步骤

1,修改value.yaml文件

nodePort: 8309 #MySQL对外端口

port: 8306 #MySQL内部端口



readinitialDelaySeconds: 60 #就绪探针 根据服务器性能修改默认 60s

liveinitialDelaySeconds: 60 #启动探针 根据服务器性能修改默认 60s

image: “mysql” #修改为镜像地址

tag: “5.7.30” #镜像版本



mysqlRootPassword: baidu@123 # mysql-root的密码

##设置普通用户和mysqladmin ping 使用用户设置,如果”livenessroot: false” 既使用mysqlUser进行mysqladmin ping、如果”livenessroot: true”既使用root进行mysqladmin ping。

livenessroot: true



hostPath: /data/mysql-poc # 修改此处为mysql数据存储目录,hostpath方式



##默认使用hostPath方式存储如果使用local-path将persistence下的enabled改为true、如果需要hostPath的将enabled改为false及为hostPath。

persistence:

enabled: false



##监控需要的参数

mysqlexporter:

enabled: true #监控开关默认true

mysqlUserexporter: “root” #exporter使用什么用户采集,密码默认使用mysqlRootPassword的变量

db: “” #监控那个库

2,部署

helm install mysql-poc –name mysql-poc

连接地址说明

K8S集群内连接

通过如下命令获取service

kubectl get service|grep mysql

通过k8s service和port(一般为8306)进行连接

K8S集群外连接

通过如下命令获取nodePort

kubectl get service|grep mysql

那么集群外访问地址为:

k8s master的IP地址或者申请的VIP地址:nodePort(一般为8309)



版权声明:本文为qq_43348295原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。