0

我开始使用 Kubernetes 来理解 Pod、对象等概念。我开始了解 Persistent Volume 和 Persistent Volume Claim ,据我了解,如果我将数据从 mysql pod 保存到持久卷,则无论我删除 mysql pod,数据都会保存,数据会保存在卷上,但是我不认为它适用于我的情况......

我有一个spring boot pod,我将数据保存在mysql pod中,数据已保存,我可以检索,但是当我重新启动我的pod,删除或替换它们时,保存的数据会丢失,所以我想我搞砸了,你能请给我一个提示好吗?谢谢...

下面是我的 Kubernetes 文件:

  • mysql 吊舱:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
  labels:
    app: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels: #must match Service and DeploymentLabels
        app: mysql
    spec:
      containers:
        - image: mysql:5.7
          args:
            - "--ignore-db-dir=lost+found"
          name: mysql #name of the db
          env:
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: db-secret #name of the secret obj
                  key: password #which value from inside the secret to take
            - name: MYSQL_ROOT_USER
              valueFrom:
                secretKeyRef:
                  name: db-secret
                  key: username
            - name: MYSQL_DATABASE
              valueFrom:
                configMapKeyRef:
                  name: db-config
                  key: name
          ports:
            - containerPort: 3306
              name: mysql
        volumeMounts: #mount volume obtained from PVC
          - name: mysql-persistent-storage
            mountPath: /var/lib/mysql #mounting in the container will be here


  volumes:
    - name: mysql-persistent-storage #obtaining volume from PVC
      persistentVolumeClaim:
        claimName: mysql-pv-claim # can use the same claim in different pods

apiVersion: v1
kind: Service
metadata:
  name: mysql #DNS name
  labels:
    app: mysql
spec:
  ports:
    - port: 3306
      targetPort: 3306
  selector: #mysql pod should contain same label
    app: mysql
  clusterIP: None # we use DNS

持久卷和持久卷声明文件:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim #name of our pvc
  labels:
    app: mysql
spec:
  volumeName: host-pv #claim that volume created with this name
  accessModes:
    - ReadWriteOnce
  storageClassName: standard
  resources:
    requests:
      storage: 1Gi

apiVersion: v1 #version of our PV
kind: PersistentVolume #kind of obj we gonna create
metadata:
  name: host-pv # name of our PV
spec: #spec of our PV
  capacity: #size
    storage: 4Gi
  volumeMode: Filesystem #storage Type, File and Blcok
  storageClassName: standard
  accessModes:
    - ReadWriteOnce  # can be mount from multiple pods on a single nod, cam be use by multiple pods, multiple pods can use this pv but only from a single node
   # - ReadOnlyMany    #                                on multiple nodes
   # - WriteOnlyMany   #  doar pt multiple nods, nu hostPath type
  hostPath: #which type of pv
    path: "/mnt/data"
    type: DirectoryOrCreate
  persistentVolumeReclaimPolicy: Retain

我的 Spring 书 K8 文件:

apiVersion: v1
kind: Service
metadata:
  name: book-service
spec:
  selector:
    app: book-example
  ports:
    - protocol: 'TCP'
      port: 8080
      targetPort: 8080
  type: LoadBalancer

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: book-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: book-example
  template:
    metadata:
      labels:
        app: book-example
    spec:
      containers:
        - name: book-container
          image: cinevacineva/kubernetes_book_pv:latest
          imagePullPolicy: Always
      #    ports:
      #      - containerPort: 8080
          env:
            - name: DB_HOST
              valueFrom:
                configMapKeyRef:
                  name: db-config
                  key: host
            - name: DB_NAME
              valueFrom:
                configMapKeyRef:
                  name: db-config
                  key: name
            - name: DB_USERNAME
              valueFrom:
                secretKeyRef:
                  name: db-user
                  key: username
            - name: DB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: db-user
                  key: password

#  & minikube -p minikube docker-env | Invoke-Expression    links docker images we create with minikube, nu mai trebe sa ppusham
4

1 回答 1

2

...if i save data from mysql pod to a persistent volume, the data is saved no matter if i delete the mysql pod, the data is saved on the volume, but i don't think it works in my case...

当 Pod 切换节点时,您之前的数据将不可用。要使用hostPath你并不真的需要 PVC/PV。尝试:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
  ...
spec:
  ...
  template:
    ...
    spec:
      ...
      nodeSelector:    # <-- make sure your pod runs on the same node
        <node label>: <value unique to the mysql node>
      volumes:         # <-- mount the data path on the node, no pvc/pv required.
      - name: mysql-persistent-storage
        hostPath:
          path: /mnt/data
          type: DirectoryOrCreate
      containers:
      - name: mysql
        ...
        volumeMounts:  # <-- let mysql write to it
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
  
于 2022-02-11T01:27:55.197 回答