在K8S Volume中使用 subPath

一.subpath简单说明

在Pod中共享卷以供多方使用是很有用的。VolumeMounts.subPath属性可用于指定所引用的卷内的子路径,而不是其根路径。

二.subpath使用场景

  • 1个Pod中可以有多个容器,这里将不同容器的路径挂载在存储卷volume的子路径,这种场景需要使用到subpath。
  • volume支持将configMap/secret挂载到容器的路径,但是会覆盖容器路径下原有的文件。如何支持选定configmap/secret的key-value挂载到容器中,且不会覆盖掉原目录下的文件,这个时候可以用subpath。

三.示例

 容器/etc/nginx/conf.d/目录下原先是有文件的,需挂载不同的文件到该容器/etc/nginx/conf.d/目录下。

kubectl get configmap -n liang

NAME                          DATA   AGE
kube-root-ca.crt              1      39d
smarttest-saas-calculator-cm   1      2d2h
smarttest-saas-ops-cm     1      2d3h
smarttest-saas-www-cm          1      2d3h

 

 vim deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: smartops-saas
  name: smartops-saas-deploy
  namespace: liang
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: smartops-saas
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: smartops-saas
    spec:
      containers:
      - image: ccr.ccs.tencentyun.com/devops/xxxxx:latest
        imagePullPolicy: IfNotPresent
        name: smarttest-saas
        ports:
        - containerPort: 80
          name: port
          protocol: TCP
         resources:
          limits:
            cpu: 1000m
            memory: 1000Mi
          requests:
            cpu: 800m
            memory: 800Mi
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: /etc/nginx/conf.d/ops.conf
          subPath: ops.conf
          name: smarttest-saas-ops-volume
        - mountPath: /etc/nginx/conf.d/web-calculator.conf
          subPath: web-calculator.conf
          name: smarttest-saas-calculator-volume
        - mountPath: /usr/local/etc/php-fpm.d/www.conf
          subPath: www.conf
          name: smarttest-saas-www-volume
      dnsPolicy: ClusterFirst
      imagePullSecrets:
      - name: devops-dockerhub
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      volumes:
      - configMap:
          name: smarttest-saas-ops-cm
        name: smarttest-saas-ops-volume
      - configMap:
          name: smarttest-saas-calculator-cm
        name: smarttest-saas-calculator-volume
      - configMap:
          name: smarttest-saas-www-cm
        name: smarttest-saas-www-volume

 

posted @ 2022-04-24 18:55  梁博客  阅读(1242)  评论(0)    收藏  举报