
What Is kubectl?
kubectl is a command-line utility that is used to manage and interact with a Kubernetes cluster. It provides a convenient way for administrators to control and monitor the state of the cluster and its resources.
With kubectl, you can create, inspect, update, and delete resources in the cluster, such as pods, services, and deployments. It also allows you to view logs, troubleshoot issues, and perform other management tasks. The tool provides a simple, human-readable interface for interacting with the Kubernetes API, making it easy to automate common tasks and perform complex operations.
In this article I provide a quick guide explaining some of the most common commands. Refer to this expanded kubectl cheat sheet to see an extended list of commands and use cases.
Namespace Commands
Kubernetes namespaces allow for the separation of resources within a cluster into isolated environments for better organization and management.
To list all namespaces in the cluster, the command kubectl get namespaces can be used.
To create a new namespace, the command kubectl create namespace is used.
To set the current namespace for the kubectl context, use the command kubectl config set-context –current –namespace=testns.
And to delete a namespace, kubectl delete namespace can be executed.
Node Commands
Nodes in a Kubernetes cluster are worker machines that host containers, running the applications and services in the cluster and managed by the master node.
To retrieve a list of all nodes, the command kubectl get nodes can be used.
The command kubectl top node displays resource usage information for a specific node.
The kubectl cordon/uncordon node command either marks a node as schedulable or unschedulable. The cordon command makes it so that no new pods can be scheduled on the node, while uncordon allows new pods to be scheduled.
kubectl drain node is used to evacuate all pods from a node in preparation for maintenance. This command removes the pods and reschedules them on other available nodes in the cluster, allowing for maintenance to be performed on the node without disrupting the running applications and services.
Scaling Commands
Kubernetes lets you automate many management tasks, including provisioning and scaling. Instead of manually allocating resources, you can create automated processes that save time, let you respond quickly to peaks in demand, and conserve costs by scaling down when resources are not needed. It can be used alongside the cluster autoscaler by allocating only the resources that are needed.
The Kubernetes autoscaling mechanism uses two layers:
- Pod-based scaling—supported by the Horizontal Pod Autoscaler (HPA) and the newer Vertical Pod Autoscaler (VPA).
- Node-based scaling—supported by the Cluster Autoscaler.
The kubectl command-line tool provides several commands for scaling resources in a Kubernetes cluster. Here are some of the most commonly used commands:
- kubectl scale deployment: This command is used to scale the number of replicas for a deployment in a Kubernetes cluster. It takes the deployment name and the desired number of replicas as arguments.
- kubectl scale statefulset: This command is used to scale the number of replicas for a statefulset in a Kubernetes cluster. It takes the statefulset name and the desired number of replicas as arguments.
- kubectl autoscale: This command is used to automatically scale the number of replicas for a deployment based on resource usage. It takes the deployment name, the minimum and maximum number of replicas, and the CPU utilization target as arguments.
Commands for Managing Deployments, Pods, and Secrets
Kubernetes provides a set of commands for managing resources such as deployments, pods, and secrets. The following are some common kubectl commands for managing these resources:
- kubectl get: This command lists the specified resources in a cluster. For example, kubectl get pods will list all of the pods in the cluster, and kubectl get deployments will list the deployments.
- kubectl create: This command creates new resources (of the specified type) in a cluster. For example, kubectl create -f <file.yaml> creates a new pod, deployment, or secret based on the configuration defined in the specified YAML file.
- kubectl delete: This command deletes the specified resources from the cluster. For example, kubectl delete pod <pod-name> will delete the named pod from the cluster.
- kubectl describe: This command is used to display detailed information about a given resource. For example, kubectl describe pod <pod-name> will return details about a specific pod, including its status, events, and logs.
- kubectl edit: This command enables developers to edit the configuration of a given resource in the cluster. For example, kubectl edit deployment <deployment-name> will open an editor to modify the configuration of a specific deployment.
Volume Commands
Kubernetes volumes are a way to persist data generated by containers in a pod. They allow data to persist even if the pod or the node it is running on fails. Commands for managing volumes include:
- kubectl get pv: Used to list Persistent Volumes (PVs) in a cluster. PVs are the underlying storage resources that are used to provide storage for pods.
- kubectl describe pv: Used to display detailed information about a specific Persistent Volume. This includes information such as the capacity, access modes, and the current status of the PV.
- kubectl delete pv: Used to delete a Persistent Volume. This can be useful when you need to reclaim storage resources in the cluster.
- kubectl cp: Used to copy files and directories between containers and the local file system. This command can be used to copy data into or out of a volume, allowing you to persist or retrieve data as needed.
DaemonSet Commands
DaemonSets are a type of resource in Kubernetes that make sure that a specific pod runs on all nodes in the cluster or on a specific set of nodes. This is useful for running system-level services like monitoring or logging agents.
Here are several kubectl commands that can be used for managing DaemonSets:
- kubectl create daemonset: This command kubectl create daemonset creates a new DaemonSet in the cluster and schedules the specified pod to run on every node. The DaemonSet monitors the health of the pod and automatically reschedules it in the event of a failure or a change in the cluster, such as the addition or removal of nodes.
- kubectl describe daemonset <daemonset-name>: This command retrieves detailed information about a specific DaemonSet, including its status, events, and the pods it is managing.
- kubectl edit daemonset <daemonset-name>: This command opens an editor to edit the configuration of a specific DaemonSet.
Books For Learning Kubernetes Administration:
Conclusion
In conclusion, kubectl is an essential tool for anyone working with Kubernetes. With its simple and straightforward command-line interface, kubectl provides a convenient way to manage and interact with a Kubernetes cluster and its resources, including pods, services, deployments, and DaemonSets.
Whether you are a seasoned Kubernetes administrator or just starting out, this kubectl guide provides a comprehensive overview of the basic concepts and commands you need to know to become a Kubernetes pro. Whether you’re looking to manage resources, monitor the state of your cluster, or perform complex operations, kubectl provides the means to do so, making it an indispensable tool for anyone working with Kubernetes.
Author Bio: Gilad David Maayan
Gilad David Maayan is a technology writer who has worked with over 150 technology companies including SAP, Imperva, Samsung NEXT, NetApp and Check Point, producing technical and thought leadership content that elucidates technical solutions for developers and IT leadership. Today he heads Agile SEO, the leading marketing agency in the technology industry.
LinkedIn: https://www.linkedin.com/in/giladdavidmaayan/