Managing Kubernetes Resources: A Comprehensive Guide to Create, Update, Delete, and Watch Operations

2 min read

Managing Kubernetes Resources (Create, Update, Delete, Watch)

Kubernetes provides a rich API that allows developers and administrators to interact with and manage cluster resources such as Pods, Services, Deployments, ConfigMaps, and more. Managing Kubernetes resources involves actions like creating, updating, deleting, and monitoring these resources.

This article explores the core operations for managing Kubernetes resources using the kubectl CLI and the Kubernetes API.

Core Kubernetes Resource Management Actions

  1. Create: Add new resources to the cluster.
  2. Update: Modify existing resources in the cluster.
  3. Delete: Remove resources from the cluster.
  4. Watch: Monitor changes to resources in real-time.

1. Creating Kubernetes Resources

Resources in Kubernetes can be created either declaratively (using YAML/JSON files) or imperatively (direct commands).

Declarative Creation

Declarative resource management involves defining the desired state of a resource in a YAML/JSON file and applying it to the cluster using the kubectl apply command.

Example: Create a Deployment

# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
        - name: nginx
          image: nginx:1.21
          ports:
            - containerPort: 80

Apply the file:

kubectl apply -f deployment.yaml

Imperative Creation

Imperative resource management allows you to create resources directly from the command line.

Example: Create a Pod Imperatively

kubectl run my-pod --image=nginx:1.21 --port=80

2. Updating Kubernetes Resources

You can update resources using both imperative and declarative approaches.

Imperative Updates

Imperative updates modify resource fields using commands.

Example: Scale a Deployment

kubectl scale deployment my-deployment --replicas=5

Declarative Updates

Declarative updates involve modifying the YAML/JSON definition and reapplying it.

Steps:

  1. Edit the deployment.yaml file:
   spec:
     replicas: 5
  1. Apply the updated file:
   kubectl apply -f deployment.yaml

Direct Editing

You can also edit resources directly in the cluster:

kubectl edit deployment my-deployment

This command opens the resource definition in your default text editor, where you can make changes.

3. Deleting Kubernetes Resources

Kubernetes allows you to delete resources both declaratively and imperatively.

Delete Using YAML File

To delete resources managed by a YAML file:

kubectl delete -f deployment.yaml

Delete Resources by Name

You can delete specific resources by name using kubectl delete.

Example: Delete a Deployment

kubectl delete deployment my-deployment

Delete All Resources of a Type

You can delete all resources of a specific type in a namespace:

kubectl delete pods --all

Force Delete Stuck Resources

If a resource is stuck in a terminating state, you can force delete it:

kubectl delete pod my-pod --grace-period=0 --force

4. Watching Kubernetes Resources

Kubernetes supports watching resources to monitor real-time changes. This is useful for debugging or understanding how resources behave.

Watch Using kubectl

The --watch flag allows you to monitor changes to a resource.

Example: Watch Pods in Real-Time

kubectl get pods --watch

Watch Resource Events

Kubernetes events provide details about what’s happening in the cluster.

Example: View Events

kubectl get events --watch

Describe Resources for Details

The kubectl describe command gives detailed information about a resource.

Example: Describe a Pod

kubectl describe pod my-pod

Namespace Management

Namespaces are used to organize resources in a cluster. Most kubectl commands allow specifying a namespace using the -n flag.

Examples:

  • List all resources in a namespace:
  kubectl get all -n my-namespace
  • Create resources in a specific namespace:
  kubectl apply -f deployment.yaml -n my-namespace

Advanced Management Techniques

Dry-Run Mode

The --dry-run flag simulates the creation, update, or deletion of a resource without making actual changes.

Example: Test Resource Creation

kubectl apply -f deployment.yaml --dry-run=client

Export Resource YAML

You can export the definition of an existing resource for backup or modification.

Example: Export Deployment YAML

kubectl get deployment my-deployment -o yaml > deployment-export.yaml

Patch Resources

Patching modifies specific fields in a resource without replacing the entire object.

Example: Add an Annotation

kubectl patch deployment my-deployment -p '{"metadata": {"annotations": {"key": "value"}}}'

Best Practices for Resource Management

  1. Use Declarative Configuration: Store all resource definitions in version-controlled YAML files for consistency and traceability.
  2. Leverage Namespaces: Use namespaces to isolate environments (e.g., dev, staging, production).
  3. Monitor Resources: Regularly use kubectl get, kubectl describe, and kubectl logs to monitor the health of resources.
  4. Automate Resource Management: Use CI/CD pipelines to automate the deployment and update of Kubernetes resources.

Conclusion

Managing Kubernetes resources efficiently requires understanding the key operations: create, update, delete, and watch. Kubernetes provides powerful tools like kubectl and declarative configuration to simplify these tasks. By mastering these core operations, you can ensure your applications and infrastructure run smoothly in a Kubernetes environment.