Basics of YAML in Kubernetes

Maciej
2 min readJul 29, 2021

Record your intent with a Kubernetes object

Kubernetes objects are persistent entity 1 on Kubernetes . Kubernetes uses these entities to represent the state of the cluster.

The following contents can be expressed:

  • What containerized applications are running (and on which nodes they are running)
  • Resources available from those applications
  • Policies on how the application behaves, such as restarts, upgrades, fault tolerance policies, etc.
  • Once you create an object, Kubernetes always keeps it in existence.

Manipulating Kubernetes objects using the Kubernetes API

When working with Kubernetes objects, you must use the Kubernetes API regardless of whether you create, modify, or delete them.

kubectl command line interface

If you use the kubectl command line interface, this CLI will issue the Kubernetes API instructions needed for processing on your behalf.

You can also use the client library from your program and use the Kubernetes API directly.

Object spec and object status

Most Kubernetes objects have two nested object fields that manage the object’s settings.

  • spec: The spec should describe the features that you want the object to have as desired.
  • status: It indicates the current state of the object, and that information is provided and updated by the Kubernetes system and its components. The Kubernetes control plane always and proactively manages the current state to match the desired state you specify.

How to write a Kubernetes object ???

Required fields:

  • apiVersion: Which version of Kubernetes API to use to create an object.
  • kind: What kind of object to create.
  • metadata: Information for uniquely identifying an object, string name, UID, and any namespace are applicable.
  • spec: Desirable state of object.

When you create an object in Kubernetes, you must pass the object’s spec that describes the desired state, along with the basic information about the object (for example, its name).

When creating an object using the Kubernetes API (whether you call the API directly or use kubectl), the API request must include that information in the Body part of the request in JSON format.

The exact format of the spec is different for each Kubernetes object and each object has its own nested fields. The Kubernetes API Reference helps you find the spec format for every object you can create with Kubernetes. For example, see PodSpec v1 core for the format of specs for pod objects and DeploymentSpec v1 apps for the format of specs for Deployment objects.

More info we can find in official documentation.

--

--

Maciej

DevOps Consultant. I’m strongly focused on automation, security, and reliability.