|
1 | 1 | ---
|
2 |
| -title: "Working with Kubernetes Objects" |
3 |
| -weight: 40 |
| 2 | +title: Understanding Kubernetes Objects |
| 3 | +content_type: concept |
| 4 | +weight: 10 |
4 | 5 | description: >
|
5 |
| - Kubernetes objects are persistent entities in the Kubernetes system. Kubernetes uses these entities to represent the state of your cluster. |
| 6 | + Kubernetes objects are persistent entities in the Kubernetes system. |
| 7 | + Kubernetes uses these entities to represent the state of your cluster. |
6 | 8 | Learn about the Kubernetes object model and how to work with these objects.
|
| 9 | +simple_list: true |
| 10 | +card: |
| 11 | + name: concepts |
| 12 | + weight: 40 |
7 | 13 | ---
|
| 14 | + |
| 15 | +
|
| 16 | + |
| 17 | +This page explains how Kubernetes objects are represented in the Kubernetes API, and how you can |
| 18 | +express them in `.yaml` format. |
| 19 | + |
| 20 | +
|
| 21 | + |
| 22 | +## Objects in Kubernetes {#kubernetes-objects} |
| 23 | + |
| 24 | +*Kubernetes objects* are persistent entities in the Kubernetes system. Kubernetes uses these |
| 25 | +entities to represent the state of your cluster. Specifically, they can describe: |
| 26 | + |
| 27 | +* What containerized applications are running (and on which nodes) |
| 28 | +* The resources available to those applications |
| 29 | +* The policies around how those applications behave, such as restart policies, upgrades, and fault-tolerance |
| 30 | + |
| 31 | +A Kubernetes object is a "record of intent"--once you create the object, the Kubernetes system |
| 32 | +will constantly work to ensure that object exists. By creating an object, you're effectively |
| 33 | +telling the Kubernetes system what you want your cluster's workload to look like; this is your |
| 34 | +cluster's *desired state*. |
| 35 | + |
| 36 | +To work with Kubernetes objects--whether to create, modify, or delete them--you'll need to use the |
| 37 | +[Kubernetes API](/docs/concepts/overview/kubernetes-api/). When you use the `kubectl` command-line |
| 38 | +interface, for example, the CLI makes the necessary Kubernetes API calls for you. You can also use |
| 39 | +the Kubernetes API directly in your own programs using one of the |
| 40 | +[Client Libraries](/docs/reference/using-api/client-libraries/). |
| 41 | + |
| 42 | +### Object spec and status |
| 43 | + |
| 44 | +Almost every Kubernetes object includes two nested object fields that govern |
| 45 | +the object's configuration: the object *`spec`* and the object *`status`*. |
| 46 | +For objects that have a `spec`, you have to set this when you create the object, |
| 47 | +providing a description of the characteristics you want the resource to have: |
| 48 | +its _desired state_. |
| 49 | + |
| 50 | +The `status` describes the _current state_ of the object, supplied and updated |
| 51 | +by the Kubernetes system and its components. The Kubernetes |
| 52 | +{{< glossary_tooltip text="control plane" term_id="control-plane" >}} continually |
| 53 | +and actively manages every object's actual state to match the desired state you |
| 54 | +supplied. |
| 55 | + |
| 56 | +For example: in Kubernetes, a Deployment is an object that can represent an |
| 57 | +application running on your cluster. When you create the Deployment, you |
| 58 | +might set the Deployment `spec` to specify that you want three replicas of |
| 59 | +the application to be running. The Kubernetes system reads the Deployment |
| 60 | +spec and starts three instances of your desired application--updating |
| 61 | +the status to match your spec. If any of those instances should fail |
| 62 | +(a status change), the Kubernetes system responds to the difference |
| 63 | +between spec and status by making a correction--in this case, starting |
| 64 | +a replacement instance. |
| 65 | + |
| 66 | +For more information on the object spec, status, and metadata, see the |
| 67 | +[Kubernetes API Conventions](https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md). |
| 68 | + |
| 69 | +### Describing a Kubernetes object |
| 70 | + |
| 71 | +When you create an object in Kubernetes, you must provide the object spec that describes its |
| 72 | +desired state, as well as some basic information about the object (such as a name). When you use |
| 73 | +the Kubernetes API to create the object (either directly or via `kubectl`), that API request must |
| 74 | +include that information as JSON in the request body. **Most often, you provide the information to |
| 75 | +`kubectl` in a .yaml file.** `kubectl` converts the information to JSON when making the API |
| 76 | +request. |
| 77 | + |
| 78 | +Here's an example `.yaml` file that shows the required fields and object spec for a Kubernetes Deployment: |
| 79 | + |
| 80 | +{{< codenew file="application/deployment.yaml" >}} |
| 81 | + |
| 82 | +One way to create a Deployment using a `.yaml` file like the one above is to use the |
| 83 | +[`kubectl apply`](/docs/reference/generated/kubectl/kubectl-commands#apply) command |
| 84 | +in the `kubectl` command-line interface, passing the `.yaml` file as an argument. Here's an example: |
| 85 | + |
| 86 | +```shell |
| 87 | +kubectl apply -f https://k8s.io/examples/application/deployment.yaml |
| 88 | +``` |
| 89 | + |
| 90 | +The output is similar to this: |
| 91 | + |
| 92 | +``` |
| 93 | +deployment.apps/nginx-deployment created |
| 94 | +``` |
| 95 | + |
| 96 | +### Required fields |
| 97 | + |
| 98 | +In the `.yaml` file for the Kubernetes object you want to create, you'll need to set values for the following fields: |
| 99 | + |
| 100 | +* `apiVersion` - Which version of the Kubernetes API you're using to create this object |
| 101 | +* `kind` - What kind of object you want to create |
| 102 | +* `metadata` - Data that helps uniquely identify the object, including a `name` string, `UID`, and optional `namespace` |
| 103 | +* `spec` - What state you desire for the object |
| 104 | + |
| 105 | +The precise format of the object `spec` is different for every Kubernetes object, and contains |
| 106 | +nested fields specific to that object. The [Kubernetes API Reference](/docs/reference/kubernetes-api/) |
| 107 | +can help you find the spec format for all of the objects you can create using Kubernetes. |
| 108 | + |
| 109 | +For example, see the [`spec` field](/docs/reference/kubernetes-api/workload-resources/pod-v1/#PodSpec) |
| 110 | +for the Pod API reference. |
| 111 | +For each Pod, the `.spec` field specifies the pod and its desired state (such as the container image name for |
| 112 | +each container within that pod). |
| 113 | +Another example of an object specification is the |
| 114 | +[`spec` field](/docs/reference/kubernetes-api/workload-resources/stateful-set-v1/#StatefulSetSpec) |
| 115 | +for the StatefulSet API. For StatefulSet, the `.spec` field specifies the StatefulSet and |
| 116 | +its desired state. |
| 117 | +Within the `.spec` of a StatefulSet is a [template](/docs/concepts/workloads/pods/#pod-templates) |
| 118 | +for Pod objects. That template describes Pods that the StatefulSet controller will create in order to |
| 119 | +satisfy the StatefulSet specification. |
| 120 | +Different kinds of object can also have different `.status`; again, the API reference pages |
| 121 | +detail the structure of that `.status` field, and its content for each different type of object. |
| 122 | + |
| 123 | +## {{% heading "whatsnext" %}} |
| 124 | + |
| 125 | +If you're new to Kubernetes, read more about the following: |
| 126 | + |
| 127 | +* [Pods](/docs/concepts/workloads/pods/) which are the most important basic Kubernetes objects. |
| 128 | +* [Deployment](/docs/concepts/workloads/controllers/deployment/) objects. |
| 129 | +* [Controllers](/docs/concepts/architecture/controller/) in Kubernetes. |
| 130 | +* [kubectl](/docs/reference/kubectl/) and [kubectl commands](/docs/reference/generated/kubectl/kubectl-commands). |
| 131 | + |
| 132 | +To learn about the Kubernetes API in general, visit: |
| 133 | + |
| 134 | +* [Kubernetes API overview](/docs/reference/using-api/) |
| 135 | + |
| 136 | +To learn about objects in Kubernetes in more depth, read other pages in this section: |
| 137 | +
|
0 commit comments