简体   繁体   English

Kubernetes 持久卷和主机路径

[英]Kubernetes Persistent Volume and hostpath

I was experimenting with something with Kubernetes Persistent Volumes, I can't find a clear explanation in Kubernetes documentation and the behaviour is not the one I am expecting so I like to ask here.我正在尝试使用 Kubernetes Persistent Volumes 进行一些实验,我在 Kubernetes 文档中找不到明确的解释,而且行为不是我所期望的,所以我想在这里提问。

I configured following Persistent Volume and Persistent Volume Claim.我配置了以下持久卷和持久卷声明。

kind: PersistentVolume
apiVersion: v1
metadata:
  name: store-persistent-volume
  namespace: test
spec:
  storageClassName: hostpath
  capacity:
    storage: 2Gi
  accessModes:
  - ReadWriteOnce
  hostPath:
    path: "/Volumes/Data/data"

---

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: store-persistent-volume-claim
  namespace: test
spec:
  storageClassName: hostpath
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

and the following Deployment and Service configuration.以及以下部署和服务配置。

kind: Deployment
apiVersion: apps/v1beta2
metadata:
  name: store-deployment
  namespace: test
spec:
  replicas: 1
  selector:
    matchLabels:
      k8s-app: store
  template:
    metadata:
      labels:
        k8s-app: store
    spec:
      volumes:
      - name: store-volume
        persistentVolumeClaim:
          claimName: store-persistent-volume-claim
      containers:
      - name: store
        image: localhost:5000/store
        ports:
        - containerPort: 8383
          protocol: TCP
        volumeMounts:
        - name: store-volume
          mountPath: /data

---
#------------ Service ----------------#

kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: store
  name: store
  namespace: test
spec:
  type: LoadBalancer
  ports:
  - port: 8383
    targetPort: 8383
  selector:
    k8s-app: store

As you can see I defined '/Volumes/Data/data' as Persistent Volume and expecting that to mount that to '/data' container.如您所见,我将“/Volumes/Data/data”定义为 Persistent Volume,并希望将其安装到“/data”容器中。

So I am assuming whatever in '/Volumes/Data/data' in the host should be visible at '/data' directory at container.所以我假设主机中“/Volumes/Data/data”中的任何内容都应该在容器的“/data”目录中可见。 Is this assumption correct?这个假设正确吗? Because this is definitely not happening at the moment.因为目前这绝对不会发生。

My second assumption is, whatever I save at '/data' should be visible at host, which is also not happening.我的第二个假设是,我在 '/data' 中保存的任何内容都应该在主机上可见,这也不会发生。

I can see from Kubernetes console that everything started correctly, (Persistent Volume, Claim, Deployment, Pod, Service...)我可以从 Kubernetes 控制台看到一切正常启动,(持久卷、声明、部署、Pod、服务......)

Am I understanding the persistent volume concept correctly at all?我是否正确理解了持久卷概念?

PS.附注。 I am trying this in a Mac with Docker (18.05.0-ce-mac67(25042) -Channel edge), may be it should not work at Mac?我正在使用 Docker (18.05.0-ce-mac67(25042) -Channel edge) 的 Mac 中尝试这个,它可能不应该在 Mac 上工作?

Thx for answers谢谢解答

Assuming you are using multi-node Kubernetes cluster, you should be able to see the data mounted locally at /Volumes/Data/data on the specific worker node that pod is running假设您使用的是多节点 Kubernetes 集群,您应该能够在pod 运行的特定工作节点上的/Volumes/Data/data 中看到本地挂载的数据

You can check on which worker your pod is scheduled by using the command kubectl get pods -o wide -n test您可以使用命令kubectl get pods -o wide -n test检查您的 pod 被调度的工作人员

Please note, as per kubernetes docs, HostPath (Single node testing only – local storage is not supported in any way and WILL NOT WORK in a multi-node cluster) PersistentVolume请注意,根据 kubernetes 文档,HostPath(仅限单节点测试 - 本地存储不受任何方式支持,不会在多节点集群中工作) PersistentVolume

It does work in my case.在我的情况下它确实有效。

WIth the following commands you can check your persistentVolumes and claims: 使用以下命令,您可以检查您的持久卷和声明:

kubectl get pv

kubectl get pvc

and see whether the volume you defined are bound with your claims. 并查看您定义的卷是否与您的声明绑定。

Once your pod started you can enter the container and see your data at /data 吊舱启动后,您可以进入容器并在/data查看/data

kubectl exec -ti <your_pod> -- bash

当您使用主机路径时,您应该在运行 pod 的工作节点中检查这个“/data”。

Like the guy said above.就像楼上那位说的。 You need to run a 'kubectl get po -n test -o wide' and you will see the node the pod is hosted on.您需要运行“kubectl get po -n test -o wide”,然后您将看到 Pod 所在的节点。 Then if you SSH that worker you can see the volume然后,如果您通过 SSH 连接该工作人员,您可以看到音量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM