|
| 1 | +--- |
| 2 | +title: 构建一个基本的 DaemonSet |
| 3 | +content_type: task |
| 4 | +weight: 5 |
| 5 | +--- |
| 6 | +<!-- |
| 7 | +title: Building a Basic DaemonSet |
| 8 | +content_type: task |
| 9 | +weight: 5 |
| 10 | +--> |
| 11 | + |
| 12 | +<!-- overview --> |
| 13 | + |
| 14 | +<!-- |
| 15 | +This page demonstrates how to build a basic {{< glossary_tooltip text="DaemonSet" term_id="daemonset" >}} that runs a Pod on every node in a Kubernetes cluster. |
| 16 | +It covers a simple use case of mounting a file from the host, logging its contents using |
| 17 | +an [init container](/docs/concepts/workloads/pods/init-containers/), and utilizing a pause container. |
| 18 | +--> |
| 19 | +本页演示如何构建一个基本的 {{< glossary_tooltip text="DaemonSet" term_id="daemonset" >}}, |
| 20 | +用其在 Kubernetes 集群中的每个节点上运行 Pod。 |
| 21 | +这个简单的使用场景包含了从主机挂载一个文件,使用 |
| 22 | +[Init 容器](/zh-cn/docs/concepts/workloads/pods/init-containers/)记录文件的内容, |
| 23 | +以及使用 `pause` 容器。 |
| 24 | + |
| 25 | +## {{% heading "prerequisites" %}} |
| 26 | + |
| 27 | +{{< include "task-tutorial-prereqs.md" >}} |
| 28 | + |
| 29 | +<!-- |
| 30 | +A Kubernetes cluster with at least two nodes (one control plane node and one worker node) to demonstrate the behavior of DaemonSets. |
| 31 | +--> |
| 32 | +为了演示 DaemonSet 的行为,Kubernetes 集群至少需包含两个节点(一个控制平面节点和一个工作节点)。 |
| 33 | + |
| 34 | +<!-- |
| 35 | +## Define the DaemonSet |
| 36 | +
|
| 37 | +In this task, a basic DaemonSet is created which ensures that the copy of a Pod is scheduled on every node. |
| 38 | +The Pod will use an init container to read and log the contents of `/etc/machine-id` from the host, |
| 39 | +while the main container will be a `pause` container, which keeps the Pod running. |
| 40 | +--> |
| 41 | +## 定义 DaemonSet {#define-the-daemonset} |
| 42 | + |
| 43 | +在此任务中,将创建一个基本的 DaemonSet,确保 Pod 的副本被调度到每个节点上。 |
| 44 | +此 Pod 将使用 Init 容器从主机读取并记录 `/etc/machine-id` 的内容, |
| 45 | +而主容器将是一个 `pause` 容器,用于保持 Pod 运行。 |
| 46 | + |
| 47 | +{{% code_sample file="application/basic-daemonset.yaml" %}} |
| 48 | + |
| 49 | +<!-- |
| 50 | +1. Create a DaemonSet based on the (YAML) manifest: |
| 51 | +--> |
| 52 | +1. 基于(YAML)清单创建 DaemonSet: |
| 53 | + |
| 54 | + ```shell |
| 55 | + kubectl apply -f https://k8s.io/examples/application/basic-daemonset.yaml |
| 56 | + ``` |
| 57 | + |
| 58 | +<!-- |
| 59 | +1. Once applied, you can verify that the DaemonSet is running a Pod on every node in the cluster: |
| 60 | +--> |
| 61 | +2. 完成创建操作后,你可以验证 DaemonSet 是否在集群中的每个节点上运行 Pod: |
| 62 | + |
| 63 | + ```shell |
| 64 | + kubectl get pods -o wide |
| 65 | + ``` |
| 66 | + |
| 67 | + <!-- |
| 68 | + The output will list one Pod per node, similar to: |
| 69 | + --> |
| 70 | + |
| 71 | + 输出将列出每个节点上有一个 Pod,类似于: |
| 72 | + |
| 73 | + ``` |
| 74 | + NAME READY STATUS RESTARTS AGE IP NODE |
| 75 | + example-daemonset-xxxxx 1/1 Running 0 5m x.x.x.x node-1 |
| 76 | + example-daemonset-yyyyy 1/1 Running 0 5m x.x.x.x node-2 |
| 77 | + ``` |
| 78 | + |
| 79 | +<!-- |
| 80 | +1. You can inspect the contents of the logged `/etc/machine-id` file by checking the log directory mounted from the host: |
| 81 | +--> |
| 82 | +3. 你可以通过检查从主机挂载的日志目录来查看 `/etc/machine-id` 文件的日志内容: |
| 83 | + |
| 84 | + ```shell |
| 85 | + kubectl exec <pod-name> -- cat /var/log/machine-id.log |
| 86 | + ``` |
| 87 | + |
| 88 | + <!-- |
| 89 | + Where `<pod-name>` is the name of one of your Pods. |
| 90 | + --> |
| 91 | + |
| 92 | + 其中 `<pod-name>` 是某一个 Pod 的名称。 |
| 93 | + |
| 94 | +## {{% heading "cleanup" %}} |
| 95 | + |
| 96 | +```shell |
| 97 | +kubectl delete --cascade=foreground --ignore-not-found --now daemonsets/example-daemonset |
| 98 | +``` |
| 99 | + |
| 100 | +<!-- |
| 101 | +This simple DaemonSet example introduces key components like init containers and host path volumes, |
| 102 | +which can be expanded upon for more advanced use cases. For more details refer to |
| 103 | +[DaemonSet](/docs/concepts/workloads/controllers/daemonset/). |
| 104 | +--> |
| 105 | +这个简单的 DaemonSet 例子介绍了 Init 容器和主机路径卷这类关键组件, |
| 106 | +你可以在此基础上扩展以应对更高级的使用场景。有关细节参阅 |
| 107 | +[DaemonSet](/zh-cn/docs/concepts/workloads/controllers/daemonset/)。 |
0 commit comments