hostPath作为kubernetes中的卷

Ani*_*r P 7 kubernetes

我正在尝试将hostPath配置为kubernetes中的卷。我已经登录到VM服务器,通常在该服务器上使用kubernetes命令,例如kubectl。

以下是Pod Yaml:

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: helloworldanilhostpath
spec:
  replicas: 1
  template:
    metadata:
      labels:
        run: helloworldanilhostpath
    spec:
      volumes:
        - name: task-pv-storage
          hostPath:
            path: /home/openapianil/samplePV
            type: Directory
      containers:
      - name: helloworldv1
        image: ***/helloworldv1:v1
        ports:
        - containerPort: 9123
        volumeMounts:
         - name: task-pv-storage
           mountPath: /mnt/sample
Run Code Online (Sandbox Code Playgroud)

在VM服务器中,我创建了“ / home / openapianil / samplePV”文件夹,并且其中有一个文件。它有一个sample.txt文件。

一旦我尝试创建此部署。它不会发生并显示错误-
警告FailedMount 28s(x7超过59s)kubelet,aks-nodepool1-39499429-1 MountVolume.SetUp的卷“ task-pv-storage”失败:hostPath类型检查失败:/ home / openapianil / samplePV为不是目录。

谁能帮助我在这里理解问题。

Jan*_*art 5

hostPath类型卷是指计划运行Pod的节点(VM /计算机)上的目录(aks-nodepool1-39499429-1在这种情况下)。因此,您至少需要在该节点上创建此目录。

为了确保您的Pod始终在该特定节点上调度,您需要spec.nodeSelector在PodTemplate中进行设置:

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: helloworldanilhostpath
spec:
  replicas: 1
  template:
    metadata:
      labels:
        run: helloworldanilhostpath
    spec:
      nodeSelector:
        kubernetes.io/hostname: aks-nodepool1-39499429-1
      volumes:
        - name: task-pv-storage
          hostPath:
            path: /home/openapianil/samplePV
            type: Directory
      containers:
      - name: helloworldv1
        image: ***/helloworldv1:v1
        ports:
        - containerPort: 9123
        volumeMounts:
         - name: task-pv-storage
           mountPath: /mnt/sample
Run Code Online (Sandbox Code Playgroud)

在大多数情况下,使用这种类型的卷是个坏主意。有一些特殊的用例,但您可能不是其中的一个!

如果出于某种原因需要本地存储,则使用localPersistentVolumes会是一个更好的解决方案。