LibraryApplying and Deleting Kubernetes Objects

Applying and Deleting Kubernetes Objects

Learn about Applying and Deleting Kubernetes Objects as part of Docker and Kubernetes DevOps

Applying and Deleting Kubernetes Objects

In Kubernetes, you interact with the cluster by creating, updating, and deleting objects. These objects represent the desired state of your applications and infrastructure. The primary tool for this interaction is

code
kubectl
, the Kubernetes command-line interface.

Understanding Kubernetes Objects

Kubernetes objects are persistent entities in the Kubernetes system. They represent the state of your cluster. Common objects include Pods, Deployments, Services, and ConfigMaps. Each object has a specific schema defined by its kind, API version, and metadata.

Kubernetes objects define the desired state of your cluster.

You declare the desired state of your applications and infrastructure using YAML or JSON files. Kubernetes then works to achieve and maintain this state.

Kubernetes operates on a declarative model. Instead of telling Kubernetes how to do something, you tell it what you want. This is achieved by defining objects in configuration files, typically written in YAML. These files describe the desired state, such as the number of replicas for an application, the container image to use, or the network ports to expose. The Kubernetes control plane continuously monitors the cluster's actual state and compares it to the desired state, making adjustments as needed.

Applying Kubernetes Objects with `kubectl apply`

The

code
kubectl apply
command is used to create or update objects in your Kubernetes cluster based on configuration files. It's the recommended way to manage objects because it handles updates intelligently, only changing what's necessary.

What is the primary kubectl command for creating or updating Kubernetes objects?

kubectl apply

To use

code
kubectl apply
, you need a configuration file (e.g.,
code
my-app.yaml
). The command would look like this:

bash
kubectl apply -f my-app.yaml

If the object defined in

code
my-app.yaml
does not exist,
code
kubectl apply
will create it. If it already exists,
code
kubectl apply
will update it to match the configuration in the file. This is known as a declarative update.

Using kubectl apply ensures that Kubernetes manages the changes, making it easier to track and revert if necessary.

Deleting Kubernetes Objects with `kubectl delete`

To remove objects from your Kubernetes cluster, you use the

code
kubectl delete
command. Like
code
apply
, it can target objects by file or by label selector.

To delete an object defined in a file:

bash
kubectl delete -f my-app.yaml

You can also delete objects by their type and name, or by using label selectors.

Example: Deleting a Pod named 'my-pod':

bash
kubectl delete pod my-pod

Example: Deleting all Pods with the label 'app=frontend':

bash
kubectl delete pods -l app=frontend
What kubectl command is used to remove objects from a Kubernetes cluster?

kubectl delete

Understanding the Lifecycle

When you delete an object, Kubernetes initiates a graceful termination process. For example, when a Pod is deleted, Kubernetes sends a SIGTERM signal to the containers within the Pod, allowing them to shut down cleanly. After a grace period, if the containers haven't terminated, Kubernetes sends a SIGKILL signal.

The kubectl apply command uses a three-way merge strategy to update objects. It compares the live configuration in the cluster, the configuration in your local file, and the last applied configuration stored in the cluster's annotations. This allows for efficient and safe updates, minimizing conflicts and ensuring that only intended changes are made.

📚

Text-based content

Library pages focus on text content

Understanding how to apply and delete objects is fundamental to managing applications in Kubernetes. It allows you to control the desired state of your workloads and infrastructure.

Learning Resources

Kubernetes Official Documentation: kubectl apply(documentation)

The official reference for the `kubectl apply` command, detailing its syntax, flags, and usage patterns.

Kubernetes Official Documentation: kubectl delete(documentation)

The official reference for the `kubectl delete` command, covering how to remove resources from the cluster.

Kubernetes Official Documentation: Managing Resources(documentation)

An overview of Kubernetes objects and how to manage them, including declarative configuration.

Kubernetes Official Documentation: Object Lifecycle(documentation)

Explains the lifecycle of Pods, including how they are created, updated, and terminated, which is relevant to deletion.

Kubernetes Tutorial: kubectl Basics(tutorial)

A foundational tutorial that introduces `kubectl` and basic cluster operations, including applying configurations.

DigitalOcean: How To Use Kubernetes Manifest Files(blog)

A practical guide on creating and using YAML manifest files for Kubernetes objects, essential for `kubectl apply`.

Red Hat Developer: Understanding Kubernetes Object Management(blog)

Discusses the principles behind managing Kubernetes objects and the importance of declarative approaches.

Learnk8s: kubectl Cheat Sheet(documentation)

A comprehensive cheat sheet for `kubectl` commands, including `apply` and `delete` with common examples.

Stack Overflow: kubectl apply vs kubectl create(wikipedia)

A community discussion explaining the differences and best practices between `kubectl apply` and `kubectl create`.

YouTube: Kubernetes Tutorial for Beginners - kubectl(video)

A video tutorial covering essential `kubectl` commands, including how to apply and delete resources.