# 在 Windows 上安装 kubectl

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

---

<!--
reviewers:
- mikedanese
title: Install and Set Up kubectl on Windows
content_type: task
weight: 10
-->

## 准备开始

<!--
You must use a kubectl version that is within one minor version difference of
your cluster. For example, a v1.36 client can communicate
with v1.35, v1.36,
and v1.37 control planes.
Using the latest compatible version of kubectl helps avoid unforeseen issues.
-->
kubectl 版本和集群版本之间的差异必须在一个小版本号内。
例如：v1.36 版本的客户端能与 v1.35、
v1.36 和 v1.37 版本的控制面通信。
用最新兼容版的 kubectl 有助于避免不可预见的问题。

<!--
## Install kubectl on Windows

The following methods exist for installing kubectl on Windows:
-->
## 在 Windows 上安装 kubectl {#install-kubectl-on-windows}

在 Windows 系统中安装 kubectl 有如下几种方法：

<!--
- [Install kubectl binary on Windows (via direct download or curl)](#install-kubectl-binary-on-windows-via-direct-download-or-curl)
- [Install on Windows using Chocolatey, Scoop, or winget](#install-nonstandard-package-tools)
-->
- [在 Windows 上安装 kubectl（通过直接下载或使用 curl）](#install-kubectl-binary-on-windows-via-direct-download-or-curl)
- [在 Windows 上用 Chocolatey、Scoop 或 winget 安装](#install-nonstandard-package-tools)

<!--
### Install kubectl binary on Windows (via direct download or curl)
-->
### 在 Windows 上安装 kubectl（通过直接下载或使用 curl） {#install-kubectl-binary-on-windows-via-direct-download-or-curl}

<!--
1. You have two options for installing kubectl on your Windows device
   
   - Direct download:

     Download the latest 1.36 patch release binary directly for your specific architecture by visiting the [Kubernetes release page](https://kubernetes.io/releases/download/#binaries). Be sure to select the correct binary for your architecture (e.g., amd64, arm64, etc.).

   - Using curl:

     If you have `curl` installed, use this command:
-->
1. 你有两种方式可以在 Windows 设备上安装 kubectl

   直接下载：

   通过访问 [Kubernetes 发布页面](https://kubernetes.io/releases/download/#binaries)
   直接下载特定于你的体系结构的二进制文件的最新 1.36 补丁版本。
   请务必选择适用于你的体系结构的二进制文件（例如，amd64、arm64 等）。

   使用 curl：

   如果你已安装 `curl`，可以使用以下命令：

   ```powershell
   curl.exe -LO "https://dl.k8s.io/release/v1.36.0/bin/windows/amd64/kubectl.exe"
   ```

   
<div class="alert alert-info" role="note"><h4 class="alert-heading">说明：</h4><!--
   To find out the latest stable version (for example, for scripting), take a look at
   [https://dl.k8s.io/release/stable.txt](https://dl.k8s.io/release/stable.txt).
   -->
<p>要想找到最新稳定的版本（例如：为了编写脚本），可以看看这里 <a href="https://dl.k8s.io/release/stable.txt">https://dl.k8s.io/release/stable.txt</a>。</p></div>


<!--
1. Validate the binary (optional)

   Download the `kubectl` checksum file:
-->
2. 验证该可执行文件（可选步骤）

   下载 `kubectl` 校验和文件：

   ```powershell
   curl.exe -LO "https://dl.k8s.io/v1.36.0/bin/windows/amd64/kubectl.exe.sha256"
   ```

   <!--
   Validate the `kubectl` binary against the checksum file:
   -->
   基于校验和文件，验证 `kubectl` 的可执行文件：

   <!--
   - Using Command Prompt to manually compare `CertUtil`'s output to the checksum file downloaded:
   -->

   - 在命令行环境中，手工对比 `CertUtil` 命令的输出与校验和文件：

     ```cmd
     CertUtil -hashfile kubectl.exe SHA256
     type kubectl.exe.sha256
     ```

   <!--
   - Using PowerShell to automate the verification using the `-eq` operator to
     get a `True` or `False` result:
   -->

   - 用 PowerShell 自动验证，用运算符 `-eq` 来直接取得 `True` 或 `False` 的结果：

     ```powershell
     $(Get-FileHash -Algorithm SHA256 .\kubectl.exe).Hash -eq $(Get-Content .\kubectl.exe.sha256)
     ```

<!--
1. Append or prepend the `kubectl` binary folder to your `PATH` environment variable.

1. Test to ensure the version of `kubectl` is the same as downloaded:
-->
3. 将 `kubectl` 二进制文件夹追加或插入到你的 `PATH` 环境变量中。

4. 测试一下，确保此 `kubectl` 的版本和期望版本一致：

   ```cmd
   kubectl version --client
   ```

   <!--
   Or use this for detailed view of version:
   -->
   或者使用下面命令来查看版本的详细信息：

   ```cmd
   kubectl version --client --output=yaml
   ```


<div class="alert alert-info" role="note"><h4 class="alert-heading">说明：</h4><!--
[Docker Desktop for Windows](https://docs.docker.com/docker-for-windows/#kubernetes)
adds its own version of `kubectl` to `PATH`. If you have installed Docker Desktop before,
you may need to place your `PATH` entry before the one added by the Docker Desktop
installer or remove the Docker Desktop's `kubectl`.
-->
<p><a href="https://docs.docker.com/docker-for-windows/#kubernetes">Windows 版的 Docker Desktop</a>
将其自带版本的 <code>kubectl</code> 添加到 <code>PATH</code>。
如果你之前安装过 Docker Desktop，可能需要把此 <code>PATH</code> 条目置于 Docker Desktop 安装的条目之前，
或者直接删掉 Docker Desktop 的 <code>kubectl</code>。</p></div>


<!--
### Install on Windows using Chocolatey, Scoop, or winget {#install-nonstandard-package-tools}
-->
### 在 Windows 上用 Chocolatey、Scoop 或 winget 安装 {#install-nonstandard-package-tools}

<!--
1. To install kubectl on Windows you can use either [Chocolatey](https://chocolatey.org)
   package manager, [Scoop](https://scoop.sh) command-line installer, or
   [winget](https://learn.microsoft.com/en-us/windows/package-manager/winget/) package manager.
-->
1. 要在 Windows 上安装 kubectl，你可以使用包管理器 [Chocolatey](https://chocolatey.org)、
   命令行安装器 [Scoop](https://scoop.sh) 或包管理器 [winget](https://learn.microsoft.com/zh-cn/windows/package-manager/winget/)。

   <ul class="nav nav-tabs" id="tabs-kubectl-win-install" role="tablist"><li class="nav-item"><a data-bs-toggle="tab" class="nav-link active" href="#tabs-kubectl-win-install-0" role="tab" aria-controls="tabs-kubectl-win-install-0" aria-selected="true">choco</a></li>
	  
		<li class="nav-item"><a data-bs-toggle="tab" class="nav-link" href="#tabs-kubectl-win-install-1" role="tab" aria-controls="tabs-kubectl-win-install-1">scoop</a></li>
		<li class="nav-item"><a data-bs-toggle="tab" class="nav-link" href="#tabs-kubectl-win-install-2" role="tab" aria-controls="tabs-kubectl-win-install-2">winget</a></li></ul>

<div class="tab-content" id="tabs-kubectl-win-install-content"><div class="tab-body tab-pane fadeshow active"
        id="tabs-kubectl-win-install-0" role="tabpanel" aria-labelledby="tabs-kubectl-win-install-0-tab" tabindex="kubectl-win-install"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-powershell" data-lang="powershell"><span class="line"><span class="cl"><span class="n">choco</span> <span class="n">install</span> <span class="nb">kubernetes-cli</span>
</span></span></code></pre></div></div><div class="tab-body tab-pane fade"
        id="tabs-kubectl-win-install-1" role="tabpanel" aria-labelledby="tabs-kubectl-win-install-1-tab" tabindex="kubectl-win-install"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-powershell" data-lang="powershell"><span class="line"><span class="cl"><span class="n">scoop</span> <span class="n">install</span> <span class="n">kubectl</span>
</span></span></code></pre></div></div><div class="tab-body tab-pane fade"
        id="tabs-kubectl-win-install-2" role="tabpanel" aria-labelledby="tabs-kubectl-win-install-2-tab" tabindex="kubectl-win-install"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-powershell" data-lang="powershell"><span class="line"><span class="cl"><span class="n">winget</span> <span class="n">install</span> <span class="n">-e</span> <span class="p">-</span><span class="n">-id</span> <span class="n">Kubernetes</span><span class="p">.</span><span class="py">kubectl</span>
</span></span></code></pre></div></div></div>


<!--
1. Test to ensure the version you installed is up-to-date:
-->
2. 测试一下，确保安装的是最新版本：

   ```powershell
   kubectl version --client
   ```

<!--
1. Navigate to your home directory:
-->
3. 导航到你的 home 目录：

   ```powershell
   # 当你用 cmd.exe 时，则运行： cd %USERPROFILE%
   cd ~
   ```

<!--
1. Create the `.kube` directory:
-->
4. 创建目录 `.kube`：

   ```powershell
   mkdir .kube
   ```

<!--
1. Change to the `.kube` directory you just created:
-->
5. 切换到新创建的目录 `.kube`：

   ```powershell
   cd .kube
   ```

<!--
1. Configure kubectl to use a remote Kubernetes cluster:
 -->
6. 配置 kubectl，以接入远程的 Kubernetes 集群：

   ```powershell
   New-Item config -type file
   ```


<div class="alert alert-info" role="note"><h4 class="alert-heading">说明：</h4><!--
Edit the config file with a text editor of your choice, such as Notepad.
-->
<p>编辑配置文件，你需要先选择一个文本编辑器，比如 Notepad。</p></div>


<!--
## Verify kubectl configuration
-->
## 验证 kubectl 配置 {#verify-kubectl-configration}


	<!-- 
title: "verify kubectl install"
description: "How to verify kubectl."
headless: true
build:
  list: never
  render: never
  publishResources: false
-->
<!-- 
In order for kubectl to find and access a Kubernetes cluster, it needs a
[kubeconfig file](/docs/concepts/configuration/organize-cluster-access-kubeconfig/),
which is created automatically when you create a cluster using
[kube-up.sh](https://github.com/kubernetes/kubernetes/blob/master/cluster/kube-up.sh)
or successfully deploy a Minikube cluster.
By default, kubectl configuration is located at `~/.kube/config`.

Check that kubectl is properly configured by getting the cluster state:
-->
<p>为了让 kubectl 能发现并访问 Kubernetes 集群，你需要一个
<a href="/zh-cn/docs/concepts/configuration/organize-cluster-access-kubeconfig/">kubeconfig 文件</a>，
该文件在
<a href="https://github.com/kubernetes/kubernetes/blob/master/cluster/kube-up.sh">kube-up.sh</a>
创建集群时，或成功部署一个 Minikube 集群时，均会自动生成。
通常，kubectl 的配置信息存放于文件 <code>~/.kube/config</code> 中。</p>
<p>通过获取集群状态的方法，检查是否已恰当地配置了 kubectl：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">kubectl cluster-info
</span></span></code></pre></div><!-- 
If you see a URL response, kubectl is correctly configured to access your cluster.

If you see a message similar to the following, kubectl is not configured correctly
or is not able to connect to a Kubernetes cluster.
-->
<p>如果返回一个 URL，则意味着 kubectl 成功地访问到了你的集群。</p>
<p>如果你看到如下所示的消息，则代表 kubectl 配置出了问题，或无法连接到 Kubernetes 集群。</p>
<pre tabindex="0"><code>The connection to the server &lt;server-name:port&gt; was refused - did you specify the right host or port?
（访问 &lt;server-name:port&gt; 被拒绝 - 你指定的主机和端口是否有误？）
</code></pre><!-- 
For example, if you are intending to run a Kubernetes cluster on your laptop (locally),
you will need a tool like [Minikube](https://minikube.sigs.k8s.io/docs/start/) to be
installed first and then re-run the commands stated above.

If `kubectl cluster-info` returns the url response, but you can't access your cluster,
check whether it is configured properly using the following command:
-->
<p>例如，如果你想在自己的笔记本上（本地）运行 Kubernetes 集群，你需要先安装一个
<a href="https://minikube.sigs.k8s.io/docs/start/">Minikube</a>
这样的工具，然后再重新运行上面的命令。</p>
<p>如果命令 <code>kubectl cluster-info</code> 返回了 URL，但你还不能访问集群，
那可以用以下命令来检查配置是否妥当：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">kubectl cluster-info dump
</span></span></code></pre></div><!--
### Troubleshooting the 'No Auth Provider Found' error message {#no-auth-provider-found}

In Kubernetes 1.26, kubectl removed the built-in authentication for the following cloud
providers' managed Kubernetes offerings. These providers have released kubectl plugins
to provide the cloud-specific authentication. For instructions, refer to the following provider documentation:
-->
<h3 id="no-auth-provider-found">排查&quot;找不到身份验证提供商&quot;的错误信息<a class="td-heading-self-link" href="#no-auth-provider-found" aria-label="Heading self-link"></a></h3>
<p>在 Kubernetes 1.26 中，kubectl 删除了以下云提供商托管的 Kubernetes 产品的内置身份验证。
这些提供商已经发布了 kubectl 插件来提供特定于云的身份验证。
有关说明，请参阅以下提供商文档：</p>
<!--
* Azure AKS: [kubelogin plugin](https://azure.github.io/kubelogin/)
* Google Kubernetes Engine: [gke-gcloud-auth-plugin](https://cloud.google.com/kubernetes-engine/docs/how-to/cluster-access-for-kubectl#install_plugin)
-->
<ul>
<li>Azure AKS：<a href="https://azure.github.io/kubelogin/">kubelogin 插件</a></li>
<li>CKE：<a href="https://cloud.google.com/kubernetes-engine/docs/how-to/cluster-access-for-kubectl#install_plugin">gke-gcloud-auth-plugin</a></li>
</ul>
<!--
There could also be other causes for the same error message that are unrelated
to that change.
-->
<p>也可能存在其他与此更改无关的原因导致出现相同的错误消息。</p>


<!--
## Optional kubectl configurations and plugins

### Enable shell autocompletion
-->
## kubectl 可选配置和插件 {#optional-kubectl-configurations}

### 启用 shell 自动补全功能 {#enable-shell-autocompletion}

<!--
kubectl provides autocompletion support for Bash, Zsh, Fish, and PowerShell,
which can save you a lot of typing.

Below are the procedures to set up autocompletion for PowerShell.
-->
kubectl 为 Bash、Zsh、Fish 和 PowerShell 提供自动补全功能，可以为你节省大量的输入。

下面是设置 PowerShell 自动补全功能的操作步骤。


	<!--
title: "PowerShell auto-completion"
description: "Some optional configuration for powershell auto-completion."
headless: true
-->
<!--
The kubectl completion script for PowerShell can be generated with the command `kubectl completion powershell`.

To do so in all your shell sessions, add the following line to your `$PROFILE` file:
-->
<p>你可以使用命令 <code>kubectl completion powershell</code> 生成 PowerShell 的 kubectl 自动补全脚本。</p>
<p>如果需要自动补全在所有 Shell 会话中生效，请将以下命令添加到 <code>$PROFILE</code> 文件中：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-powershell" data-lang="powershell"><span class="line"><span class="cl"><span class="n">kubectl</span> <span class="n">completion</span> <span class="n">powershell</span> <span class="p">|</span> <span class="nb">Out-String</span> <span class="p">|</span> <span class="nb">Invoke-Expression</span>
</span></span></code></pre></div><!--
This command will regenerate the auto-completion script on every PowerShell start up. You can also add the generated script directly to your `$PROFILE` file.
-->
<p>此命令将在每次 PowerShell 启动时重新生成自动补全脚本。你还可以将生成的自动补全脚本添加到 <code>$PROFILE</code> 文件中。</p>
<!--
To add the generated script to your `$PROFILE` file, run the following line in your powershell prompt:
-->
<p>如果需要将自动补全脚本直接添加到 <code>$PROFILE</code> 文件中，请在 PowerShell 命令行运行以下命令：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-powershell" data-lang="powershell"><span class="line"><span class="cl"><span class="n">kubectl</span> <span class="n">completion</span> <span class="n">powershell</span> <span class="p">&gt;&gt;</span> <span class="nv">$PROFILE</span>
</span></span></code></pre></div><!--
After reloading your shell, kubectl autocompletion should be working.
-->
<p>完成上述操作后重启 Shell，kubectl 的自动补全就可以工作了。</p>


<!--
### Configure kuberc

See [kuberc](/docs/reference/kubectl/kuberc) for more information.
-->
### 配置 kuberc  {#configure-kuberc}

更多信息请参见 [kuberc](/zh-cn/docs/reference/kubectl/kuberc)。

<!--
### Install `kubectl convert` plugin
-->
### 安装 `kubectl convert` 插件   {#install-kubectl-convert-plugin}


	<!--
title: "kubectl-convert overview"
description: >-
  A kubectl plugin that allows you to convert manifests from one version
  of a Kubernetes API to a different version.
headless: true
-->
<!--
A plugin for Kubernetes command-line tool `kubectl`, which allows you to convert manifests between different API
versions. This can be particularly helpful to migrate manifests to a non-deprecated api version with newer Kubernetes release.
For more info, visit [migrate to non deprecated apis](/docs/reference/using-api/deprecation-guide/#migrate-to-non-deprecated-apis)
-->
<p>一个 Kubernetes 命令行工具 <code>kubectl</code> 的插件，允许你将清单在不同 API 版本间转换。
这对于将清单迁移到新的 Kubernetes 发行版上未被废弃的 API 版本时尤其有帮助。
更多信息请访问<a href="/zh-cn/docs/reference/using-api/deprecation-guide/#migrate-to-non-deprecated-apis">迁移到非弃用 API</a></p>


<!--
1. Download the latest release with the command:
-->
1. 用以下命令下载最新发行版：

   ```powershell
   curl.exe -LO "https://dl.k8s.io/release/v1.36.0/bin/windows/amd64/kubectl-convert.exe"
   ```

<!--
1. Validate the binary (optional).
-->
2. 验证该可执行文件（可选步骤）。

   <!--
   Download the `kubectl-convert` checksum file:
   -->
   下载 `kubectl-convert` 校验和文件：

   ```powershell
   curl.exe -LO "https://dl.k8s.io/v1.36.0/bin/windows/amd64/kubectl-convert.exe.sha256"
   ```

   <!--
   Validate the `kubectl-convert` binary against the checksum file:
   -->
   基于校验和验证 `kubectl-convert` 的可执行文件：

   <!--
   - Using Command Prompt to manually compare `CertUtil`'s output to the checksum file downloaded:
   -->

   - 用提示的命令对 `CertUtil` 的输出和下载的校验和文件进行手动比较。

     ```cmd
     CertUtil -hashfile kubectl-convert.exe SHA256
     type kubectl-convert.exe.sha256
     ```

   <!--
   - Using PowerShell to automate the verification using the `-eq` operator to get
     a `True` or `False` result:
   -->

   - 使用 PowerShell `-eq` 操作使验证自动化，获得 `True` 或者 `False` 的结果：

     ```powershell
     $($(CertUtil -hashfile .\kubectl-convert.exe SHA256)[1] -replace " ", "") -eq $(type .\kubectl-convert.exe.sha256)
     ```

<!--
1. Append or prepend the `kubectl-convert` binary folder to your `PATH` environment variable.

1. Verify the plugin is successfully installed.
-->
3. 将 `kubectl-convert` 二进制文件夹附加或添加到你的 `PATH` 环境变量中。

4. 验证插件是否安装成功。

   ```shell
   kubectl convert --help
   ```

   <!--
   If you do not see an error, it means the plugin is successfully installed.
   -->
   如果你没有看到任何错误就代表插件安装成功了。

<!--
1. After installing the plugin, clean up the installation files:
-->
5. 安装插件后，清理安装文件：

   ```powershell
   del kubectl-convert.exe
   del kubectl-convert.exe.sha256
   ```

## 接下来


	<!-- 
title: "What's next?"
description: "What's next after installing kubectl."
headless: true
-->
<!-- 
* Learn about [kubectl](/docs/concepts/overview/kubectl/) and its role in the Kubernetes ecosystem.
* [Install Minikube](https://minikube.sigs.k8s.io/docs/start/)
* See the [getting started guides](/docs/setup/) for more about creating clusters.
* [Learn how to launch and expose your application.](/docs/tasks/access-application-cluster/service-access-application-cluster/)
* If you need access to a cluster you didn't create, see the
  [Sharing Cluster Access document](/docs/tasks/access-application-cluster/configure-access-multiple-clusters/).
* Read the [kubectl reference docs](/docs/reference/kubectl/kubectl/)
-->
<ul>
<li>了解 <a href="/zh-cn/docs/concepts/overview/kubectl/">kubectl</a> 及其在 Kubernetes 生态系统中的作用。</li>
<li><a href="https://minikube.sigs.k8s.io/docs/start/">安装 Minikube</a>。</li>
<li>有关创建集群的更多信息，请参阅<a href="/zh-cn/docs/setup/">入门指南</a>。</li>
<li><a href="/zh-cn/docs/tasks/access-application-cluster/service-access-application-cluster/">学习如何启动并对外公开你的应用程序</a>。</li>
<li>如果你需要访问其他人创建的集群，
请参阅<a href="/zh-cn/docs/tasks/access-application-cluster/configure-access-multiple-clusters/">共享集群接入文档</a>。</li>
<li>阅读 <a href="/zh-cn/docs/reference/kubectl/kubectl/">kubectl 参考文档</a>。</li>
</ul>
