【博客695】k8s subPathExpr作用

k8s subPathExpr作用

场景:

对于一个deployment或者job拉起的服务,所有pod都是一样的配置,如果都挂载了宿主机的同一个目录,那么就会互相干扰,我们希望挂载相同目录,且在这个目录下,每个pod建立一个自己的目录,去写入自己的东西

使用subPathExpr

apiVersion: batch/v1
kind: Job
metadata:
  name: subpath-jobs
  labels:
    name: subpath-jobs
spec:
  completions: 5
  parallelism: 5
  backoffLimit: 0
  template:
    spec:
      restartPolicy: "Never"
      containers:
        - name: busybox
          image: busybox
          workingDir: /outputs
          command: [ "touch" ]
          args: [ "a_file.txt" ]
          env:
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: metadata.name     
          volumeMounts:
            - name: job-output
              mountPath: /outputs
              subPathExpr: $(POD_NAME)
      volumes:
      - name: job-output
        hostPath:
          path: /data/outputs
          type: DirectoryOrCreate

效果:

# ls -R /data
/data:
outputs

/data/outputs:
subpath-jobs-6968q  subpath-jobs-6zp4x  subpath-jobs-nhh96  subpath-jobs-tl8fx  subpath-jobs-w2h9f

/data/outputs/subpath-jobs-6968q:
a_file.txt

/data/outputs/subpath-jobs-6zp4x:
a_file.txt

/data/outputs/subpath-jobs-nhh96:
a_file.txt

/data/outputs/subpath-jobs-tl8fx:
a_file.txt

/data/outputs/subpath-jobs-w2h9f:
a_file.txt

你可能感兴趣的:(kubernetes,docker,容器,云原生)