DEV Community

Abhay Singh Kathayat
Abhay Singh Kathayat

Posted on

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

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
Enter fullscreen mode Exit fullscreen mode

Apply the file:

kubectl apply -f deployment.yaml
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Declarative Updates

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

Steps:

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

Direct Editing

You can also edit resources directly in the cluster:

kubectl edit deployment my-deployment
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Delete Resources by Name

You can delete specific resources by name using kubectl delete.

Example: Delete a Deployment

kubectl delete deployment my-deployment
Enter fullscreen mode Exit fullscreen mode

Delete All Resources of a Type

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

kubectl delete pods --all
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Watch Resource Events

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

Example: View Events

kubectl get events --watch
Enter fullscreen mode Exit fullscreen mode

Describe Resources for Details

The kubectl describe command gives detailed information about a resource.

Example: Describe a Pod

kubectl describe pod my-pod
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • Create resources in a specific namespace:
  kubectl apply -f deployment.yaml -n my-namespace
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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"}}}'
Enter fullscreen mode Exit fullscreen mode

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.


Top comments (0)