|
1 | 1 | ---
|
2 |
| -title: 使用 Kubernetes 对象 |
3 |
| -weight: 40 |
| 2 | +title: Kubernetes 对象 |
| 3 | +content_type: concept |
| 4 | +weight: 10 |
4 | 5 | description: >
|
5 |
| - Kubernetes 对象是 Kubernetes 系统中的持久性实体。Kubernetes 使用这些实体表示你的集群状态。 |
| 6 | + Kubernetes 对象是 Kubernetes 系统中的持久性实体。 |
| 7 | + Kubernetes 使用这些实体表示你的集群状态。 |
6 | 8 | 了解 Kubernetes 对象模型以及如何使用这些对象。
|
| 9 | +simple_list: true |
| 10 | +card: |
| 11 | + name: concepts |
| 12 | + weight: 40 |
7 | 13 | ---
|
| 14 | +<!-- |
| 15 | +title: Objects In Kubernetes |
| 16 | +content_type: concept |
| 17 | +weight: 10 |
| 18 | +description: > |
| 19 | + Kubernetes objects are persistent entities in the Kubernetes system. |
| 20 | + Kubernetes uses these entities to represent the state of your cluster. |
| 21 | + Learn about the Kubernetes object model and how to work with these objects. |
| 22 | +simple_list: true |
| 23 | +card: |
| 24 | + name: concepts |
| 25 | + weight: 40 |
| 26 | +--> |
| 27 | + |
| 28 | +<!-- overview --> |
| 29 | + |
| 30 | +<!-- |
| 31 | +This page explains how Kubernetes objects are represented in the Kubernetes API, and how you can |
| 32 | +express them in `.yaml` format. |
| 33 | +--> |
| 34 | +本页说明了在 Kubernetes API 中是如何表示 Kubernetes 对象的, |
| 35 | +以及如何使用 `.yaml` 格式的文件表示 Kubernetes 对象。 |
| 36 | + |
| 37 | +<!-- body --> |
| 38 | + |
| 39 | +<!-- |
| 40 | +## Understanding Kubernetes objects {#kubernetes-objects} |
| 41 | +
|
| 42 | +*Kubernetes objects* are persistent entities in the Kubernetes system. Kubernetes uses these |
| 43 | +entities to represent the state of your cluster. Specifically, they can describe: |
| 44 | +
|
| 45 | +* What containerized applications are running (and on which nodes) |
| 46 | +* The resources available to those applications |
| 47 | +* The policies around how those applications behave, such as restart policies, upgrades, and fault-tolerance |
| 48 | +--> |
| 49 | +## 理解 Kubernetes 对象 {#kubernetes-objects} |
| 50 | + |
| 51 | +在 Kubernetes 系统中,**Kubernetes 对象**是持久化的实体。 |
| 52 | +Kubernetes 使用这些实体去表示整个集群的状态。 |
| 53 | +具体而言,它们描述了如下信息: |
| 54 | + |
| 55 | +* 哪些容器化应用正在运行(以及在哪些节点上运行) |
| 56 | +* 可以被应用使用的资源 |
| 57 | +* 关于应用运行时行为的策略,比如重启策略、升级策略以及容错策略 |
| 58 | + |
| 59 | +<!-- |
| 60 | +A Kubernetes object is a "record of intent"--once you create the object, the Kubernetes system |
| 61 | +will constantly work to ensure that object exists. By creating an object, you're effectively |
| 62 | +telling the Kubernetes system what you want your cluster's workload to look like; this is your |
| 63 | +cluster's *desired state*. |
| 64 | +--> |
| 65 | +Kubernetes 对象是一种“意向表达(Record of Intent)”。一旦创建该对象, |
| 66 | +Kubernetes 系统将不断工作以确保该对象存在。通过创建对象,你本质上是在告知 |
| 67 | +Kubernetes 系统,你想要的集群工作负载状态看起来应是什么样子的, |
| 68 | +这就是 Kubernetes 集群所谓的**期望状态(Desired State)**。 |
| 69 | + |
| 70 | +<!-- |
| 71 | +To work with Kubernetes objects--whether to create, modify, or delete them--you'll need to use the |
| 72 | +[Kubernetes API](/docs/concepts/overview/kubernetes-api/). When you use the `kubectl` command-line |
| 73 | +interface, for example, the CLI makes the necessary Kubernetes API calls for you. You can also use |
| 74 | +the Kubernetes API directly in your own programs using one of the |
| 75 | +[Client Libraries](/docs/reference/using-api/client-libraries/). |
| 76 | +--> |
| 77 | +操作 Kubernetes 对象 —— 无论是创建、修改或者删除 —— 需要使用 |
| 78 | +[Kubernetes API](/zh-cn/docs/concepts/overview/kubernetes-api)。 |
| 79 | +比如,当使用 `kubectl` 命令行接口(CLI)时,CLI 会调用必要的 Kubernetes API; |
| 80 | +也可以在程序中使用[客户端库](/zh-cn/docs/reference/using-api/client-libraries/), |
| 81 | +来直接调用 Kubernetes API。 |
| 82 | + |
| 83 | +<!-- |
| 84 | +### Object spec and status |
| 85 | +
|
| 86 | +Almost every Kubernetes object includes two nested object fields that govern |
| 87 | +the object's configuration: the object *`spec`* and the object *`status`*. |
| 88 | +For objects that have a `spec`, you have to set this when you create the object, |
| 89 | +providing a description of the characteristics you want the resource to have: |
| 90 | +its _desired state_. |
| 91 | +--> |
| 92 | +### 对象规约(Spec)与状态(Status) {#object-spec-and-status} |
| 93 | + |
| 94 | +几乎每个 Kubernetes 对象包含两个嵌套的对象字段,它们负责管理对象的配置: |
| 95 | +对象 **`spec`(规约)** 和 对象 **`status`(状态)**。 |
| 96 | +对于具有 `spec` 的对象,你必须在创建对象时设置其内容,描述你希望对象所具有的特征: |
| 97 | +**期望状态(Desired State)**。 |
| 98 | + |
| 99 | +<!-- |
| 100 | +The `status` describes the _current state_ of the object, supplied and updated |
| 101 | +by the Kubernetes system and its components. The Kubernetes |
| 102 | +{{< glossary_tooltip text="control plane" term_id="control-plane" >}} continually |
| 103 | +and actively manages every object's actual state to match the desired state you |
| 104 | +supplied. |
| 105 | +--> |
| 106 | +`status` 描述了对象的**当前状态(Current State)**,它是由 Kubernetes |
| 107 | +系统和组件设置并更新的。在任何时刻,Kubernetes |
| 108 | +{{< glossary_tooltip text="控制平面" term_id="control-plane" >}} |
| 109 | +都一直都在积极地管理着对象的实际状态,以使之达成期望状态。 |
| 110 | + |
| 111 | +<!-- |
| 112 | +For example: in Kubernetes, a Deployment is an object that can represent an |
| 113 | +application running on your cluster. When you create the Deployment, you |
| 114 | +might set the Deployment `spec` to specify that you want three replicas of |
| 115 | +the application to be running. The Kubernetes system reads the Deployment |
| 116 | +spec and starts three instances of your desired application--updating |
| 117 | +the status to match your spec. If any of those instances should fail |
| 118 | +(a status change), the Kubernetes system responds to the difference |
| 119 | +between spec and status by making a correction--in this case, starting |
| 120 | +a replacement instance. |
| 121 | +--> |
| 122 | +例如,Kubernetes 中的 Deployment 对象能够表示运行在集群中的应用。 |
| 123 | +当创建 Deployment 时,你可能会设置 Deployment 的 `spec`,指定该应用要有 3 个副本运行。 |
| 124 | +Kubernetes 系统读取 Deployment 的 `spec`, |
| 125 | +并启动我们所期望的应用的 3 个实例 —— 更新状态以与规约相匹配。 |
| 126 | +如果这些实例中有的失败了(一种状态变更),Kubernetes 系统会通过执行修正操作来响应 |
| 127 | +`spec` 和 `status` 间的不一致 —— 意味着它会启动一个新的实例来替换。 |
| 128 | + |
| 129 | +<!-- |
| 130 | +For more information on the object spec, status, and metadata, see the |
| 131 | +[Kubernetes API Conventions](https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md). |
| 132 | +--> |
| 133 | +关于对象 spec、status 和 metadata 的更多信息,可参阅 |
| 134 | +[Kubernetes API 约定](https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md)。 |
| 135 | + |
| 136 | +<!-- |
| 137 | +### Describing a Kubernetes object |
| 138 | +
|
| 139 | +When you create an object in Kubernetes, you must provide the object spec that describes its |
| 140 | +desired state, as well as some basic information about the object (such as a name). When you use |
| 141 | +the Kubernetes API to create the object (either directly or via `kubectl`), that API request must |
| 142 | +include that information as JSON in the request body. **Most often, you provide the information to |
| 143 | +`kubectl` in a .yaml file.** `kubectl` converts the information to JSON when making the API |
| 144 | +request. |
| 145 | +--> |
| 146 | +### 描述 Kubernetes 对象 {#describing-a-kubernetes-object} |
| 147 | + |
| 148 | +创建 Kubernetes 对象时,必须提供对象的 `spec`,用来描述该对象的期望状态, |
| 149 | +以及关于对象的一些基本信息(例如名称)。 |
| 150 | +当使用 Kubernetes API 创建对象时(直接创建,或经由 `kubectl`), |
| 151 | +API 请求必须在请求主体中包含 JSON 格式的信息。 |
| 152 | +**大多数情况下,你需要提供 `.yaml` 文件为 kubectl 提供这些信息**。 |
| 153 | +`kubectl` 在发起 API 请求时,将这些信息转换成 JSON 格式。 |
| 154 | + |
| 155 | +<!-- |
| 156 | +Here's an example `.yaml` file that shows the required fields and object spec for a Kubernetes Deployment: |
| 157 | +--> |
| 158 | +这里有一个 `.yaml` 示例文件,展示了 Kubernetes Deployment 的必需字段和对象 `spec`: |
| 159 | + |
| 160 | +{{< codenew file="application/deployment.yaml" >}} |
| 161 | + |
| 162 | +<!-- |
| 163 | +One way to create a Deployment using a `.yaml` file like the one above is to use the |
| 164 | +[`kubectl apply`](/docs/reference/generated/kubectl/kubectl-commands#apply) command |
| 165 | +in the `kubectl` command-line interface, passing the `.yaml` file as an argument. Here's an example: |
| 166 | +--> |
| 167 | +相较于上面使用 `.yaml` 文件来创建 Deployment,另一种类似的方式是使用 `kubectl` 命令行接口(CLI)中的 |
| 168 | +[`kubectl apply`](/docs/reference/generated/kubectl/kubectl-commands#apply) 命令, |
| 169 | +将 `.yaml` 文件作为参数。下面是一个示例: |
| 170 | + |
| 171 | +```shell |
| 172 | +kubectl apply -f https://k8s.io/examples/application/deployment.yaml |
| 173 | +``` |
| 174 | + |
| 175 | +<!-- |
| 176 | +The output is similar to this: |
| 177 | +--> |
| 178 | +输出类似下面这样: |
| 179 | + |
| 180 | +``` |
| 181 | +deployment.apps/nginx-deployment created |
| 182 | +``` |
| 183 | + |
| 184 | +<!-- |
| 185 | +### Required fields |
| 186 | +
|
| 187 | +In the `.yaml` file for the Kubernetes object you want to create, you'll need to set values for the following fields: |
| 188 | +
|
| 189 | +* `apiVersion` - Which version of the Kubernetes API you're using to create this object |
| 190 | +* `kind` - What kind of object you want to create |
| 191 | +* `metadata` - Data that helps uniquely identify the object, including a `name` string, `UID`, and optional `namespace` |
| 192 | +* `spec` - What state you desire for the object |
| 193 | +--> |
| 194 | +### 必需字段 {#required-fields} |
| 195 | + |
| 196 | +在想要创建的 Kubernetes 对象所对应的 `.yaml` 文件中,需要配置的字段如下: |
| 197 | + |
| 198 | +* `apiVersion` - 创建该对象所使用的 Kubernetes API 的版本 |
| 199 | +* `kind` - 想要创建的对象的类别 |
| 200 | +* `metadata` - 帮助唯一标识对象的一些数据,包括一个 `name` 字符串、`UID` 和可选的 `namespace` |
| 201 | +* `spec` - 你所期望的该对象的状态 |
| 202 | + |
| 203 | +<!-- |
| 204 | +The precise format of the object `spec` is different for every Kubernetes object, and contains |
| 205 | +nested fields specific to that object. The [Kubernetes API Reference](/docs/reference/kubernetes-api/) |
| 206 | +can help you find the spec format for all of the objects you can create using Kubernetes. |
| 207 | +--> |
| 208 | +对每个 Kubernetes 对象而言,其 `spec` 之精确格式都是不同的,包含了特定于该对象的嵌套字段。 |
| 209 | +[Kubernetes API 参考](/zh-cn/docs/reference/kubernetes-api/)可以帮助你找到想要使用 |
| 210 | +Kubernetes 创建的所有对象的规约格式。 |
| 211 | + |
| 212 | +<!-- |
| 213 | +For example, see the [`spec` field](/docs/reference/kubernetes-api/workload-resources/pod-v1/#PodSpec) |
| 214 | +for the Pod API reference. |
| 215 | +For each Pod, the `.spec` field specifies the pod and its desired state (such as the container image name for |
| 216 | +each container within that pod). |
| 217 | +Another example of an object specification is the |
| 218 | +[`spec` field](/docs/reference/kubernetes-api/workload-resources/stateful-set-v1/#StatefulSetSpec) |
| 219 | +for the StatefulSet API. For StatefulSet, the `.spec` field specifies the StatefulSet and |
| 220 | +its desired state. |
| 221 | +Within the `.spec` of a StatefulSet is a [template](/docs/concepts/workloads/pods/#pod-templates) |
| 222 | +for Pod objects. That template describes Pods that the StatefulSet controller will create in order to |
| 223 | +satisfy the StatefulSet specification. |
| 224 | +Different kinds of object can also have different `.status`; again, the API reference pages |
| 225 | +detail the structure of that `.status` field, and its content for each different type of object. |
| 226 | +--> |
| 227 | +例如,参阅 Pod API 参考文档中 |
| 228 | +[`spec` 字段](/zh-cn/docs/reference/kubernetes-api/workload-resources/pod-v1/#PodSpec)。 |
| 229 | +对于每个 Pod,其 `.spec` 字段设置了 Pod 及其期望状态(例如 Pod 中每个容器的容器镜像名称)。 |
| 230 | +另一个对象规约的例子是 StatefulSet API 中的 |
| 231 | +[`spec` 字段](/zh-cn/docs/reference/kubernetes-api/workload-resources/stateful-set-v1/#StatefulSetSpec)。 |
| 232 | +对于 StatefulSet 而言,其 `.spec` 字段设置了 StatefulSet 及其期望状态。 |
| 233 | +在 StatefulSet 的 `.spec` 内,有一个为 Pod 对象提供的[模板](/zh-cn/docs/concepts/workloads/pods/#pod-templates)。 |
| 234 | +该模板描述了 StatefulSet 控制器为了满足 StatefulSet 规约而要创建的 Pod。 |
| 235 | +不同类型的对象可以有不同的 `.status` 信息。API 参考页面给出了 `.status` 字段的详细结构, |
| 236 | +以及针对不同类型 API 对象的具体内容。 |
| 237 | + |
| 238 | +## {{% heading "whatsnext" %}} |
| 239 | + |
| 240 | +<!-- |
| 241 | +If you're new to Kubernetes, read more about the following: |
| 242 | +
|
| 243 | +* [Pods](/docs/concepts/workloads/pods/) which are the most important basic Kubernetes objects. |
| 244 | +* [Deployment](/docs/concepts/workloads/controllers/deployment/) objects. |
| 245 | +* [Controllers](/docs/concepts/architecture/controller/) in Kubernetes. |
| 246 | +* [kubectl](/docs/reference/kubectl/) and [kubectl commands](/docs/reference/generated/kubectl/kubectl-commands). |
| 247 | +--> |
| 248 | +如果你刚开始学习 Kubernetes,可以进一步阅读以下信息: |
| 249 | + |
| 250 | +* 最重要的 Kubernetes 基本对象 [Pod](/zh-cn/docs/concepts/workloads/pods/)。 |
| 251 | +* [Deployment](/zh-cn/docs/concepts/workloads/controllers/deployment/) 对象。 |
| 252 | +* Kubernetes 中的[控制器](/zh-cn/docs/concepts/architecture/controller/)。 |
| 253 | +* [kubectl](/zh-cn/docs/reference/kubectl/) 和 |
| 254 | + [kubectl 命令](/docs/reference/generated/kubectl/kubectl-commands)。 |
| 255 | + |
| 256 | +<!-- |
| 257 | +To learn about the Kubernetes API in general, visit: |
| 258 | +
|
| 259 | +* [Kubernetes API overview](/docs/reference/using-api/) |
| 260 | +
|
| 261 | +To learn about objects in Kubernetes in more depth, read other pages in this section: |
| 262 | +--> |
| 263 | +从总体上了解 Kubernetes API,可以查阅: |
| 264 | + |
| 265 | +* [Kubernetes API 概述](/zh-cn/docs/reference/using-api/) |
| 266 | + |
| 267 | +若要更深入地了解 Kubernetes 对象,可以阅读本节的其他页面: |
| 268 | + |
| 269 | +<!-- Docsy automatically includes a list of pages in the section --> |
0 commit comments