kubernetes-guide/docs/chap07/7.3.md

3.4 KiB

valueFrom

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: env-valuefrom
  name: env-valuefrom
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: env-valuefrom
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: env-valuefrom
    spec:
      containers:
      - command:
        - sh
        - -c
        - env
        env:
        - name: TZ
          value: Asia/Shanghai
        - name: LANG
          value: C.UTF-8
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              key: special.how
              name: special-config
        image: busybox
        imagePullPolicy: IfNotPresent
        name: env-valuefrom
        resources:
          limits:
            cpu: 100m
            memory: 100Mi
          requests:
            cpu: 10m
            memory: 10Mi
      dnsPolicy: ClusterFirst
      restartPolicy: Never

envFrom

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: env-valuefrom
  name: env-valuefrom
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: env-valuefrom
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: env-valuefrom
    spec:
      containers:
      - command:
        - sh
        - -c
        - env
        env:
        - name: TZ
          value: Asia/Shanghai
        - name: LANG
          value: C.UTF-8
        envFrom:
        - configMapRef:
            name: game-config-env-file
          prefix: fromCm_
        image: busybox
        imagePullPolicy: IfNotPresent
        name: env-valuefrom
        resources:
          limits:
            cpu: 100m
            memory: 100Mi
          requests:
            cpu: 10m
            memory: 10Mi
      dnsPolicy: ClusterFirst
      restartPolicy: Never

文件挂载

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: busybox
      command: [ "/bin/sh", "-c", "ls /etc/config/" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        # Provide the name of the ConfigMap containing the files you want
        # to add to the container
        name: special-config
  restartPolicy: Never

自定义文件名

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: busybox
      command: [ "/bin/sh","-c","cat /etc/config/keys" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: special-config
        items:
        - key: special.how
          path: keys
  restartPolicy: Never

指定文件权限

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: busybox
      command: [ "/bin/sh","-c","ls -l /etc/config/..data/" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: special-config
        items:
        - key: special.how
          path: keys
        defaultMode: 0666
  restartPolicy: Never