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
kubectl
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
kubectl apply
kubectl
command for creating or updating Kubernetes objects?kubectl apply
To use
kubectl apply
my-app.yaml
kubectl apply -f my-app.yaml
If the object defined in
my-app.yaml
kubectl apply
kubectl apply
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
kubectl delete
apply
To delete an object defined in a file:
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':
kubectl delete pod my-pod
Example: Deleting all Pods with the label 'app=frontend':
kubectl delete pods -l app=frontend
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
The official reference for the `kubectl apply` command, detailing its syntax, flags, and usage patterns.
The official reference for the `kubectl delete` command, covering how to remove resources from the cluster.
An overview of Kubernetes objects and how to manage them, including declarative configuration.
Explains the lifecycle of Pods, including how they are created, updated, and terminated, which is relevant to deletion.
A foundational tutorial that introduces `kubectl` and basic cluster operations, including applying configurations.
A practical guide on creating and using YAML manifest files for Kubernetes objects, essential for `kubectl apply`.
Discusses the principles behind managing Kubernetes objects and the importance of declarative approaches.
A comprehensive cheat sheet for `kubectl` commands, including `apply` and `delete` with common examples.
A community discussion explaining the differences and best practices between `kubectl apply` and `kubectl create`.
A video tutorial covering essential `kubectl` commands, including how to apply and delete resources.