Introduction: Learning Objectives
In this section, you will be able to
- Define Kubectl and the command structure,
- List the three command types, their features, and advantages, and
- List commonly used commands with specific examples.
Kubectl – The Kubernetes CLI
Kubectl is the Kubernetes command line interface (or CLI). Kubectl stands for Kube Command Tool Line.
Kubectl is one of the key tools for working with Kubernetes and it helps users deploy applications, inspect and manage cluster resources, view logs, and more. It provides many features for users who work with Kubernetes clusters and manage running cluster workloads.
And three Kubectl command types are imperative commands, imperative object configuration, and declarative object configuration.
Kubectl Command Structure
Kubectl commands use the following structure:
Kubectl [command] [type] [name] [flags]
Keeping each component in order is critical:
“command” means any operation to be performed, like ‘create’, ‘get’, ‘apply’, or ‘delete’,
“type” means resource type, like ‘pod’, ‘deployment’, or ‘ReplicaSet’,
“name” means resource name (if applicable), and
“flags” means special options or modifiers that override default values.
Imperative commands
Imperative commands allow you to create, update, and delete live objects directly.
- Operations should be specified to the command as arguments or flags.
- Imperative commands are easy to learn and run.
For example, to create a pod with a specific container, simply run the command as shown, specifying only a pod name and the container image.
kubectl run nginx – -image nginx
- But imperative commands don’t provide an audit trail, which is important for tracking changes.
- They aren’t flexible since options are limited, they don’t use templates, and they cannot integrate with change review processes.
- But they are ideal for development and test environments.
Imperative commands Disadvantages
Imperative commands do also have disadvantages.
- Suppose a developer runs a command to deploy an application.
- Another developer wants to deploy that same application, but they cannot because there is no configuration file.
- The second developer must check with the first developer for the exact command to deploy and then run it.
- It would be best if both developers used a template for the deployment since it overcomes the limitations of working with imperative commands.
Imperative object configuration
In imperative object configuration,
- the kubectl command specifies required operations, optional flags, and at least one file name.
- The specified configuration file must contain a full definition of the objects in YAML or JSON format.
- To create the objects defined in the file, run the command ‘kubectl create -f nginx.yaml’.
- Using the same configuration templates in multiple environments will produce identical results.
A configuration file:
- may be stored in a source control system like Git,
- it can integrate with change processes,
- and it provides audit trails and templates for creating new objects.
Disadvantages:
- But using it requires understanding of the object schema,
- and requires writing a YAML or JSON file.
Imperative Object Configuration Limitations
- You need to specify all necessary command operations. For example, if a developer performs an update operation that isn’t merged into the configuration file, then another developer cannot use the updated configuration in future deployments.
- The second developer instead uses the original or previous configuration.
Declarative object configuration
It is better to define the desired state in a shared configuration file, then when you deploy, Kubernetes automatically determines the necessary operations. This is known as declarative object configuration.
Declarative object configuration stores configuration data in files. Operations are identified by Kubectl instead of being specified by the user.
This works on directories or individual files. For example, the ‘kubectl apply -f nginx/’ command mentions a directory (as shown), then applies configuration data to all files in that directory.
The user is not required to perform any operations since they are performed by the system automatically.
Configuration files define a desired state, and Kubernetes actualizes that state. And this approach is the ideal method for production systems.
Here is an example of declarative configuration.
A developer performs updates to a running application. Since configuration data is stored in the shared template, there is still one source of truth for the configuration of this object.
Now, even if another developer misses several of these updates, all they need to do is apply the current configuration template to ensure the deployed object is as expected. Kubernetes automatically determines and performs the necessary operations to match the current state to the desired state.
Here are a few of the commonly used Kubectl commands and descriptions:
- The get command accesses a file, container, or any other resource,
- The delete command deletes a file or container,
- The autoscale command applies the autoscaling process to the selected file or container, and so on.
You can find all Kubectl commands at Kubernetes.io.
Kubectl ‘get’ commands allow you to list:
- services in a current namespace,
- pods in all namespaces,
- a particular deployment,
- and pods in the current namespace.
Kubectl ‘apply’ commands
- create resources using YAML or JSON files.
- They use extensions like .yaml, .yml, or .json.
- You can use ‘apply’ commands to create resources: from multiple files or from a URL.
Kubectl ‘scale’ commands
- scale the number of replicas.
- You can use ‘scale’ commands to scale: a ReplicaSet named ‘foo’ to 3, or a resource in ‘foo.yaml’ to 3.
Let’s create a deployment with three replicas of the nginx image.
Create the deployment using an ‘apply’ command.
The output confirms the deployment creation.
The ‘get deployment’ command provides the specific ‘my-dep’ deployment details.
The output confirms the creation of three replicas:ready, up-to-date, and available.
Kubernetes Kubectl Cheatsheet
Cheat Sheet: Understanding Kubernetes Architecture | |
Command | Description |
for …do | Runs a for command multiple times as specified. |
kubectl apply | Applies a configuration to a resource. |
kubectl config get-clusters | Displays clusters defined in the kubeconfig. |
kubectl config get-contexts | Displays the current context. |
kubectl create | Creates a resource. |
kubectl delete | Deletes resources. |
kubectl describe | Shows details of a resource or group of resources. |
kubectl expose | Exposes a resource to the internet as a Kubernetes service. |
kubectl get | Displays resources. |
kubectl get pods | Lists all the Pods. |
kubectl get pods -o wide | Lists all the Pods with details. |
kubectl get deployments | Lists the deployments created. |
kubectl get services | Lists the services created. |
kubectl proxy | Creates a proxy server between a localhost and the Kubernetes API server. |
kubectl run | Creates and runs a particular image in a pod. |
kubectl version | Prints the client and server version information. |
Cheat Sheet: Managing Applications with Kubernetes | |
Command | Description |
kubectl autoscale deployment | Autoscales a Kubernetes Deployment. |
kubectl create configmap | Creates a ConfigMap resource. |
kubectl get deployments -o wide | Lists deployments with details. |
kubectl get hpa | Lists Horizontal Pod Autoscalers (hpa) |
kubectl scale deployment | Scales a deployment. |
kubectl set image deployment | Updates the current deployment. |
kubectl rollout | Manages the rollout of a resource. |
kubectl rollout restart | Restarts the resource so that the containers restart. |
kubectl rollout undo | Rollbacks the resource. |
Summary
In this section, we learned that
- Kubectl is the Kubernetes command line interface.
- The Kubectl command structure is: kubectl, command, type, name, flags.
- Imperative commands are the easiest to learn, have no audit trail, and are not flexible.
- Imperative object configuration uses templates to ensure proper deployment replication.
- Declarative object configuration is automated, requires no user input, and is ideal for productin systems.
- And finally, the kubernetes kubectl cheatsheet is provided for easy reference of k8s cli