将文件 (.sql) 从 minikube/host 挂载到部署 (MySQL)

mount file (.sql) from minikube/host to deployment (MySQL)

我正在尝试使用 Hyper-V 从主机 运行 minikube 集群挂载文件并传入带有部署 yaml 的 MySQL 容器,我尝试将文件添加到 minikube 虚拟机(使用 ssh),然后使用 PV 和 Claim 将其安装到部署中,我尝试从本地主机安装 运行 minikube(我的电脑),但我仍然没有看到该文件。

当前配置是:我在 Hyper-V VM 上有 运行 minikube 文件夹,名为 data ,在这个文件夹中,我有要传输到容器 (pod) 的文件。

PV Yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: sqlvolume
spec:
  accessModes:
    - ReadWriteOnce
  capacity:
    storage: 1Gi
  hostPath:
    path: /data

claim.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  creationTimestamp: null
  labels:
    io.kompose.service: sqlvolume
  name: sqlvolume
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 100Mi
status: {}

deployment.yaml (MySQL)

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: C:\Users\itayb\Desktop\K8S-Statefulset-NodeJs-App-With-MySql\kompose.exe convert
    kompose.version: 1.24.0 (7c629530)
  labels:
    io.kompose.service: mysql
  name: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      io.kompose.service: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      annotations:
        kompose.cmd: C:\Users\itayb\Desktop\K8S-Statefulset-NodeJs-App-With-MySql\kompose.exe convert
        kompose.version: 1.24.0 (7c629530)
      creationTimestamp: null
      labels:
        io.kompose.service: mysql
    spec:
      containers:
        - env:
            - name: MYSQL_DATABASE
              value: crud
            - name: MYSQL_ROOT_PASSWORD
              value: root
          image: mysql
          name: mysql
          ports:
            - containerPort: 3306
          volumeMounts:
            - mountPath: /data
              name: sqlvolume
          # resources:
          #     requests:
          #       memory: "64Mi"
          #       cpu: "250m"
          #     limits:
          #       memory: "128Mi"
          #       cpu: "500m"
      hostname: mysql
      restartPolicy: Always
      volumes:
        - name: sqlvolume
          persistentVolumeClaim:
            claimName: sqlvolume
status: {}

我不介意如何实现,我的计算机上有 Hyper-V Minikube 运行,我想从主机传输文件 mysql.sql(或者从我创建的 PV ) 到 pod.

我怎样才能做到这一点?

您可以尝试使用 hostPath 类型的 PersistentVolume

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-volume
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/data/<file_name>"

PersistentVolumeClaim

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pv-claim
spec:
  volumeName: "pv-volume"
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

部署(更改pvc名称)

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: C:\Users\itayb\Desktop\K8S-Statefulset-NodeJs-App-With-MySql\kompose.exe convert
    kompose.version: 1.24.0 (7c629530)
  labels:
    io.kompose.service: mysql
  name: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      io.kompose.service: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      annotations:
        kompose.cmd: C:\Users\itayb\Desktop\K8S-Statefulset-NodeJs-App-With-MySql\kompose.exe convert
        kompose.version: 1.24.0 (7c629530)
      creationTimestamp: null
      labels:
        io.kompose.service: mysql
    spec:
      containers:
        - env:
            - name: MYSQL_DATABASE
              value: crud
            - name: MYSQL_ROOT_PASSWORD
              value: root
          image: mysql
          name: mysql
          ports:
            - containerPort: 3306
          volumeMounts:
            - mountPath: /data
              name: sqlvolume
         resources:
             requests:
               memory: "64Mi"
               cpu: "250m"
             limits:
               memory: "128Mi"
               cpu: "500m"
      hostname: mysql
      restartPolicy: Always
      volumes:
        - name: sqlvolume
          persistentVolumeClaim:
            claimName: pv-claim
status: {}