1

I'm having an issue with volumes on Kubernetes when I'm trying to mount hostPath volumes. (i also tried with PVC, but no success)

Dockerfile:

FROM node:16
WORKDIR /usr/src/app
COPY package.json /usr/src/app
RUN yarn install
COPY . /usr/src/app
EXPOSE 3000
ENTRYPOINT ["yarn", "start:dev"]

docker-compose.yml:

version: '3.8'

services:
  api:
    container_name: api
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - .:/usr/src/app
      - /usr/src/app/node_modules
    ports:
      - 3000:3000
    restart: always
    labels:
      kompose.volume.type: 'hostPath'
  database:
    container_name: database
    image: postgres:latest
    ports:
      - 5432:5432
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: task-management

api-development.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: kompose -f docker-compose.yml convert
    kompose.version: 1.26.1 (HEAD)
    kompose.volume.type: hostPath
  creationTimestamp: null
  labels:
    io.kompose.service: api
  name: api
spec:
  replicas: 1
  selector:
    matchLabels:
      io.kompose.service: api
  strategy:
    type: Recreate
  template:
    metadata:
      annotations:
        kompose.cmd: kompose -f docker-compose.yml convert
        kompose.version: 1.26.1 (HEAD)
        kompose.volume.type: hostPath
      creationTimestamp: null
      labels:
        io.kompose.service: api
    spec:
      containers:
        - image: task-management_api
          name: api
          imagePullPolicy: Never
          ports:
            - containerPort: 3000
          resources: {}
          volumeMounts:
            - mountPath: /usr/src/app
              name: api-hostpath0
            - mountPath: /usr/src/app/node_modules
              name: api-hostpath1
      restartPolicy: Always
      volumes:
        - hostPath:
            path: /Users/handrei/workspace/devs/nest-ws/task-management
          name: api-hostpath0
        - hostPath:
          name: api-hostpath1
status: {}

the error I received from the pod is the next one:

kubectl logs api-84b56776c5-v86c7

yarn run v1.22.17
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Couldn't find a package.json file in "/usr/src/app"

I assume that's something wrong with volumes because applying the deployment and service without volumes it's working

andrei
  • 19
  • 5
  • A `hostPath` volume mount will mount a directory from whichever random node the container happens to be running on; it's not a reliable way of getting data or source code. I'd avoid trying to use a clustered container system like Kubernetes as a live development environment; instead, use Node locally for day-to-day development and use a self-contained image (without any volume mounts at all) in Kubernetes. – David Maze Mar 06 '22 at 20:45
  • Ok, I used that method with docker-compose, and I wanted to try it on Kubernetes too, but if you say this is not the way of doing it, I'll believe you :) thanks a lot – andrei Mar 07 '22 at 06:04
  • The other thing you'll find, if you're trying to translate a Compose setup into Kubernetes, is the `node_modules` directory is empty and nothing in Kubernetes will every copy data there. You'll need to delete all of the volume declarations from your Deployment spec for this to run. – David Maze Mar 07 '22 at 10:14
  • thanks a lot @DavidMaze. i managed to make it run – andrei Mar 07 '22 at 16:23

1 Answers1

2

A hostPath volume mounts a file or directory from the host node's filesystem into your Pod.

To the required path property, you can also specify a type for a hostPath volume.

NOTE: HostPath volumes present many security risks, and it is a best practice to avoid the use of HostPaths when possible. When a HostPath volume must be used, it should be scoped to only the required file or directory, and mounted as ReadOnly.


As @David Maze mentioned before, It's better idea to

use Node locally for day-to-day development and use a self-contained image (without any volume mounts at all) in Kubernetes. (...)

The node_modules directory is empty and nothing in Kubernetes will every copy data there. You'll need to delete all of the volume declarations from your Deployment spec for this to run.


This quide will help you to translate a Docker Compose File to Kubernetes Resources.

See also this questions on StackOverflow:

kkopczak
  • 742
  • 2
  • 8