|
| 1 | +--- |
| 2 | +title: 在 Windows 上安装 kubectl |
| 3 | +content_type: task |
| 4 | +weight: 10 |
| 5 | +card: |
| 6 | + name: tasks |
| 7 | + weight: 20 |
| 8 | + title: Windows 安装 kubectl |
| 9 | +--- |
| 10 | +<!-- |
| 11 | +--- |
| 12 | +reviewers: |
| 13 | +- mikedanese |
| 14 | +title: Install and Set Up kubectl on Windows |
| 15 | +content_type: task |
| 16 | +weight: 10 |
| 17 | +card: |
| 18 | + name: tasks |
| 19 | + weight: 20 |
| 20 | + title: Install kubectl on Windows |
| 21 | +--- |
| 22 | +--> |
| 23 | + |
| 24 | +## {{% heading "prerequisites" %}} |
| 25 | + |
| 26 | +<!-- |
| 27 | +You must use a kubectl version that is within one minor version difference of your cluster. |
| 28 | +For example, a v1.2 client should work with v1.1, v1.2, and v1.3 master. |
| 29 | +Using the latest version of kubectl helps avoid unforeseen issues. |
| 30 | +--> |
| 31 | +kubectl 版本和集群版本之间的差异必须在一个小版本号内。 |
| 32 | +例如:v1.2 版本的客户端只能与 v1.1、v1.2 和 v1.3 版本的集群一起工作。 |
| 33 | +用最新版的 kubectl 有助于避免不可预见的问题。 |
| 34 | + |
| 35 | +<!-- |
| 36 | +## Install kubectl on Windows |
| 37 | +--> |
| 38 | +## 在 Windows 上安装 kubectl {#install-kubectl-on-windows} |
| 39 | + |
| 40 | +<!-- |
| 41 | +The following methods exist for installing kubectl on Windows: |
| 42 | +--> |
| 43 | +在 Windows 系统中安装 kubectl 有如下几种方法: |
| 44 | + |
| 45 | +- [用 curl 在 Windows 上安装 kubectl](#install-kubectl-binary-with-curl-on-windows) |
| 46 | +- [用 PowerShell 从 PSGallery 安装](#install-with-powershell-from-psgallery) |
| 47 | +- [在 Windows 上用 Chocolatey 或 Scoop 安装](#install-on-windows-using-chocolatey-or-scoop) |
| 48 | +- [作为谷歌云 SDK 的一部分,在 Windows 上安装](#install-on-windows-as-part-of-the-google-cloud-sdk) |
| 49 | + |
| 50 | +<!-- |
| 51 | +### Install kubectl binary with curl on Windows |
| 52 | +--> |
| 53 | +### 用 curl 在 Windows 上安装 kubectl {#install-kubectl-binary-with-curl-on-windows} |
| 54 | + |
| 55 | +<!-- |
| 56 | +1. Download the [latest release {{< param "fullversion" >}}](https://dl.k8s.io/release/{{< param "fullversion" >}}/bin/windows/amd64/kubectl.exe). |
| 57 | +
|
| 58 | + Or if you have `curl` installed, use this command: |
| 59 | +--> |
| 60 | +1. 下载 [最新发行版 {{< param "fullversion" >}}](https://dl.k8s.io/release/{{< param "fullversion" >}}/bin/windows/amd64/kubectl.exe)。 |
| 61 | + |
| 62 | + 如果你已安装了 `curl`,也可以使用此命令: |
| 63 | + |
| 64 | + ```powershell |
| 65 | + curl -LO https://dl.k8s.io/release/{{< param "fullversion" >}}/bin/windows/amd64/kubectl.exe |
| 66 | + ``` |
| 67 | + |
| 68 | + <!-- |
| 69 | + 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). |
| 70 | + --> |
| 71 | + {{< note >}} |
| 72 | + 要想找到最新稳定的版本(例如:为了编写脚本),可以看看这里 [https://dl.k8s.io/release/stable.txt](https://dl.k8s.io/release/stable.txt)。 |
| 73 | + {{< /note >}} |
| 74 | + |
| 75 | + <!-- |
| 76 | + 1. Validate the binary (optional) |
| 77 | +
|
| 78 | + Download the kubectl checksum file: |
| 79 | + --> |
| 80 | +1. 验证该可执行文件(可选步骤) |
| 81 | + |
| 82 | + 下载 kubectl 校验和文件: |
| 83 | + |
| 84 | + ```powershell |
| 85 | + curl -LO https://dl.k8s.io/{{< param "fullversion" >}}/bin/windows/amd64/kubectl.exe.sha256 |
| 86 | + ``` |
| 87 | + |
| 88 | + <!-- |
| 89 | + Validate the kubectl binary against the checksum file: |
| 90 | + --> |
| 91 | + 基于校验和文件,验证 kubectl 的可执行文件: |
| 92 | + |
| 93 | + <!-- |
| 94 | + - Using Command Prompt to manually compare `CertUtil`'s output to the checksum file downloaded: |
| 95 | + --> |
| 96 | + - 在命令行环境中,手工对比 `CertUtil` 命令的输出与校验和文件: |
| 97 | + |
| 98 | + ```cmd |
| 99 | + CertUtil -hashfile kubectl.exe SHA256 |
| 100 | + type kubectl.exe.sha256 |
| 101 | + ``` |
| 102 | +
|
| 103 | + <!-- |
| 104 | + - Using PowerShell to automate the verification using the `-eq` operator to get a `True` or `False` result: |
| 105 | + --> |
| 106 | + - 用 PowerShell 自动验证,用运算符 `-eq` 来直接取得 `True` 或 `False` 的结果: |
| 107 | +
|
| 108 | + ```powershell |
| 109 | + $($(CertUtil -hashfile .\kubectl.exe SHA256)[1] -replace " ", "") -eq $(type .\kubectl.exe.sha256) |
| 110 | + ``` |
| 111 | +
|
| 112 | + <!-- |
| 113 | + 1. Add the binary in to your `PATH`. |
| 114 | +
|
| 115 | + 1. Test to ensure the version of `kubectl` is the same as downloaded: |
| 116 | + --> |
| 117 | +1. 将可执行文件的路径添加到 `PATH`。 |
| 118 | +
|
| 119 | +1. 测试一下,确保此 `kubectl` 的版本和期望版本一致: |
| 120 | +
|
| 121 | + ```cmd |
| 122 | + kubectl version --client |
| 123 | + ``` |
| 124 | + |
| 125 | +<!-- |
| 126 | +[Docker Desktop for Windows](https://docs.docker.com/docker-for-windows/#kubernetes) adds its own version of `kubectl` to `PATH`. |
| 127 | +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`. |
| 128 | +--> |
| 129 | +{{< note >}} |
| 130 | +[Windows 版的 Docker Desktop](https://docs.docker.com/docker-for-windows/#kubernetes) |
| 131 | +将其自带版本的 `kubectl` 添加到 `PATH`。 |
| 132 | +如果你之前安装过 Docker Desktop,可能需要把此 `PATH` 条目置于 Docker Desktop 安装的条目之前, |
| 133 | +或者直接删掉 Docker Desktop 的 `kubectl`。 |
| 134 | +{{< /note >}} |
| 135 | + |
| 136 | +<!-- |
| 137 | +### Install with PowerShell from PSGallery |
| 138 | +--> |
| 139 | +### 用 PowerShell 从 PSGallery 安装 {#install-with-powershell-from-psgallery} |
| 140 | + |
| 141 | +<!-- |
| 142 | +If you are on Windows and using the [PowerShell Gallery](https://www.powershellgallery.com/) package manager, you can install and update kubectl with PowerShell. |
| 143 | +--> |
| 144 | +如果你工作在 Windows 平台上,且使用 [PowerShell Gallery](https://www.powershellgallery.com/) 包管理器, |
| 145 | +则可以用 PowerShell 安装、更新 kubectl。 |
| 146 | + |
| 147 | +<!-- |
| 148 | +1. Run the installation commands (making sure to specify a `DownloadLocation`): |
| 149 | +--> |
| 150 | +1. 运行安装命令(确保提供了参数 `DownloadLocation`): |
| 151 | + |
| 152 | + ```powershell |
| 153 | + Install-Script -Name 'install-kubectl' -Scope CurrentUser -Force |
| 154 | + install-kubectl.ps1 [-DownloadLocation <path>] |
| 155 | + ``` |
| 156 | + |
| 157 | + <!-- |
| 158 | + If you do not specify a `DownloadLocation`, `kubectl` will be installed in the user's `temp` Directory. |
| 159 | + --> |
| 160 | + {{< note >}} |
| 161 | + 如果没有指定 `DownloadLocation`,`kubectl` 则会被安装到用户的 `temp` 目录下。 |
| 162 | + {{< /note >}} |
| 163 | + |
| 164 | + <!-- |
| 165 | + The installer creates `$HOME/.kube` and instructs it to create a config file. |
| 166 | + --> |
| 167 | + 安装程序创建 `$HOME/.kube`,并指示其创建配置文件。 |
| 168 | + |
| 169 | + <!-- |
| 170 | + 1. Test to ensure the version you installed is up-to-date: |
| 171 | + --> |
| 172 | +1. 测试一下,确保你安装的是最新版本: |
| 173 | + |
| 174 | + ```powershell |
| 175 | + kubectl version --client |
| 176 | + ``` |
| 177 | + |
| 178 | +<!-- |
| 179 | +Updating the installation is performed by rerunning the two commands listed in step 1. |
| 180 | +--> |
| 181 | +{{< note >}} |
| 182 | +更新安装是通过重新运行步骤 1 中的两个命令而实现。 |
| 183 | +{{< /note >}} |
| 184 | + |
| 185 | +<!-- |
| 186 | +### Install on Windows using Chocolatey or Scoop |
| 187 | +--> |
| 188 | +### 在 Windows 上用 Chocolatey 或 Scoop 安装 {#install-on-windows-using-chocolatey-or-scoop} |
| 189 | + |
| 190 | +<!-- |
| 191 | +1. To install kubectl on Windows you can use either [Chocolatey](https://chocolatey.org) package manager or [Scoop](https://scoop.sh) command-line installer. |
| 192 | +--> |
| 193 | +1. 要在 Windows 上安装 kubectl,你可以使用包管理器 [Chocolatey](https://chocolatey.org) |
| 194 | + 或是命令行安装器 [Scoop](https://scoop.sh)。 |
| 195 | + |
| 196 | + {{< tabs name="kubectl_win_install" >}} |
| 197 | + {{% tab name="choco" %}} |
| 198 | + ```powershell |
| 199 | + choco install kubernetes-cli |
| 200 | + ``` |
| 201 | + {{% /tab %}} |
| 202 | + {{% tab name="scoop" %}} |
| 203 | + ```powershell |
| 204 | + scoop install kubectl |
| 205 | + ``` |
| 206 | + {{% /tab %}} |
| 207 | + {{< /tabs >}} |
| 208 | + |
| 209 | + <!-- |
| 210 | + 1. Test to ensure the version you installed is up-to-date: |
| 211 | + --> |
| 212 | +2. 测试一下,确保安装的是最新版本: |
| 213 | + |
| 214 | + ```powershell |
| 215 | + kubectl version --client |
| 216 | + ``` |
| 217 | + |
| 218 | + <!-- |
| 219 | + 1. Navigate to your home directory: |
| 220 | + --> |
| 221 | +3. 导航到你的 home 目录: |
| 222 | + |
| 223 | + <!-- |
| 224 | + # If you're using cmd.exe, run: cd %USERPROFILE% |
| 225 | + --> |
| 226 | + ```powershell |
| 227 | + # 当你用 cmd.exe 时,则运行: cd %USERPROFILE% |
| 228 | + cd ~ |
| 229 | + ``` |
| 230 | + |
| 231 | + <!-- |
| 232 | + 1. Create the `.kube` directory: |
| 233 | + --> |
| 234 | +4. 创建目录 `.kube`: |
| 235 | + |
| 236 | + ```powershell |
| 237 | + mkdir .kube |
| 238 | + ``` |
| 239 | + |
| 240 | + <!-- |
| 241 | + 1. Change to the `.kube` directory you just created: |
| 242 | + --> |
| 243 | +5. 切换到新创建的目录 `.kube` |
| 244 | + |
| 245 | + ```powershell |
| 246 | + cd .kube |
| 247 | + ``` |
| 248 | + |
| 249 | + <!-- |
| 250 | + 1. Configure kubectl to use a remote Kubernetes cluster: |
| 251 | + --> |
| 252 | +6. 配置 kubectl,以接入远程的 Kubernetes 集群: |
| 253 | + |
| 254 | + ```powershell |
| 255 | + New-Item config -type file |
| 256 | + ``` |
| 257 | + |
| 258 | +<!-- |
| 259 | +Edit the config file with a text editor of your choice, such as Notepad. |
| 260 | +--> |
| 261 | +{{< note >}} |
| 262 | +编辑配置文件,你需要先选择一个文本编辑器,比如 Notepad。 |
| 263 | +{{< /note >}} |
| 264 | + |
| 265 | +<!-- |
| 266 | +### Install on Windows as part of the Google Cloud SDK |
| 267 | +--> |
| 268 | +### 作为谷歌云 SDK 的一部分,在 Windows 上安装 {#install-on-windows-as-part-of-the-google-cloud-sdk} |
| 269 | + |
| 270 | +{{< include "included/install-kubectl-gcloud.md" >}} |
| 271 | + |
| 272 | +<!-- |
| 273 | +## Verify kubectl configuration |
| 274 | +--> |
| 275 | +## 验证 kubectl 配置 {#verify-kubectl-configration} |
| 276 | + |
| 277 | +{{< include "included/verify-kubectl.md" >}} |
| 278 | + |
| 279 | +<!-- |
| 280 | +## Optional kubectl configurations |
| 281 | +
|
| 282 | +### Enable shell autocompletion |
| 283 | +--> |
| 284 | +## kubectl 可选配置 {#optional-kubectl-configurations} |
| 285 | + |
| 286 | +### 启用 shell 自动补全功能 {#enable-shell-autocompletion} |
| 287 | + |
| 288 | +<!-- |
| 289 | +kubectl provides autocompletion support for Bash and Zsh, which can save you a lot of typing. |
| 290 | +
|
| 291 | +Below are the procedures to set up autocompletion for Zsh, if you are running that on Windows. |
| 292 | +--> |
| 293 | +kubectl 为 Bash 和 Zsh 提供自动补全功能,可以减轻许多输入的负担。 |
| 294 | + |
| 295 | +下面是设置 Zsh 自动补全功能的操作步骤,前提是你在 Windows 上面运行的是 Zsh。 |
| 296 | + |
| 297 | +{{< include "included/optional-kubectl-configs-zsh.md" >}} |
| 298 | + |
| 299 | +## {{% heading "whatsnext" %}} |
| 300 | + |
| 301 | +{{< include "included/kubectl-whats-next.md" >}} |
0 commit comments