簡體   English   中英

如何在Kubernetes中將卷安裝到Pod

[英]How to mount a volume to a pod in Kubernetes

我正在嘗試將我的Express服務器部署從Kubernetes集群中的Deployment更改為Statefulsets。 將volumeMounts添加到yaml文件后,我從Pod的日志中得到以下錯誤:

npm ERR! path /usr/src/app/package.json
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall open
npm ERR! enoent ENOENT: no such file or directory, open '/usr/src/app/package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent 

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2019-08-23T13_55_03_058Z-debug.log

我以前的文件:

Docker文件

FROM node:10.16-alpine

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

CMD [ "npm", "start" ]

服務Yaml文件

apiVersion: v1
kind: Service
metadata:
  name: image-server-cluster-ip-service
spec:
  type: ClusterIP
  selector:
    component: image-server
  ports:
  - port: 3001
    targetPort: 3001

部署yaml文件

apiVersion: apps/v1
kind: Deployment
metadata:
  name: image-server-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: image-server
  template:
    metadata:
      labels:
        component: image-server
    spec:
      containers:
      - name: image-server
        image: gcr.io/my-project/image-server:v0.0.10
        ports:
        - containerPort: 3001
        env:
          - name: MONGO_HOST
            value: mongo-cluster-ip-service
          - name: MONGO_PORT
            value: '27017'

我當前的文件:

headless service file

apiVersion: v1
kind: Service
metadata:
  name: image-server-cluster-ip-service
spec:
  clusterIP: None
  selector:
    component: image-server
  ports:
  - port: 3001
    targetPort: 3001

statefulsets yaml文件

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: image-server-deployment
spec:
  serviceName: "image-server-cluster-ip-service"
  replicas: 2
  selector:
    matchLabels:
      component: image-server
  template:
    metadata:
      labels:
        component: image-server
    spec:
      containers:
      - name: image-server
        image: gcr.io/my-project/image-server:v0.0.10
        ports:
        - containerPort: 3001
        env:
          - name: MONGO_HOST
            value: mongo-cluster-ip-service
          - name: MONGO_PORT
            value: '27017'
        volumeMounts:
          - mountPath: /usr/src/app
            name: image-server-vol
  volumeClaimTemplates:
    - metadata:
        name: image-server-vol
      spec:
        accessModes: ["ReadWriteOnce"]
        resources:
          requests:
            storage: 1Gi

我認為在添加卷裝載時,可能會刪除package.json文件。

將卷安裝到吊艙的正確方法是什么?

在評論討論中,您的用例是處理用戶上傳的文件。

出於各種原因,您不應為此用途使用PV。 原因之一是您的兩個StatefulSet副本將裝載不同的卷,因此,如果用戶上載文件,並且該請求由一個副本處理,然后稍后他們嘗試查看其文件,而該請求由另一個副本處理,則它不會在那里。 考慮使用blobstore服務,例如S3,Google Cloud Filestore,Minio等。這將需要更多代碼來編寫,並且可能會引入一些客戶端庫,但這是一種更好的做法。

為了您的啟發,盡管您不應該在此用例中使用PV,但這是為什么它導致您看到的特定錯誤的原因:您正在將卷掛載到/usr/src/app因此無論什么時候它都會消失容器中文件系統中的位置,即在構建Docker映像時通過COPY命令放置的所有應用程序源代碼。 將來在使用PV時,請確保將卷安裝到其他路徑,以免損壞您故意放置的文件。

您的StatefulSet聲明似乎還可以。
但是,當您使用volumeClaimTemplates時 ,是否應用配置之前聲明了PersistentVolume

為了使用volumeClaimTemplate ,您需要處於官方文檔中所述的情況。 特別是這句話:

本教程假定您的集群已配置為動態設置PersistentVolumes。 如果您的集群未配置為這樣做,那么在開始本教程之前,您將必須手動配置兩個1 GiB卷

這似乎工作正常:

    volumeMounts:
      - mountPath: /usr/src/app
        name: image-server-vol

但是,您正在安裝應用程序的工作目錄/usr/src/app 而是將卷掛載到/usr/image

您正在遷移尚未創建的voulume image-server-vol

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM