apiVersion: v1
kind: Secret
metadata:
  name: secret-single-mysql
  namespace: devops
  labels:
    app: pod-single-mysql
type: Opaque
data:
  # mysql-root-password需经过base64命令行下使用base64 <<< "密码",默认密码123456
  mysql-root-password: "MTIzNDU2"
---
kind: ConfigMap
apiVersion: v1
metadata:
  name: configmap-single-mysql
  namespace: devops
data:
  my.cnf: |
    [mysqld]
    user=mysql                            # MySQL启动用户
    default-storage-engine=INNODB         # 创建新表时将使用的默认存储引擎
    character-set-server=utf8mb4          # 设置mysql服务端默认字符集
    collation-server=utf8mb4_general_ci   # 数据库字符集对应一些排序等规则，注意要和character-set-server对应
    default-authentication-plugin=mysql_native_password
    max_connections=1000    # 允许最大连接数
    max_connect_errors=100  # 最大错误连接数
    [mysql]
    default-character-set=utf8mb4
    [client]
    default-character-set=utf8mb4  # 设置mysql客户端默认字符集
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: pod-single-mysql
  namespace: devops
spec:
  selector:
    matchLabels:
      app: pod-single-mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: pod-single-mysql
    spec:
      containers:
        - image: registry.cn-hangzhou.aliyuncs.com/zhengqing/mysql:8.0
          imagePullPolicy: IfNotPresent
          name: pod-single-mysql
          env:
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  key: mysql-root-password
                  name: secret-single-mysql
            - name: TZ
              value: Asia/Shanghai
            - name: LANG
              value: en_US.UTF-8
            - name: MYSQL_DATABASE
              value: demo
          ports:
            - name: svc-mysql-1m0s
              containerPort: 3306
          livenessProbe:
            tcpSocket:
              port: 3306
          readinessProbe:
            tcpSocket:
              port: 3306
          resources:
            limits:
              cpu: "1"
              memory: "2G"
            requests:
              cpu: "1"
              memory: "512Mi"
          volumeMounts:
            - name: vod-pv-single-mysql
              mountPath: /var/lib/mysql # 容器内部文件系统的挂载点，它定义了容器内部将外部存储卷（如 PersistentVolume、ConfigMap、Secret 等）挂载到哪个路径下。通过 mountPath，容器可以访问这些挂载的数据或配置
            - name: vod-pv-single-mysql-files
              mountPath: /var/lib/mysql-files
            - name: vod-cm-single-mysql
              mountPath: /etc/mysql/my.cnf # 将其挂载到容器的/etc/mysql/my.cnf路径下
              # 配合configmap使用
              subPath: my.cnf # mountPath 下的子路径，它允许容器将挂载的数据卷中的特定文件或目录挂载到容器中的指定路径下。这样可以实现更加精细的文件系统级别的访问控制
      volumes:
        - name: timezone
          hostPath:
            path: /usr/share/zoneinfo/Asia/Shanghai
        - name: vod-pv-single-mysql
          hostPath:
            path: /mnt/data/pod-single-mysql # /data目录挂载到宿主机的/mnt/data/pod-single-mysql目录
            type: DirectoryOrCreate
        - name: vod-pv-single-mysql-files
          hostPath:
            path: /mnt/data/pod-single-mysql-files
            type: DirectoryOrCreate
        - name: vod-cm-single-mysql
          configMap:
            name: configmap-single-mysql
            items:
              - key: my.cnf # configmap中的my.cnf(key)
                path: my.cnf # volumeMounts.subPath中的my.cnf
---
apiVersion: v1
kind: Service
metadata:
  name: svc-mysql-1m0s # 不能超过15个字符
  namespace: devops
  labels:
    app: pod-single-mysql
spec:
  type: NodePort
  ports:
    - port: 3306
      targetPort: 3306
      nodePort: 30006 # 范围 30000-32767
  selector:
    app: pod-single-mysql
