Kubernetes Cheat Sheet
Containerization is the best-in-class strategy for deploying and working with software on a large scale. It is a modern way of processing software or applications that run easily in new computing environments. However, these containers need to be managed and processed, and that is the main motive of container management systems. Kubernetes is quite possibly the most notable and widely-used container management system out there.
In this Kubernetes cheat sheet, we will learn in detail about Kubernetes, its benefits, architecture, along with all the basic yet important Kubectl commands for achieving various tasks in Kubernetes.
What is Kubernetes?
Having containers in your working environment helps a lot in the management of the applications and also prevents any sort of downtime. They manage the continuous functioning of applications by keeping backup containers. For the management of these containers, a system is used that aligns the task to them automatically in addition to distributing the resources equally.
This is exactly where Kubernetes comes into play. It is an open-source container orchestration platform that helps companies to execute their distributed systems smoothly. Time-taking processes such as application scaling, managing downtimes, aligning resources, providing deployment patterns, etc. are automatically managed by Kubernetes. It helps you in the following ways:
- Kubernetes allows you to do automatic rollouts and rollbacks. This means that you can define the exact state in which you want the future containers to get deployed. Therefore, automating Kubernetes for creating new containers and deploying them consistently, and deleting the old ones is possible.
- Fitting containers on specific nodes along with configurations regarding the resources gets simple. A user can define the total RAM and CPU each container would require and define the node clusters onto which they’ll be deployed.
- It makes the identification of containers quick by making the use of their IP address or DNS system. In addition, it manages the load by distributing them equally and eventually stabilizes the load.
- Keeps all confidential information such as OAuth tokens, passwords, and SSH keys away from unauthorized individuals. Also, it allows organizations to update configurations without rebuilding containers.
- Enables administrators to choose the storage from various options such as local storage, public or private cloud providers, databases, etc.
- Its real-time monitoring and self-healing ability run regular scans for checking failed containers and replacing them, removing containers that don’t respond, fixing containers that underperform, etc. It ensures to fix everything and then bring them to action.
The architecture of Kubernetes is made up of a control plane which is also known as master, an etcd which is the distributed storage system responsible for consistent functioning, and tons of cluster nodes or Kubelets.
In one environment, the default setup offers only one master node that is the point of contact for all the worker nodes. However, you can have multiple master nodes in case of high demand. Following are some of the frequently-used terms associated with Kubernetes architecture:
- Pod: A term used to represent a group of containers
- Labels: Utilized for identifying the pod
- Kubelet: They maintain the pod sets
- Proxy: These help in balancing the load of the containers
- Etcd: It is a consistent and highly available key-value storage for clustering data
- CAdvisor: Used for real-time monitoring of usage
- Replication controller: Helpful in managing pod replications
- Scheduler: Responsible for scheduling pods on worker nodes
Kubernetes Cheat Sheet
Kubernetes cheat sheet is basically a set of Kubectl commands, which is the command-line configuration tool for Kubernetes through which feasible communication with Kubernetes API server becomes possible. These commands are used for executing several actions on Kubernetes such as creating, inspecting, updating, and deleting Kubernetes objects in no time. This cheat sheet is an epicenter for all the frequently-used Kubectl commands that you’ll have to utilize from time to time while working with Kubernetes.
An individual will have the full liberty to use either the complete word mentioned in the command or the shortcode variation mentioned along with the heading of each section. The outcome will be the same.
1. Cluster management
It is the process of managing multiple Kubernetes clusters under one organization. Clusters used for development, testing, and production are aligned with a single infrastructure. The whole management work is done by using the below-mentioned codes.
|Display end-point details regarding master and worker nodes within a cluster.||kubectl cluster-info|
|Used for checking the Kubernetes version by showing it on screen.||kubectl version|
|Display all the details associated with configurations.||kubectl config view|
|Used for listing down all the API (Application Program Interface) resources collectively.||kubectl api-resources|
|List down all the API versions present for the users.||kubectl api-versions|
|Run this command for listing all the data and information.||kubectl get all –all-namespaces|
2. Events (ev)
Events are typically used for discovery purposes and are automatically created every time resources get changed, face some issues, etc. that are important for administrators and employers to know.
|Display all the recently occurred events.||kubectl get events|
|Mentions all the warnings collectively on the screen.||kubectl get events –field-selector type=Warning|
|Write down all the events except for the one associated with pods.||kubectl get events –field-selector involvedObject.kind!=Pod|
|Pull out the event from a specific node with the help of its name.||kubectl get events –field-selector involvedObject.kind=Node, involvedObject.name=<node_name>|
|Filter normal events from the list of events.||kubectl get events –field-selector type!=Normal|
3. Namespaces (ns)
A lot of virtual clusters get supported by one physical cluster. All of these virtual clusters are termed namespaces that are used by various environments and users across multiple teams and projects.
|Use this command for creating namespaces.||kubectl create namespace <namespace_name>|
|Display namespace either with the help of name or listing down the whole group.||kubectl get namespace <namespace_name>|
|Get information regarding the state of a namespace.||kubectl describe namespace <namespace_name>|
|Run this command for deleting a namespace by using its name.||kubectl delete namespace <namespace_name>|
|Used for editing and modifying the details associated with a namespace.||kubectl edit namespace <namespace_name>|
|Display all the resources used by a namespace such as CPU, memory, etc.||kubectl top namespace <namespace_name>|
4. Nodes (no)
Every pod under the Kubernetes environment runs on nodes which is basically a worker machine that can be virtual or physical based on the cluster. Nodes are handled by managers.
|Update the traces associated with nodes.||kubectl taint node <node_name>|
|List all the nodes.||kubectl get node|
|Run this command for deleting one or more than one node.||kubectl delete node <node_name>|
|Mention the usage of resources by the nodes. For example, listing down the consumption of CPU and memory.||kubectl top node|
|Used for listing down the resources divided for the pods.||kubectl describe nodes | grep Allocated -A 5|
|Check the pods that are running on a specific node.||kubectl get pods -o wide | grep <node_name>|
|Used for annotating nodes.||kubectl annotate node <node_name>|
|Disable a node to get scheduled by marking it through this code.||kubectl cordon node <node_name>|
|Enable a node to get scheduled by marking it through this code.||kubectl uncordon node <node_name>|
|Drain a node completely to make it fit for maintenance.||kubectl drain node <node_name>|
|Add or update labels associated with one or more multiple nodes at once.||kubectl label node|
5. Pods (po)
The most basic deployment unit in Kubernetes is Pods. They are responsible for running instances under clusters and a single Pod can be composed of a few containers.
|Run this command for listing down all the pods.||kubectl get pod|
|Used for deleting a specific pod by using its name.||kubectl delete pod <pod_name>|
|Display the state of a particular pod.||kubectl describe pod <pod_name>|
|Used for creating pods.||kubectl create pod <pod_name>|
|Used for making a pod function that is not defined by the dedicated container.||kubectl exec <pod_name> -c <container_name> <command>|
|Get an interactive shell for pods that are aligned to single containers.||kubectl exec -it <pod_name> /bin/sh|
|Mentions all the resources associated with a specific pod.||kubectl top pod|
|Add or update the annotation of pods.||kubectl annotate pod <pod_name> <annotation>|
|Add or update the labels of the pod.||kubectl label pod <pod_name>|
6. Services (svc)
In Kubernetes, Service is used for defining a logical set of Pods and policies through which one can access the Pods. The pattern defined is often referred to as micro-service.
|List single or multiple services.||kubectl get services|
|Used for displaying the real state of services.||kubectl describe services|
|Expose replication controllers, services, deployments, or even pod as a new service for Kubernetes.||kubectl expose deployment [deployment_name]|
|Modifying or updating the details of one or more services in one shot.||kubectl edit services|
Consider logs as updates in the form of notifications that help individuals to stay updated regarding all the things happening in an application. They are useful for identifying issues, updates, etc in the first place.
|This command is utilized for printing the logs.||kubectl logs <pod_name>|
|Set past one hour for printing logs.||kubectl logs –since=1h <pod_name>|
|Print the latest 30 lines of logs.||kubectl logs –tail=20 <pod_name>|
|Choose a specific service and container for getting the logs from.||kubectl logs -f <service_name> [-c <$container>]|
|Get the logs of a specific pod and keep on getting the future ones for the same.||kubectl logs -f <pod_name>|
|Get the logs of containers inside a particular pod.||kubectl logs -c <container_name> <pod_name>|
|Used for viewing the logs for the pods that gets failed||kubectl logs –previous <pod_name>|
|List down the logs related with pods named “pod_prefix”||kubetail <pod_prefix>|
|Get the logs for previous five minutes||kubetail <pod_prefix> -s 5m|
8. Daemonsets (ds)
Daemonset enables a few or every node to run the copy of Pods. Right after the addition of a node in a cluster, Pods will be added to the same. Similarly, on the removal, the Pod will also be removed.
|Used for listing daemonsets.||kubectl get daemonset|
|Edit and update the definition of one or more daemonset.||kubectl edit daemonset <daemonset_name>|
|Deleting a daemonset by using its name.||kubectl delete daemonset <daemonset_name>|
|Create a new daemonset and assign a specific name.||kubectl create daemonset <daemonset_name>|
|Manage all the rollouts of a daemonset.||kubectl rollout daemonset|
|Display the state of a particular daemonset situated inside a particular namespace.||kubectl describe ds <daemonset_name> -n <namespace_name>|
9. Displaying the State of Resources
For getting detailed information regarding any resources, make the use of kubectl describe command. All the uninitialized resources will also be listed by running this command.
|To check the details regarding nodes.||kubectl describe nodes [node-name]|
|Display details about a particular pod.||kubectl describe pods [pod-name]|
|Display information regarding pods that are listed on pod.json through the name of the type.||Kubectl describe –f pod.json|
|Get information regarding pods that are managed by a particular replication controller.||kubectl describe pods [replication-controller-name]|
|Get information regarding all the pods.||kubectl describe pods|
10. Apply o Update a Resource
For applying and updating resources, the kubectl apply command is used by taking files or stdin as inputs.
|Create new services that are mentioned inside [service-name].yaml.||kubectl apply -f [service-name].yaml|
|Create new replication controllers that are described within the [controller-name].yaml.||kubectl apply -f [controller-name].yaml|
|Used for creating objects described within any directory, yml, or xml files.||kubectl apply -f [directory-name]|
|Used for updating resources through editing it via text editors. This command is an addition of two commands, i.e. kubectl get and kubectl apply.||kubectl edit svc/[service-name]|
|Run this command for opening text files within your text editor defined by you.||KUBE_EDITOR=”[editor-name]” kubectl edit svc/[service-name]|
To have a better vision about implementing the aforementioned Kubernetes cheat sheet more effectively, it would be better for you to learn about Kubernetes components. The functioning of Kubernetes is possible because of its clusters. These clusters are composed of worker machines known as nodes for running the applications through containers. These nodes further host pods that are the most basic and integral component of application workloads.
All of these nodes and pods functioning under one Kubernetes environment are managed by the control plane. This control plane functions on various computer devices under one infrastructure that eventually ensures the availability of the containers and fault management.
Components of the Control Plane
The main decision-maker under the Kubernetes environment is the control plane, which is responsible for modifying the functioning of the cluster, keeping up with the events associated with clusters, etc. Following are the integral components of the same:
- Kube-apiserver: This is a front-end component that validates and confronts data for API objects.
- Etcd: It is a highly available key-value store used for backing cluster data.
- Kube-scheduler: This crucial component keeps an eye on new pods and selects nodes to make them run on those pods.
- Kube-controller-manager: All the controller processes such as endpoints controllers, replication controllers, token controllers, etc. are managed by these components.
- Cloud-controller-manager: It is used for integrating the clusters with the cloud provider’s API. Further, it distinguishes between the components that are running on the cloud platforms from the ones running only on the clusters.
Components of the Node
Components of a node are available at every node running under a Kubernetes environment. They are typically responsible for running all the pods and therefore, providing Kubernetes runtime infrastructure.
- Kubelet: They are available on all the nodes and their primary job is ensuring that containers are running perfectly without any fault in the pods. It only keeps the track of containers that are defined into PodSpecs.
- Kube-proxy: It is a network proxy that is responsible for maintaining networks on nodes. They are configured with a certain set of rules through which communication between the clusters and the pods becomes possible.
- Container runtime: It is the software responsible for running containers. Kubernetes supports various container runtimes like CRI-O, Docker, or any Kubernetes Container Runtime Interface (CRI) implementation.
Kubernetes is an open-source container orchestration platform that facilitates the management of distributed systems. The architecture of Kubernetes is made up of a control plane that is also known as master, an etcd that is the distributed storage system responsible for consistent functioning, and tons of cluster nodes or Kubelets.
The blog contains a Kubernetes cheat sheet that is a set of all the important and frequently-used Kubectl commands. They help in making the communication with the Kubernetes API server possible.