k8s 部署mysql 5.7的完整配置

k8s 部署mysql 5.7的完整配置

码农世界 2024-06-18 后端 82 次浏览 0个评论

k8s 部署mysql 5.7的完整配置

mysql-configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-config
  namespace: default
data:
  my.cnf: |
    [client]
    port = 3306
    socket = /var/run/mysqld/mysqld.sock
    [mysqld_safe]
    pid-file = /var/run/mysqld/mysqld.pid
    socket = /var/run/mysqld/mysqld.sock
    nice = 0
    [mysqld]
    skip-host-cache
    skip-name-resolve
    user = mysql
    pid-file = /var/run/mysqld/mysqld.pid
    socket = /var/run/mysqld/mysqld.sock
    port = 3306
    basedir = /usr
    datadir = /var/lib/mysql
    tmpdir = /tmp
    lc-messages-dir = /usr/share/mysql
    explicit_defaults_for_timestamp = true
    symbolic-links = 0
    !includedir /etc/mysql/conf.d/
    # Custom settings
    lower_case_table_names = 1
    wait_timeout = 2147483
    interactive_timeout = 2147483
    max_connections = 20000
    max_connect_errors = 6000
    open_files_limit = 65535
    table_open_cache = 512
    max_allowed_packet = 500M
    binlog_cache_size = 1M
    max_heap_table_size = 8M
    tmp_table_size = 64M
    read_buffer_size = 2M
    read_rnd_buffer_size = 8M
    sort_buffer_size = 8M
    join_buffer_size = 8M
    key_buffer_size = 512M
    thread_cache_size = 32
    query_cache_size = 128M
    default-storage-engine = INNODB
    innodb_file_per_table = 1
    innodb_open_files = 500
    innodb_buffer_pool_size = 4G
    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 = 1G
    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 = 32M
    myisam_max_sort_file_size = 10G
    sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

mysql-deployment.yaml

docker pull registry.cn-hangzhou.aliyuncs.com/acs-sample/mysql:5.7
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-deployment
  namespace: default
  labels:
    app: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: registry.cn-hangzhou.aliyuncs.com/acs-sample/mysql:5.7   #由于国内镜像问题,需要自己的外部镜像地址
        ports:
        - containerPort: 3306
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "123456"     #设置自己打用户密码
        volumeMounts:
        - name: mysql-config-volume
          mountPath: /etc/mysql/my.cnf
          subPath: my.cnf
      volumes:
      - name: mysql-config-volume
        configMap:
          name: mysql-config
          items:
          - key: my.cnf
            path: my.cnf

mysql-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: mysql-service
spec:
  selector:
    app: mysql
  ports:
    - port: 3306
      targetPort: 3306
      nodePort: 30306  # 这里指定暴露的NodePort
  type: NodePort
kubectl apply -f mysql-configmap.yaml
kubectl apply -f mysql-deployment.yaml
kubectl apply -f mysql-service.yaml

验证部署

kubectl get pods -l app=mysql
kubectl exec -it  -- cat /etc/mysql/conf.d/my.cnf

将 替换为实际的 MySQL Pod 名称。

上述 my.cnf 配置文件中包括了许多针对性能优化和其他功能的配置项:

lower_case_table_names=1:使表名不区分大小写。

wait_timeout 和 interactive_timeout:设置连接超时时间。

max_connections:最大连接数。

innodb_buffer_pool_size:InnoDB 缓冲池大小。

innodb_log_file_size:InnoDB 日志文件大小。

其他各种优化参数。

通过这些配置,可以优化 MySQL 的性能并确保其在 Kubernetes 环境中稳定运行。

转载请注明来自码农世界,本文标题:《k8s 部署mysql 5.7的完整配置》

百度分享代码,如果开启HTTPS请参考李洋个人博客
每一天,每一秒,你所做的决定都会改变你的人生!

发表评论

快捷回复:

评论列表 (暂无评论,82人围观)参与讨论

还没有评论,来说两句吧...

Top