Kubernetes Cheat Sheet: 8 Commands You Can’t Do Without
Kubernetes is a container orchestrator made up of master nodes and worker nodes. It allows communication only through an API server, which serves as the central component of the control plane. The API server exposes an HTTP REST API that enables communication between internal components, such as users and the cluster, and between external components.
You can think of the API server as the main user interface or the front end of Kubernetes. It allows you to query, update, or manage the state of Kubernetes objects or resources. To establish these interactions, the Kubernetes API can make REST requests directly, use client libraries, or receive direct commands through the kubectl command line.
kubectl can help you perform a variety of actions, including:
- Deploy containerized applications
- Run Kubernetes operations
- Monitoring tasks
- Inspect and manage cluster resources
- Viewing system logs
Before you start using kubectl, it’s important to have a basic understanding of the command structure. Here is the general command syntax:
kubectl [command] [TYPE] [NAME] [flags]
Let’s review each of these attributes:
- ordered: describes the type of operation to be performed. Common operations include create, write, obtain, to apply and wipe off. These commands create new Kubernetes objects, modify existing objects, or request information about existing objects. You can specify multiple resources in a single command.
- TYPE— Describes the type of resource targeted by your command. Common options are pod, service, deployment, set of demons, set of states, work Where Scheduled task.
- NAME— This is case sensitive and specifies the name of the resource your command should apply to. It is not mandatory to provide a resource name – if you provide a name, the command is limited to that specific resource (or you receive an error if there is no resource with that name). If you don’t specify it, the command applies to all resources in the namespace’s current cluster.
- flags—They indicate special options or requests for specific information. They can also be used as modifiers to override default values or environment variables.
Top 8 kubectl commands explained
1. List Kubernetes resources
Use the kubectl get operation to list one or more resources. For example, use kubectl get pods to list all Kubernetes pods. Added an exit flag like get wide -o pods will list the pods and additional data, such as their associated node names.
The get operation can list additional resources such as services and replication controllers. Use the kubectl command get rc Where get service to list all replication services and controllers.
The different variants of obtain allow you to perform actions such as specifying specific nodes and reducing the length of resources using short aliases.
While the get command provides a compact list of resources, the kubectl describe The command offers detailed reports on the status of one or more resources. Kubernetes resources. The kubectl describe pods The command describes all of your Kubernetes pods. If a replication controller manages pods, you can use the describe the pods
The kubectl describe the operation can focus on specific Pods or nodes. For example, you can use the kubectl command describe nodes
3. Create and edit
You can use the to apply command in kubectl to create resources from specific files or standard input (stdin). The kubectl apply -F servicename.yaml The command creates a new service using a specific YAML file. The -F flag indicates the use of a file.
For example, if you want to create a new RC using the contents of your YAML file, you can use the apply -f controllername.yaml ordered. Another option is to use broader commands like apply -f
The kubectl remove the operation terminates services and resources that you no longer need. It is essential for managing Kubernetes, allowing you to free up compute capacity for different Kubernetes tasks.
For example, you can use the kubectl delete pods – all command to remove all pods. When deleting pods, it’s safer to use resource names and types specified in separate YAML files. If you use the examplepod.yaml file to create a pod, you can delete the pod using the command delete -f examplepod.yaml.
Kubect can also remove services and pods that share specific tags, which you can assign using the label operation. For example, the delete pods,services -l name=
5. Persistent Volume (PV)
A Kubernetes Persistent Volume is a mechanism for provisioning storage in a Kubernetes cluster. It can be configured manually by an administrator or automatically using StorageClasses. PVs are separate resources within the cluster, independent of the individual Pods that use them. If a pod fails, the PV remains in place and can be mounted on other pods.
Behind the scenes, PV objects interact with physical storage devices using NFS, iSCSI, or with public cloud storage services.
Here are three useful commands you can use to work with PersistentVolumes.
Run the following command to create a PV on a node (provide your PV manifest URL):
kubectl apply -f https://k8s.io/examples/pods/storage/pv-volume.yaml
Create a PersistentVolumeClaim (PVC) that requests a PV with the specific criteria. This allows for dynamic PV provisioning. Run this command to create the PVC in the cluster:
kubectl apply -f https://k8s.io/examples/pods/storage/pv-claim.yaml
As soon as you create the PVC, the Kubernetes control plane starts looking for an appropriate PV. When it finds one, it binds the PVC to the PV. Run this command to see the status of a PV:
kubectl get pv task-pv-volume
6. Security Context
Run Workloads Securely in Kubernetes can be difficult. Various settings affect the security controls used by the Kubernetes API. One of the powerful tools provided by Kubernetes is to define a securityContext that all pod manifests can use.
Using security contexts in Kubernetes is simple. All you need to do is include the security context block in a deployment manifest when deploying pods. For example, the following block instructs Kubernetes to run a pod with user ID 1000 and group ID 2000:
Unlike RBAC, a security context does not require you to define different file types (such as roles and role bindings) to enforce security rules. Simply add the required security context code when declaring your deployment, and Kubernetes will automatically apply the rules for you.
7. Deployment management
There are several useful commands for managing Kubernetes deployments. Deployment management also covers StatefulSet and DaemonSet management.
When updating a deployment, stateful set, or daemon set, you can use the deployment status command to display the update status. You can also undeploy using the command deployment cancel
These commands are rare in practice because most administrators use tools like Helm to manage deployments.
You can use the command scale –replicas=N
Since you’re likely using Helm or the pod autoscaler to make static changes, you’re unlikely to use this approach in practice or make manual changes. It is also possible to configure basic Autoscaling Kubernetes abilities with the autoscaling kubectl operation. However, this command only works with the CPU utilization metric.
8. Execution of orders
You can run commands through kubectl using the executive operation, which executes commands on containers or pods. For example, you can use the kubectl exec
For another example, you can use the exec
That’s it! Although this list of commands won’t let you do everything in Kubernetes, it’s enough to accomplish many daily tasks. Hope this helps you as you improve your mastery of containerized environments to become a Kubernetes hero.