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 be used to represent a wide range of different resources, such as pods, services, deployments, and so on. Kubernetes objects are defined using the YAML or JSON format.
Also, every object can be controlled by the Kubernetes API. This means that you can create, update, delete, and query objects using the RESTful Kubernetes API. The Kubernetes API is exposed by the kube-apiserver component of the Kubernetes control plane.
The request to fetch information about a pod object might look like this:
GET https://<kubernetes-cluster>.com/api/v1/namespaces/<namespace>/pods/<pod-name>
Each Kubernetes object is defined in a config file with 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:
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: nginx
image: nginx:1.14.2 # use specific version, not latest
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 above described object 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.