{"id":456,"date":"2025-10-01T05:28:13","date_gmt":"2025-10-01T05:28:13","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=456"},"modified":"2025-10-01T05:28:13","modified_gmt":"2025-10-01T05:28:13","slug":"kubectl-aliases","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=456","title":{"rendered":"How to Setup Kubectl Aliases with Kuberc (Native Method)"},"content":{"rendered":"<p>In this blog, you will learn how to set up kubectl aliases using Kubectl user preferences (kuberc) configuration file..<\/p>\n<p>Before Kubernetes v1.33, we had to manually add kubectl aliases in <code>.bashrc<\/code> or <code>.zshrc<\/code>, mixing them with other shell configurations. <\/p>\n<p>This was not a native solution. Starting with Kubernetes v1.33, we can now use a dedicated YAML file called <code>kuberc<\/code> to define aliases. Kubectl directly reads these aliases from the file, making configuration much cleaner<\/p>\n<p>By the end of this blog, you will have learned<\/p>\n<ol>\n<li>Setup the Kuberc on the local machine.<\/li>\n<li>Configure aliases to interact with <a href=\"https:\/\/devopscube.com\/kubernetes-architecture-explained\/\" rel=\"noreferrer\">Kubernetes<\/a>.<\/li>\n<li>Test the configured aliases<\/li>\n<\/ol>\n<p>To setup this, we need to update our <a href=\"https:\/\/devopscube.com\/invalid-apiversion-client-authentication-k8s-io\/\" rel=\"noreferrer\">Kubectl<\/a> with appropriate version.<\/p>\n<h2 id=\"upgrade-kubectl-to-v133\">Upgrade kubectl to v1.33+<\/h2>\n<p>For this feature to work, the Kubectl version should be <code>1.33<\/code> or higher.<\/p>\n<p>Visit the official <a href=\"https:\/\/kubernetes.io\/docs\/tasks\/tools\/install-kubectl-linux\/?ref=devopscube.com\">kubectl documentation<\/a> and upgrade kubectl to version <code>1.33<\/code> or later.<\/p>\n<p>To validate the installed <code>kubectl<\/code> version, run the following command. Make sure the version is 1.33 or higher.<\/p>\n<pre><code class=\"language-bash\">$ kubectl version  --client\n\nClient Version: v1.33.1\nKustomize Version: v5.6.0<\/code><\/pre>\n<p>Once the version is confirmed, we can configure the Kuberc.<\/p>\n<p>The configuration of the Kuberc involves the creation of files and adding alias configurations.<\/p>\n<h2 id=\"enable-kuberc-feature\">Enable Kuberc Feature<\/h2>\n<p>Kubectl will not automatically use the <code>kuberc<\/code> file even after we upgrade the version to 1.33.<\/p>\n<p>To enable this, we need to set an environment variable.<\/p>\n<pre><code class=\"language-bash\">export KUBECTL_KUBERC=true<\/code><\/pre>\n<p>Setting the value <code>true<\/code> is telling the Kubectl to use the kuberc for aliases.<\/p>\n<div class=\"kg-card kg-callout-card kg-callout-card-blue\">\n<div class=\"kg-callout-emoji\">\ud83d\udca1<\/div>\n<div class=\"kg-callout-text\">To permanently set the environment variable, store <code spellcheck=\"false\" style=\"white-space: pre-wrap;\">export KUBECTL_KUBERC<\/code> to the system files, <code spellcheck=\"false\" style=\"white-space: pre-wrap;\">\/.bashrc<\/code><sub style=\"white-space: pre-wrap;\"> or <\/sub><code spellcheck=\"false\" style=\"white-space: pre-wrap;\">\/.bash-profile<\/code> or <code spellcheck=\"false\" style=\"white-space: pre-wrap;\">~\/.profile<\/code>.<\/div>\n<\/div>\n<h2 id=\"kuberc-file-location\">Kuberc File Location<\/h2>\n<p>The default <code>kuberc<\/code> configuration file path is <code>~\/.kube\/kuberc<\/code><\/p>\n<p>When you enable the Kuberc feature using the environment variable, Kubectl will look into the default location.<\/p>\n<p>If you want to give a custom path, you need to use the <code>--kuberc<\/code> flag with the <code>kubectl<\/code> command to give the configuration path.<\/p>\n<pre><code>kubectl --kuberc &lt;CUSTOM KUBERC CONFIG PATH&gt; &lt;COMMAND&gt;<\/code><\/pre>\n<div class=\"kg-card kg-callout-card kg-callout-card-blue\">\n<div class=\"kg-callout-emoji\">\ud83d\udca1<\/div>\n<div class=\"kg-callout-text\">The <code spellcheck=\"false\" style=\"white-space: pre-wrap;\">--kuberc<\/code> flag lets you use different kuberc files for various projects or environments.<\/p>\n<p>If you use this flag, Kubectl ignores the default kuberc instead it uses the specified one.<\/p><\/div>\n<\/div>\n<h2 id=\"create-a-kuberc-file\">Create a Kuberc File<\/h2>\n<p>The <code>kuberc<\/code> configuration is also similar to the <a href=\"https:\/\/devopscube.com\/kubernetes-objects-resources\/\" rel=\"noreferrer\">Kubernetes object<\/a> manifest, which means the configuration has the <code>apiVersion<\/code> and the <code>kind<\/code>.<\/p>\n<p>Create a YAML file in the home directory (<code>~\/.kube\/kuberc<\/code>)<\/p>\n<pre><code>vim ~\/.kube\/kuberc<\/code><\/pre>\n<p>Once the file is created, we can add our necessary aliases to it.<\/p>\n<h3 id=\"example-kuberc-configuration\">Example Kuberc Configuration<\/h3>\n<p>Here is the example structure of the <code>kuberc<\/code> YAML configuration.<\/p>\n<pre><code class=\"language-yaml\">apiVersion: kubectl.config.k8s.io\/v1beta1\nkind: Preference\naliases:\n  - name: kgp           \n    command: get        \n    appendArgs:\n      - pods            \n  - name: klogs         \n    command: logs       \n    appendArgs:\n      - --follow    \n      - --tail=50           \n  - name: crns          \n    command: create namespace \n    appendArgs:\n      - dev-project-01\n\noverrides:\n  - command: apply\n    flags:\n      - name: server-side\n        default: \"true\"\n  - command: delete\n    flags:\n      - name: interactive\n        default: \"true\"\n  - command: get\n    flags:\n      - name: output\n        default: \"yaml\"\n  - command: exec\n    flags:\n      - name: it<\/code><\/pre>\n<p>Since this is an beta feature of the configuration, the <code>apiVersion<\/code> is <code>kubectl.config.k8s.io\/v1beta1<\/code> and the <code>kind<\/code> is <code>Preference<\/code><\/p>\n<p>This indicates that the object is the user&#8217;s preferences for kubectl.<\/p>\n<p>Now, you need to understand the following two key configs in kuberc.<\/p>\n<h3 id=\"1-aliases\">1. aliases<\/h3>\n<p>Here is what the parameters under <code>aliases<\/code> section mean.<\/p>\n<ul>\n<li><code>name<\/code> &#8211;&gt; The alias of the command that we want to use<\/li>\n<li><code>command<\/code> &#8211;&gt; Refers to what action should be performed, like <code>get<\/code>, <code>apply<\/code> or <code>delete<\/code>.<\/li>\n<li><code>appendArgs<\/code> &#8211;&gt; Refers to the object that we want to use with the action, like <a href=\"https:\/\/devopscube.com\/kubernetes-pod\/\">pods<\/a>, <a href=\"https:\/\/devopscube.com\/kubernetes-deployment-tutorial\/\" rel=\"noreferrer\">deployments<\/a>, secrets, etc.<\/li>\n<\/ul>\n<p>In the <code>kuberc<\/code> configuration, we can override the alias.<\/p>\n<h3 id=\"2-overrides\">2. Overrides<\/h3>\n<p>Ovverides modify default behavior of certain commands.<\/p>\n<p>For example, the alias <code>kgp<\/code> runs <code>kubectl get po<\/code>. But what if I want to run,<\/p>\n<pre><code class=\"language-bash\">kubectl get po my-pod -o yaml<\/code><\/pre>\n<p>This is where the <strong>override<\/strong> function comes in.<\/p>\n<p>In the overrides section, you can define flags as key-value pairs to set conditions like <code>output<\/code>,  <code>server-side<\/code> and <code>interactive<\/code>, <\/p>\n<ol>\n<li><code>output<\/code> &#8211; Show the output of the object in a specific format like JSON or YAML.<\/li>\n<li><code>server-side<\/code> &#8211; Execute the command without confirmation<\/li>\n<li><code>interactive<\/code> &#8211; Before execution, prompt for confirmation<\/li>\n<li><code>it<\/code>: Attach <code>-it<\/code> flags to exec commands<\/li>\n<\/ol>\n<p>Now we have added our aliases to a file. But keep in mind, by default the <code>kuberc<\/code> feature is disabled on the system.<\/p>\n<h2 id=\"validating-kuberc\">Validating Kuberc<\/h2>\n<p>Now, we have enabled the Kuberc feature and created the Kuberc configuration file in <code>~\/.kube\/kuberc<\/code>.<\/p>\n<p>We can try the aliases from the given configuration.<\/p>\n<pre><code>kubectl crns<\/code><\/pre>\n<p>The above command will create a new namespace named <code>dev-project-01<\/code><\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/05\/image-92.png\" class=\"kg-image\" alt=\"the output of the namespace creation using the kuberc alias\" loading=\"lazy\" width=\"960\" height=\"390\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/05\/image-92.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/05\/image-92.png 960w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<p>Let&#8217;s try another one from the <code>override<\/code> configuration.<\/p>\n<pre><code>kubectl delete ns dev-project-01 <\/code><\/pre>\n<p>This command will prompt for confirmation<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/05\/image-93.png\" class=\"kg-image\" alt=\"the output of the prompt for the confirmation of the alias execution\" loading=\"lazy\" width=\"1342\" height=\"450\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/05\/image-93.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w1000\/2025\/05\/image-93.png 1000w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/05\/image-93.png 1342w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<p>The validation ensures that the Kuberc feature is successfully enabled.<\/p>\n<h3 id=\"combining-kuberc-with-shell-aliases-and-autocompletion\">Combining&nbsp;Kuberc&nbsp;with Shell Aliases and Autocompletion<\/h3>\n<p>We can still use the <code>.bashrc<\/code> or <code>.zshrc<\/code> along with <code>kuberc<\/code> for better convenience.<\/p>\n<p>For example, set <code>alias k=kubectl<\/code> in <code>.bashrc<\/code> so that we can use <code>k kgp<\/code> instead of using <code>kubectl kgp<\/code>.<\/p>\n<p>Also, if you customize only a few commands in <code>kuberc<\/code> we can use the rest of the aliases from the traditional one.<\/p>\n<p>It works with autocompletion as well. So, simply pressing the Tab key will fill in the commands.<\/p>\n<h3 id=\"disabling-kuberc\">Disabling Kuberc<\/h3>\n<p>After the configuration, if you feel like you want to disable Kuberc temporarily, you can do that using the following variable.<\/p>\n<pre><code class=\"language-bash\">export KUBERC=off\n<\/code><\/pre>\n<p>If you are using feature gate,<\/p>\n<pre><code class=\"language-bash\">export KUBECTL_KUBERC=true<\/code><\/pre>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>In this blog, we explored how to enable and configure the new <code>kuberc<\/code> feature to enable kubectl alias.<\/p>\n<p>Using <code>kuberc<\/code>, you can define aliases in a clean, YAML-based configuration file instead of cluttering your shell profiles. <\/p>\n<p>You can even version-control your <code>kuberc<\/code> files on GitHub for collaboration<\/p>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/kubectl-aliases\/\" target=\"_blank\" rel=\"noopener noreferrer\">How to Setup Kubectl Aliases with Kuberc (Native Method) \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/kubectl-aliases\/<\/p>\n","protected":false},"author":1,"featured_media":457,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-456","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/456","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=456"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/456\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/457"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}