|
| 1 | +--- |
| 2 | +title: kubectl 用户偏好设置(kuberc) |
| 3 | +content_type: concept |
| 4 | +weight: 70 |
| 5 | +--- |
| 6 | +<!-- |
| 7 | +title: Kubectl user preferences (kuberc) |
| 8 | +content_type: concept |
| 9 | +weight: 70 |
| 10 | +--> |
| 11 | + |
| 12 | +{{< feature-state state="alpha" for_k8s_version="1.33" >}} |
| 13 | + |
| 14 | +<!-- |
| 15 | +A Kubernetes `kuberc` configuration file allows you to define preferences for kubectl, such as default options and command aliases. |
| 16 | +Unlike the kubeconfig file, a `kuberc` configuration file does **not** contain cluster details, usernames or passwords. |
| 17 | +--> |
| 18 | +Kubernetes `kuberc` 配置文件允许你定义 kubectl 的偏好设置,例如默认选项和命令别名。 |
| 19 | +与 kubeconfig 文件不同,`kuberc` 配置文件**不**包含集群详情、用户名或密码。 |
| 20 | + |
| 21 | +<!-- |
| 22 | +The default location of this configuration file is `$HOME/.kube/kuberc`. |
| 23 | +You can instruct `kubectl` to look at a custom path for this configuration using the `--kuberc` command line argument. |
| 24 | +--> |
| 25 | +此配置文件的默认位置是 `$HOME/.kube/kuberc`。 |
| 26 | +你可以使用 `--kuberc` 命令行参数指示 `kubectl` 查找此配置的自定义路径。 |
| 27 | + |
| 28 | +<!-- |
| 29 | +## aliases |
| 30 | +
|
| 31 | +Within a `kuberc` configuration, _aliases_ allow you to define custom shortcuts |
| 32 | +for kubectl commands, optionally with preset command line arguments. |
| 33 | +--> |
| 34 | +## aliases |
| 35 | + |
| 36 | +在 `kuberc` 配置中,**别名(aliases)** 允许你为 kubectl 命令定义自定义快捷方式, |
| 37 | +并且可以带有预设的命令行参数。 |
| 38 | + |
| 39 | +<!-- |
| 40 | +### name |
| 41 | +
|
| 42 | +Alias name must not collide with the built-in commands. |
| 43 | +--> |
| 44 | +### name |
| 45 | + |
| 46 | +别名名称不能与内置命令冲突。 |
| 47 | + |
| 48 | +<!-- |
| 49 | +### command |
| 50 | +
|
| 51 | +Specify the underlying built-in command that your alias will execute. This includes support for subcommands like `create role`. |
| 52 | +--> |
| 53 | +### command |
| 54 | + |
| 55 | +指定你的别名将执行的底层内置命令。这包括对如 `create role` 等子命令的支持。 |
| 56 | + |
| 57 | +<!-- |
| 58 | +### flags |
| 59 | +
|
| 60 | +Specify default values for command line arguments (which the kuberc format terms _flags_). |
| 61 | +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. |
| 62 | +--> |
| 63 | +### flags |
| 64 | + |
| 65 | +为命令行参数指定默认值(kuberc 格式中称为 **标志(flags)**)。 |
| 66 | +如果你在运行 kubectl 时明确指定了命令行参数,那么你提供的值将优先于 kuberc 中定义的默认值。 |
| 67 | + |
| 68 | +<!-- |
| 69 | +#### Example {#flags-example} |
| 70 | +--> |
| 71 | +#### 示例 {#flags-example} |
| 72 | + |
| 73 | +```yaml |
| 74 | +apiVersion: kubectl.config.k8s.io/v1alpha1 |
| 75 | +kind: Preference |
| 76 | +aliases: |
| 77 | +- name: getn |
| 78 | + command: get |
| 79 | + flags: |
| 80 | + - name: output |
| 81 | + default: json |
| 82 | +``` |
| 83 | +
|
| 84 | +<!-- |
| 85 | +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. |
| 86 | +--> |
| 87 | +使用此别名,运行 `kubectl getn pods` 将默认输出 JSON 格式。然而, |
| 88 | +如果你执行 `kubectl getn pods -oyaml`,输出将会是 YAML 格式。 |
| 89 | + |
| 90 | +<!-- |
| 91 | +### prependArgs |
| 92 | + |
| 93 | +Insert arbitrary arguments immediately after the kubectl command and its subcommand (if any). |
| 94 | + |
| 95 | +#### Example {#prependArgs-example} |
| 96 | +--> |
| 97 | +### prependArgs |
| 98 | + |
| 99 | +在 kubectl 命令及其子命令(如果有)之后立即插入任意参数。 |
| 100 | + |
| 101 | +#### 示例 {#prependArgs-example} |
| 102 | + |
| 103 | +```yaml |
| 104 | +apiVersion: kubectl.config.k8s.io/v1alpha1 |
| 105 | +kind: Preference |
| 106 | +aliases: |
| 107 | + - name: getn |
| 108 | + command: get |
| 109 | + prependArgs: |
| 110 | + - namespace |
| 111 | + flags: |
| 112 | + - name: output |
| 113 | + default: json |
| 114 | +``` |
| 115 | + |
| 116 | +<!-- |
| 117 | +`kubectl getn test-ns` will be translated to `kubectl get namespace test-ns --output json`. |
| 118 | + |
| 119 | +### appendArgs |
| 120 | + |
| 121 | +Append arbitrary arguments to the end of the kubectl command. |
| 122 | + |
| 123 | +#### Example {#appendArgs-example} |
| 124 | +--> |
| 125 | +`kubectl getn test-ns` 将被翻译为 `kubectl get namespace test-ns --output json`。 |
| 126 | + |
| 127 | +### appendArgs |
| 128 | + |
| 129 | +将任意参数添加到 kubectl 命令的末尾。 |
| 130 | + |
| 131 | +#### 示例 {#appendArgs-example} |
| 132 | + |
| 133 | +```yaml |
| 134 | +apiVersion: kubectl.config.k8s.io/v1alpha1 |
| 135 | +kind: Preference |
| 136 | +aliases: |
| 137 | +- name: runx |
| 138 | + command: run |
| 139 | + flags: |
| 140 | + - name: image |
| 141 | + default: busybox |
| 142 | + - name: namespace |
| 143 | + default: test-ns |
| 144 | + appendArgs: |
| 145 | + - -- |
| 146 | + - custom-arg |
| 147 | +``` |
| 148 | + |
| 149 | +<!-- |
| 150 | +`kubectl runx test-pod` will be translated to `kubectl run test-pod --namespace test-ns --image busybox -- custom-arg`. |
| 151 | +--> |
| 152 | +`kubectl runx test-pod` 将被翻译为 |
| 153 | +`kubectl run test-pod --namespace test-ns --image busybox -- custom-arg`. |
| 154 | + |
| 155 | +<!-- |
| 156 | +## Command overrides |
| 157 | + |
| 158 | +Within a `kuberc` configuration, _command overrides_ let you specify custom values for command line arguments. |
| 159 | + |
| 160 | +### command |
| 161 | + |
| 162 | +Specify the built-in command. This includes support for subcommands like `create role`. |
| 163 | +--> |
| 164 | +## 命令覆盖 |
| 165 | + |
| 166 | +在 `kuberc` 配置中,**命令覆盖**允许你为命令行参数指定自定义值。 |
| 167 | + |
| 168 | +### command |
| 169 | + |
| 170 | +指定内置命令。这包括对如 `create role` 等子命令的支持。 |
| 171 | + |
| 172 | +<!-- |
| 173 | +### flags |
| 174 | + |
| 175 | +Within a `kuberc`, configuration, command line arguments are termed _flags_ (even if they do not represent a boolean type). |
| 176 | +You can use `flags` to set the default value of a command line argument. |
| 177 | +--> |
| 178 | +### flags |
| 179 | + |
| 180 | +在 `kuberc` 配置中,命令行参数被称为 **标志(flags)**(即使它们不代表布尔类型)。 |
| 181 | +你可以使用 `flags` 来设置命令行参数的默认值。 |
| 182 | + |
| 183 | +<!-- |
| 184 | +If you explicitly specify a flag on your terminal, explicit value will always take precedence over the value you defined in kuberc using `overrides`. |
| 185 | +--> |
| 186 | +如果你在终端中显式地指定了一个**标志**,那么显式指定的值将始终优先于你在 |
| 187 | +`kuberc` 中使用 `overrides` 定义的值。 |
| 188 | +{{< note >}} |
| 189 | + |
| 190 | +<!-- |
| 191 | +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` |
| 192 | +in this context refers to specifying a default value that is different from the |
| 193 | +compiled-in default value. |
| 194 | +--> |
| 195 | +你不能使用 `kuberc` 来覆盖命令行参数的值,以使其优先于用户在命令行中指定的值。 |
| 196 | +这里的 **`overrides`** 指的是指定一个与编译时默认值不同的默认值。 |
| 197 | +{{< /note >}} |
| 198 | + |
| 199 | +<!-- |
| 200 | +#### Example: |
| 201 | +--> |
| 202 | +#### 示例: |
| 203 | + |
| 204 | +```yaml |
| 205 | +apiVersion: kubectl.config.k8s.io/v1alpha1 |
| 206 | +kind: Preference |
| 207 | +overrides: |
| 208 | +- command: delete |
| 209 | + flags: |
| 210 | + - name: interactive |
| 211 | + default: "true" |
| 212 | +``` |
| 213 | + |
| 214 | +<!-- |
| 215 | +With this override, running `kubectl delete pod/test-pod` will default to prompting for confirmation. |
| 216 | +However, `kubectl delete pod/test-pod --interactive=false` will bypass the confirmation. |
| 217 | + |
| 218 | +The kubectl maintainers encourage you to adopt kuberc with the given defaults: |
| 219 | +--> |
| 220 | +通过此覆盖,运行 `kubectl delete pod/test-pod` 将默认提示确认。 |
| 221 | +然而,`kubectl delete pod/test-pod --interactive=false` 将绕过确认。 |
| 222 | + |
| 223 | +kubectl 维护者鼓励你采用具有给定默认值的 kuberc: |
| 224 | + |
| 225 | +```yaml |
| 226 | +apiVersion: kubectl.config.k8s.io/v1alpha1 |
| 227 | +kind: Preference |
| 228 | +overrides: |
| 229 | + - command: apply |
| 230 | + flags: |
| 231 | + - name: server-side |
| 232 | + default: "true" |
| 233 | + - command: delete |
| 234 | + flags: |
| 235 | + - name: interactive |
| 236 | + default: "true" |
| 237 | +``` |
| 238 | + |
| 239 | +<!-- |
| 240 | +## Disable kuberc |
| 241 | + |
| 242 | +To temporarily disable the kuberc functionality, simply export the environment variable `KUBERC` with the value `off`: |
| 243 | +--> |
| 244 | +## 禁用 kuberc |
| 245 | + |
| 246 | +要临时禁用 kuberc 特性,只需导出环境变量 `KUBERC` 并将其值设置为 `off`: |
| 247 | + |
| 248 | +```shell |
| 249 | +export KUBERC=off |
| 250 | +``` |
| 251 | + |
| 252 | +<!-- |
| 253 | +or disable the feature gate: |
| 254 | +--> |
| 255 | +或者禁用此特性门控: |
| 256 | + |
| 257 | +```shell |
| 258 | +export KUBECTL_KUBERC=false |
| 259 | +``` |
0 commit comments