在这里插入图片描述
注,**未作持久化存储,仅限于理解,学习,不得使用于生产环境。。。**
如果做了持久化,使用NFS对数据共享磁盘目录共享,就不存在的问题。
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/qiluo-images/mysql:5.7.44

mysql-pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data/mysql"  # 本地路径,生产环境中应使用更可靠的存储解决方案

mysql-pvc.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi

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: "jcgs@666666"
        volumeMounts:
        - name: mysql-config-volume
          mountPath: /etc/mysql/my.cnf
          subPath: my.cnf
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-config-volume
        configMap:
          name: mysql-config
          items:
          - key: my.cnf
            path: my.cnf
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pvc

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 <mysql-pod-name> -- 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 环境中稳定运行。

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐