Drupal Kubernetes持久存储导致文件在容器中消失

Drupal Kubernetes持久存储导致文件在容器中消失,drupal,kubernetes,drupal-8,Drupal,Kubernetes,Drupal 8,正在尝试使用在Kubernetes上创建Drupal容器 当在/var/www/html上装载一个持久卷,并使用docker exec-it bash检查Drupal容器时,没有可见的文件。因此,无法提供任何文件 工作流程 1-创建谷歌计算磁盘 gcloud compute disks create --size=20GB --zone=us-central1-c drupal-1 2-将新创建的google计算磁盘注册到kubernetes群集实例 kubectl create -f gce

正在尝试使用在Kubernetes上创建Drupal容器

当在
/var/www/html
上装载一个持久卷,并使用
docker exec-it bash
检查Drupal容器时,没有可见的文件。因此,无法提供任何文件

工作流程

1-创建谷歌计算磁盘

gcloud compute disks create --size=20GB --zone=us-central1-c drupal-1
2-将新创建的google计算磁盘注册到kubernetes群集实例

kubectl create -f gce-volumes.yaml
3-创建Drupal吊舱

kubectl create -f drupal-deployment.yaml
定义文件的灵感来自wordpress,my
drupal deployment.yaml

apiVersion: v1
kind: Service
metadata:
  name: drupal
  labels:
    app: drupal
spec:
  ports:
    - port: 80
  selector:
    app: drupal
    tier: frontend
  type: LoadBalancer
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: dp-pv-claim
  labels:
    app: drupal
spec:
  accessModes:
    - ReadWriteOnce
  resources:
      requests:
          storage: 20Gi
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: drupal
  labels:
    app: drupal
spec:
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: drupal
        tier: frontend
    spec:
      containers:
      - name: drupal
        image: drupal:8.2.3-apache
        ports:
        - name: drupal
          containerPort: 80
        volumeMounts:
        - name: drupal-persistent-storage
          mountPath: /var/www/html
      volumes:
      - name: drupal-persistent-storage
        persistentVolumeClaim:
          claimName: dp-pv-claim
apiVersion: v1
kind: PersistentVolume
metadata:
  name: drupal-pv-1
spec:
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteOnce
  gcePersistentDisk:
    pdName: drupal-1
    fsType: ext4
gce卷。yaml

apiVersion: v1
kind: Service
metadata:
  name: drupal
  labels:
    app: drupal
spec:
  ports:
    - port: 80
  selector:
    app: drupal
    tier: frontend
  type: LoadBalancer
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: dp-pv-claim
  labels:
    app: drupal
spec:
  accessModes:
    - ReadWriteOnce
  resources:
      requests:
          storage: 20Gi
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: drupal
  labels:
    app: drupal
spec:
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: drupal
        tier: frontend
    spec:
      containers:
      - name: drupal
        image: drupal:8.2.3-apache
        ports:
        - name: drupal
          containerPort: 80
        volumeMounts:
        - name: drupal-persistent-storage
          mountPath: /var/www/html
      volumes:
      - name: drupal-persistent-storage
        persistentVolumeClaim:
          claimName: dp-pv-claim
apiVersion: v1
kind: PersistentVolume
metadata:
  name: drupal-pv-1
spec:
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteOnce
  gcePersistentDisk:
    pdName: drupal-1
    fsType: ext4

是什么导致这些文件消失的?如何成功地将Drupal安装文件保存在容器中的
/var/www/html

您将在
/var/www/html
上装载
persistentStorage
,因此,如果您的基本映像中该位置有文件,则该文件夹将替换为装载,并且基本映像中的文件将不再可用

如果您的内容将是动态的,并且您希望将这些文件保存在
persistentStorage
中,这是一种方法,但是您首先需要填充
persistentStorage

一种解决方案是将文件放在基本映像中的不同文件夹中,并在运行Pod时将其复制过来,但是每次启动Pod时都会发生这种情况,并且可能会覆盖更改,除非复制脚本首先检查文件夹是否为空

另一种选择是让作业只执行一次(在运行Drupal Pod并装载此存储之前)

注:

GKEPersistentStorage只允许
ReadWriteOnce
(只能安装在一个Pod上)或
ReadOnlyMany
(即只能读取但可以安装在多个Pod上),并且不能同时以不同的模式安装,因此最终只能运行其中一个Pod。(即,它不会缩放)