Objects, YAML, and apply
Read a Deployment file by tracing where each setting goes and what changes after you apply it.
What you will understand
- Recognize an object, its manifest, and the fields Kubernetes fills in.
- Follow YAML indentation to distinguish a Deployment from its Pod template.
- Preview a change and compare the requested result with the reported result.
Builds on: From kubectl to a running Pod
What gets created when you apply YAML
You have a web app and want Kubernetes to keep one copy running. You write a short file describing the app and run kubectl apply. When someone asks what you deployed, you can point to two different things: the file you wrote and the Deployment that now exists in the cluster.
A Kubernetes object is a record the Kubernetes API manages. A Deployment is one type of object. A manifest is the description you submit, usually as a file written in YAML. kubectl reads the file and sends an API request. The server uses apiVersion and kind to identify the shape of the object, checks the fields, adds defaults where appropriate, and stores the accepted result. The Deployment controller then reads that result and does further work. The order of lines in the YAML describes a structure; it does not tell the controller which action to run first.
Metadata is information about the object, such as its name, namespace, and labels. Think of web in namespace test and web in namespace live. Each identifies a different object of the same type. Within a namespace, delete and recreate web and its name can stay the same. The server assigns the replacement a new UID, which identifies that particular object’s lifetime. An owner reference uses the UID so a brand-new object does not inherit dependents just because it has the same name.
How does the YAML map to a real object?
The outer fields describe the Deployment, and the template describes the Pods it will create. The API adds identity and status fields.
Choose a resource type
- YAML documentapps/v1 Deploymentdeclares type
- API schemaapps/v1 Deploymentvalidates shape
apiVersion apps/v1 and kind Deployment select a specific resource schema. YAML is one serialization of that object.
Find both sets of metadata
- Deployment metadataDeployment specsame object
- Deployment specPod template metadatanested recipe
Deployment metadata identifies the parent. template.metadata describes labels of child Pods, not the parent’s existing metadata.
Read identity and status
- Name + namespace + UID
- spec.replicas: 2status.availableReplicas: 1eventual observation
The API supplies a UID and metadata. Controllers report status; the authored spec remains the desired input.
A Deployment is shown; not every Kubernetes kind has both spec and status.
Explore another case
Read the Deployment and its Pod template
YAML represents keys and values, nested groups, and lists. Indentation tells you where a field belongs. A line starting with a dash adds an item to a list. In the example, containers lists one entry named web. The Deployment has its own name, object-demo, several levels above it. Read upward from a line until you reach its enclosing key when you are unsure which object or field it describes.
apiVersion: apps/v1 # API group and version for Deployments.
kind: Deployment
metadata:
name: object-demo
labels:
app: object-demo # Label on the Deployment object itself.
spec:
replicas: 1 # Integer, not the string "1".
selector:
matchLabels:
app: object-demo
template:
metadata:
labels:
app: object-demo # Separate labels for created Pods.
spec:
containers: # A sequence; here it has one container item.
- name: web
image: nginx:1.27.5Here spec holds the configuration the controller should work toward. replicas: 1 is a number. Quoting it as "1" makes it a string, which does not fit that field. The Deployment’s outer labels describe the Deployment; the labels under template.metadata describe the Pods it creates. The Deployment selector is a rule for identifying the Pods and must agree with the template labels.
In a disposable namespace, save the file as object-demo.yaml. explain shows the API’s field definitions, server dry-run asks the server to check the proposed write without saving it, and diff shows the proposed changes. A diff with differences exits with code 1, so read the output before treating it as a failure.
# Discover field structure from the API schema.
kubectl explain deployment.spec.template.spec.containers
# Ask the server to validate/admit without persisting the result.
kubectl apply --dry-run=server -f object-demo.yaml
# A diff returns a nonzero status when differences exist; read the output.
kubectl diff -f object-demo.yaml
kubectl apply -f object-demo.yaml
kubectl get deployment object-demo -o yamlWhat changes after you apply new YAML?
A preview describes a possible write. An apply stores the new desired count, then controllers work toward it.
- kubectlPreview the replica changeAPI serverDry run checks the proposal without storing it.
- kubectlApply replicas: 2DeploymentThe new desired replica count is saved.
- Deployment and ReplicaSet controllersAdd capacityNew PodReported availability increases after the new Pod starts and becomes ready.
A preview describes a possible write. An apply stores the new desired count, then controllers work toward it.
Default client-side apply is used; advanced managedFields conflict resolution is deferred.
Explore another case
Ask for another replica and check the result
Change replicas to 2 and apply it. Look at spec.replicas and status.availableReplicas in the retrieved YAML. The first states what you asked for. The second reports what is available so far. The second Pod still needs a node, a running container, and time to become available. Some object types use different fields. For example, a ConfigMap holds its settings in data rather than a workload-style spec.
What to keep in the manifest
Plain kubectl apply computes changes using the previous apply configuration. The server-side form tracks which field managers own values and can report conflicts. A separate operation such as kubectl scale can set a value that a later apply changes again. Keep the source file focused on your input fields. Live status and server-assigned UIDs belong to the current cluster object. Try these differences in the module lab.
Check your understanding
Your file says two replicas and the Deployment status says one available. What should you inspect next?
Read both fields as written. Two is the accepted target and one is the observation. List the Pods and inspect the new member’s events or conditions to see whether it is waiting for placement, startup, or readiness.
Primary documentation
Go to the source
Reviewed 2026-10-27 · Kubernetes 1.37 · Related local exercise