# kubectl config

> 修改 kubeconfig 文件

---

LLMS index: [llms.txt](/llms.txt)

---

<!--
title: kubectl config
content_type: tool-reference
weight: 30
auto_generated: true
description: >-
  Modify kubeconfig files
no_list: true
-->

## 简介

<!--
Modify kubeconfig files using subcommands like "kubectl config set current-context my-context".

 The loading order follows these rules:

  1.  If the --kubeconfig flag is set, then only that file is loaded. The flag may only be set once and no merging takes place.
  2.  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.
  3.  Otherwise, ${HOME}/.kube/config is used and no merging takes place.
-->
使用 `kubectl config set current-context my-context` 等子命令修改 kubeconfig 文件。

加载顺序遵循以下规则：

1. 如果设置了 `--kubeconfig` 标志，则仅加载该文件。该标志只能设置一次，并且不会发生合并。
2. 如果设置了 `$KUBECONFIG` 环境变量，则将其用作路径列表（系统的正常路径分隔规则），这些路径会被合并。
   当某个值被修改时，也会在定义这部分内容的文件中修改此值。当某个值被创建时，也会在存在的第一个文件中创建此值。
   如果链中不存在文件，则它会创建列表中的最后一个文件。
3. 否则，将使用 `${HOME}/.kube/config`，并且不会发生合并。

```shell
kubectl config SUBCOMMAND
```

## 选项

<table style="width: 100%; table-layout: fixed;">
<colgroup>
<col span="1" style="width: 10px;" />
<col span="1" />
</colgroup>
<tbody>

<tr>
<td colspan="2">-h, --help</td>
</tr>
<tr>
<td></td><td style="line-height: 130%; word-wrap: break-word;">
<p>
<!--
help for config
-->
关于 config 的帮助信息。
</p>
</td>
</tr>

<tr>
<td colspan="2">--kubeconfig string</td>
</tr>
<tr>
<td></td><td style="line-height: 130%; word-wrap: break-word;">
<p>
<!--
use a particular kubeconfig file
-->
使用特定的 kubeconfig 文件。
</p>
</td>
</tr>

</tbody>
</table>

## 继承于父命令的选项

<table style="width: 100%; table-layout: fixed;">
<colgroup>
<col span="1" style="width: 10px;" />
<col span="1" />
</colgroup>
<tbody>

<tr>
<td colspan="2">--as string</td>
</tr>
<tr>
<td></td><td style="line-height: 130%; word-wrap: break-word;">
<p>
<!--
Username to impersonate for the operation. User could be a regular user or a service account in a namespace.
-->
操作所用的伪装用户名。用户可以是常规用户或命名空间中的服务账号。
</p>
</td>
</tr>

<tr>
<td colspan="2">--as-group strings</td>
</tr>
<tr>
<td></td><td style="line-height: 130%; word-wrap: break-word;">
<p>
<!--
Group to impersonate for the operation, this flag can be repeated to specify multiple groups.
-->
操作所用的伪装用户组，此标志可以被重复设置以指定多个组。
</p>
</td>
</tr>

<tr>
<td colspan="2">--as-uid string</td>
</tr>
<tr>
<td></td><td style="line-height: 130%; word-wrap: break-word;">
<p>
<!--
UID to impersonate for the operation.
-->
操作所用的伪装 UID。
</p>
</td>
</tr>

<tr>
<td colspan="2">--cache-dir string&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<!--Default: "$HOME/.kube/cache"-->默认值："$HOME/.kube/cache"</td>
</tr>
<tr>
<td></td><td style="line-height: 130%; word-wrap: break-word;">
<p>
<!--
Default cache directory
-->
默认缓存目录。
</p>
</td>
</tr>

<tr>
<td colspan="2">--certificate-authority string</td>
</tr>
<tr>
<td></td><td style="line-height: 130%; word-wrap: break-word;">
<p>
<!--
Path to a cert file for the certificate authority
-->
证书机构的证书文件的路径。
</p>
</td>
</tr>

<tr>
<td colspan="2">--client-certificate string</td>
</tr>
<tr>
<td></td><td style="line-height: 130%; word-wrap: break-word;">
<p>
<!--
Path to a client certificate file for TLS
-->
TLS 客户端证书文件的路径。
</p>
</td>
</tr>

<tr>
<td colspan="2">--client-key string</td>
</tr>
<tr>
<td></td><td style="line-height: 130%; word-wrap: break-word;">
<p>
<!--
Path to a client key file for TLS
-->
TLS 客户端密钥文件的路径。
</p>
</td>
</tr>

<tr>
<td colspan="2">--cluster string</td>
</tr>
<tr>
<td></td><td style="line-height: 130%; word-wrap: break-word;">
<p>
<!--
The name of the kubeconfig cluster to use
-->
要使用的 kubeconfig 中集群的名称。
</p>
</td>
</tr>

<tr>
<td colspan="2">--context string</td>
</tr>
<tr>
<td></td><td style="line-height: 130%; word-wrap: break-word;">
<p>
<!--
The name of the kubeconfig context to use
-->
要使用的 kubeconfig 上下文的名称。
</p>
</td>
</tr>

<tr>
<td colspan="2">--disable-compression</td>
</tr>
<tr>
<td></td><td style="line-height: 130%; word-wrap: break-word;">
<p>
<!--
If true, opt-out of response compression for all requests to the server
-->
如果为 true，则对服务器所有请求的响应不再压缩。
</p>
</td>
</tr>

<tr>
<td colspan="2">--insecure-skip-tls-verify</td>
</tr>
<tr>
<td></td><td style="line-height: 130%; word-wrap: break-word;">
<p>
<!--
If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure
-->
如果为 true，则不检查服务器证书的有效性。这将使你的 HTTPS 连接不安全。
</p>
</td>
</tr>

<tr>
<td colspan="2">--kuberc string</td>
</tr>
<tr>
<td></td><td style="line-height: 130%; word-wrap: break-word;">
<p>
<!--
Path to the kuberc file to use for preferences. This can be disabled by exporting KUBECTL_KUBERC=false feature gate or turning off the feature KUBERC=off.
-->
用于偏好设置的 kuberc 文件的路径。可以通过导出 KUBECTL_KUBERC=false
特性门控或关闭 KUBERC=off 特性门控来禁用此功能。
</p>
</td>
</tr>

<tr>
<td colspan="2">--kubeconfig string</td>
</tr>
<tr>
<td></td><td style="line-height: 130%; word-wrap: break-word;">
<p>
<!--
Path to the kubeconfig file to use for CLI requests.
-->
CLI 请求要使用的 kubeconfig 文件的路径。
</p>
</td>
</tr>

<tr>
<td colspan="2">--match-server-version</td>
</tr>
<tr>
<td></td><td style="line-height: 130%; word-wrap: break-word;">
<p>
<!--
Require server version to match client version
-->
要求服务器版本与客户端版本匹配。
</p>
</td>
</tr>

<tr>
<td colspan="2">-n, --namespace string</td>
</tr>
<tr>
<td></td><td style="line-height: 130%; word-wrap: break-word;">
<p>
<!--
If present, the namespace scope for this CLI request
-->
如果存在，则是此 CLI 请求的命名空间范围。
</p>
</td>
</tr>

<tr>
<td colspan="2">--password string</td>
</tr>
<tr>
<td></td><td style="line-height: 130%; word-wrap: break-word;">
<p>
<!--
Password for basic authentication to the API server
-->
对 API 服务器进行基本身份验证所用的密码。
</p>
</td>
</tr>

<tr>
<td colspan="2">--profile string&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<!--Default: "none"-->默认值："none"</td>
</tr>
<tr>
<td></td><td style="line-height: 130%; word-wrap: break-word;">
<p>
<!--
Name of profile to capture. One of (none|cpu|heap|goroutine|threadcreate|block|mutex|trace)
-->
要记录的性能分析信息。可选值为（none|cpu|heap|goroutine|threadcreate|block|mutex|trace）。
</p>
</td>
</tr>

<tr>
<td colspan="2">--profile-output string&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<!--Default: "profile.pprof"-->默认值："profile.pprof"</td>
</tr>
<tr>
<td></td><td style="line-height: 130%; word-wrap: break-word;">
<p>
<!--
Name of the file to write the profile to
-->
性能分析信息要写入的目标文件的名称。
</p>
</td>
</tr>

<tr>
<td colspan="2">--request-timeout string&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<!--Default: "0"-->默认值："0"</td>
</tr>
<tr>
<td></td><td style="line-height: 130%; word-wrap: break-word;">
<p>
<!--
The length of time to wait before giving up on a single server request. Non-zero values should contain a corresponding time unit (e.g. 1s, 2m, 3h). A value of zero means don't timeout requests.
-->
在放弃某个服务器请求之前等待的时长。非零值应包含相应的时间单位（例如 1s、2m、3h）。
值为零表示请求不会超时。
</p>
</td>
</tr>

<tr>
<td colspan="2">-s, --server string</td>
</tr>
<tr>
<td></td><td style="line-height: 130%; word-wrap: break-word;">
<p>
<!--
The address and port of the Kubernetes API server
-->
Kubernetes API 服务器的地址和端口。
</p>
</td>
</tr>

<tr>
<td colspan="2">--storage-driver-buffer-duration duration&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<!--Default: 1m0s-->默认值：1m0s</td>
</tr>
<tr>
<td></td><td style="line-height: 130%; word-wrap: break-word;">
<p>
<!--
Writes in the storage driver will be buffered for this duration, and committed to the non memory backends as a single transaction
-->
对存储驱动的写入操作将被缓存的时长；缓存的操作会作为一个事务提交给非内存后端。
</p>
</td>
</tr>

<tr>
<td colspan="2">--storage-driver-db string&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<!--Default: "cadvisor"-->默认值："cadvisor"</td>
</tr>
<tr>
<td></td><td style="line-height: 130%; word-wrap: break-word;">
<p>
<!--
database name
-->
数据库名称。
</p>
</td>
</tr>

<tr>
<td colspan="2">--storage-driver-host string&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<!--Default: "localhost:8086"-->默认值："localhost:8086"</td>
</tr>
<tr>
<td></td><td style="line-height: 130%; word-wrap: break-word;"><p>
<!--
database host:port
-->
数据库 host:port。
</p>
</td>
</tr>

<tr>
<td colspan="2">--storage-driver-password string&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<!--Default: "root"-->默认值："root"</td>
</tr>
<tr>
<td></td><td style="line-height: 130%; word-wrap: break-word;"><p>
<!--
database password
-->
数据库密码。
</p>
</td>
</tr>

<tr>
<td colspan="2">--storage-driver-secure</td>
</tr>
<tr>
<td></td><td style="line-height: 130%; word-wrap: break-word;"><p>
<!--
use secure connection with database
-->
使用与数据库的安全连接。
</p>
</td>
</tr>

<tr>
<td colspan="2">--storage-driver-table string&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<!--Default: "stats"-->默认值："stats"</td>
</tr>
<tr>
<td></td><td style="line-height: 130%; word-wrap: break-word;"><p>
<!--
table name
-->
表名。
</p>
</td>
</tr>

<tr>
<td colspan="2">--storage-driver-user string&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<!--Default: "root"-->默认值："root"</td>
</tr>
<tr>
<td></td><td style="line-height: 130%; word-wrap: break-word;"><p>
<!--
database username
-->
数据库用户名。
</p>
</td>
</tr>

<tr>
<td colspan="2">--tls-server-name string</td>
</tr>
<tr>
<td></td><td style="line-height: 130%; word-wrap: break-word;">
<p>
<!--
Server name to use for server certificate validation. If it is not provided, the hostname used to contact the server is used
-->
服务器证书验证所用的服务器名称。如果未提供，则使用与服务器通信所用的主机名。
</p>
</td>
</tr>

<tr>
<td colspan="2">--token string</td>
</tr>
<tr>
<td></td><td style="line-height: 130%; word-wrap: break-word;"><p>
<!--
Bearer token for authentication to the API server
-->
向 API 服务器进行身份验证的持有者令牌。
</p>
</td>
</tr>

<tr>
<td colspan="2">--user string</td>
</tr>
<tr>
<td></td><td style="line-height: 130%; word-wrap: break-word;"><p>
<!--
The name of the kubeconfig user to use
-->
要使用的 kubeconfig 用户的名称。
</p>
</td>
</tr>

<tr>
<td colspan="2">--username string</td>
</tr>
<tr>
<td></td><td style="line-height: 130%; word-wrap: break-word;"><p>
<!--
Username for basic authentication to the API server
-->
对 API 服务器进行基本身份验证时所用的用户名。
</p>
</td>
</tr>

<tr>
<td colspan="2">--version version[=true]</td>
</tr>
<tr>
<td></td><td style="line-height: 130%; word-wrap: break-word;"><p>
<!--
--version, --version=raw prints version information and quits; --version=vX.Y.Z... sets the reported version
-->
--version, --version=raw 打印版本信息并退出；--version=vX.Y.Z... 设置报告的版本。
</p>
</td>
</tr>

<tr>
<td colspan="2">--warnings-as-errors</td>
</tr>
<tr>
<td></td><td style="line-height: 130%; word-wrap: break-word;"><p>
<!--
Treat warnings received from the server as errors and exit with a non-zero exit code
-->
将从服务器收到的警告视为错误，并以非零退出码退出。
</p>
</td>
</tr>

</tbody>
</table>

## 另请参见

<!--
* [kubectl](../kubectl/)	 - kubectl controls the Kubernetes cluster manager
* [kubectl config current-context](kubectl_config_current-context/)	 - Display the current-context
* [kubectl config delete-cluster](kubectl_config_delete-cluster/)	 - Delete the specified cluster from the kubeconfig
* [kubectl config delete-context](kubectl_config_delete-context/)	 - Delete the specified context from the kubeconfig
* [kubectl config delete-user](kubectl_config_delete-user/)	 - Delete the specified user from the kubeconfig
* [kubectl config get-clusters](kubectl_config_get-clusters/)	 - Display clusters defined in the kubeconfig
* [kubectl config get-contexts](kubectl_config_get-contexts/)	 - Describe one or many contexts
* [kubectl config get-users](kubectl_config_get-users/)	 - Display users defined in the kubeconfig
* [kubectl config rename-context](kubectl_config_rename-context/)	 - Rename a context from the kubeconfig file
* [kubectl config set](kubectl_config_set/)	 - Set an individual value in a kubeconfig file
* [kubectl config set-cluster](kubectl_config_set-cluster/)	 - Set a cluster entry in kubeconfig
* [kubectl config set-context](kubectl_config_set-context/)	 - Set a context entry in kubeconfig
* [kubectl config set-credentials](kubectl_config_set-credentials/)	 - Set a user entry in kubeconfig
* [kubectl config unset](kubectl_config_unset/)	 - Unset an individual value in a kubeconfig file
* [kubectl config use-context](kubectl_config_use-context/)	 - Set the current-context in a kubeconfig file
* [kubectl config view](kubectl_config_view/)	 - Display merged kubeconfig settings or a specified kubeconfig file
-->
* [kubectl](../kubectl/) - kubectl 控制 Kubernetes 集群管理器
* [kubectl config current-context](kubectl_config_current-context/) - 显示当前上下文
* [kubectl config delete-cluster](kubectl_config_delete-cluster/) - 从 kubeconfig 中删除指定集群信息
* [kubectl config delete-context](kubectl_config_delete-context/) - 从 kubeconfig 中删除指定的上下文信息
* [kubectl config delete-user](kubectl_config_delete-user/) - 从 kubeconfig 中删除指定的用户信息
* [kubectl config get-clusters](kubectl_config_get-clusters/) - 显示 kubeconfig 中指定集群的信息
* [kubectl config get-contexts](kubectl_config_get-contexts/) - 显示 kubeconfig 中指定上下文的信息
* [kubectl config get-users](kubectl_config_get-users/) - 显示 kubeconfig 中指定用户的信息
* [kubectl config rename-context](kubectl_config_rename-context/) - 对 kubeconfig 文件的上下文重命名
* [kubectl config set](kubectl_config_set/) - 在 kubeconfig 文件中设置单个值
* [kubectl config set-cluster](kubectl_config_set-cluster/) - 在 kubeconfig 中设置集群条目
* [kubectl config set-context](kubectl_config_set-context/) - 在 kubeconfig 中设置上下文条目
* [kubectl config set-credentials](kubectl_config_set-credentials/) - 在 kubeconfig 中设置用户条目
* [kubectl config unset](kubectl_config_unset/) - 取消设置 kubeconfig 文件中的单个值
* [kubectl config use-context](kubectl_config_use-context/) - 设置在 kubeconfig 文件中的当前上下文
* [kubectl config view](kubectl_config_view/) - 显示合并的 kubeconfig 设置或指定的 kubeconfig 文件

---

Section pages:

- [kubectl config current-context](/zh-cn/docs/reference/kubectl/generated/kubectl_config/kubectl_config_current-context/): 显示当前上下文
- [kubectl config delete-cluster](/zh-cn/docs/reference/kubectl/generated/kubectl_config/kubectl_config_delete-cluster/): 从 kubeconfig 中删除指定的集群
- [kubectl config delete-context](/zh-cn/docs/reference/kubectl/generated/kubectl_config/kubectl_config_delete-context/): 从 kubeconfig 中删除指定的上下文
- [kubectl config delete-user](/zh-cn/docs/reference/kubectl/generated/kubectl_config/kubectl_config_delete-user/): 从 kubeconfig 中删除指定的用户
- [kubectl config get-clusters](/zh-cn/docs/reference/kubectl/generated/kubectl_config/kubectl_config_get-clusters/): 显示 kubeconfig 中定义的集群
- [kubectl config get-contexts](/zh-cn/docs/reference/kubectl/generated/kubectl_config/kubectl_config_get-contexts/): 描述一个或多个上下文
- [kubectl config get-users](/zh-cn/docs/reference/kubectl/generated/kubectl_config/kubectl_config_get-users/): 显示 kubeconfig 中定义的用户
- [kubectl config rename-context](/zh-cn/docs/reference/kubectl/generated/kubectl_config/kubectl_config_rename-context/): 重命名 kubeconfig 文件中的上下文
- [kubectl config set](/zh-cn/docs/reference/kubectl/generated/kubectl_config/kubectl_config_set/): 设置 kubeconfig 文件中的单个值
- [kubectl config set-cluster](/zh-cn/docs/reference/kubectl/generated/kubectl_config/kubectl_config_set-cluster/): 设置 kubeconfig 中的集群条目
- [kubectl config set-context](/zh-cn/docs/reference/kubectl/generated/kubectl_config/kubectl_config_set-context/): 设置 kubeconfig 中的上下文条目
- [kubectl config set-credentials](/zh-cn/docs/reference/kubectl/generated/kubectl_config/kubectl_config_set-credentials/): 设置 kubeconfig 中的用户条目
- [kubectl config unset](/zh-cn/docs/reference/kubectl/generated/kubectl_config/kubectl_config_unset/): 去除 kubeconfig 文件中的某个值的设置
- [kubectl config use-context](/zh-cn/docs/reference/kubectl/generated/kubectl_config/kubectl_config_use-context/): 在 kubeconfig 文件中设置当前上下文
- [kubectl config view](/zh-cn/docs/reference/kubectl/generated/kubectl_config/kubectl_config_view/): 显示合并的 kubeconfig 配置或指定的 kubeconfig 文件
