Using Imperative Commands For Managing Kubernetes Objects

Using Imperative Commands For Managing Kubernetes Objects

DevopsKubernetes

Kubernetes teams need rapid object management. They often choose kubectl imperative commands for direct control. This guide shows how to create, update, delete, and manage live Kubernetes objects on the fly. You learn syntax, patterns, workflow, and real command examples.


TL;DR

  • kubectl Imperative commands let you manage live objects with single-line commands.
  • Common commands: create, run, expose, scale, label, annotate, patch, delete.
  • Syntax: kubectl --options.
  • Imperative flow: user → API server → etcd → scheduler → kubelet.
  • Use cases: quick testing, demos, bootstrapping resources, CI scripts.
  • Prefer declarative YAML for production; use imperative for ad-hoc tasks.

Imperative Commands Basics

kubectl ships built-in commands for direct control of Kubernetes objects. You issue commands at shell prompt. kubectl translates them into API calls. API server writes state to etcd. Scheduler assigns pods to nodes. Kubelet manages workloads. Imperative commands skip writing YAML files. You get instant results. You can still view YAML later with kubectl get -o yaml.


Imperative Commands Syntax

All commands share a common syntax pattern:

Key parts:

  • <command>: create, run, expose, label, annotate, patch, scale, delete.
  • <resource-type>: pods, deployments, services, configmaps, secrets, namespaces.
  • <name>: arbitrary string. Lowercase. No spaces.
  • [flags]: options controlling image, ports, labels, selectors, replicas.

Example:

This command calls POST /apis/apps/v1/namespaces/default/deployments.


Imperative Commands for Creation

Imperative creation helps in rapid prototyping. You configure objects in one line. kubectl supports multiple resource types.

Deployment

This creates a Deployment with two replicas.

Pod

Here --restart=Never sets a Pod instead of Deployment.

Service

The command maps Deployment pods to a ClusterIP Service.

ConfigMap

This embeds key1 and file content into a ConfigMap.


Imperative Commands for Updates

Imperative updates let you tweak live objects without full YAML. Use scale, label, annotate, patch, and rollout.

Scale

Adjusts replicas in one call.

Label

Adds or updates labels on existing Pods.

Annotate

Stores metadata on Service objects.

Patch

Applies JSON Merge Patch on the Deployment spec.

Rollout Restart

Triggers a rolling restart by updating pod template hash.


Imperative Commands for Deletion

Remove objects with delete commands. This calls the DELETE method on resources.

You can delete by label selector:

This deletes all Pods with the label env=dev.


Use Cases and Patterns

Use imperative commands when you need fast feedback. Ideal for:

  • Local demos and workshops.
  • CI/CD pipelines that bootstrap test clusters.
  • Debug scripts requiring quick updates.
  • Edge cases where small patches suffice.
  • Learning and experimentation in training labs.

Reserve declarative YAML for production workloads. Store versioned manifests in Git. Use kubectl apply for drift control.


References

Suggested Reading

PostHashID: 740d856262845f61b8656b7cd39e5a9cd53ee01056be899c6b0ccf9c4a4c709d

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.