Introduction
You have already learned about the core components of a Kubernetes cluster. In this lesson, you will learn about Kubernetes objects and how they work together.
What are Kubernetes Objects?
Kubernetes objects are entities which are persisted inside the Kubernetes cluster. More precisely, they are persisted inside the etcd key-value store. Kubernetes objects can represent a wide range of different resources, such as Pods, Services, Deployments, etc.
These objects are defined using the YAML or JSON format and can be managed using the RESTful Kubernetes API, which is exposed by the kube-apiserver component of the Kubernetes control plane. This means that you can create, update, delete, and query objects using the API.
Here's an example request to fetch information about a Pod object:
1GET https://<kubernetes-cluster>.com/api/v1/namespaces/<namespace>/pods/<pod-name>
Each Kubernetes object is defined in a config file in the YAML or JSON format. The config file contains the specification of the object in a declarative way. This enables you to define the desired state of the object. Kubernetes will then permanently check if the actual state of the object matches the desired state. If not, Kubernetes will try to make the actual state match the desired state. This is called the reconciliation loop.
A simple Kubernetes Pod object is declared as follows:
12345678apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: nginx:1.21.3
Let's break down the different parts of the Pod object:
- The apiVersion field specifies the version of the Kubernetes API which is used to define the object. Each object can have different API versions.
- The kind field specifies the type of the Kubernetes object.
- The metadata field contains the metadata of the object.
- The spec field contains the specification of the object. The spec field is different for each object type. For example, the spec field of a Pod object contains the specification of the containers that are part of the Pod.
These are the mandatory fields of a Kubernetes object. However, you can also add additional fields to the object.
The object described above will create a Pod with the name my-pod which contains a single container with the name my-container. The container will use the nginx image in version 1.21.3.