使用 PodPreset 将信息注入 Pod

    本页展示如何在创建 Pod 时 使用 PodPreset 对象将类似 、卷挂载和 环境变量 这类信息注入到 Pod 中。

    你需要一个运行的 Kubernetes 集群以及配置好与集群通信的 kubectl 命令行工具。 如果你还没有集群,可以使用 安装一个。 确保你已经在集群中启用了 PodPreset

    在这一步中,你要创建一个 PodPreset 对象,其中包含卷挂载和一个环境变量。 下面是 PodPreset 的清单:

    PodPreset 对象的名称必须是一个合法的 DNS 子域名

    在清单中,你可以看到 PodPreset 有一个名为 DB_PORT 的环境变量定义, 和一个名为 cache-volume 的卷挂载定义,该卷挂载于 /cache 下。 设定此 PodPreset 将应用于所有匹配 role:frontend 标签的 Pods。

    创建 PodPreset:

    1. kubectl apply -f https://k8s.io/examples/podpreset/preset.yaml

    检查所创建的 PodPreset:

    1. kubectl get podpreset
    1. NAME AGE
    2. allow-database 1m

    下面的清单定义了一个带有标签 role: frontend 的 Pod(与 PodPreset 的选择算符匹配):

    podpreset/pod.yaml 使用 PodPreset 将信息注入 Pod - 图1

    1. apiVersion: v1
    2. kind: Pod
    3. metadata:
    4. name: website
    5. labels:
    6. app: website
    7. role: frontend
    8. spec:
    9. containers:
    10. - name: website
    11. image: nginx
    12. ports:
    13. - containerPort: 80

    创建 Pod:

    1. kubectl create -f https://k8s.io/examples/podpreset/pod.yaml

    验证 Pod 出于运行状态:

    1. kubectl get pods
    1. NAME READY STATUS RESTARTS AGE
    2. website 1/1 Running 0 4m

    查看被准入控制器更改过的 Pod 规约,以了解 PodPreset 在 Pod 上执行过的操作:

    1. kubectl get pod website -o yaml

    1. apiVersion: v1
    2. kind: Pod
    3. metadata:
    4. name: website
    5. labels:
    6. app: website
    7. role: frontend
    8. annotations:
    9. podpreset.admission.kubernetes.io/podpreset-allow-database: "resource version"
    10. spec:
    11. containers:
    12. - name: website
    13. image: nginx
    14. volumeMounts:
    15. - mountPath: /cache
    16. name: cache-volume
    17. ports:
    18. - containerPort: 80
    19. env:
    20. - name: DB_PORT
    21. value: "6379"
    22. volumes:
    23. - name: cache-volume
    24. emptyDir: {}

    Pod 的环境变量 DB_PORTvolumeMountpodpreset.admission.kubernetes.io 注解 表明 PodPreset 确实起了作用。

    这里的示例展示了如何通过 PodPreset 修改 Pod 规约,PodPreset 中定义了 ConfigMap 作为环境变量取值来源。

    包含 ConfigMap 定义的清单:

    podpreset/configmap.yaml 使用 PodPreset 将信息注入 Pod - 图2

    1. apiVersion: v1
    2. kind: ConfigMap
    3. metadata:
    4. name: etcd-env-config
    5. data:
    6. number_of_members: "1"
    7. initial_cluster_state: new
    8. initial_cluster_token: DUMMY_ETCD_INITIAL_CLUSTER_TOKEN
    9. discovery_token: DUMMY_ETCD_DISCOVERY_TOKEN
    10. discovery_url: http://etcd_discovery:2379
    11. etcdctl_peers: http://etcd:2379
    12. duplicate_key: FROM_CONFIG_MAP
    13. REPLACE_ME: "a value"

    创建 ConfigMap:

    1. kubectl create -f https://k8s.io/examples/podpreset/configmap.yaml

    引用该 ConfigMap 的 PodPreset 的清单:

    1. apiVersion: settings.k8s.io/v1alpha1
    2. kind: PodPreset
    3. metadata:
    4. name: allow-database
    5. spec:
    6. selector:
    7. matchLabels:
    8. role: frontend
    9. env:
    10. - name: DB_PORT
    11. value: "6379"
    12. - name: duplicate_key
    13. value: FROM_ENV
    14. - name: expansion
    15. value: $(REPLACE_ME)
    16. envFrom:
    17. - configMapRef:
    18. name: etcd-env-config
    19. volumeMounts:
    20. - mountPath: /cache
    21. name: cache-volume
    22. volumes:
    23. - name: cache-volume
    24. emptyDir: {}

    创建 PodPreset:

    1. kubectl create -f https://k8s.io/examples/podpreset/allow-db.yaml

    下面的清单包含与 PodPreset 匹配的 Pod:

    1. apiVersion: v1
    2. kind: Pod
    3. metadata:
    4. name: website
    5. labels:
    6. app: website
    7. role: frontend
    8. spec:
    9. containers:
    10. - name: website
    11. image: nginx
    12. ports:
    13. - containerPort: 80

    创建 Pod:

    查看 Pod 规约被准入控制器修改后的结果,了解 PodPreset 应用之后的效果:

    1. kubectl get pod website -o yaml

    podpreset/allow-db-merged.yaml 使用 PodPreset 将信息注入 Pod - 图3

    1. apiVersion: v1
    2. kind: Pod
    3. metadata:
    4. name: website
    5. labels:
    6. app: website
    7. role: frontend
    8. annotations:
    9. podpreset.admission.kubernetes.io/podpreset-allow-database: "resource version"
    10. spec:
    11. containers:
    12. - name: website
    13. image: nginx
    14. volumeMounts:
    15. name: cache-volume
    16. ports:
    17. env:
    18. - name: DB_PORT
    19. value: "6379"
    20. - name: duplicate_key
    21. value: FROM_ENV
    22. - name: expansion
    23. value: $(REPLACE_ME)
    24. envFrom:
    25. - configMapRef:
    26. name: etcd-env-config
    27. volumes:
    28. - name: cache-volume
    29. emptyDir: {}

    Pod 的环境变量 DB_PORTpodpreset.admission.kubernetes.io 注解 表明 PodPreset 确实起了作用。

    以下示例展示了(通过 ReplicaSet 创建 Pod 后)只有 Pod 规约会被 PodPreset 所修改, 其他资源类型(如 ReplicaSet、Deployment)不受影响。

    下面是本例所用 PodPreset 的清单:

    1. apiVersion: settings.k8s.io/v1alpha1
    2. kind: PodPreset
    3. metadata:
    4. name: allow-database
    5. spec:
    6. selector:
    7. matchLabels:
    8. role: frontend
    9. env:
    10. - name: DB_PORT
    11. value: "6379"
    12. volumeMounts:
    13. - mountPath: /cache
    14. name: cache-volume
    15. volumes:
    16. - name: cache-volume
    17. emptyDir: {}

    创建 Preset:

    1. kubectl apply -f https://k8s.io/examples/podpreset/preset.yaml

    此清单定义了一个管理三个应用 Pod 的 ReplicaSet:

    podpreset/replicaset.yaml 使用 PodPreset 将信息注入 Pod - 图4

    1. apiVersion: apps/v1
    2. kind: ReplicaSet
    3. metadata:
    4. name: frontend
    5. spec:
    6. replicas: 3
    7. selector:
    8. matchLabels:
    9. role: frontend
    10. matchExpressions:
    11. - {key: role, operator: In, values: [frontend]}
    12. template:
    13. metadata:
    14. labels:
    15. app: guestbook
    16. role: frontend
    17. spec:
    18. containers:
    19. - name: php-redis
    20. image: gcr.io/google_samples/gb-frontend:v3
    21. resources:
    22. requests:
    23. cpu: 100m
    24. memory: 100Mi
    25. env:
    26. - name: GET_HOSTS_FROM
    27. value: dns
    28. ports:
    29. - containerPort: 80

    创建 ReplicaSet:

    1. kubectl create -f https://k8s.io/examples/podpreset/replicaset.yaml

    验证 ReplicaSet 所创建的 Pod 处于运行状态:

    1. kubectl get pods

    输出显示 Pod 正在运行:

    1. NAME READY STATUS RESTARTS AGE
    2. frontend-2l94q 1/1 Running 0 2m18s
    3. frontend-6vdgn 1/1 Running 0 2m18s
    4. frontend-jzt4p 1/1 Running 0 2m18s

    查看 ReplicaSet 的 spec 内容:

    1. kubectl get replicasets frontend -o yaml

    查看被影响的 Pod 的规约的命令是:

    1. kubectl get pod --selector=role=frontend -o yaml

    1. apiVersion: v1
    2. kind: Pod
    3. metadata:
    4. name: frontend
    5. labels:
    6. app: guestbook
    7. role: frontend
    8. annotations:
    9. podpreset.admission.kubernetes.io/podpreset-allow-database: "resource version"
    10. spec:
    11. containers:
    12. - name: php-redis
    13. image: gcr.io/google_samples/gb-frontend:v3
    14. resources:
    15. requests:
    16. cpu: 100m
    17. memory: 100Mi
    18. volumeMounts:
    19. - mountPath: /cache
    20. name: cache-volume
    21. env:
    22. - name: GET_HOSTS_FROM
    23. value: dns
    24. - name: DB_PORT
    25. value: "6379"
    26. ports:
    27. - containerPort: 80
    28. volumes:
    29. - name: cache-volume
    30. emptyDir: {}

    再一次,Pod 的 podpreset.admission.kubernetes.io 注解表明 PodPreset 已经被应用过。

    这里的示例展示了如何通过多个 PodPreset 对象修改 Pod 规约。

    第一个 PodPreset 的清单如下:

    podpreset/preset.yaml 使用 PodPreset 将信息注入 Pod - 图5

    1. apiVersion: settings.k8s.io/v1alpha1
    2. kind: PodPreset
    3. metadata:
    4. name: allow-database
    5. spec:
    6. selector:
    7. matchLabels:
    8. role: frontend
    9. env:
    10. - name: DB_PORT
    11. value: "6379"
    12. volumeMounts:
    13. - mountPath: /cache
    14. name: cache-volume
    15. volumes:
    16. - name: cache-volume
    17. emptyDir: {}
    1. kubectl apply -f https://k8s.io/examples/podpreset/preset.yaml

    下面是第二个 PodPreset 的清单:

    1. apiVersion: settings.k8s.io/v1alpha1
    2. metadata:
    3. name: proxy
    4. spec:
    5. matchLabels:
    6. role: frontend
    7. volumeMounts:
    8. - mountPath: /etc/proxy/configs
    9. name: proxy-volume
    10. volumes:
    11. - name: proxy-volume
    12. emptyDir: {}

    创建第二个 PodPreset:

    下面是包含可被修改的 Pod 定义的清单(此 Pod 同时被两个 PodPreset 匹配到):

    podpreset/pod.yaml 使用 PodPreset 将信息注入 Pod - 图6

    1. apiVersion: v1
    2. kind: Pod
    3. metadata:
    4. name: website
    5. labels:
    6. app: website
    7. role: frontend
    8. spec:
    9. containers:
    10. - name: website
    11. image: nginx
    12. ports:
    13. - containerPort: 80

    创建 Pod:

    1. kubectl create -f https://k8s.io/examples/podpreset/pod.yaml

    查看被准入控制器更改后的 Pod 规约,以了解被两个 PodPreset 一同修改 后的效果:

    1. kubectl get pod website -o yaml

    1. apiVersion: v1
    2. kind: Pod
    3. metadata:
    4. name: website
    5. labels:
    6. app: website
    7. role: frontend
    8. annotations:
    9. podpreset.admission.kubernetes.io/podpreset-allow-database: "resource version"
    10. podpreset.admission.kubernetes.io/podpreset-proxy: "resource version"
    11. spec:
    12. containers:
    13. - name: website
    14. image: nginx
    15. volumeMounts:
    16. - mountPath: /cache
    17. name: cache-volume
    18. - mountPath: /etc/proxy/configs
    19. name: proxy-volume
    20. ports:
    21. - containerPort: 80
    22. env:
    23. - name: DB_PORT
    24. value: "6379"
    25. volumes:
    26. - name: cache-volume
    27. emptyDir: {}
    28. - name: proxy-volume
    29. emptyDir: {}

    Pod 定义中的 DB_PORT 环境变量、proxy-volume 卷挂载以及 两个 podpreset.admission.kubernetes.io 可以证明两个 Preset 都被应用了。

    这里的示例展示了 PodPreset 与原 Pod 存在冲突时,Pod 规约不会被修改。 本例中的冲突是指 PodPreset 中的 volumeMount 与 Pod 中定义的卷挂载在 mountPath 上有冲突。

    下面是 PodPreset 的清单:

    podpreset/conflict-preset.yaml 使用 PodPreset 将信息注入 Pod - 图7

    1. apiVersion: settings.k8s.io/v1alpha1
    2. kind: PodPreset
    3. metadata:
    4. name: allow-database
    5. spec:
    6. selector:
    7. matchLabels:
    8. role: frontend
    9. env:
    10. - name: DB_PORT
    11. value: "6379"
    12. volumeMounts:
    13. - mountPath: /cache
    14. name: other-volume
    15. volumes:
    16. - name: other-volume
    17. emptyDir: {}

    注意 mountPath 的取值是 /cache。 创建 PodPreset:

    1. kubectl apply -f https://k8s.io/examples/podpreset/conflict-preset.yaml

    下面是 Pod 的清单:

    1. apiVersion: v1
    2. kind: Pod
    3. metadata:
    4. name: website
    5. labels:
    6. app: website
    7. role: frontend
    8. spec:
    9. containers:
    10. - name: website
    11. image: nginx
    12. volumeMounts:
    13. - mountPath: /cache
    14. name: cache-volume
    15. ports:
    16. - containerPort: 80
    17. volumes:
    18. - name: cache-volume
    19. emptyDir: {}

    注意清单中 volumeMount 元素的取值与 PodPreset 中的路径值相同。

    创建 Pod:

    1. kubectl create -f https://k8s.io/examples/podpreset/conflict-pod.yaml

    查看 Pod 规约:

    1. kubectl get pod website -o yaml

    podpreset/conflict-pod.yaml 使用 PodPreset 将信息注入 Pod - 图8

    1. apiVersion: v1
    2. kind: Pod
    3. metadata:
    4. name: website
    5. labels:
    6. app: website
    7. role: frontend
    8. spec:
    9. containers:
    10. - name: website
    11. image: nginx
    12. volumeMounts:
    13. - mountPath: /cache
    14. name: cache-volume
    15. ports:
    16. - containerPort: 80
    17. volumes:
    18. - name: cache-volume
    19. emptyDir: {}

    这里你可以看到 Pod 上并没有 PodPreset 的注解 podpreset.admission.kubernetes.io`。 这意味着没有 PodPreset 被应用到 Pod 之上。

    不过 还是为所发生的冲突留下了一条警告性质的日志。 你可以通过 kubectl 来查看此警告信息:

    1. kubectl -n kube-system logs -l=component=kube-apiserver

    输出类似于:

    1. W1214 13:00:12.987884 1 admission.go:147] conflict occurred while applying podpresets: allow-database on pod: err: merging volume mounts for allow-database has a conflict on mount path /cache:
    2. v1.VolumeMount{Name:"other-volume", ReadOnly:false, MountPath:"/cache", SubPath:"", MountPropagation:(*v1.MountPropagationMode)(nil), SubPathExpr:""}
    3. does not match
    4. core.VolumeMount{Name:"cache-volume", ReadOnly:false, MountPath:"/cache", SubPath:"", MountPropagation:(*core.MountPropagationMode)(nil), SubPathExpr:""}
    5. in container

    注意这里关于卷挂载路径冲突的消息。

      输出显示 PodPreset 已经被删除: