系统环境

  • Kubernetes版本:1.20.0

  • MySQL版本:8.0.21

部署服务

[root@k8s01 mysql]# cat mysql.yaml
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-config
  namespace: tools-env
  labels:
    app: mysql
data:
  my.cnf: |-
    [client-server]
    explicit_defaults_for_timestamp=true
    datadir = /var/lib/mysql
    
    [mysqld]
    port= 3306
    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock
    pid-file=/var/run/mysqld/mysqld.pid
    log_queries_not_using_indexes = 1
    bind-address = 0.0.0.0
    skip-name-resolve
    back_log = 600
    max_connections = 1000
    max_connect_errors = 6000
    lower_case_table_names = 1
    open_files_limit = 65535
    table_open_cache = 128
    max_allowed_packet = 4M
    binlog_cache_size = 1M
    max_heap_table_size = 8M
    tmp_table_size = 16M
    read_buffer_size = 2M
    read_rnd_buffer_size = 8M
    sort_buffer_size = 8M
    join_buffer_size = 8M
    thread_cache_size = 8
    key_buffer_size = 4M
    ft_min_word_len = 4
    transaction_isolation = REPEATABLE-READ
    log_bin = mysql-bin
    binlog_format = mixed
    performance_schema = 0
    explicit_defaults_for_timestamp
    innodb_file_per_table = 1
    innodb_open_files = 500
    innodb_buffer_pool_size = 64M
    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 = 8M
    myisam_max_sort_file_size = 10G
    myisam_repair_threads = 1
    interactive_timeout = 28800
    wait_timeout = 28800
    sql_mode=""
    [mysqldump]
    quick
    [myisamchk]
    key_buffer_size = 8M
    sort_buffer_size = 8M
    read_buffer = 4M
    write_buffer = 4M
    
    [client]
    port = 3306
    socket=/var/lib/mysql/mysql.sock
    
    [mysqld_safe]
    log-error=/logs/mysql/mysqld.log
    
    innodb_buffer_pool 
    innodb_buffer_pool_instance
    innodb_data_file_path
    transaction_isolation
    innodb_log_buffer_size
    innodb_log_file_size
    innodb_log_files_in_group
    max_connections
    expire_logs_days
    slow_query_log
    long_query_time
    binlog_format
    interactive_timeout
    wait_timeout
    innodb_flush_method
    log_queries_not_using_indexes
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-data-pvc
  namespace: tools-env
  labels:
    app: redis
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 50Gi
  storageClassName: managed-nfs-storage
---
apiVersion: v1
kind: Service
metadata:
  name: mysql
  namespace: tools-env
  labels:
    app: mysql
spec:
  type: NodePort
  ports:
  - name: mysql
    port: 3306
    targetPort: 3306
    nodePort: 30336
  selector:
    app: mysql
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
  namespace: tools-env
  labels:
    app: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:     
      containers:
      - name: mysql
        image: mysql:8.0.21
        imagePullPolicy: IfNotPresent 
        ports:
        - containerPort: 3306
        env:
        - name: MYSQL_ROOT_PASSWORD    #配置Root用户默认密码
          value: "xxxxxxxx"
        resources:
          limits:
            cpu: 2000m
            memory: 512Mi
          requests:
            cpu: 2000m
            memory: 512Mi
        livenessProbe:
          initialDelaySeconds: 30
          periodSeconds: 10
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 3
          exec:
            command: ["mysqladmin", "-uroot", "-p${MYSQL_ROOT_PASSWORD}", "ping"]
        readinessProbe:  
          initialDelaySeconds: 10
          periodSeconds: 10
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 3
          exec:
            command: ["mysqladmin", "-uroot", "-p${MYSQL_ROOT_PASSWORD}", "ping"]
        volumeMounts:
        - name: data
          mountPath: /var/lib/mysql
        - name: config
          mountPath: /etc/mysql/conf.d/my.cnf
          subPath: my.cnf
        - name: localtime
          readOnly: true
          mountPath: /etc/localtime
      volumes:
      - name: data
        persistentVolumeClaim:
          claimName: mysql-data-pvc
      - name: config      
        configMap:
          name: mysql-config
      - name: localtime
        hostPath:
          type: File
          path: /etc/localtime

授权远程登录

[root@k8s01 mysql]# kubectl exec -it -n tools-env mysql-74465f65cc-tn9fk bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
bash-4.4# mysql -ptest-pwd

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 27
Server version: 8.0.33 MySQL Community Server - GPL

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> GRANT ALL ON *.* TO 'root'@'%';
Query OK, 0 rows affected (0.00 sec)

mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'test-pwd';
Query OK, 0 rows affected (0.04 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> select host,user from mysql.user;                                 
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| %         | root             |
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+
5 rows in set (0.00 sec)

文章作者: 鲜花的主人
版权声明: 本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 爱吃可爱多
MySQL Kubernetes 数据库 Kubernetes
喜欢就支持一下吧
打赏
微信 微信
支付宝 支付宝