{"id":8176,"date":"2023-06-12T10:48:33","date_gmt":"2023-06-12T05:18:33","guid":{"rendered":"https:\/\/www.interviewbit.com\/blog\/?p=8176"},"modified":"2023-06-12T10:49:55","modified_gmt":"2023-06-12T05:19:55","slug":"kubectl-commands","status":"publish","type":"post","link":"https:\/\/www.interviewbit.com\/blog\/kubectl-commands\/","title":{"rendered":"Top Kubectl Commands You Must Know"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<div class=\"gutentoc tocactive nostyle\"><div class=\"gutentoc-toc-wrap\"><div class=\"gutentoc-toc-title-wrap\"><div class=\"gutentoc-toc-title\">Table Of Contents<\/div><div id=\"open\" class=\"text_open\">show<\/div><\/div><div id=\"toclist\"><div class=\"gutentoc-toc__list-wrap\"><ul class=\"gutentoc-toc__list\"><li><a href=\"#what-is-kubernetes\">What is Kubernetes?<\/a><\/li><li><a href=\"#kubectl-commands\">Kubectl Commands<\/a><\/li><ul class=\"gutentoc-toc__list\"><li><a href=\"#1-cluster-management\">1. Cluster Management<\/a><\/li><li><a href=\"#2-namespaces\">2. Namespaces<\/a><\/li><li><a href=\"#3-node-operations\">3. Node operations<\/a><\/li><li><a href=\"#4-listing-resources-\">4. Listing Resources&nbsp;<\/a><\/li><li><a href=\"#5-daemonsets-\">5. Daemonsets&nbsp;<\/a><\/li><li><a href=\"#6-events\">6. Events<\/a><\/li><li><a href=\"#7-logs\">7. Logs<\/a><\/li><li><a href=\"#8-deployments\">8. Deployments<\/a><\/li><li><a href=\"#9-replication-controllers\">9. Replication Controllers<\/a><\/li><li><a href=\"#10-replicasets\">10. ReplicaSets<\/a><\/li><li><a href=\"#11-secrets\">11. Secrets<\/a><\/li><li><a href=\"#12-services-and-service-accounts-\">12. Services and Service Accounts:&nbsp;<\/a><\/li><\/ul><li><a href=\"#kubectl-syntax\">Kubectl Syntax<\/a><\/li><li><a href=\"#kubectl-best-practices\">Kubectl Best Practices<\/a><\/li><ul class=\"gutentoc-toc__list\"><li><a href=\"#1-know-the-defaults\">1. Know the defaults<\/a><\/li><li><a href=\"#2-implement-simple-aliases\">2. Implement simple aliases<\/a><\/li><li><a href=\"#3-learn-how-to-use-help\">3. Learn how to use help<\/a><\/li><li><a href=\"#4-present-output-at-regular-intervals\">4. Present output at regular intervals<\/a><\/li><li><a href=\"#5-utilise-the-dry-run-for-testing\">5. Utilise the dry run for testing<\/a><\/li><\/ul><li><a href=\"#conclusion\">Conclusion<\/a><\/li><li><a href=\"#faqs\">FAQs<\/a><\/li><ul class=\"gutentoc-toc__list\"><li><a href=\"#q1-what-is-kubectl-used-for\">Q.1: What is Kubectl used for?<\/a><\/li><li><a href=\"#q2-what-are-the-features-of-kubectl\">Q.2: What are the features of Kubectl?<\/a><\/li><li><a href=\"#q3-how-does-kubectl-command-work\">Q.3: How does Kubectl command work?<\/a><\/li><\/ul><li><a href=\"#important-resources\">Important Resources<\/a><\/li><\/ul><\/div><\/div><\/div><\/div>\n\n\n\n<p>If you are going through this article, you are presumably familiar with Kubernetes and wish to interact with a Kubernetes cluster. Be it any way you have provisioned the cluster, the standard command line employed to communicate with it is Kubectl. This article expects that you have a fundamental understanding of Kubernetes and the Kubectl commands.<br><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img  loading=\"lazy\"  src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAP+KeNJXAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=\"  alt=\"Kubectl\"  class=\"wp-image-8181 pk-lazyload\"  width=\"462\"  height=\"512\"  data-pk-sizes=\"auto\"  data-ls-sizes=\"(max-width: 462px) 100vw, 462px\"  data-pk-src=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/Kubectl-923x1024.png\"  data-pk-srcset=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/Kubectl-923x1024.png 923w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/Kubectl-271x300.png 271w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/Kubectl-768x852.png 768w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/Kubectl-1385x1536.png 1385w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/Kubectl-1847x2048.png 1847w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/Kubectl-380x421.png 380w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/Kubectl-550x610.png 550w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/Kubectl-800x887.png 800w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/Kubectl-1160x1286.png 1160w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/Kubectl.png 3132w\" ><\/figure>\n\n\n\n<p><strong>Kubectl provides three techniques:<\/strong><\/p>\n\n\n\n<ul><li>The technique of \u201cImperative commands\u201d directly expresses to Kubernetes what operation to execute on which objects, for instance: kubectl delete service or kubectl create pod.<br><\/li><li>The technique of \u201cImperative Object Configuration\u201d is like the first one, except that it functions on manifest files instead of objects directly, for instance, kubectl create -f manifest.yaml.<br><\/li><li>The technique of \u201cDeclarative Object Configuration\u201d takes manifest files as input but utilises an \u201cupsert\u201d logic and develops objects if they don\u2019t exist, or it revises prevailing objects that differ from the specifications in the input manifest files. kubectl apply -f manifest.yaml is the command for this technique.<br><\/li><\/ul>\n\n\n\n<p>It is to be noted that a proper declarative approach does not exist yet as Kubectl can\u2019t auto-delete objects. The \u201c&#8211;prune\u201d option for the \u201ckubectl apply\u201d command lets you attain a fully declarative approach, but currently, this option is in alpha at the time of this writing and therefore it is not deemed good enough for general use. Before diving into the code, it is important to note a few things.<\/p>\n\n\n\n<p>They usually regard the object notation as an object type, followed by a slash, then the object name. For instance, the notation will be \u201cpods\/mypod\u201d to address the \u201cmypod\u201d pod. A few commands receive different notations (e.g., \u201ckubectl get pod mypod\u201d), hence you might observe different notations utilised throughout this article.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img  loading=\"lazy\"  width=\"1024\"  height=\"514\"  src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAP+KeNJXAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=\"  alt=\"Kubectl1\"  class=\"wp-image-8182 pk-lazyload\"  data-pk-sizes=\"auto\"  data-ls-sizes=\"(max-width: 1024px) 100vw, 1024px\"  data-pk-src=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/Kubectl1-1024x514.png\"  data-pk-srcset=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/Kubectl1-1024x514.png 1024w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/Kubectl1-300x151.png 300w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/Kubectl1-768x385.png 768w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/Kubectl1-1536x771.png 1536w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/Kubectl1-2048x1028.png 2048w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/Kubectl1-380x191.png 380w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/Kubectl1-550x276.png 550w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/Kubectl1-800x402.png 800w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/Kubectl1-1160x582.png 1160w\" ><\/figure>\n\n\n\n<h2 id=\"what-is-kubernetes\">What is Kubernetes?<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img  loading=\"lazy\"  src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAP+KeNJXAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=\"  alt=\"What is Kubernetes\"  class=\"wp-image-8183 pk-lazyload\"  width=\"512\"  height=\"345\"  data-pk-sizes=\"auto\"  data-ls-sizes=\"(max-width: 512px) 100vw, 512px\"  data-pk-src=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/What-is-Kubernetes-1024x690.png\"  data-pk-srcset=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/What-is-Kubernetes-1024x690.png 1024w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/What-is-Kubernetes-300x202.png 300w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/What-is-Kubernetes-768x518.png 768w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/What-is-Kubernetes-1536x1035.png 1536w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/What-is-Kubernetes-2048x1380.png 2048w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/What-is-Kubernetes-380x256.png 380w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/What-is-Kubernetes-550x371.png 550w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/What-is-Kubernetes-800x539.png 800w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/What-is-Kubernetes-1160x782.png 1160w\" ><\/figure>\n\n\n\n<p>A fully resource-centred system, <a rel=\"noreferrer noopener\" href=\"https:\/\/www.interviewbit.com\/blog\/kubernetes-architecture\/\" target=\"_blank\"><strong>Kubernetes<\/strong><\/a> provides an internal state of resources, and every Kubernetes operations are CRUD operation on these resources. You can fully manage Kubernetes by handling these resources, and Kubernetes determines what to do based on the present state of resources. For this purpose, we organize the Kubernetes API reference as a list of resource types, along with their associated operations.<\/p>\n\n\n\n<p>Let\u2019s take an example into consideration.<\/p>\n\n\n\n<p>Assume that you want to build a ReplicaSet resource. To accomplish so, you would determine the ReplicaSet in a file named replicaset.yaml file, and then pass the following command:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;strong>$ kubectl create -f replicaset.yaml&lt;\/strong><\/pre>\n\n\n\n<p>Certainly, this builds your ReplicaSet in Kubernetes. But what goes on behind the scenes?<\/p>\n\n\n\n<p>Kubernetes includes a create ReplicaSet operation, and like other Kubernetes operations, it is brought to light as an API endpoint. The exact API endpoint for this operation is:<br><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">POST \/apis\/apps\/v1\/namespaces\/{namespace}\/replicasets<\/pre>\n\n\n\n<p>You can locate the API endpoints of all Kubernetes\u2019 operations in the API reference, comprising the above-mentioned endpoint. To create a definite request to an endpoint, you require to pre-pend the URL of the API server to the endpoint paths that are mentioned in the API reference.<\/p>\n\n\n\n<p>When you run the above command, kubectl initiates an HTTP POST request to the above API endpoint. The ReplicaSet definition (which you added in the replicaset.yaml file) is passed in the request&#8217;s body. This is how kubectl functions for all commands that connect with the Kubernetes cluster. In all these instances, kubectl only makes HTTP requests to the relevant Kubernetes API endpoints. Note that it\u2019s entirely possible to control Kubernetes with a tool such as curl by manually issuing HTTP requests to the Kubernetes API. Kubectl simply makes it simpler for you to utilise the Kubernetes API. These are the fundamentals of what kubectl is and how it works.<\/p>\n\n\n\n<h2 id=\"kubectl-commands\">Kubectl Commands<\/h2>\n\n\n\n<p>The kubectl command-line utility is a mighty tool, and let\u2019s go through the basic kubectl commands that apply to all Kubernetes objects.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img  loading=\"lazy\"  width=\"1024\"  height=\"295\"  src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAP+KeNJXAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=\"  alt=\"Kubectl Commands\"  class=\"wp-image-8184 pk-lazyload\"  data-pk-sizes=\"auto\"  data-ls-sizes=\"(max-width: 1024px) 100vw, 1024px\"  data-pk-src=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/Kubectl-Command-1024x295.png\"  data-pk-srcset=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/Kubectl-Command-1024x295.png 1024w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/Kubectl-Command-300x86.png 300w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/Kubectl-Command-768x221.png 768w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/Kubectl-Command-1536x443.png 1536w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/Kubectl-Command-2048x590.png 2048w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/Kubectl-Command-380x110.png 380w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/Kubectl-Command-550x159.png 550w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/Kubectl-Command-800x231.png 800w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/04\/Kubectl-Command-1160x334.png 1160w\" ><\/figure>\n\n\n\n<h3 id=\"1-cluster-management\">1. Cluster Management<\/h3>\n\n\n\n<p>A Kubernetes cluster is a collection of nodes that execute containerized applications. It lets containers run across several machines and environments: cloud-based, virtual, on-premises and physical. Listed below are the kubectl commands that can be utilised to manage a cluster.<\/p>\n\n\n\n<ul><li><strong>kubectl cluster-info<br><\/strong>Display endpoint information regarding the services and master in the cluster<\/li><\/ul>\n\n\n\n<ul><li><strong>kubectl version<\/strong><br>Show the Kubernetes version functioning on the client and server<\/li><\/ul>\n\n\n\n<ul><li><strong>kubectl config view<\/strong><br>Get the configuration of the cluster<\/li><\/ul>\n\n\n\n<ul><li><strong>kubectl api-resources<\/strong><br>Make a list of the available API resources<\/li><\/ul>\n\n\n\n<ul><li><strong>kubectl api-versions<\/strong><br>Make a list of the available API versions&nbsp;<\/li><\/ul>\n\n\n\n<ul><li><strong>kubectl get all &#8211;all-namespaces<\/strong><br>List everything<\/li><\/ul>\n\n\n\n<h3 id=\"2-namespaces\">2. Namespaces<\/h3>\n\n\n\n<p>Shortcode = ns<\/p>\n\n\n\n<ul><li><strong>kubectl create namespace &lt;namespace_name&gt;<\/strong><br>Create namespace &lt;name&gt;<\/li><\/ul>\n\n\n\n<ul><li><strong>kubectl get namespace &lt;namespace_name&gt;<\/strong><br>List one or more namespaces<\/li><\/ul>\n\n\n\n<ul><li><strong>kubectl describe namespace &lt;namespace_name&gt;<\/strong><br>Show the detailed condition of one or more namespace<\/li><\/ul>\n\n\n\n<ul><li><strong>kubectl delete namespace &lt;namespace_name&gt;<\/strong><br>Delete a namespace<\/li><\/ul>\n\n\n\n<ul><li><strong>kubectl edit namespace &lt;namespace_name&gt;<\/strong><br>Edit and modify the namespace\u2019s definition<\/li><\/ul>\n\n\n\n<ul><li><strong>kubectl top namespace &lt;namespace_name&gt;<\/strong><br>Display Resource (CPU\/Memory\/Storage) usage for a namespace<\/li><\/ul>\n\n\n\n<h3 id=\"3-node-operations\">3. Node operations<\/h3>\n\n\n\n<p>A Node is a worker machine in Kubernetes and can either be a virtual or a physical machine, which depends on the cluster. Every Node is handled by the control plane. A Node can contain several pods, and the Kubernetes control plane handles scheduling the pods automatically across the Nodes in the cluster. The following commands can be utilised for Node Operations.<\/p>\n\n\n\n<ul><li><strong>kubectl taint node &lt;node_name><\/strong><br>Revise the taints on one or more nodes<br><\/li><li><strong>kubectl get node<\/strong><br>List one or more nodes<br><\/li><li><strong>kubectl delete node &lt;node_name><\/strong><br>Delete a node or multiple nodes<br><\/li><li><strong>kubectl top node<\/strong><br>Display Resource usage (CPU\/Memory\/Storage) for nodes<\/li><\/ul>\n\n\n\n<ul><li><strong>kubectl describe nodes | grep Allocated -A 5<\/strong><br>Resource allocation per node<br><\/li><li><strong>kubectl get pods -o wide | grep &lt;node_name><\/strong><br>Pods running on a node<br><\/li><li><strong>kubectl annotate node &lt;node_name><\/strong><br>Annotate a node<br><\/li><li><strong>kubectl cordon node &lt;node_name><\/strong><br>Mark a node as unschedulable<br><\/li><li><strong>kubectl uncordon node &lt;node_name><\/strong><br>Mark node as schedulable<br><\/li><li><strong>kubectl drain node &lt;node_name><\/strong><br>Drain a node in preparation for maintenance<br><\/li><li><strong>kubectl label node<br><\/strong>Add the labels of one or more nodes<\/li><\/ul>\n\n\n\n<h3 id=\"4-listing-resources-\"><span id=\"4-listing-resources\">4. Listing Resources&nbsp;<\/span><\/h3>\n\n\n\n<p>Kubernetes resources are also regarded as Kubernetes objects related to a certain namespace, you can either utilise individual Kubectl get commands to jot down every resource one by one, or you can jot down all the resources in a Kubernetes namespace by executing a single command. Mentioned below is the list of commands to get the resource information.<\/p>\n\n\n\n<ul><li><strong>kubectl get namespaces<\/strong><br>Create a plain-text list of all namespaces<br><\/li><li><strong>kubectl get pods<\/strong><br>Create a plain-text list of all pods<br><\/li><li><strong>kubectl get pods -o wide<\/strong><br>Create a comprehensive plain-text list of all pods<br><\/li><li><strong>kubectl get pods\u2013field-selector=spec. nodeName=[server-name]<\/strong><br>Create a list of all pods functioning on a certain node server<br><\/li><li><strong>kubectl get replicationcontroller [replication-controller-name]<\/strong><br>In plain text, make a lst a specific replication controller<br><\/li><li><strong>kubectl get replicationcontroller, services<\/strong><br>Generate a plain-text list of all replication services and controllers\u00a0<\/li><\/ul>\n\n\n\n<h3 id=\"5-daemonsets-\"><span id=\"5-daemonsets\">5. Daemonsets&nbsp;<\/span><\/h3>\n\n\n\n<p>A Daemonset assures that some or all\u00a0 Nodes run a copy of a Pod. As nodes are incorporated into the cluster, Pods are implemented to them. As nodes are erased from the cluster, those Pods are garbage collected. Erasing a DaemonSet will clean up the Pods it created.<\/p>\n\n\n\n<ul><li><strong>kubectl get daemonset<\/strong><br>List one or more daemonsets<br><\/li><li><strong>kubectl edit daemonset &lt;daemonset_name><\/strong><br>Edit and modify the definition of one or more daemonset<br><\/li><li><strong>kubectl delete daemonset &lt;daemonset_name><br><\/strong>Delete a daemonset<br><\/li><li><strong>kubectl create daemonset &lt;daemonset_name><\/strong><br>Create a new daemonset<br><\/li><li><strong>kubectl rollout daemonset<\/strong><br>Manage the rollout of a daemonset<br><\/li><li><strong>kubectl describe ds &lt;daemonset_name> -n &lt;namespace_name><\/strong><br>Show the comprehensive state of daemonsets within a namespace<\/li><\/ul>\n\n\n\n<h3 id=\"6-events\">6. Events<\/h3>\n\n\n\n<p>Shortcode = ev<\/p>\n\n\n\n<p>Kubernetes events are objects that display what is happening within a cluster, like what decisions were implemented by the scheduler or why some pods were erased from the node. Events are the first thing to look at for application, along with infrastructure operations when something is not functioning as anticipated. Mentioned below are the Kubectl commands to get the events.<br><\/p>\n\n\n\n<ul><li><strong>kubectl get events<\/strong><br>List current events for all resources in the system<\/li><\/ul>\n\n\n\n<ul><li><strong>kubectl get events &#8211;field-selector type=Warning<\/strong><br>List Warnings only<\/li><\/ul>\n\n\n\n<ul><li><strong>kubectl get events &#8211;field-selector involvedObject.kind!=Pod<\/strong><br>List events but exclude Pod events<\/li><\/ul>\n\n\n\n<ul><li><strong>kubectl get events &#8211;field-selector involvedObject.kind=Node, involvedObject.name=&lt;node_name&gt;<\/strong><br>Pull events for a single node with a distinct name<\/li><\/ul>\n\n\n\n<ul><li><strong>kubectl get events &#8211;field-selector type!=Normal<\/strong><br>From a list of events, filter out normal events<\/li><\/ul>\n\n\n\n<h3 id=\"7-logs\">7. Logs<\/h3>\n\n\n\n<p>You can use Kubernetes logs commands to monitor, log and debug the pods<\/p>\n\n\n\n<ul><li><strong>kubectl logs &lt;pod_name&gt;<\/strong><br>Print the logs for a pod<\/li><\/ul>\n\n\n\n<ul><li><strong>kubectl logs &#8211;since=1h &lt;pod_name&gt;<\/strong><br>Print the logs for a pod for the last hour&nbsp;<\/li><\/ul>\n\n\n\n<ul><li><strong>kubectl logs &#8211;tail=20 &lt;pod_name&gt;<\/strong><br>Get the current 20 lines of logs<\/li><\/ul>\n\n\n\n<ul><li><strong>kubectl logs -f &lt;service_name&gt; [-c &lt;$container&gt;]<\/strong><br>Get logs from a service and choose which container optionally<\/li><\/ul>\n\n\n\n<ul><li><strong>kubectl logs -f &lt;pod_name&gt;<\/strong><br>Adhere to new logs and print the logs for a pod<\/li><\/ul>\n\n\n\n<ul><li><strong>kubectl logs -c &lt;container_name&gt; &lt;pod_name&gt;<\/strong><br>For a container in a pod, Print the logs&nbsp;<\/li><\/ul>\n\n\n\n<ul><li><strong>kubectl logs &lt;pod_name&gt; pod.log<\/strong><br>Output the logs for a pod into a \u2018pod.log\u2019 file&nbsp;<\/li><\/ul>\n\n\n\n<ul><li><strong>kubectl logs &#8211;previous &lt;pod_name&gt;<\/strong><br>View the logs for the last failed pod<\/li><\/ul>\n\n\n\n<h3 id=\"8-deployments\">8. Deployments<\/h3>\n\n\n\n<p>Shortcode = deploy.<\/p>\n\n\n\n<p>A Kubernetes Deployment is utilised to inform Kubernetes how to design or change instances of the pods that hold a containerized application. Deployments can enhance the number of replica pods, enable the rollout of revised code in a controlled way, or roll back to an earlier deployment version if required.<\/p>\n\n\n\n<ul><li><strong>kubectl get deployment<\/strong><br>List one or more deployments<\/li><\/ul>\n\n\n\n<ul><li><strong>kubectl describe deployment &lt;deployment_name><\/strong><br>Show the in-depth state of one or more deployments<br><\/li><li><strong>kubectl edit deployment &lt;deployment_name><\/strong><br>Edit and revise the definition of one or more deployments on the server<br><\/li><li><strong>kubectl create deployment &lt;deployment_name><br><\/strong>\u00a0Generate one a new deployment<br><\/li><li><strong>kubectl delete deployment &lt;deployment_name><br><\/strong>\u00a0Delete deployments<br><\/li><li>\u00a0<strong>kubectl rollout status deployment &lt;deployment_name><\/strong><br>Check the rollout status of a deployment<\/li><\/ul>\n\n\n\n<h3 id=\"9-replication-controllers\">9. Replication Controllers<\/h3>\n\n\n\n<p>Shortcode = rc<\/p>\n\n\n\n<ul><li><strong>kubectl get rc<\/strong><br>Make a list of&nbsp; the replication controllers<\/li><\/ul>\n\n\n\n<ul><li><strong>kubectl get rc &#8211;namespace=\u201d&lt;namespace_name>\u201d<\/strong>\u00a0<br>Make a list of\u00a0 the replication controllers by namespace<\/li><\/ul>\n\n\n\n<h3 id=\"10-replicasets\">10. ReplicaSets<\/h3>\n\n\n\n<p>Shortcode = rs<\/p>\n\n\n\n<ul><li><strong>kubectl get replicasets<\/strong><br>List ReplicaSets<\/li><\/ul>\n\n\n\n<ul><li><strong>kubectl describe replicasets &lt;replicaset_name&gt;<\/strong><br>Show&nbsp;the detailed state of one or more ReplicaSets<\/li><\/ul>\n\n\n\n<ul><li><strong>kubectl scale &#8211;replicas=[x]<\/strong><br>Scale a ReplicaSet<\/li><\/ul>\n\n\n\n<h3 id=\"11-secrets\">11. Secrets<\/h3>\n\n\n\n<p>A Kubernetes Secret is an object that comprises a minor portion of sensitive data like a token, a key or a password. Such data might otherwise be inserted in an image or in a Pod specification. Users can build Secrets and the system also generates a few Secrets with the help of the following Kubectl commands.<\/p>\n\n\n\n<ul><li><strong>kubectl create secret<\/strong><br>Create a secret<br><\/li><li><strong>kubectl get secrets<\/strong><br>List secrets<br><\/li><li><strong>kubectl describe secrets<\/strong><br>List details about secrets<br><\/li><li><strong>kubectldelete secret &lt;secret_name><\/strong><br>Delete a secret<\/li><\/ul>\n\n\n\n<h3 id=\"12-services-and-service-accounts-\"><span id=\"12-services-and-service-accounts\">12. Services and Service Accounts:&nbsp;<\/span><\/h3>\n\n\n\n<p>A Kubernetes service is a logical abstraction for a deployed group of pods in a cluster (which all perform the same function) and Service accounts are used to provide an identity for pods. Pods that want to interact with the API server will authenticate with a particular service account.<\/p>\n\n\n\n<ul><li><strong>kubectl get services<br><\/strong>Make a list of\u00a0 one or more services<br><\/li><li><strong>kubectl describe services<\/strong><br>Show the detailed state of a service<br><\/li><li><strong>kubectl expose deployment [deployment_name]<\/strong><br>Reveal a replication controller, service, deployment or pod as a new Kubernetes service<br><\/li><li><strong>kubectl edit services<\/strong><br>Edit and modify the definition of one or more services<br><\/li><li><strong>kubectl get serviceaccounts<\/strong><br>List service accounts<br><\/li><li><strong>kubectl describe serviceaccounts<\/strong><br>Show the in-depth state of one or more service accounts<br><\/li><li><strong>kubectl replace serviceaccount<\/strong><br>Replace a service account<br><\/li><li><strong>kubectl delete serviceaccount &lt;service_account_name><\/strong><br>Delete a service account<\/li><\/ul>\n\n\n\n<h2 id=\"kubectl-syntax\">Kubectl Syntax<\/h2>\n\n\n\n<p>Kubectl commands adhere to syntax or a common structure, which lets administrators read and verify every Kubectl command entered in the terminal window. There are four important parameters to each Kubectl call:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">kubectl &lt;command> &lt;type> &lt;name> &lt;flags><\/pre>\n\n\n\n<p>The &lt;command&gt; parameter is the operation that should be executed on a resource. Kubectl backs several operations, such as describe, create, get, execute and delete.<\/p>\n\n\n\n<p>The &lt;type> parameter specifies the resource type, like pods, bindings, and nodes. Usually, Resource type designations make use of abbreviations to streamline the command line. For instance, the \u201cpersistentvolumeclaims\u201d type can be shortened to \u201cpvc.\u201d The &lt;type> parameter is strong since there are several resource types, which also include namespaces, services, jobs, resource quotas, replication controllers, leases and events. Programmers and Kubernetes administrators should be acquainted with a complete list of resource types.<\/p>\n\n\n\n<p>The &lt;name> parameter defines the name of the resource in the environment. If we omit the name parameter, the details for all resources are returned, similarly to a wildcard argument. Also, administrators can point out multiple resource types and names in the exact command line, as mentioned below.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">kubectl &lt;command> &lt;type> &lt;name1> &lt;name2> \u2026 &lt;nameX><\/pre>\n\n\n\n<p>This is effective when the names are all the same resource type, for instance:<\/p>\n\n\n\n<p>kubectl get pod test-pod1 test-pod2<\/p>\n\n\n\n<p>Kubectl syntax also backs the combination of several resource types and names on the exact command line in two ways:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">kubectl &lt;command> &lt;type1\/name1> &lt;type2\/name2> \u2026 &lt;typeX\/nameX><\/pre>\n\n\n\n<p>or:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">kubectl get pod\/test-pod1 replicationcontroller\/xyzcorp-rc1<\/pre>\n\n\n\n<p>Lastly, the &lt;flags&gt; parameter incorporates optional flags to the command line. Flags differ with the command, so not all flags are available for all commands. For instance, the -s, (one dash shorthand notation) or &#8211;server (two dashes, longhand notation) flags designate the port and address of the Kubernetes API server.<\/p>\n\n\n\n<p>The -o or &#8211;output &lt;flag&gt; sends responses to a terminal window in a certain format. For instance, the -o yaml flag will output a YAML-formatted API object, whereas the -o json flag will output a JSON-formatted API object.<\/p>\n\n\n\n<h2 id=\"kubectl-best-practices\">Kubectl Best Practices<\/h2>\n\n\n\n<p>The Kubectl command line too seeks a level of specialisation in resource types, operations, and associated syntax. Let\u2019s look at the five considerations that cut down the chances of blunders while working with Kubectl.<br><\/p>\n\n\n\n<h3 id=\"1-know-the-defaults\">1. Know the defaults<\/h3>\n\n\n\n<p>A well-designed CLI will carry out the most frequently used option or path for most operations. Depending on the default when appropriate cut down time and lessen spelling and syntax errors in long and complicated commands. In Kubectl, for instance, the \u201cget\u201d operation offers an \u201call-namespaces\u201d flag, which by default is set to false but can be true or false, hence when admins utilise a \u201cget\u201d operation, it will just list the requested object in the present namespace by default, instead of all namespaces. If this is the intended behaviour, there is no requirement to incorporate the flag and option explicitly, which potentially lessens mistakes and oversights.<\/p>\n\n\n\n<h3 id=\"2-implement-simple-aliases\">2. Implement simple aliases<\/h3>\n\n\n\n<p>Often CLIs utilise readable phrases and terms for complicated resources. Though the aim is to make command line operations, flags, and resources more human-readable, it also incorporates extreme opportunities for spelling errors, specifically where admins frequently use long names. Using precise and effective aliases lets admins utilise the same items having shorter designations, which are less readable but also less susceptible to errors. In Kubectl, for instance, it is much easier and faster to replace the \u201creplicationcontrollers\u201d resource type with the \u201crc\u201d alias, or the \u201chorizontalpodautoscalers\u201d resource type with the \u201chpa\u201d alias. Be persistent with the usage of aliases, especially when several aliases are available to let the script be more readable to others.<\/p>\n\n\n\n<h3 id=\"3-learn-how-to-use-help\">3. Learn how to use help<\/h3>\n\n\n\n<p>Command lines can be complex to remember with complicated syntax and granular options. Administrators and programmers can readily feel overwhelmed when they design or evaluate scripts with commands that call for arcane details or are used rarely. Well-designed CLIs offer an extensive help system. To access kubectl\u2019s help system, utilise kubectl help at the kubectl command line; help unveil the proper syntax and highlight options for sought-after operations. Additionally, the ongoing version reference is promptly available with the version operation. Learn the ways to access logs for debugging and operational details. For Kubectl, the <strong>logs<\/strong> operation reads and shows logs for a container in a pod.<\/p>\n\n\n\n<h3 id=\"4-present-output-at-regular-intervals\">4. Present output at regular intervals<\/h3>\n\n\n\n<p>Kubectl offers a way to output text to a stdout &#8212; standard output &#8212; device, including a terminal window. Incorporate output messages to highlight significant waypoints in the script if a script takes longer to perform as compared to what a user expects. This can restrict users from quitting a script inappropriately as they think the script was hung.<\/p>\n\n\n\n<h3 id=\"5-utilise-the-dry-run-for-testing\">5. Utilise the dry run for testing<\/h3>\n\n\n\n<p>With the dry run option in kubectl, the CLI can step through a script without executing none of the operations. The dry run option helps debugging and script testing. In Kubectl, the <strong>run<\/strong> operation owns a \u201cdry-run=true\/false\u201d flag that helps to test and evades unforeseen consequences on production systems.<\/p>\n\n\n\n<h2 id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>This list is a good starting point for your Kubernetes administrative journey. These commands are comparatively easy, user-friendly, and varied on the basis of overall functionality. Hopefully, it hit on many different Kubernetes components and gave some quick management tips through Kubectl. These commands are extremely powerful and are significant for team members of all skill levels.<\/p>\n\n\n\n<h2 id=\"faqs\">FAQs<\/h2>\n\n\n\n<h3 id=\"q1-what-is-kubectl-used-for\"><span id=\"q-1-what-is-kubectl-used-for\">Q.1: What is Kubectl used for?<\/span><\/h3>\n\n\n\n<p><strong>Ans:<\/strong> Kubectl lets you run commands against Kubernetes clusters. You can make use of Kubectl to deploy applications, inspect and handle cluster resources, and view logs.<\/p>\n\n\n\n<h3 id=\"q2-what-are-the-features-of-kubectl\"><span id=\"q-2-what-are-the-features-of-kubectl\">Q.2: What are the features of Kubectl?<\/span><\/h3>\n\n\n\n<p><strong>Ans:<\/strong><\/p>\n\n\n\n<ul><li>We know Kubectl as the swiss army knife of container management and orchestration<br><\/li><li>It is a huge CLI that runs commands against the Kubernetes cluster and manages the cluster manager<br><\/li><li>Kubectl makes this process more seamless and straightforward<br><\/li><li>Kubectl lets users design, supervise, update, and delete Kubernetes objects<br><\/li><li>Each Kubernetes command has an API<br><\/li><\/ul>\n\n\n\n<h3 id=\"q3-how-does-kubectl-command-work\"><span id=\"q-3-how-does-kubectl-command-work\">Q.3: How does Kubectl command work?<\/span><\/h3>\n\n\n\n<p><strong>Ans:<\/strong> Kubectl, the command-line tool is used to run commands against Kubernetes clusters. It accomplishes this by authenticating with the Master Node of your cluster and making API calls to perform a variety of management actions.<br><br><\/p>\n\n\n\n<h2 id=\"important-resources\">Important Resources<\/h2>\n\n\n\n<ul><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.interviewbit.com\/kubernetes-interview-questions\/\" target=\"_blank\">Kubernetes Interview Questions<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.interviewbit.com\/kubernetes-cheat-sheet\/\" target=\"_blank\">Kubernetes Cheat Sheet<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.interviewbit.com\/blog\/kubectl-commands\/\" target=\"_blank\">Kubectl Commands<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.interviewbit.com\/blog\/kubernetes-vs-docker\/\" target=\"_blank\">Kubernetes vs Docker<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.interviewbit.com\/blog\/openshift-vs-kubernetes\/\" target=\"_blank\">OpenShift Vs Kubernetes<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"If you are going through this article, you are presumably familiar with Kubernetes and wish to interact with&hellip;\n","protected":false},"author":4,"featured_media":8180,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_daextam_enable_autolinks":"1","csco_singular_sidebar":"","csco_page_header_type":"","csco_appearance_grid":"","csco_page_load_nextpost":"","csco_post_video_location":[],"csco_post_video_location_hash":"","csco_post_video_url":"","csco_post_video_bg_start_time":0,"csco_post_video_bg_end_time":0},"categories":[748],"tags":[1362],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/posts\/8176"}],"collection":[{"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/comments?post=8176"}],"version-history":[{"count":7,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/posts\/8176\/revisions"}],"predecessor-version":[{"id":19958,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/posts\/8176\/revisions\/19958"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/media\/8180"}],"wp:attachment":[{"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/media?parent=8176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/categories?post=8176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/tags?post=8176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}