Browse Courses

Kubectl

This document explains kubectl, the Kubernetes command-line tool, its command structure, types of commands, and best practices for managing cluster resources.

This document introduces kubectl, the Kubernetes CLI, covering its command structure, the three main command types (imperative, imperative object configuration, declarative), their features, advantages, and practical usage for managing cluster resources and workloads.


Introduction to Kubectl

Kubectl is the command-line interface (CLI) for Kubernetes. It enables users to deploy applications, inspect and manage cluster resources, view logs, and perform various administrative tasks. Kubectl is essential for interacting with Kubernetes clusters and managing workloads.


Kubectl Command Structure

Kubectl commands follow a specific structure:

ComponentDescription
commandOperation to perform (e.g., create, get, apply)
typeResource type (e.g., pod, deployment, service)
nameResource name (if applicable)
flagsOptions or modifiers for the command

Example:

1kubectl get pods --all-namespaces

Types of Kubectl Commands

There are three main types of kubectl commands:

Command TypeDescriptionBest Use Case
ImperativeDirectly creates, updates, or deletes live objects using command arguments and flagsDevelopment, quick changes
Imperative Object ConfigurationUses a configuration file to create or update objects; requires specifying the fileReusable templates, testing
Declarative Object ConfigurationApplies configuration files to define desired state; kubectl determines necessary actionsProduction, automation

Imperative Commands

Imperative commands are easy to learn and use for direct operations. They do not provide an audit trail or support templates, making them less suitable for production. Example:

1kubectl run nginx --image=nginx

Imperative Object Configuration

This approach uses configuration files (YAML or JSON) to define objects. It supports version control and templates, but requires specifying all operations. Example:

1kubectl create -f nginx.yaml

Declarative Object Configuration

Declarative configuration defines the desired state in files. Kubectl automatically determines and performs the necessary operations to match the cluster state. This is ideal for production and automation. Example:

1kubectl apply -f ./configs/

Common Kubectl Commands and Usage

CommandDescription
kubectl getLists resources (pods, services, deployments, etc.)
kubectl deleteDeletes resources
kubectl applyCreates or updates resources from configuration files
kubectl autoscaleApplies autoscaling to resources
kubectl scaleScales the number of replicas for a resource

Examples:

1kubectl get pods --all-namespaces
2kubectl apply -f deployment.yaml
3kubectl scale --replicas=3 deployment/my-dep

Practical Example: Creating and Managing a Deployment

To create a deployment with three replicas of the nginx image using declarative configuration:

1kubectl apply -f nginx-deployment.yaml

To check the deployment status:

1kubectl get deployment my-dep

The output confirms the creation of three replicas, all up-to-date and available.


Kubectl CLI

CommandDescription
for …doRuns a for command multiple times as specified.
kubectl applyApplies a configuration to a resource.
kubectl config get-clustersDisplays clusters defined in the kubeconfig.
kubectl config get-contextsDisplays the current context.
kubectl createCreates a resource.
kubectl deleteDeletes resources.
kubectl describeShows details of a resource or group of resources.
kubectl exposeExposes a resource to the internet as a Kubernetes service.
kubectl getDisplays resources.
kubectl get podsLists all the Pods.
kubectl get pods -o wideLists all the Pods with details.
kubectl get deploymentsLists the deployments created.
kubectl get servicesLists the services created.
kubectl proxyCreates a proxy server between a localhost and the Kubernetes API server.
kubectl runCreates and runs a particular image in a pod.
kubectl versionPrints the client and server version information.

Conclusion

Kubectl is the primary tool for managing Kubernetes clusters. Understanding its command structure and the differences between imperative, imperative object configuration, and declarative approaches is essential for effective cluster management and automation.


FAQ

Kubectl is the Kubernetes command-line interface (CLI) used to deploy applications, inspect and manage cluster resources, and perform administrative tasks.

  1. Imperative commands use configuration files, declarative commands do not
  2. Declarative commands require specifying all operations manually
  3. Imperative commands directly create, update, or delete live objects; declarative commands apply configuration files to define desired state
  4. Declarative commands are only for development
(3) Imperative commands act directly on live objects, while declarative commands use configuration files to define and achieve the desired state.

There will be no configuration file or audit trail, making it difficult for others to reproduce or track changes to the deployment.

  1. It uses configuration files in YAML or JSON format
  2. It supports version control and templates
  3. It automatically determines necessary operations to match desired state
  4. It can integrate with change review processes
(3) Only declarative configuration automatically determines necessary operations; imperative object configuration requires explicit commands.

Declarative configuration stores the desired state in files, enabling automation, audit trails, and consistent deployments across environments.

Kubectl apply is used in declarative workflows to create or update resources based on configuration files, ensuring the cluster matches the desired state.

Command TypeCharacteristic
ImperativeDirect, no audit trail, quick changes
Imperative Object ConfigurationUses files, supports templates, version control
Declarative Object ConfigurationAutomation, audit trail, ideal for production
Imperative-Direct, Imperative Object-Uses files, Declarative-Automation.

Declarative object configuration is ideal for production systems because it automates operations and maintains a single source of truth.

True. Declarative configuration enables automation and consistency, making it best for production.

The configuration file should be checked to ensure it accurately defines the desired state and is being applied to the correct resource.

  1. kubectl get
  2. kubectl delete
  3. kubectl autoscale
  4. kubectl build
(4) kubectl build is not a standard kubectl command.