|
| 1 | +--- |
| 2 | +title: Kubectl user preferences (kuberc) |
| 3 | +content_type: concept |
| 4 | +weight: 70 |
| 5 | +--- |
| 6 | + |
| 7 | +{{< feature-state state="alpha" for_k8s_version="1.33" >}} |
| 8 | + |
| 9 | +A Kubernetes `kuberc` configuration file allows you to define preferences for kubectl, such as default options and command aliases. |
| 10 | +Unlike the kubeconfig file, a `kuberc` configuration file does **not** contain cluster details, usernames or passwords. |
| 11 | + |
| 12 | +The default location of this configuration file is `$HOME/.kube/kuberc`. |
| 13 | +You can instruct `kubectl` to look at a custom path for this configuration using the `--kuberc` command line argument. |
| 14 | + |
| 15 | +## aliases |
| 16 | + |
| 17 | +Within a `kuberc` configuration, _aliases_ allow you to define custom shortcuts |
| 18 | +for kubectl commands, optionally with preset command line arguments. |
| 19 | + |
| 20 | +### name |
| 21 | + |
| 22 | +Alias name must not collide with the built-in commands. |
| 23 | + |
| 24 | +### command |
| 25 | + |
| 26 | +Specify the underlying built-in command that your alias will execute. This includes support for subcommands like `create role`. |
| 27 | + |
| 28 | +### flags |
| 29 | + |
| 30 | +Specify default values for command line arguments (which the kuberc format terms _flags_). |
| 31 | +If you explicitly specify a command line argument when you run kubectl, the value you provide takes precedence over the default one defined in kuberc. |
| 32 | + |
| 33 | +#### Example: |
| 34 | + |
| 35 | +```yaml |
| 36 | +apiVersion: kubectl.config.k8s.io/v1alpha1 |
| 37 | +kind: Preference |
| 38 | +aliases: |
| 39 | +- name: getn |
| 40 | + command: get |
| 41 | + flags: |
| 42 | + - name: output |
| 43 | + default: json |
| 44 | +``` |
| 45 | +
|
| 46 | +With this alias, running `kubectl getn pods` will default JSON output. However, if you execute `kubectl getn pods -oyaml`, the output will be in YAML format. |
| 47 | + |
| 48 | +### prependArgs |
| 49 | + |
| 50 | +Insert arbitrary arguments immediately after the kubectl command and its subcommand (if any). |
| 51 | + |
| 52 | +#### Example: |
| 53 | + |
| 54 | +```yaml |
| 55 | +apiVersion: kubectl.config.k8s.io/v1alpha1 |
| 56 | +kind: Preference |
| 57 | +aliases: |
| 58 | + - name: getn |
| 59 | + command: get |
| 60 | + prependArgs: |
| 61 | + - namespace |
| 62 | + flags: |
| 63 | + - name: output |
| 64 | + default: json |
| 65 | +``` |
| 66 | + |
| 67 | +`kubectl getn test-ns` will be translated to `kubectl get namespace test-ns --output json`. |
| 68 | + |
| 69 | +### appendArgs |
| 70 | + |
| 71 | +Append arbitrary arguments to the end of the kubectl command. |
| 72 | + |
| 73 | +#### Example: |
| 74 | + |
| 75 | +```yaml |
| 76 | +apiVersion: kubectl.config.k8s.io/v1alpha1 |
| 77 | +kind: Preference |
| 78 | +aliases: |
| 79 | +- name: runx |
| 80 | + command: run |
| 81 | + flags: |
| 82 | + - name: image |
| 83 | + default: busybox |
| 84 | + - name: namespace |
| 85 | + default: test-ns |
| 86 | + appendArgs: |
| 87 | + - -- |
| 88 | + - custom-arg |
| 89 | +``` |
| 90 | + |
| 91 | +`kubectl runx test-pod` will be translated to `kubectl run test-pod --namespace test-ns --image busybox -- custom-arg`. |
| 92 | + |
| 93 | +## Command Overrides |
| 94 | + |
| 95 | +Within a `kuberc` configuration, _command overrides_ let you specify custom values for command line arguments. |
| 96 | + |
| 97 | +### command |
| 98 | + |
| 99 | +Specify the built-in command. This includes support for subcommands like `create role`. |
| 100 | + |
| 101 | +### flags |
| 102 | + |
| 103 | +Within a `kuberc`, configuration, command line arguments are termed _flags_ (even if they do not represent a boolean type). |
| 104 | +You can use `flags` to set the default value of a command line argument. |
| 105 | + |
| 106 | + If you explicitly specify a flag on your terminal, explicit value will always take precedence over the value you defined in kuberc using `overrides`. |
| 107 | + |
| 108 | + {{< note >}} |
| 109 | + You cannot use `kuberc` to override the value of a command line argument to take precedence over what the user specifies on the command line. The term `overrides` |
| 110 | + in this context refers to specifying a default value that is different from the |
| 111 | + compiled-in default value. |
| 112 | + {{< /note >}} |
| 113 | + |
| 114 | +#### Example: |
| 115 | + |
| 116 | +```yaml |
| 117 | +apiVersion: kubectl.config.k8s.io/v1alpha1 |
| 118 | +kind: Preference |
| 119 | +overrides: |
| 120 | +- command: delete |
| 121 | + flags: |
| 122 | + - name: interactive |
| 123 | + default: "true" |
| 124 | +``` |
| 125 | + |
| 126 | +With this override, running `kubectl delete pod/test-pod` will default to prompting for confirmation. |
| 127 | +However, `kubectl delete pod/test-pod --interactive=false` will bypass the confirmation. |
| 128 | + |
| 129 | +The kubectl maintainers encourage you to adopt kuberc with the given defaults: |
| 130 | + |
| 131 | +```yaml |
| 132 | +apiVersion: kubectl.config.k8s.io/v1alpha1 |
| 133 | +kind: Preference |
| 134 | +overrides: |
| 135 | + - command: apply |
| 136 | + flags: |
| 137 | + - name: server-side |
| 138 | + default: "true" |
| 139 | + - command: delete |
| 140 | + flags: |
| 141 | + - name: interactive |
| 142 | + default: "true" |
| 143 | +``` |
| 144 | + |
| 145 | +## Disable kuberc |
| 146 | + |
| 147 | +To temporarily disable the kuberc functionality, simply export the environment variable `KUBERC` with the value `off`: |
| 148 | + |
| 149 | +```shell |
| 150 | +export KUBERC=off |
| 151 | +``` |
| 152 | + |
| 153 | +or disable the feature gate: |
| 154 | + |
| 155 | +```shell |
| 156 | +export KUBECTL_KUBERC=false |
| 157 | +``` |
0 commit comments