{"id":8926,"date":"2020-01-17T10:35:15","date_gmt":"2020-01-17T10:35:15","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=8926"},"modified":"2021-11-13T10:48:58","modified_gmt":"2021-11-13T10:48:58","slug":"working-with-kubernetes-kubeconfig-file-and-kubectl-config-command-reference","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/working-with-kubernetes-kubeconfig-file-and-kubectl-config-command-reference\/","title":{"rendered":"Kubernetes Commands: kubectl config \u2013 Tutorials and Examples"},"content":{"rendered":"\n<p>The kubectl command-line tool uses kubeconfig files(file named with &#8211; config) to find the information it needs to choose a cluster and communicate with the API server of a cluster.<\/p>\n\n\n\n<p>Note:<br> A file that is used to configure access to clusters is called a kubeconfig file. This is a generic way of referring to configuration files. It does not mean that there is a file named kubeconfig.<\/p>\n\n\n\n<p>How to set kubeconfig file to kubectl?<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Method 1 &#8211; By default, kubectl looks for a file named config in the $HOME\/.kube directory.<\/li><li>Method 2 &#8211; You can specify other kubeconfig files by setting the KUBECONFIG environment variable or <\/li><li>Method 3 &#8211; You can specify other kubeconfig files by setting the &#8211;kubeconfig flag.<\/li><\/ul>\n\n\n\n<p>The loading order follows these rules:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Rules 1 &#8211; If the &#8211;kubeconfig flag is set, then only that file is loaded. The flag may only be set once and no merging takes place.<\/li><li>Rules 2 &#8211; If $KUBECONFIG environment variable is set, then it is used as a list of paths (normal path delimiting rules for your system). These paths are merged. When a value is modified, it is modified in the file that defines the stanza. When a value is created, it is created in the first file that exists. If no files in the chain exist, then it creates the last file in the list.<\/li><li>Rules 3 &#8211; Otherwise, ${HOME}\/.kube\/config is used and no merging takes place.<\/li><\/ul>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">$ kubectl help config\n\nAvailable Commands:\n  current-context Displays the current-context\n  <span class=\"hljs-keyword\">delete<\/span>-cluster  Delete the specified cluster <span class=\"hljs-keyword\">from<\/span> the kubeconfig\n  <span class=\"hljs-keyword\">delete<\/span>-context  Delete the specified context <span class=\"hljs-keyword\">from<\/span> the kubeconfig\n  <span class=\"hljs-keyword\">get<\/span>-clusters    Display clusters defined in the kubeconfig\n  <span class=\"hljs-keyword\">get<\/span>-contexts    Describe one or many contexts\n  rename-context  Renames a context from the kubeconfig file.\n  <span class=\"hljs-keyword\">set<\/span>             Sets an individual value in a kubeconfig file\n  <span class=\"hljs-keyword\">set<\/span>-cluster     Sets a cluster entry in kubeconfig\n  <span class=\"hljs-keyword\">set<\/span>-context     Sets a context entry in kubeconfig\n  <span class=\"hljs-keyword\">set<\/span>-credentials Sets a user entry in kubeconfig\n  unset           Unsets an individual value in a kubeconfig file\n  use-context     Sets the current-context in a kubeconfig file\n  view            Display merged kubeconfig settings or a specified kubeconfig file\n\nDisplays the kubectl config current-context command help.\n$ kubectl config current-context --help\n\n$ kubectl config view \n\n$ kubectl config <span class=\"hljs-keyword\">get<\/span>-contexts --help\n# List all the contexts in your kubeconfig file\n$ kubectl config <span class=\"hljs-keyword\">get<\/span>-contexts\n# Describe one context in your kubeconfig file.\n$ kubectl config <span class=\"hljs-keyword\">get<\/span>-contexts my-context\n\n$ kubectl config <span class=\"hljs-keyword\">get<\/span>-clusters --help \n# List the clusters kubectl knows about\n$ kubectl config <span class=\"hljs-keyword\">get<\/span>-clusters\n\n$ kubectl config use-context --help      \n\n# Use the context for the minikube cluster\n$ kubectl config use-context minikube\n     \n$ kubectl config <span class=\"hljs-keyword\">set<\/span>-cluster --help\n\n# Set only the server field on the e2e cluster entry without touching other values.\n$ kubectl config <span class=\"hljs-keyword\">set<\/span>-cluster e2e --server=https:\/\/1.2.3.4\n\n# Embed certificate authority data for the e2e cluster entry\n$ kubectl config <span class=\"hljs-keyword\">set<\/span>-cluster e2e --certificate-authority=~\/.kube\/e2e\/kubernetes.ca.crt\n\n# Disable cert checking for the dev cluster entry\n$ kubectl config <span class=\"hljs-keyword\">set<\/span>-cluster e2e --insecure-skip-tls-verify=true\n\n$ kubectl config <span class=\"hljs-keyword\">set<\/span>-context --help\n\n# Set the user field on the gce context entry without touching other values\nkubectl config <span class=\"hljs-keyword\">set<\/span>-context gce --user=cluster-admin\n\n$ kubectl config <span class=\"hljs-keyword\">set<\/span>-credentials --help\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$ kubectl options\n      --certificate-authority=<span class=\"hljs-string\">''<\/span>: Path to a cert file <span class=\"hljs-keyword\">for<\/span> the certificate authority\n      --client-certificate=<span class=\"hljs-string\">''<\/span>: Path to a client certificate file <span class=\"hljs-keyword\">for<\/span> TLS\n      --client-key=<span class=\"hljs-string\">''<\/span>: Path to a client key file <span class=\"hljs-keyword\">for<\/span> TLS\n      --cluster=<span class=\"hljs-string\">''<\/span>: The name of the kubeconfig cluster to <span class=\"hljs-keyword\">use<\/span>\n      --<span class=\"hljs-title\">context<\/span>='': <span class=\"hljs-title\">The<\/span> <span class=\"hljs-title\">name<\/span> <span class=\"hljs-title\">of<\/span> <span class=\"hljs-title\">the<\/span> <span class=\"hljs-title\">kubeconfig<\/span> <span class=\"hljs-title\">context<\/span> <span class=\"hljs-title\">to<\/span> <span class=\"hljs-title\">use<\/span>\n      --<span class=\"hljs-title\">insecure<\/span>-<span class=\"hljs-title\">skip<\/span>-<span class=\"hljs-title\">tls<\/span>-<span class=\"hljs-title\">verify<\/span>=<span class=\"hljs-title\">false<\/span>: <span class=\"hljs-title\">If<\/span> <span class=\"hljs-title\">true<\/span>, <span class=\"hljs-title\">the<\/span> <span class=\"hljs-title\">server<\/span>'<span class=\"hljs-title\">s<\/span> <span class=\"hljs-title\">certificate<\/span> <span class=\"hljs-title\">will<\/span> <span class=\"hljs-title\">not<\/span> <span class=\"hljs-title\">be<\/span> <span class=\"hljs-title\">checked<\/span> <span class=\"hljs-title\">for<\/span>\n<span class=\"hljs-title\">validity<\/span>. <span class=\"hljs-title\">This<\/span> <span class=\"hljs-title\">will<\/span> <span class=\"hljs-title\">make<\/span> <span class=\"hljs-title\">your<\/span> <span class=\"hljs-title\">HTTPS<\/span> <span class=\"hljs-title\">connections<\/span> <span class=\"hljs-title\">insecure<\/span>\n      --<span class=\"hljs-title\">kubeconfig<\/span>='': <span class=\"hljs-title\">Path<\/span> <span class=\"hljs-title\">to<\/span> <span class=\"hljs-title\">the<\/span> <span class=\"hljs-title\">kubeconfig<\/span> <span class=\"hljs-title\">file<\/span> <span class=\"hljs-title\">to<\/span> <span class=\"hljs-title\">use<\/span> <span class=\"hljs-title\">for<\/span> <span class=\"hljs-title\">CLI<\/span> <span class=\"hljs-title\">requests<\/span>.\n      --<span class=\"hljs-title\">token<\/span>='': <span class=\"hljs-title\">Bearer<\/span> <span class=\"hljs-title\">token<\/span> <span class=\"hljs-title\">for<\/span> <span class=\"hljs-title\">authentication<\/span> <span class=\"hljs-title\">to<\/span> <span class=\"hljs-title\">the<\/span> <span class=\"hljs-title\">API<\/span> <span class=\"hljs-title\">server<\/span>\n      --<span class=\"hljs-title\">user<\/span>='': <span class=\"hljs-title\">The<\/span> <span class=\"hljs-title\">name<\/span> <span class=\"hljs-title\">of<\/span> <span class=\"hljs-title\">the<\/span> <span class=\"hljs-title\">kubeconfig<\/span> <span class=\"hljs-title\">user<\/span> <span class=\"hljs-title\">to<\/span> <span class=\"hljs-title\">use<\/span>\n      --<span class=\"hljs-title\">username<\/span>='': <span class=\"hljs-title\">Username<\/span> <span class=\"hljs-title\">for<\/span> <span class=\"hljs-title\">basic<\/span> <span class=\"hljs-title\">authentication<\/span> <span class=\"hljs-title\">to<\/span> <span class=\"hljs-title\">the<\/span> <span class=\"hljs-title\">API<\/span> <span class=\"hljs-title\">server<\/span>\n      --<span class=\"hljs-title\">password<\/span>='': <span class=\"hljs-title\">Password<\/span> <span class=\"hljs-title\">for<\/span> <span class=\"hljs-title\">basic<\/span> <span class=\"hljs-title\">authentication<\/span> <span class=\"hljs-title\">to<\/span> <span class=\"hljs-title\">the<\/span> <span class=\"hljs-title\">API<\/span> <span class=\"hljs-title\">server<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Skeleton\/Example of kube config file<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/f8956d4ee208b2519b095ad631eac7a0.js\"><\/script>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_24754\"><iframe loading=\"lazy\"  id=\"_ytid_84432\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_24754\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>The kubectl command-line tool uses kubeconfig files(file named with &#8211; config) to find the information it needs to choose a cluster and communicate with the API server of a cluster&#8230;. <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[4859],"tags":[],"class_list":["post-8926","post","type-post","status-publish","format-standard","hentry","category-kubernetes"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/8926","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=8926"}],"version-history":[{"count":7,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/8926\/revisions"}],"predecessor-version":[{"id":25098,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/8926\/revisions\/25098"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=8926"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=8926"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=8926"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}