Understanding the kubectl Command Hierarchy
The primary interface for interacting with a Kubernetes control plane is the kubectl binary. Running kubectl --help reveals a logically grouped set of verbs designed to streamline cluster operations. These groups include foundational resource creation, intermediate inspection and modification, deployment lifecycle management, cluster administration, troubleshooting utilities, and advanced configuration tools like apply and patch. The syntax follows a predictable pattern:
kubectl [global-flags] <command> [resource-type] [resource-name] [options]
Appending --help to any subcommand provides granular documentation to its specific flags and usage constraints.
Deploying a Stateless Workload
To instantiate a containerized application, use the deployment generator. The following commend initializes a single-replica Deployment managing Nginx containers:
kubectl create deployment web-tier-frontend --image=nginx:1.22-alpine --port=80 --replicas=1
Verify the controller's scheduling status by querying the API server:
kubectl get deployments
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
web-tier-frontend 1 1 1 1 15s
Tracking the underlying pods with verbose output reveals network assignments and node placement:
kubectl get pods -o wide
Exposing Workloads to External Traffic
Cluster-internal pod IPs are ephemeral and unreachable from outside the cluster boundary. To enable external connectivity, generate a Service object that routes traffic to the Deployment's backing pods:
kubectl expose deployment web-tier-frontend --type=NodePort --name=frontend-access --port=80
Retrieve the allocated high-numbered port and service configuration:
kubectl get svc -o wide
With a NodePort service type, the application becomes reachable via <Any-Worker-Node-IP>:<Assigned-NodePort>. This exposes the workload on a consistent port across all cluster nodes.
Horizontal Scaling Operations
Adjusting the desired replica count triggers the Deployment controller to reconcile the state. To expand the fleet from one to four pods:
kubectl scale --replicas=4 deployment/web-tier-frontend
Monitor the provisioning lifecycle in real time by appending the watch flag:
kubectl get pods -w
Reducing capacity follows the same pattern. Specifying a lower --replicas value initiates graceful termination of excess pods:
kubectl scale --replicas=2 deployment/web-tier-frontend
Executing Rolling Updates and Version Rollbacks
Modifying the container image triggers a zero-downtime rolling strategy. The controller incrementally terminates old pods while scheduling new ones with the updated image:
kubectl set image deployment/web-tier-frontend web-tier-frontend=nginx:1.25-alpine --record
Inspect individual pods to confirm the new image digest is active:
kubectl describe pod -l app=web-tier-frontend
If validation fails or regressions occur, revert to the previous stable revision:
kubectl rollout undo deployment/web-tier-frontend
Target a specific historical deployment revision using --to-revision=<number> for precise rollback control.
Validating Intra-Cluster Service Discovery
Kubernetes automatically provisions DNS records for Services, enabling reliable name-based routing. Deploy a backend workload and a diagnostic pod to verify connectivity:
kubectl create deployment api-gateway --image=ikubernetes/myapp:v1 --replicas=2
kubectl expose deployment api-gateway --port=80 --name=backend-svc
Launch an ephemeral client pod to test DNS resolution and HTTP routing:
kubectl run debug-client --image=busybox --restart=Never --rm -it -- /bin/sh
From within the container, execute requests against the Service DNS name:
wget -qO- http://backend-svc
wget -qO- http://frontend-access:80
Each request is distributed across available endpoints, demonstrating the built-in round-robin load balancing mechanism. Modifying the Deployment replicas or performing upgrades automatically updates the Service endpoints without requiring client reconfiguration.
Core Resource Management Command Reference
kubectl create deployment: Initializes a controller managing replica sets for declarative pod orchestration.kubectl get: Retrieves resource state, supporting label selectors and namespace scoping.kubectl expose: Generates a Service endpoint for existing Pods, Deployments, or ReplicaSets.kubectl describe: Outputs comprehensive event logs and configuration details for a specific resource.kubectl scale: Overrides replica counts for scalable workload controllers.kubectl set image: Mutates container image references within running controllers to trigger rollouts.kubectl rollout: Manages update history, monitors deployment status, and executes version reversions.