如何在一个 pod 上挂载 hostpath,并将 podname 作为子目录。

huangapple go评论63阅读模式
英文:

How to mount hostpath on a pod with podname as a subdirectory

问题

我需要将hostpath挂载到一个Pod上,但是这个hostpath应该有一个与POD名称相关的子目录。我知道类似这样的操作可以在StatefulSets中使用,但由于某些限制,需要在部署中进行hostpath挂载,其中根目录将具有podnames作为子目录。这是否可以实现?

英文:

I have a use-case where I need to mount a hostpath to a pod but that hostpath should have a subdirectory with PODNAME, I know something like this can be used with statefulsets but due to some constraints a hostpath mount is needed in deployment where the root directory will have sub directories as podnames.

Can this be achieved?

答案1

得分: 2

  • name: pod_name
    valueFrom:
    fieldRef:
    apiVersion: v1
    fieldPath: metadata.name

请随意参考我的 GitHub YAML:https://github.com/harsh4870/OCI-public-logging-uma-agent/blob/main/deployment.yaml

apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:

  • name: app
    image:
    env:

    • name: pod_name
      valueFrom:
      fieldRef:
      apiVersion: v1
      fieldPath: metadata.name
      volumeMounts:
    • mountPath: /app_logs
      name: app-logs
      subPathExpr: $(pod_name)
      volumes:
  • name: mydir
    hostPath:
    path: /var/log/app_logs
    type: DirectoryOrCreate

这是完整文章,我使用了downwardAPI来记录日志:https://medium.com/@harsh.manvar111/oke-logging-with-uma-agent-in-oci-d6f55a8bcc02

英文:

You can use the downward API of Kubernetes as Environment variables :

- name: pod_name
        valueFrom:
          fieldRef:
            apiVersion: v1
            fieldPath: metadata.name

Feel free to refer my GitHub YAML : https://github.com/harsh4870/OCI-public-logging-uma-agent/blob/main/deployment.yaml

apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - name: app
    image: <App image>
    env:
      - name: pod_name
        valueFrom:
          fieldRef:
            apiVersion: v1
            fieldPath: metadata.name
    volumeMounts:
    - mountPath: /app_logs
      name: app-logs
      subPathExpr: $(pod_name)
  volumes:
  - name: mydir
    hostPath:
      path: /var/log/app_logs
      type: DirectoryOrCreate

Here is full article i have used downwardAPI for logging : https://medium.com/@harsh.manvar111/oke-logging-with-uma-agent-in-oci-d6f55a8bcc02

huangapple
  • 本文由 发表于 2023年2月13日 22:55:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75437526.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定