Course contentsShow
← Docker, Kubernetes & CI/CD
Lesson 248 of 8024. Kubernetes Architecture and Core ObjectsFree lesson

Anatomy of a Kubernetes Object Definition

You will learn to structure YAML manifests by identifying the required fields including apiVersion, kind, metadata, and the desired spec configuration.

In this lesson, you will learn how to structure a Kubernetes object definition by identifying the four essential fields required in every YAML manifest.

In Kubernetes, we use YAML files to describe the "desired state" of our infrastructure. Think of an object definition as a formal contract you submit to the API server. While the configuration can vary depending on what you are building (like a Pod or a Deployment), every object must contain four top-level fields to be valid.

The Four Pillars of a Manifest

  1. apiVersion: This tells Kubernetes which version of the API you are using to create this object (e.g., v1 for Pods or apps/v1 for Deployments).
  2. kind: This identifies the type of object you want to create. It defines whether you are defining a Pod, Service, Namespace, or Deployment.
  3. metadata: This is the "ID card" for your object. It includes fields like name and labels, which help the system and you track and organize the resource.
  4. spec: This is the heart of the definition. Here, you describe the "desired state." If you are creating a Pod, you define the container images, ports, and volumes here.

Example: A Simple Pod Definition

apiVersion: v1
kind: Pod
metadata:
  name: my-web-server
spec:
  containers:
  - name: nginx
    image: nginx:latest

When you pass this file to kubectl, the API server validates these four sections. It checks the apiVersion and kind to route the request, reads the metadata to register the resource, and then hands the spec to the Scheduler and Controller Managers to begin the work of reconciliation. If any of these top-level fields are missing or misspelled, Kubernetes will reject the file because it cannot categorize or act upon your request. By mastering these four building blocks, you are providing the API server with all the instructions it needs to maintain your desired state in the cluster.

Key Takeaway: Every Kubernetes object definition relies on a standard structure consisting of apiVersion, kind, metadata, and spec to ensure the API server can correctly parse and enforce your desired configuration.