Kubectl Commands

Photo of author

By admin

Containers have completely changed the way of developing and deploying modern software. They ease the management process and scalability of applications and are now being adopted by software companies all around the globe.

But along with the tons of benefits, they also get complicated as organizations may have to deploy thousands of containers, and managing them manually becomes next to impossible. Therefore, to reduce this complexity, companies have started taking the aid of container orchestration platforms like Kubernetes.

Controlled by the Kubectl command line, Kubernetes automated the manual process of managing and deploying containerized applications. The blog discusses Kubernetes along with the popular Kubectl commands that’ll make it easier for you to work with Kubernetes.

So, let’s get started.

What is Kubernetes?

Also referred to as ‘kube’, Kubernetes is currently the most popular container orchestration platform that automates the process of managing and scaling containerized applications. It was established by Google in 2014 and has been helping applications in the microservice environments since then.

Some users often refer to K8s as “operating systems for the cloud’ because they are extremely robust, portable, and extensible. They efficiently handle all the containerized workloads and services and can be hosted across public, private, or hybrid clouds. All of these unique attributes make Kubernetes an ideal platform for hosting and managing applications that require rapid scaling.

What can Kubernetes Do?

One of the major advantages of using Kubernetes is that it lets users schedule and run container clusters irrespective of them being a physical or a virtual machine (VMs).

Moreover, Kubernetes allows businesses and organizations of all sizes to fully rely on a container-based infrastructure for developing software without the need for extensive manual efforts. Following are some of the major highlights of Kubernetes:

  • Efficiently orchestrates containers through various hosts.
  • Uses the hardware and resources to the fullest.
  • Provides full control and automatic management for deployments and updates.
  • Automatically scales the applications on the fly.
  • Supervises the resource usage by applications to make sure that you never come across issues when you intend to run them.
  • Performs regular health check-ups and offers a self-healing option for the apps. This allows applications to auto-restart, auto-replicate, and scale automatically. Moreover, the automatic load balancing feature evenly spreads the load across different sections.
  • Keeps real-time track of applications to automatically stop them from consuming too many resources.
  • In case of shortage of resources or death of the host, Kubernetes moves the applications from one host to another instantly to keep them running.

What is Kubectl?

Kubectl carries out HTTP requests to the Kubernetes API. On the execution of its command line, kubectl sends a request to the API endpoint and later transfers information into the body of the request.

In simple words, it is a command-line tool through which users control Kubernetes clusters and their job is to proceed the requests to the Kubernetes API endpoints. You can consider it as a cockpit from which Kubernetes is managed and all the Kubernetes operations are performed with ease.

The following is the syntax for using Kubectl commands:

kubectl [command] [TYPE] [NAME] [flags]

The various sections of the syntax are defined below:

  1. Command: This objectifies the operation that will be performed for achieving a specific goal. For example, “delete” for deleting a container.
  2. Type: This is case-sensitive and used to define the resource type. For example, “componentstatuses” or “cs”
  3. Name: Describes the resource name and is case-sensitive. For example, kubectl get pods.
  4. Flags: Used to specify optional flags such as using -s or –server flag for defining the address of the Kubernetes API server.

Kubectl Commands

Here is the list of the most popular Kubectl command along with their descriptions:

Commands Description
kubectl alpha SUBCOMMAND [flags] This command lists down all the alpha features that are disabled by default.
kubectl annotate (-f FILENAME | TYPE NAME | TYPE/NAME) KEY_1=VAL_1 … KEY_N=VAL_N [–overwrite] [–all] [–resource-version=version] [flags] Used for adding or updating the annotations of resources.
kubectl api-resources [flags] Displays the list of all the available APIs on the screen.
kubectl api-versions [flags] Displays the accessible API versions.
kubectl apply -f FILENAME [flags] Implements change in configuration of any resource.
kubectl attach POD -c CONTAINER [-i] [-t] [flags] Connects to a container on the fly for viewing the output.
kubectl auth [flags] [options] Allows to check all the permissions.
kubectl autoscale (-f FILENAME | TYPE NAME | TYPE/NAME) [–min=MINPODS] –max=MAXPODS [–cpu-percent=CPU] [flags] All the sets of pods managed by the replication controller will be scaled automatically.
kubectl certificate SUBCOMMAND [options] Manipulates resources for improvements.
kubectl cluster-info [flags] Lists the endpoint info about the pods.
kubectl completion SHELL [options] Shell completion code.
kubectl config SUBCOMMAND [flags] Improves all the files of kubeconfig.
kubectl convert -f FILENAME [options] Transforms config files for various API versions in both the format, YAML and JSON.
kubectl cordon NODE [options] Configures a specific node as unschedulable.
kubectl cp <file-spec-src> <file-spec-dest> [options] This command copies files or directories for a container.
kubectl create -f FILENAME [flags] Creates resources from files or a stdin
kubectl delete (-f FILENAME | TYPE [NAME | /NAME | -l label | –all]) [flags] Delete resources from file or stdin. Else, you can also specify the label, name, and resource selector for deleting a specific resource.
kubectl describe (-f FILENAME | TYPE [NAME_PREFIX | /NAME | -l label]) [flags] Lists the state of resources.
kubectl top [flags] [options] Lists down the usage of resources such as CPU and memory.
kubectl drain NODE [options] Prepares drain node for improvement.
kubectl edit (-f FILENAME | TYPE NAME | TYPE/NAME) [flags] Edits the descriptions of resources with the help of default editors.
kubectl exec POD [-c CONTAINER] [-i] [-t] [flags] [– COMMAND [args…]] Useful for running a command against a node.
kubectl explain [–recursive=false] [flags] Lists down the documentation regarding resources like pods, nodes, services, etc.
kubectl expose (-f FILENAME | TYPE NAME | TYPE/NAME) [–port=port] [–protocol=TCP|UDP] [–target-port=number-or-name] [–name=name] [–external-ip=external-ip-of-service] [–type=type] [flags] Adopts controller or pods as a new service for Kubernetes.
kubectl get (-f FILENAME | TYPE [NAME | /NAME | -l label]) [–watch] [–sort-by=FIELD] [[-o | –output]=OUTPUT_FORMAT] [flags] Displays resources (one or more than one)
kubectl kustomize <dir> [flags] [options] Displays API resources generated from kustomization.yaml file.
kubectl label (-f FILENAME | TYPE NAME | TYPE/NAME) KEY_1=VAL_1 … KEY_N=VAL_N [–overwrite] [–all] [–resource-version=version] [flags] Modifies or changes the label of resources.
kubectl logs POD [-c CONTAINER] [–follow] [flags] Copies the log of the container inside a pod.
kubectl options Displays all the command-line which are applied globally.
kubectl patch (-f FILENAME | TYPE NAME | TYPE/NAME) –patch PATCH [flags] Update fields of the available resources.
kubectl plugin [flags] [options] Lists down utilities for users to interact with plugins.
kubectl port-forward POD [LOCAL_PORT:]REMOTE_PORT […[LOCAL_PORT_N:]REMOTE_PORT_N] [flags] Forwards ports to pods.
kubectl proxy [–port=PORT] [–www=static-dir] [–www-prefix=prefix] [–api-prefix=prefix] [flags] Uses a proxy server for running Kubernetes API.
kubectl replace -f FILENAME Replace a resource from a file or stdin.
kubectl rollout SUBCOMMAND [options] Allows management of new resources.
kubectl scale (-f FILENAME | TYPE NAME | TYPE/NAME) –replicas=COUNT [–resource-version=version] [–current-replicas=count] [flags] Updates the size of a replication controller.
kubectl uncordon NODE [options] Enlists a specific node as schedulable.
kubectl version [–client] [flags] Displays Kubernetes version.
kubectl taint NODE NAME KEY_1=VAL_1:TAINT_EFFECT_1 … KEY_N=VAL_N:TAINT_EFFECT_N [options] Remove the glitches of nodes.

Conclusion

Container orchestration platforms like Kubernetes have taken the world by storm. They help to increase productivity and manage containers without any fuss. Kubectl is a command line that controls Kubernetes and you can perform several operations with the help of Kubectl commands. We have covered almost all the important Kubectl commands that you can use to perform several actions on Kubernetes and manage containers efficiently.

Leave a Comment