# 在 macOS 系统上安装和设置 kubectl

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

---

<!-- 
reviewers:
- mikedanese
title: Install and Set Up kubectl on macOS
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 macOS
-->
## 在 macOS 系统上安装 kubectl {#install-kubectl-on-macos}

<!--
The following methods exist for installing kubectl on macOS:
-->
在 macOS 系统上安装 kubectl 有如下方法：

<!--
- [Install kubectl on macOS](#install-kubectl-on-macos)
  - [Install kubectl binary with curl on macOS](#install-kubectl-binary-with-curl-on-macos)
  - [Install with Homebrew on macOS](#install-with-homebrew-on-macos)
  - [Install with Macports on macOS](#install-with-macports-on-macos)
- [Verify kubectl configuration](#verify-kubectl-configuration)
- [Optional kubectl configurations and plugins](#optional-kubectl-configurations-and-plugins)
  - [Enable shell autocompletion](#enable-shell-autocompletion)
  - [Install `kubectl convert` plugin](#install-kubectl-convert-plugin)
-->
- [在 macOS 系统上安装 kubectl](#install-kubectl-on-macos)
  - [用 curl 在 macOS 系统上安装 kubectl](#install-kubectl-binary-with-curl-on-macos)
  - [用 Homebrew 在 macOS 系统上安装](#install-with-homebrew-on-macos)
  - [用 Macports 在 macOS 系统上安装](#install-with-macports-on-macos)
- [验证 kubectl 配置](#verify-kubectl-configuration)
- [可选的 kubectl 配置和插件](#optional-kubectl-configurations-and-plugins)
  - [启用 shell 自动补全功能](#enable-shell-autocompletion)
  - [安装 `kubectl convert` 插件](#install-kubectl-convert-plugin)

<!-- 
### Install kubectl binary with curl on macOS
-->
### 用 curl 在 macOS 系统上安装 kubectl {#install-kubectl-binary-with-curl-on-macos}

<!--
1. Download the latest release:
-->
1. 下载最新的发行版：

   <ul class="nav nav-tabs" id="tabs-download-binary-macos" role="tablist"><li class="nav-item"><a data-bs-toggle="tab" class="nav-link active" href="#tabs-download-binary-macos-0" role="tab" aria-controls="tabs-download-binary-macos-0" aria-selected="true">Intel</a></li>
	  
		<li class="nav-item"><a data-bs-toggle="tab" class="nav-link" href="#tabs-download-binary-macos-1" role="tab" aria-controls="tabs-download-binary-macos-1">Apple Silicon</a></li></ul>

<div class="tab-content" id="tabs-download-binary-macos-content"><div class="tab-body tab-pane fadeshow active"
        id="tabs-download-binary-macos-0" role="tabpanel" aria-labelledby="tabs-download-binary-macos-0-tab" tabindex="download-binary-macos"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">   curl -LO <span class="s2">&#34;https://dl.k8s.io/release/</span><span class="k">$(</span>curl -L -s https://dl.k8s.io/release/stable.txt<span class="k">)</span><span class="s2">/bin/darwin/amd64/kubectl&#34;</span>
</span></span><span class="line"><span class="cl">   </span></span></code></pre></div></div><div class="tab-body tab-pane fade"
        id="tabs-download-binary-macos-1" role="tabpanel" aria-labelledby="tabs-download-binary-macos-1-tab" tabindex="download-binary-macos"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">   curl -LO <span class="s2">&#34;https://dl.k8s.io/release/</span><span class="k">$(</span>curl -L -s https://dl.k8s.io/release/stable.txt<span class="k">)</span><span class="s2">/bin/darwin/arm64/kubectl&#34;</span>
</span></span><span class="line"><span class="cl">   </span></span></code></pre></div></div></div>


   
<div class="alert alert-info" role="note"><h4 class="alert-heading">说明：</h4>   <!--
   To download a specific version, replace the `$(curl -L -s https://dl.k8s.io/release/stable.txt)`
   portion of the command with the specific version.

   For example, to download version 1.36.0 on Intel macOS, type:
   -->
<p>如果需要下载某个指定的版本，用该指定版本号替换掉命令的这个部分：<code>$(curl -L -s https://dl.k8s.io/release/stable.txt)</code>。
例如：要为 Intel macOS 系统下载 1.36.0 版本，则输入：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">curl -LO <span class="s2">&#34;https://dl.k8s.io/release/v1.36.0/bin/darwin/amd64/kubectl&#34;</span>
</span></span></code></pre></div>   <!--
   And for macOS on Apple Silicon, type:
   -->
<p>对于 Apple Silicon 版本的 macOS，输入：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">curl -LO <span class="s2">&#34;https://dl.k8s.io/release/v1.36.0/bin/darwin/arm64/kubectl&#34;</span>
</span></span></code></pre></div></div>


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

   Download the kubectl checksum file:
-->
2. 验证可执行文件（可选操作）

   下载 kubectl 的校验和文件：

   <ul class="nav nav-tabs" id="tabs-download-checksum-macos" role="tablist"><li class="nav-item"><a data-bs-toggle="tab" class="nav-link active" href="#tabs-download-checksum-macos-0" role="tab" aria-controls="tabs-download-checksum-macos-0" aria-selected="true">Intel</a></li>
	  
		<li class="nav-item"><a data-bs-toggle="tab" class="nav-link" href="#tabs-download-checksum-macos-1" role="tab" aria-controls="tabs-download-checksum-macos-1">Apple Silicon</a></li></ul>

<div class="tab-content" id="tabs-download-checksum-macos-content"><div class="tab-body tab-pane fadeshow active"
        id="tabs-download-checksum-macos-0" role="tabpanel" aria-labelledby="tabs-download-checksum-macos-0-tab" tabindex="download-checksum-macos"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">   curl -LO <span class="s2">&#34;https://dl.k8s.io/release/</span><span class="k">$(</span>curl -L -s https://dl.k8s.io/release/stable.txt<span class="k">)</span><span class="s2">/bin/darwin/amd64/kubectl.sha256&#34;</span>
</span></span><span class="line"><span class="cl">   </span></span></code></pre></div></div><div class="tab-body tab-pane fade"
        id="tabs-download-checksum-macos-1" role="tabpanel" aria-labelledby="tabs-download-checksum-macos-1-tab" tabindex="download-checksum-macos"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">   curl -LO <span class="s2">&#34;https://dl.k8s.io/release/</span><span class="k">$(</span>curl -L -s https://dl.k8s.io/release/stable.txt<span class="k">)</span><span class="s2">/bin/darwin/arm64/kubectl.sha256&#34;</span>
</span></span><span class="line"><span class="cl">   </span></span></code></pre></div></div></div>


   <!--
   Validate the kubectl binary against the checksum file:
   -->
   根据校验和文件，验证 kubectl：

   ```bash
   echo "$(cat kubectl.sha256)  kubectl" | shasum -a 256 --check
   ```

   <!-- 
   If valid, the output is:
   -->
   验证通过时，输出如下：

   ```console
   kubectl: OK
   ```

   <!--
   If the check fails, `shasum` exits with nonzero status and prints output similar to:
   -->
   验证失败时，`shasum` 将以非零值退出，并打印如下输出：

   ```console
   kubectl: FAILED
   shasum: WARNING: 1 computed checksum did NOT match
   ```

   
<div class="alert alert-info" role="note"><h4 class="alert-heading">说明：</h4><!--
   Download the same version of the binary and checksum.
   -->
<p>下载的 kubectl 与校验和文件版本要相同。</p></div>


<!--
1. Make the kubectl binary executable.
-->
3. 将 kubectl 置为可执行文件：

   ```bash
   chmod +x ./kubectl
   ```

<!--
1. Move the kubectl binary to a file location on your system `PATH`.
-->
4. 将可执行文件 kubectl 移动到系统可寻址路径 `PATH` 内的一个位置：

   ```bash
   sudo mv ./kubectl /usr/local/bin/kubectl
   sudo chown root: /usr/local/bin/kubectl
   ```

   
<div class="alert alert-info" role="note"><h4 class="alert-heading">说明：</h4><!--
   Make sure `/usr/local/bin` is in your PATH environment variable.
   -->
<p>确保 <code>/usr/local/bin</code> 在你的 PATH 环境变量中。</p></div>


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

   ```bash
   kubectl version --client
   ```

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

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

<!--
1. After installing and validating kubectl, delete the checksum file:
-->
1. 安装并验证 kubectl 后，删除校验和文件：

   ```bash
   rm kubectl.sha256
   ```
<!-- 
### Install with Homebrew on macOS
-->
### 用 Homebrew 在 macOS 系统上安装 {#install-with-homebrew-on-macos}

<!--
If you are on macOS and using [Homebrew](https://brew.sh/) package manager,
you can install kubectl with Homebrew.
-->
如果你是 macOS 系统，且用的是 [Homebrew](https://brew.sh/) 包管理工具，
则可以用 Homebrew 安装 kubectl。

<!--
1. Run the installation command:
-->
1. 运行安装命令：

   ```bash
   brew install kubectl
   ```

   <!-- or -->
   或

   ```bash
   brew install kubernetes-cli
   ```

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

   ```bash
   kubectl version --client
   ```

<!--
### Install with Macports on macOS
-->
### 用 Macports 在 macOS 系统上安装 {#install-with-macports-on-macos}

<!--
If you are on macOS and using [Macports](https://macports.org/) package manager,
you can install kubectl with Macports.
-->
如果你用的是 macOS，且用 [Macports](https://macports.org/) 包管理工具，则你可以用 Macports 安装 kubectl。

<!--
1. Run the installation command:
-->
1. 运行安装命令：

   ```bash
   sudo port selfupdate
   sudo port install kubectl
   ```

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

   ```bash
   kubectl version --client
   ```

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


	<!-- 
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-and-plugins}

### 启用 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 Bash, Fish, and Zsh.
-->
kubectl 为 Bash、Zsh、Fish 和 PowerShell 提供自动补全功能，可以为你节省大量的输入。

下面是为 Bash、Fish 和 Zsh 设置自动补全功能的操作步骤。

<ul class="nav nav-tabs" id="tabs-kubectl-autocompletion" role="tablist"><li class="nav-item"><a data-bs-toggle="tab" class="nav-link active" href="#tabs-kubectl-autocompletion-0" role="tab" aria-controls="tabs-kubectl-autocompletion-0" aria-selected="true">Bash</a></li>
	  
		<li class="nav-item"><a data-bs-toggle="tab" class="nav-link" href="#tabs-kubectl-autocompletion-1" role="tab" aria-controls="tabs-kubectl-autocompletion-1">Fish</a></li>
		<li class="nav-item"><a data-bs-toggle="tab" class="nav-link" href="#tabs-kubectl-autocompletion-2" role="tab" aria-controls="tabs-kubectl-autocompletion-2">Zsh</a></li></ul>

<div class="tab-content" id="tabs-kubectl-autocompletion-content"><div class="tab-body tab-pane fadeshow active"
        id="tabs-kubectl-autocompletion-0" role="tabpanel" aria-labelledby="tabs-kubectl-autocompletion-0-tab" tabindex="kubectl-autocompletion"><!-- 
title: "bash auto-completion on macOS"
description: "Some optional configuration for bash auto-completion on macOS."
headless: true
build:
  list: never
  render: never
  publishResources: false
-->
<!-- 
### Introduction
-->
<h3 id="introduction">简介<a class="td-heading-self-link" href="#introduction" aria-label="Heading self-link"></a></h3>
<!-- 
The kubectl completion script for Bash can be generated with `kubectl completion bash`.
Sourcing this script in your shell enables kubectl completion.

However, the kubectl completion script depends on
[**bash-completion**](https://github.com/scop/bash-completion) which you thus have to previously install.
-->
<p>kubectl 的 Bash 补全脚本可以通过 <code>kubectl completion bash</code> 命令生成。
在你的 Shell 中导入（Sourcing）这个脚本即可启用补全功能。</p>
<p>此外，kubectl 补全脚本依赖于工具 <a href="https://github.com/scop/bash-completion"><strong>bash-completion</strong></a>，
所以你必须先安装它。</p>
<div class="alert alert-danger" role="note"><h4 class="alert-heading">警告：</h4><!-- 
There are two versions of bash-completion, v1 and v2. V1 is for Bash 3.2
(which is the default on macOS), and v2 is for Bash 4.1+. The kubectl completion
script **doesn't work** correctly with bash-completion v1 and Bash 3.2.
It requires **bash-completion v2** and **Bash 4.1+**. Thus, to be able to
correctly use kubectl completion on macOS, you have to install and use
Bash 4.1+ ([*instructions*](https://apple.stackexchange.com/a/292760)).
The following instructions assume that you use Bash 4.1+
(that is, any Bash version of 4.1 or newer).
-->
<p>bash-completion 有两个版本：v1 和 v2。v1 对应 Bash 3.2（也是 macOS 的默认安装版本），
v2 对应 Bash 4.1+。kubectl 的补全脚本<strong>无法适配</strong> bash-completion v1 和 Bash 3.2。
必须为它配备 <strong>bash-completion v2</strong> 和 <strong>Bash 4.1+</strong>。
有鉴于此，为了在 macOS 上使用 kubectl 补全功能，你必须要安装和使用 Bash 4.1+
（<a href="https://apple.stackexchange.com/a/292760"><strong>说明</strong></a>）。
后续说明假定你用的是 Bash 4.1+（也就是 Bash 4.1 或更新的版本）。</p></div>

<!-- 
### Upgrade Bash
-->
<h3 id="upgrade-bash">升级 Bash<a class="td-heading-self-link" href="#upgrade-bash" aria-label="Heading self-link"></a></h3>
<!-- 
The instructions here assume you use Bash 4.1+. You can check your Bash's version by running:
-->
<p>后续说明假定你已使用 Bash 4.1+。你可以运行以下命令检查 Bash 版本：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="nb">echo</span> <span class="nv">$BASH_VERSION</span>
</span></span></code></pre></div><!-- 
If it is too old, you can install/upgrade it using Homebrew:
-->
<p>如果版本太旧，可以用 Homebrew 安装/升级：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">brew install bash
</span></span></code></pre></div><!-- 
Reload your shell and verify that the desired version is being used:
-->
<p>重新加载 Shell，并验证所需的版本已经生效：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="nb">echo</span> <span class="nv">$BASH_VERSION</span> <span class="nv">$SHELL</span>
</span></span></code></pre></div><!-- 
Homebrew usually installs it at `/usr/local/bin/bash`.
-->
<p>Homebrew 通常把它安装为 <code>/usr/local/bin/bash</code>。</p>
<!-- 
### Install bash-completion
-->
<h3 id="install-bash-completion">安装 bash-completion<a class="td-heading-self-link" href="#install-bash-completion" aria-label="Heading self-link"></a></h3>

<div class="alert alert-info" role="note"><h4 class="alert-heading">说明：</h4><!--
As mentioned, these instructions assume you use Bash 4.1+,
which means you will install bash-completion v2 (in contrast to Bash 3.2 and bash-completion v1,
in which case kubectl completion won't work).
-->
<p>如前所述，本说明假定你使用的 Bash 版本为 4.1+，这意味着你要安装 bash-completion v2
（不同于 Bash 3.2 和 bash-completion v1，kubectl 的补全功能在该场景下无法工作）。</p></div>

<!-- 
You can test if you have bash-completion v2 already installed with `type _init_completion`.
If not, you can install it with Homebrew:
-->
<p>你可以用命令 <code>type _init_completion</code> 测试 bash-completion v2 是否已经安装。
如未安装，用 Homebrew 来安装它：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">brew install bash-completion@2
</span></span></code></pre></div><!-- 
As stated in the output of this command, add the following to your `~/.bash_profile` file:
-->
<p>如命令的输出信息所显示的，将如下内容添加到文件 <code>~/.bash_profile</code> 中：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="nv">brew_etc</span><span class="o">=</span><span class="s2">&#34;</span><span class="k">$(</span>brew --prefix<span class="k">)</span><span class="s2">/etc&#34;</span> <span class="o">&amp;&amp;</span> <span class="o">[[</span> -r <span class="s2">&#34;</span><span class="si">${</span><span class="nv">brew_etc</span><span class="si">}</span><span class="s2">/profile.d/bash_completion.sh&#34;</span> <span class="o">]]</span> <span class="o">&amp;&amp;</span> . <span class="s2">&#34;</span><span class="si">${</span><span class="nv">brew_etc</span><span class="si">}</span><span class="s2">/profile.d/bash_completion.sh&#34;</span>
</span></span></code></pre></div><!-- 
Reload your shell and verify that bash-completion v2 is correctly installed with `type _init_completion`.
-->
<p>重新加载 Shell，并用命令 <code>type _init_completion</code> 验证 bash-completion v2 已经恰当的安装。</p>
<!-- 
### Enable kubectl autocompletion
-->
<h3 id="enable-kubectl-autocompletion">启用 kubectl 自动补全功能<a class="td-heading-self-link" href="#enable-kubectl-autocompletion" aria-label="Heading self-link"></a></h3>
<!-- 
You now have to ensure that the kubectl completion script gets sourced in all
your shell sessions. There are multiple ways to achieve this:

- Source the completion script in your `~/.bash_profile` file:
-->
<p>你现在需要确保在所有的 Shell 环境中均已导入（sourced）kubectl 的补全脚本，
有若干种方法可以实现这一点：</p>
<ul>
<li>
<p>在文件 <code>~/.bash_profile</code> 中导入（Source）补全脚本：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s1">&#39;source &lt;(kubectl completion bash)&#39;</span> &gt;&gt;~/.bash_profile
</span></span></code></pre></div></li>
</ul>
<!-- 
- Add the completion script to the `/usr/local/etc/bash_completion.d` directory:
-->
<ul>
<li>
<p>将补全脚本添加到目录 <code>/usr/local/etc/bash_completion.d</code> 中：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">kubectl completion bash &gt;/usr/local/etc/bash_completion.d/kubectl
</span></span></code></pre></div></li>
</ul>
<!-- 
- If you have an alias for kubectl, you can extend shell completion to work with that alias:
-->
<ul>
<li>
<p>如果你为 kubectl 定义了别名，则可以扩展 Shell 补全来兼容该别名：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s1">&#39;alias k=kubectl&#39;</span> &gt;&gt;~/.bash_profile
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s1">&#39;complete -o default -F __start_kubectl k&#39;</span> &gt;&gt;~/.bash_profile
</span></span></code></pre></div></li>
</ul>
<!-- 
- If you installed kubectl with Homebrew (as explained
  [here](/docs/tasks/tools/install-kubectl-macos/#install-with-homebrew-on-macos)),
  then the kubectl completion script should already be in `/usr/local/etc/bash_completion.d/kubectl`.
  In that case, you don't need to do anything.
-->
<ul>
<li>
<p>如果你是用 Homebrew 安装的 kubectl
（如<a href="/zh-cn/docs/tasks/tools/install-kubectl-macos/#install-with-homebrew-on-macos">此页面</a>所描述），
则 kubectl 补全脚本应该已经安装到目录 <code>/usr/local/etc/bash_completion.d/kubectl</code>
中了。这种情况下，你什么都不需要做。</p>

<div class="alert alert-info" role="note"><h4 class="alert-heading">说明：</h4><!-- 
  The Homebrew installation of bash-completion v2 sources all the files in the
  `BASH_COMPLETION_COMPAT_DIR` directory, that's why the latter two methods work.
  -->
<p>用 Hommbrew 安装的 bash-completion v2 会初始化目录 <code>BASH_COMPLETION_COMPAT_DIR</code>
中的所有文件，这就是后两种方法能正常工作的原因。</p></div>

</li>
</ul>
<!-- 
In any case, after reloading your shell, kubectl completion should be working.
-->
<p>总之，重新加载 Shell 之后，kubectl 补全功能将立即生效。</p>
</div><div class="tab-body tab-pane fade"
        id="tabs-kubectl-autocompletion-1" role="tabpanel" aria-labelledby="tabs-kubectl-autocompletion-1-tab" tabindex="kubectl-autocompletion"><!--
title: "fish auto-completion"
description: "Optional configuration to enable fish shell auto-completion."
headless: true
build:
  list: never
  render: never
  publishResources: false
-->

<div class="alert alert-info" role="note"><h4 class="alert-heading">说明：</h4><!--
Autocomplete for Fish requires kubectl 1.23 or later.
-->
<p>自动补全 Fish 需要 kubectl 1.23 或更高版本。</p></div>

<!--
The kubectl completion script for Fish can be generated with the command `kubectl completion fish`. Sourcing the completion script in your shell enables kubectl autocompletion.

To do so in all your shell sessions, add the following line to your `~/.config/fish/config.fish` file:
-->
<p>kubectl 通过命令 <code>kubectl completion fish</code> 生成 Fish 自动补全脚本。
在 shell 中导入（Sourcing）该自动补全脚本，将启动 kubectl 自动补全功能。</p>
<p>为了在所有的 shell 会话中实现此功能，请将下面内容加入到文件 <code>~/.config/fish/config.fish</code> 中。</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">kubectl completion fish <span class="p">|</span> <span class="nb">source</span>
</span></span></code></pre></div><!--
After reloading your shell, kubectl autocompletion should be working.
-->
<p>重新加载 shell 后，kubectl 自动补全功能将立即生效。</p>
</div><div class="tab-body tab-pane fade"
        id="tabs-kubectl-autocompletion-2" role="tabpanel" aria-labelledby="tabs-kubectl-autocompletion-2-tab" tabindex="kubectl-autocompletion"><!-- 
title: "zsh auto-completion"
description: "Some optional configuration for zsh auto-completion."
headless: true
-->
<!-- 
The kubectl completion script for Zsh can be generated with the command `kubectl completion zsh`. Sourcing the completion script in your shell enables kubectl autocompletion.

To do so in all your shell sessions, add the following to your `~/.zshrc` file:
-->
<p>kubectl 通过命令 <code>kubectl completion zsh</code> 生成 Zsh 自动补全脚本。
在 Shell 中导入（Sourcing）该自动补全脚本，将启动 kubectl 自动补全功能。</p>
<p>为了在所有的 Shell 会话中实现此功能，请将下面内容加入到文件 <code>~/.zshrc</code> 中。</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-zsh" data-lang="zsh"><span class="line"><span class="cl"><span class="nb">source</span> &lt;<span class="o">(</span>kubectl completion zsh<span class="o">)</span>
</span></span></code></pre></div><!-- 
If you have an alias for kubectl, kubectl autocompletion will automatically work with it.
-->
<p>如果你为 kubectl 定义了别名，kubectl 自动补全将自动使用它。</p>
<!-- 
After reloading your shell, kubectl autocompletion should be working.

If you get an error like `complete:13: command not found: compdef`, then add the following to the beginning of your `~/.zshrc` file:
If you get an error like `2: command not found: compdef`, then add the following to the beginning of your `~/.zshrc` file:
-->
<p>重新加载 Shell 后，kubectl 自动补全功能将立即生效。</p>
<p>如果你收到 <code>2: command not found: compdef</code> 这样的错误提示，那请将下面内容添加到
<code>~/.zshrc</code> 文件的开头：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-zsh" data-lang="zsh"><span class="line"><span class="cl">autoload -Uz compinit
</span></span><span class="line"><span class="cl">compinit
</span></span></code></pre></div></div></div>


<!--
### 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. 用以下命令下载最新发行版：

   <ul class="nav nav-tabs" id="tabs-download-convert-binary-macos" role="tablist"><li class="nav-item"><a data-bs-toggle="tab" class="nav-link active" href="#tabs-download-convert-binary-macos-0" role="tab" aria-controls="tabs-download-convert-binary-macos-0" aria-selected="true">Intel</a></li>
	  
		<li class="nav-item"><a data-bs-toggle="tab" class="nav-link" href="#tabs-download-convert-binary-macos-1" role="tab" aria-controls="tabs-download-convert-binary-macos-1">Apple Silicon</a></li></ul>

<div class="tab-content" id="tabs-download-convert-binary-macos-content"><div class="tab-body tab-pane fadeshow active"
        id="tabs-download-convert-binary-macos-0" role="tabpanel" aria-labelledby="tabs-download-convert-binary-macos-0-tab" tabindex="download-convert-binary-macos"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">   curl -LO <span class="s2">&#34;https://dl.k8s.io/release/</span><span class="k">$(</span>curl -L -s https://dl.k8s.io/release/stable.txt<span class="k">)</span><span class="s2">/bin/darwin/amd64/kubectl-convert&#34;</span>
</span></span><span class="line"><span class="cl">   </span></span></code></pre></div></div><div class="tab-body tab-pane fade"
        id="tabs-download-convert-binary-macos-1" role="tabpanel" aria-labelledby="tabs-download-convert-binary-macos-1-tab" tabindex="download-convert-binary-macos"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">   curl -LO <span class="s2">&#34;https://dl.k8s.io/release/</span><span class="k">$(</span>curl -L -s https://dl.k8s.io/release/stable.txt<span class="k">)</span><span class="s2">/bin/darwin/arm64/kubectl-convert&#34;</span>
</span></span><span class="line"><span class="cl">   </span></span></code></pre></div></div></div>


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

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

   下载 kubectl-convert 校验和文件：

   <ul class="nav nav-tabs" id="tabs-download-convert-checksum-macos" role="tablist"><li class="nav-item"><a data-bs-toggle="tab" class="nav-link active" href="#tabs-download-convert-checksum-macos-0" role="tab" aria-controls="tabs-download-convert-checksum-macos-0" aria-selected="true">Intel</a></li>
	  
		<li class="nav-item"><a data-bs-toggle="tab" class="nav-link" href="#tabs-download-convert-checksum-macos-1" role="tab" aria-controls="tabs-download-convert-checksum-macos-1">Apple Silicon</a></li></ul>

<div class="tab-content" id="tabs-download-convert-checksum-macos-content"><div class="tab-body tab-pane fadeshow active"
        id="tabs-download-convert-checksum-macos-0" role="tabpanel" aria-labelledby="tabs-download-convert-checksum-macos-0-tab" tabindex="download-convert-checksum-macos"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">   curl -LO <span class="s2">&#34;https://dl.k8s.io/release/</span><span class="k">$(</span>curl -L -s https://dl.k8s.io/release/stable.txt<span class="k">)</span><span class="s2">/bin/darwin/amd64/kubectl-convert.sha256&#34;</span>
</span></span><span class="line"><span class="cl">   </span></span></code></pre></div></div><div class="tab-body tab-pane fade"
        id="tabs-download-convert-checksum-macos-1" role="tabpanel" aria-labelledby="tabs-download-convert-checksum-macos-1-tab" tabindex="download-convert-checksum-macos"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">   curl -LO <span class="s2">&#34;https://dl.k8s.io/release/</span><span class="k">$(</span>curl -L -s https://dl.k8s.io/release/stable.txt<span class="k">)</span><span class="s2">/bin/darwin/arm64/kubectl-convert.sha256&#34;</span>
</span></span><span class="line"><span class="cl">   </span></span></code></pre></div></div></div>


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

   ```bash
   echo "$(cat kubectl-convert.sha256)  kubectl-convert" | shasum -a 256 --check
   ```

   <!--
   If valid, the output is:
   -->
   验证通过时，输出为：

   ```console
   kubectl-convert: OK
   ```

   <!--
   If the check fails, `shasum` exits with nonzero status and prints output similar to:
   -->
   验证失败时，`sha256` 将以非零值退出，并打印输出类似于：

   ```console
   kubectl-convert: FAILED
   shasum: WARNING: 1 computed checksum did NOT match
   ```

   
<div class="alert alert-info" role="note"><h4 class="alert-heading">说明：</h4><!--
   Download the same version of the binary and checksum.
   -->
<p>下载相同版本的可执行文件和校验和。</p></div>


<!--
1. Make kubectl-convert binary executable
-->
3. 使 kubectl-convert 二进制文件可执行

   ```bash
   chmod +x ./kubectl-convert
   ```

<!--
1. Move the kubectl-convert binary to a file location on your system `PATH`.
-->
4. 将 kubectl-convert 可执行文件移动到系统 `PATH` 环境变量中的一个位置。

   ```bash
   sudo mv ./kubectl-convert /usr/local/bin/kubectl-convert
   sudo chown root: /usr/local/bin/kubectl-convert
   ```

   
<div class="alert alert-info" role="note"><h4 class="alert-heading">说明：</h4><!--
   Make sure `/usr/local/bin` is in your PATH environment variable.
   -->
<p>确保你的 PATH 环境变量中存在 <code>/usr/local/bin</code>。</p></div>


<!--
1. Verify plugin is successfully installed
-->
5. 验证插件是否安装成功

   ```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:
-->
6. 安装插件后，清理安装文件：

   ```bash
   rm kubectl-convert kubectl-convert.sha256
   ```

<!--
### Uninstall kubectl on macOS

Depending on how you installed `kubectl`, use one of the following methods.
-->
### 在 macOS 上卸载 kubectl   {#uninstall-kubectl-on-macos}

根据你安装 `kubectl` 的方式，使用以下某种方法来卸载：

<!--
### Uninstall kubectl using the command-line

1.  Locate the `kubectl` binary on your system:
-->
### 使用命令行卸载 kubectl   {#uninstall-kubectl-using-cli}

1. 找到你系统上的 `kubectl` 可执行文件：

   ```bash
   which kubectl
   ```

<!--
1.  Remove the `kubectl` binary:
-->
2. 移除 `kubectl` 可执行文件：

   ```bash
   sudo rm <path>
   ```

   <!--
   Replace `<path>` with the path to the `kubectl` binary from the previous step. For example, `sudo rm /usr/local/bin/kubectl`.
   -->
   将 `<path>` 替换为上一步中找到的 `kubectl` 可执行文件的路径。
   例如，`sudo rm /usr/local/bin/kubectl`。

<!--
### Uninstall kubectl using homebrew

If you installed `kubectl` using Homebrew, run the following command:
-->
### 使用 Homebrew 卸载 kubectl    {#uninstall-kubectl-using-homebrew}

如果你使用 Homebrew 安装了 `kubectl`，运行以下命令：

```bash
brew remove kubectl
```

## 接下来


	<!-- 
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>
