1.直接部署一个pod

需要挂载存储款, 可参考 之前文章设置 https://blog.csdn.net/weimeibuqieryu/article/details/140183843

2.部署yaml

先创建命名空间ruoyi

kubectl create namespace ruoyi

创建部署文件 mysql-deploy.yaml

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv
  namespace: ruoyi #使用ns ruoyi
spec:
  capacity:
    storage: 5Gi  #存储容量为 5 GiB
  accessModes:
    - ReadWriteOnce # 一个节点可以读写
  persistentVolumeReclaimPolicy: Retain  # 删除 PVC 后 PV 保留
  storageClassName: standard
  nfs:
    path: /nfs/mysql   #使用 NFS 服务器的路径和地址, 使用自己定义的
    server: 139.159.140.xxx
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pvc
  namespace: ruoyi
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  storageClassName: standard
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-config
  namespace: ruoyi
data:
  my.cnf: |
    [client]
    default-character-set=utf8mb4
    [mysql]
    default-character-set=utf8mb4
    [mysqld]
    max_connections = 2000
    secure_file_priv=/var/lib/mysql
    sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
---
apiVersion: v1
kind: Secret
metadata:
  name: mysql-secrets
  namespace: ruoyi
type: Opaque
stringData:
  root-password: password  #明文密码, 记得改为自己的
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
  namespace: ruoyi
spec:
  replicas: 1   #部署一个副本
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      nodeSelector:
        node-role.kubernetes.io/master: ""  # 指定 Pod 运行在 master 节点
      containers:
        - name: mysql
          image: mysql:8.2.0
          ports:
            - containerPort: 3306
          env:
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql-secrets
                  key: root-password
          args:
            - --character-set-server=utf8mb4
            - --collation-server=utf8mb4_unicode_ci
          volumeMounts:     # 挂载 PVC 和 ConfigMap 
            - name: mysql-persistent-storage
              mountPath: /var/lib/mysql
            - name: mysql-config
              mountPath: /etc/mysql/my.cnf
              subPath: my.cnf
      volumes:
        - name: mysql-persistent-storage
          persistentVolumeClaim:
            claimName: mysql-pvc
        - name: mysql-config
          configMap:
            name: mysql-config
---
apiVersion: v1
kind: Service
metadata:
  name: mysql
  namespace: ruoyi
spec:
  selector:
    app: mysql
  ports:
    - port: 3306
      targetPort: 3306
      nodePort: 30306 # 节点端口 30306, 可外部访问
  type: NodePort  # 暴露服务为 NodePort 类型

部署该服务

kubectl apply -f mysql-deploy.yaml

就可以使用节点的ip+30306 来访问了. 公网记得防火墙的端口要打开

Logo

快速构建 Web 应用程序

更多推荐