Kubernetes 1.20.15 部署 MySQL 5.6 并创建 face-server

本教程介绍如何在 Kubernetes 1.20.15 上部署 MySQL 5.6,设置 Root 密码,创建 face-server,并优化 MySQL 性能,包括设置 UTF8 字符集、并发连接数、缓存等配置。

1. 创建 MySQL Secret

apiVersion: v1
kind: Secret
metadata:
  name: mysql-password
type: Opaque
data:
  password: 'UEJzc05LVkg0QTVJaHpLcg=='

2. 部署 MySQL

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  replicas: 1
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - name: mysql
          image: mysql:5.6
          env:
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql-password
                  key: password
          ports:
            - containerPort: 3306
          volumeMounts:
            - name: mysql-persistent-storage
              mountPath: /var/lib/mysql
      volumes:
        - name: mysql-persistent-storage
          persistentVolumeClaim:
            claimName: mysql-pv-claim

3. 创建 MySQL Service

---
apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  selector:
    app: mysql
  ports:
    - name: mysql
      protocol: TCP
      port: 3306
      targetPort: 3306

4. 创建 MySQL PersistentVolumeClaim

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
spec:
  storageClassName: standard
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

5. 创建 MySQL 配置 ConfigMap

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-conf
data:
  my.cnf: |
    [mysqld]
    max_connections = 2000
    character-set-server = utf8mb4
    collation-server = utf8mb4_unicode_ci
    innodb_buffer_pool_size=256M
    innodb_log_buffer_size=8M
    innodb_flush_log_at_trx_commit=2
    innodb_thread_concurrency=0
    innodb_read_io_threads=64
    innodb_write_io_threads=64
    innodb_io_capacity=5000

6. 部署 face-server

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: face-server
spec:
  replicas: 1
  selector:
    matchLabels:
      app: face-server
  template:
    metadata:
      labels:
        app: face-server
    spec:
      containers:
        - name: face-server
          image: face-server:latest
          env:
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql-password
                  key: password
          ports:
            - containerPort: 8080
          volumeMounts:
            - name: mysql-conf-volume
              mountPath: /etc/mysql/conf.d
      volumes:
        - name: mysql-conf-volume
          configMap:
            name: mysql-conf
            items:
              - key: my.cnf
                path: my.cnf

7. 创建 face-server Service

---
apiVersion: v1
kind: Service
metadata:
  name: face-server
spec:
  selector:
    app: face-server
  ports:
    - name: http
      protocol: TCP
      port: 8080
      targetPort: 8080

说明

  • mysql-password Secret 用于存储 MySQL Root 密码。
  • mysql Deployment 部署 MySQL 实例,并使用 Secret 中的密码连接数据库。
  • mysql Service 提供访问 MySQL 的服务。
  • mysql-pv-claim PersistentVolumeClaim 为 MySQL 提供持久化存储。
  • mysql-conf ConfigMap 配置 MySQL 参数,包括最大连接数、字符集、缓存等。
  • face-server Deployment 部署 face-server 应用,并使用 Secret 中的密码连接 MySQL 数据库。
  • face-server Service 提供访问 face-server 的服务。

优化 MySQL 性能

  • max_connections:设置最大连接数为 2000。
  • character-set-servercollation-server:设置字符集为 utf8mb4,支持更多字符。
  • innodb_buffer_pool_size:设置 InnoDB 缓冲池大小为 256M。
  • innodb_log_buffer_size:设置 InnoDB 日志缓冲区大小为 8M。
  • innodb_flush_log_at_trx_commit:设置事务提交时刷新日志频率为 2,提高性能。
  • innodb_thread_concurrency:设置 InnoDB 线程并发度为 0,由 MySQL 自动调整。
  • innodb_read_io_threadsinnodb_write_io_threads:设置 InnoDB 读取和写入线程数量为 64。
  • innodb_io_capacity:设置 InnoDB I/O 能力为 5000。

注意

  • 请根据实际情况调整参数配置。
  • 请确保 face-server 应用已配置好连接 MySQL 数据库的地址和端口。
  • 请确保已创建 PersistentVolume 并绑定到 PersistentVolumeClaim。
  • 请确保已创建 Secret 并设置好 MySQL Root 密码。
  • 请确保已创建 ConfigMap 并设置好 MySQL 配置参数。
  • 请确保已创建 Service 并提供访问 MySQL 和 face-server 应用的端口。
Kubernetes 1.20.15 上部署 MySQL 5.6,设置 Root 密码,创建 face-server,优化性能

原文地址: https://www.cveoy.top/t/topic/nlHp 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录