Files
Qiming 9bf026e951 Clean zh/docs directory for dangling YAML files (#17341)
There are many YAML manifests sneaking into the `zh/docs/` directory.
These files should go to the `zh/examples` directory instead. Having
these "garbage" files (not referenced anywhere) is creating confusion
for the release meister when merging branches. For example, some YAML
files found in the 'master' branch are no longer there in the
release-1.16 branch. It is tedious, if possible at all, to solve all
this kind of conflicts during a rebase.

The PR cleanses the zh/docs directory for all dangling YAML files.
2019-11-04 08:24:48 +08:00

3.8 KiB
Raw Permalink Blame History

approvers, title
approvers title
jessfraz
使用 PodPreset 将信息注入 Pods

在 pod 创建时,用户可以使用 podpreset 对象将 secrets、卷挂载和环境变量等信息注入其中。 本文展示了一些 PodPreset 资源使用的示例。 用户可以从理解 Pod Presets 中了解 PodPresets 的整体情况。

{{< toc >}}

创建 Pod Preset

简单的 Pod Spec 示例

这里是一个简单的示例,展示了如何通过 Pod Preset 修改 Pod spec 。

{{< codenew file="podpreset/preset.yaml" >}}

创建 PodPreset

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

检查所创建的 PodPreset

kubectl get podpreset
NAME             AGE
allow-database   1m

新的 PodPreset 会对所有具有标签 role: frontend 的 Pods 采取行动。

用户提交的 pod spec

{{< codenew file="podpreset/pod.yaml" >}}

创建 Pod

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

列举运行中的 Pods

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

通过准入控制器后的 Pod 规约:

{{< codenew file="podpreset/merged.yaml" >}}

要查看如上输出,运行下面的命令:

kubectl get pod website -o yaml

带有 ConfigMap 的 Pod Spec 示例

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

用户提交的 pod spec

{{< codenew file="podpreset/pod.yaml" >}}

用户提交的 ConfigMap

{{< codenew file="podpreset/configmap.yaml" >}}

PodPreset 示例:

{{< codenew file="podpreset/allow-db.yaml" >}}

通过准入控制器后的 Pod spec

{{< codenew file="podpreset/allow-db-merged.yaml" >}}

带有 Pod Spec 的 ReplicaSet 示例

以下示例展示了(通过 ReplicaSet 创建 pod 后)只有 pod spec 会被 Pod Preset 所修改。

用户提交的 ReplicaSet

{{< codenew file="podpreset/replicaset.yaml" >}}

PodPreset 示例:

{{< codenew file="podpreset/preset.yaml" >}}

通过准入控制器后的 Pod spec

注意 ReplicaSet spec 没有改变,用户必须检查单独的 pod 来验证 PodPreset 已被应用。

{{< codenew file="podpreset/replicaset-merged.yaml" >}}

多 PodPreset 示例

这里的示例展示了如何通过多个 Pod 注入策略修改 Pod spec。

用户提交的 Pod 规约:

{{< codenew file="podpreset/pod.yaml" >}}

PodPreset 示例:

{{< codenew file="podpreset/preset.yaml" >}}

另一个 Pod Preset 示例:

{{< codenew file="podpreset/proxy.yaml" >}}

通过准入控制器后的 Pod 规约:

{{< codenew file="podpreset/multi-merged.yaml" >}}

冲突示例

这里的示例展示了 PodPreset 与原 Pod 存在冲突时,Pod spec 不会被修改。

用户提交的 Pod 规约:

{{< codenew file="podpreset/conflict-pod.yaml" >}}

PodPreset 示例:

{{< codenew file="podpreset/conflict-preset.yaml" >}}

因存在冲突,通过准入控制器后的 Pod spec 不会改变:

{{< codenew file="podpreset/conflict-pod.yaml" >}}

如果运行 kubectl describe... 用户会看到以下事件:

$ kubectl describe ...
....
Events:
  FirstSeen             LastSeen            Count   From                    SubobjectPath               Reason      Message
  Tue, 07 Feb 2017 16:56:12 -0700   Tue, 07 Feb 2017 16:56:12 -0700 1   {podpreset.admission.kubernetes.io/podpreset-allow-database }    conflict  Conflict on pod preset. Duplicate mountPath /cache.

删除 Pod Preset

一旦用户不再需要 pod preset,可以使用 kubectl 进行删除:

kubectl delete podpreset allow-database
podpreset "allow-database" deleted