{"id":181723,"date":"2021-02-18T17:00:30","date_gmt":"2021-02-18T14:00:30","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/getting-started-with-kubectl-to-manage-kubernetes-clusters-cloudsavvy-it\/"},"modified":"2021-02-18T17:00:30","modified_gmt":"2021-02-18T14:00:30","slug":"getting-started-with-kubectl-to-manage-kubernetes-clusters-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/getting-started-with-kubectl-to-manage-kubernetes-clusters-cloudsavvy-it\/","title":{"rendered":"#Getting Started With Kubectl to Manage Kubernetes Clusters \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a40e4c937f51\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a40e4c937f51\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/getting-started-with-kubectl-to-manage-kubernetes-clusters-cloudsavvy-it\/#Installing_Kubectl\" >Installing Kubectl<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/getting-started-with-kubectl-to-manage-kubernetes-clusters-cloudsavvy-it\/#Configuration\" >Configuration<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/getting-started-with-kubectl-to-manage-kubernetes-clusters-cloudsavvy-it\/#Contexts\" >Contexts<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/getting-started-with-kubectl-to-manage-kubernetes-clusters-cloudsavvy-it\/#Interacting_With_Your_Cluster\" >Interacting With Your Cluster<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/getting-started-with-kubectl-to-manage-kubernetes-clusters-cloudsavvy-it\/#Output_Formats\" >Output Formats<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/getting-started-with-kubectl-to-manage-kubernetes-clusters-cloudsavvy-it\/#Using_Within_Scripts\" >Using Within Scripts<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/getting-started-with-kubectl-to-manage-kubernetes-clusters-cloudsavvy-it\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#Getting Started With Kubectl to Manage Kubernetes Clusters \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9632\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/06268c7060f33c17d0b8db5f1f90faf9\/p\/uploads\/2021\/02\/748108a6.jpg\" alt=\"Graphic showing the Kubernetes logo\" width=\"1602\" height=\"902\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Kubernetes is a container orchestration engine that lets you deploy containerised workloads in a scalable way. The official command-line utility, <code>kubectl<\/code>, provides control over your clusters and the resources within.<\/p>\n<h2 id=\"installing-kubectl\"><span class=\"ez-toc-section\" id=\"Installing_Kubectl\"><\/span>Installing Kubectl<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>kubectl<\/code> is supported on Linux, macOS and Windows. Several distribution formats are offered depending on the platform. Precompiled binaries are produced for all supported operating systems and made available via direct <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">download<\/a> links.<\/p>\n<p>You\u2019ll also find <code>kubectl<\/code> within the Snap, Homebrew, Chocolatey and Scoop package managers. It can be installed via <code>apt<\/code> and <code>yum<\/code> by adding the Google Cloud repository to your system. Finally, <code>kubectl<\/code> is also provided with the Google Cloud SDK \u2013 if you already have that installed, run <code>gcloud components install kubectl<\/code> to download the tool.<\/p>\n<p>You should refer to the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/tasks\/tools\/install-kubectl\">official installation guide<\/a> to see the available options for your system. Installation steps can change over time so review the documentation before reinstalling <code>kubectl<\/code>.<\/p>\n<h2 id=\"configuration\"><span class=\"ez-toc-section\" id=\"Configuration\"><\/span>Configuration<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Configuration is stored within the <code>.kube<\/code> directory in your home folder. The default configuration file is <code>~\/.kube\/config<\/code>.<\/p>\n<p>You can add additional files to this folder. They\u2019ll be <em>merged<\/em> together into the final configuration used during runtime. If you want to use a specific configuration file, or a different set of files, you need to set the <code>--kubeconfig<\/code> flag or the <code>KUBECONFIG<\/code> environment variable.<\/p>\n<pre>kubectl --kubeconfig=\/example\/file get pods&#13;\n# OR&#13;\nKUBECONFIG=\/example\/file kubectl get pods<\/pre>\n<p>All paths written within configuration files are resolved relative to the file\u2019s own location. Paths passed to command-line flags are resolved relative to your working directory. You can view the final configuration which will be used by <code>kubectl<\/code> by running <code>kubectl config view<\/code>.<\/p>\n<p>Command line <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/concepts\/configuration\/organize-cluster-access-kubeconfig\">flags are supported<\/a> for some settings. These let you override your configuration files. Available flags include <code>--server<\/code> (cluster URL), <code>--username<\/code> (username to connect as), <code>--password<\/code>, <code>--token<\/code> (API token) and <code>--namespace<\/code> (select the cluster namespace to interact with).<\/p>\n<h2 id=\"contexts\"><span class=\"ez-toc-section\" id=\"Contexts\"><\/span>Contexts<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Within configuration files, you can define multiple \u201ccontexts.\u201d These allow you to group frequently used \u201caccess parameters,\u201d such as cluster URL and user accounts, under a named reference.<\/p>\n<p>To configure per-context settings, use <code>kubectl config set-context my-context --cluster=my-app --namespace=production<\/code>. This example would create a new context called <code>my-context<\/code> that defines default settings for the Kubernetes cluster and namespace to work with.<\/p>\n<p>Contexts are applied using the <code>kubectl config use-context my-context<\/code> command. Any further invocations of <code>kubectl<\/code> would use the parameters of the <code>my-context<\/code> context, so you\u2019d be connected to the <code>my-app<\/code> cluster in the <code>production<\/code> namespace.<\/p>\n<p>Effective use of contexts considerably simplifies interactions with <code>kubectl<\/code>. Without them, you have to manually create unique configuration files that are switched using the <code>KUBECONFIG<\/code> flag or environment variable.<\/p>\n<h2 id=\"interacting-with-your-cluster\"><span class=\"ez-toc-section\" id=\"Interacting_With_Your_Cluster\"><\/span>Interacting With Your Cluster<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Most <code>kubectl<\/code> commands use the same basic format:<\/p>\n<pre>kubectl command type name<\/pre>\n<p>The <code>command<\/code> is the operation you want to perform \u2013 usually <code>create<\/code>, <code>get<\/code>, <code>describe<\/code> or <code>delete<\/code>. The <code>type<\/code> is the kind of resource you\u2019re going to interact with, such as <code>pod<\/code> or <code>deployment<\/code>. You may use either the singular or plural form.<\/p>\n<p>The <code>name<\/code> component should be the name of the resource you\u2019re referencing. You can specify multiple names, separated by spaces, to get output in bulk. It\u2019s also possible to use the <code>-f<\/code> flag to specify the path to a JSON or YAML file containing a list of resource names.<\/p>\n<p>Here\u2019s some example commands, all of which work against your currently selected context:<\/p>\n<ul>\n<li><strong><code>kubectl get pods<\/code><\/strong> \u2013 Get the details of all your pods<\/li>\n<li><strong><code>kubectl get pod my-pod<\/code><\/strong> \u2013 Get the details of the pod called <code>my-pod<\/code><\/li>\n<li><strong><code>kubectl get pod my-pod another-pod<\/code><\/strong> \u2013 Get the details of the pods named <code>my-pod<\/code> and <code>another-pod<\/code><\/li>\n<li><strong><code>kubectl get pod\/my-pod deployment\/my-deployment<\/code><\/strong> \u2013 Retrieve the details of the pod called <code>my-pod<\/code> and the deployment called <code>my-deployment<\/code> \u2013 this syntax variation allows you to retrieve multiple resource types with one command<\/li>\n<li><strong><code>kubectl delete pod my-pod<\/code><\/strong> \u2013 Delete the pod called <code>my-pod<\/code><\/li>\n<li><strong><code>kubectl logs my-pod<\/code><\/strong> \u2013 Get log output from the <code>my-pod<\/code> pod<\/li>\n<li><strong><code>kubectl apply -f .\/manifest.yml<\/code><\/strong> \u2013 Apply a patch to your cluster from the Kubernetes manifest stored in <code>manifest.yml<\/code><\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9634\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/cbae522556ae700d4a2cd1762e8931be\/p\/uploads\/2021\/02\/26472354.jpg\" alt=\"\" width=\"967\" height=\"206\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Commands are available for all the resource types offered by your Kubernetes cluster. This even extends to custom resource definitions; they integrate with the Kubernetes API and get their own RESTful endpoints which <code>kubectl<\/code> can access.<\/p>\n<p>A complete <code>kubectl<\/code> <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/reference\/kubectl\/overview\">command reference<\/a> is available within the Kubernetes documentation. There\u2019s also a <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/reference\/kubectl\/cheatsheet\">cheat sheet<\/a> of commonly used commands when working with typical resource types.<\/p>\n<h2 id=\"output-formats\"><span class=\"ez-toc-section\" id=\"Output_Formats\"><\/span>Output Formats<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Output is usually emitted as a formatted list or table. This presents information in a human-readable style which you can quickly skim through.<\/p>\n<p>Several alternative <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/reference\/kubectl\/overview\/#formatting-output\">output options<\/a> are available. You can switch to a different formatter using the <code>-o<\/code> (or <code>--output<\/code>) flag.<\/p>\n<p>The <code>json<\/code> output style displays the JSON representation of the Kubernetes API resource you\u2019re accessing. Similarly, <code>yaml<\/code> gives you the resource\u2019s data as a YAML representation.<\/p>\n<p>When using the human-readable style, you can specify the table columns to include using the <code>custom-columns<\/code> style. Provide a comma-separated list of column name and value reference pairs:<\/p>\n<pre>kubectl get pods -o custom-columns=NAME:.metadata.name,NAMESPACE:.metadata.namespace<\/pre>\n<p>This would display the name and namespace of each pod in columns labelled <code>NAME<\/code> and <code>NAMESPACE<\/code> respectively. Instead of writing columns inline in your command, you can define them in a file and pass it to <code>--custom-columns-file<\/code>.<\/p>\n<p>There\u2019s built-in support for sorting output by the value of a particular field. Use <code>--sort-by<\/code>, passing the field\u2019s reference:<\/p>\n<pre>kubectl get pods --sort-by=.metadata.name<\/pre>\n<p>Sorting supports <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/reference\/kubectl\/jsonpath\">JSONPath expressions<\/a>. These can also be used to construct filtered queries using the <code>jsonpath<\/code> formatter. JSONPath is a query language for JSON objects which lets you more directly manipulate Kubernetes API queries in <code>kubectl<\/code>.<\/p>\n<h2 id=\"using-within-scripts\"><span class=\"ez-toc-section\" id=\"Using_Within_Scripts\"><\/span>Using Within Scripts<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>kubectl<\/code> is intended for both direct human interaction and programmatic invocation via scripts. There are some <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/reference\/kubectl\/conventions\">best practices<\/a> you should follow when scripting <code>kubectl<\/code> to ensure predictable output.<\/p>\n<p>Fully qualify references to resource types so that they\u2019re pinned to a particular version \u2013 e.g.\u00a0<code>pods.v1.core\/my-job<\/code> instead of <code>pods my-job<\/code>. This minimises the risk of Kubernetes updates breaking your script.<\/p>\n<p>Access configuration should be passed directly from your script to <code>kubectl<\/code>, ensuring you\u2019re not dependent on the outside environment. This further reduces the risk of breakage due to <code>kubectl<\/code> updates \u2013 features such as contexts may change over time, while it\u2019s less likely core command-line arguments will.<\/p>\n<p>Finally, make sure you disable human-readable output and use the JSON or YAML formatter instead. This gives your script machine-oriented data to work with, so you won\u2019t need to parse tables yourself.<\/p>\n<h2 id=\"conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>kubectl<\/code> is the go-to solution for managing a Kubernetes cluster. It\u2019s a comprehensive tool with full support for the platform\u2019s capabilities.<\/p>\n<p>The breadth of functionality makes for a lengthy command list but the clear separation between action type, resource type and resource name helps keep usage simple and memorable. If in doubt, you can <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/tasks\/tools\/install-kubectl\/#enabling-shell-autocompletion\">install shell autocompletion<\/a> to help you find an appropriate command.\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/9618\/getting-started-with-kubectl-to-manage-kubernetes-clusters\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#Getting Started With Kubectl to Manage Kubernetes Clusters \u2013 CloudSavvy IT&#8221; Kubernetes is a container orchestration engine that lets you deploy containerised workloads in a scalable way. The official command-line utility, kubectl, provides control over your clusters and the resources within. Installing Kubectl kubectl is supported on Linux, macOS and Windows. Several distribution formats are&#8230;<\/p>\n","protected":false},"author":1,"featured_media":181724,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/06268c7060f33c17d0b8db5f1f90faf9\/p\/uploads\/2021\/02\/748108a6.jpg","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-181723","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/181723","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=181723"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/181723\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/181724"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=181723"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=181723"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=181723"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}