Saiyam Pathak avatar
By Saiyam Pathak
Field CTO


Dive into the world of Kubernetes and understand the importance of Kubernetes objects. Learn how to create and manage these objects to effectively manage your workloads.

Kubernetes, a powerful container orchestration platform, is known for its ability to manage and scale applications efficiently. But what makes Kubernetes so efficient? The answer lies in Kubernetes objects. These are the fundamental entities that Kubernetes uses to represent the state of your cluster. This lesson will delve into the importance of Kubernetes objects and how to create and manage them.

The Importance of Kubernetes Objects

In the Kubernetes ecosystem, everything happens through Kubernetes objects. Whether you're deploying a new application or scaling an existing one, you're interacting with Kubernetes objects. These objects represent the desired state of your cluster, and Kubernetes works tirelessly to ensure that the actual state matches this desired state.

Creating Kubernetes Objects: Two Approaches

There are two primary ways to create Kubernetes objects: the imperative way and the declarative way.

The imperative approach involves directly interacting with the cluster to create objects. For instance, you can create a deployment object named `nginx` using the `nginx` image and open port 80 with the following command:

bash kubectl create deploy nginx --image=nginx --port=80

The declarative approach, on the other hand, involves defining the desired state of the object in a YAML or JSON file and then using `kubectl apply` to create the object. Here's how you can create a deployment using a YAML file:

First, generate the YAML file for the deployment:

bash kubectl create deploy nginx --image=nginx --port=80 --dry-run=client -oyaml > deploy.yaml

Then, apply the YAML file to create the deployment:

bash kubectl apply -f deploy.yaml

Structure of a Kubernetes Object

Let's take a closer look at the structure of a Kubernetes object. A typical object consists of four main sections:

  1. API Version: This field specifies the version of the Kubernetes API that you're using to create the object.
  2. Kind: This field specifies the type of object you want to create.
  3. Metadata: This section includes data that helps uniquely identify the object, such as its name and labels.
  4. Spec: This section defines the desired state of the object.

For example, if you're creating a Deployment object, the `kind` field would be `Deployment`, and the `spec` section would define the desired state of the Deployment, such as the number of replicas and the container image to use.

Interacting with Kubernetes Objects

All interactions with Kubernetes objects happen via the `kubectl` command-line tool. For example, you can use `kubectl get deploy` to view the current state of a deployment. To delete a deployment, you can use the following command:

bash kubectl delete deploy nginx

When you apply a YAML file to create an object, `kubectl` converts the YAML to JSON and sends it to the Kubernetes API server. The API server then deploys the object and stores its state in the etcd database.

Viewing API Resources

To view all the API resources that you can create in Kubernetes, you can use the `kubectl api-resources` command. This command lists all the available resources, along with their short names and API versions.

Additionally, you can view all the namespaces in your Kubernetes cluster using the `kubectl get namespace` or the short form `kubectl get ns` command. Namespaces are a way to divide cluster resources between multiple users or teams.

Here are the commands:

bash kubectl api-resources kubectl get namespace kubectl get ns

These commands provide a comprehensive view of the resources and namespaces in your Kubernetes cluster, helping you manage your workloads effectively.

In conclusion, Kubernetes objects are the building blocks of your Kubernetes cluster. Understanding these objects and how to interact with them is crucial for managing your workloads in Kubernetes.

Don't stop now, check out your next lesson