From 18ec3f432f69e76384dc49193766c304b2f2f0ba Mon Sep 17 00:00:00 2001 From: luzg Date: Thu, 15 Apr 2021 16:11:39 +0800 Subject: [PATCH] [zh] translate task/Install and Set Up kubectl on Windows --- .../tasks/tools/install-kubectl-windows.md | 301 ++++++++++++++++++ 1 file changed, 301 insertions(+) create mode 100644 content/zh/docs/tasks/tools/install-kubectl-windows.md diff --git a/content/zh/docs/tasks/tools/install-kubectl-windows.md b/content/zh/docs/tasks/tools/install-kubectl-windows.md new file mode 100644 index 0000000000..9b3c975056 --- /dev/null +++ b/content/zh/docs/tasks/tools/install-kubectl-windows.md @@ -0,0 +1,301 @@ +--- +title: 在 Windows 上安装 kubectl +content_type: task +weight: 10 +card: + name: tasks + weight: 20 + title: Windows 安装 kubectl +--- + + +## {{% heading "prerequisites" %}} + + +kubectl 版本和集群版本之间的差异必须在一个小版本号内。 +例如:v1.2 版本的客户端只能与 v1.1、v1.2 和 v1.3 版本的集群一起工作。 +用最新版的 kubectl 有助于避免不可预见的问题。 + + +## 在 Windows 上安装 kubectl {#install-kubectl-on-windows} + + +在 Windows 系统中安装 kubectl 有如下几种方法: + +- [用 curl 在 Windows 上安装 kubectl](#install-kubectl-binary-with-curl-on-windows) +- [用 PowerShell 从 PSGallery 安装](#install-with-powershell-from-psgallery) +- [在 Windows 上用 Chocolatey 或 Scoop 安装](#install-on-windows-using-chocolatey-or-scoop) +- [作为谷歌云 SDK 的一部分,在 Windows 上安装](#install-on-windows-as-part-of-the-google-cloud-sdk) + + +### 用 curl 在 Windows 上安装 kubectl {#install-kubectl-binary-with-curl-on-windows} + + +1. 下载 [最新发行版 {{< param "fullversion" >}}](https://dl.k8s.io/release/{{< param "fullversion" >}}/bin/windows/amd64/kubectl.exe)。 + + 如果你已安装了 `curl`,也可以使用此命令: + + ```powershell + curl -LO https://dl.k8s.io/release/{{< param "fullversion" >}}/bin/windows/amd64/kubectl.exe + ``` + + + {{< note >}} + 要想找到最新稳定的版本(例如:为了编写脚本),可以看看这里 [https://dl.k8s.io/release/stable.txt](https://dl.k8s.io/release/stable.txt)。 + {{< /note >}} + + +1. 验证该可执行文件(可选步骤) + + 下载 kubectl 校验和文件: + + ```powershell + curl -LO https://dl.k8s.io/{{< param "fullversion" >}}/bin/windows/amd64/kubectl.exe.sha256 + ``` + + + 基于校验和文件,验证 kubectl 的可执行文件: + + + - 在命令行环境中,手工对比 `CertUtil` 命令的输出与校验和文件: + + ```cmd + CertUtil -hashfile kubectl.exe SHA256 + type kubectl.exe.sha256 + ``` + + + - 用 PowerShell 自动验证,用运算符 `-eq` 来直接取得 `True` 或 `False` 的结果: + + ```powershell + $($(CertUtil -hashfile .\kubectl.exe SHA256)[1] -replace " ", "") -eq $(type .\kubectl.exe.sha256) + ``` + + +1. 将可执行文件的路径添加到 `PATH`。 + +1. 测试一下,确保此 `kubectl` 的版本和期望版本一致: + + ```cmd + kubectl version --client + ``` + + +{{< note >}} +[Windows 版的 Docker Desktop](https://docs.docker.com/docker-for-windows/#kubernetes) +将其自带版本的 `kubectl` 添加到 `PATH`。 +如果你之前安装过 Docker Desktop,可能需要把此 `PATH` 条目置于 Docker Desktop 安装的条目之前, +或者直接删掉 Docker Desktop 的 `kubectl`。 +{{< /note >}} + + +### 用 PowerShell 从 PSGallery 安装 {#install-with-powershell-from-psgallery} + + +如果你工作在 Windows 平台上,且使用 [PowerShell Gallery](https://www.powershellgallery.com/) 包管理器, +则可以用 PowerShell 安装、更新 kubectl。 + + +1. 运行安装命令(确保提供了参数 `DownloadLocation`): + + ```powershell + Install-Script -Name 'install-kubectl' -Scope CurrentUser -Force + install-kubectl.ps1 [-DownloadLocation ] + ``` + + + {{< note >}} + 如果没有指定 `DownloadLocation`,`kubectl` 则会被安装到用户的 `temp` 目录下。 + {{< /note >}} + + + 安装程序创建 `$HOME/.kube`,并指示其创建配置文件。 + + +1. 测试一下,确保你安装的是最新版本: + + ```powershell + kubectl version --client + ``` + + +{{< note >}} +更新安装是通过重新运行步骤 1 中的两个命令而实现。 +{{< /note >}} + + +### 在 Windows 上用 Chocolatey 或 Scoop 安装 {#install-on-windows-using-chocolatey-or-scoop} + + +1. 要在 Windows 上安装 kubectl,你可以使用包管理器 [Chocolatey](https://chocolatey.org) + 或是命令行安装器 [Scoop](https://scoop.sh)。 + + {{< tabs name="kubectl_win_install" >}} + {{% tab name="choco" %}} + ```powershell + choco install kubernetes-cli + ``` + {{% /tab %}} + {{% tab name="scoop" %}} + ```powershell + scoop install kubectl + ``` + {{% /tab %}} + {{< /tabs >}} + + +2. 测试一下,确保安装的是最新版本: + + ```powershell + kubectl version --client + ``` + + +3. 导航到你的 home 目录: + + + ```powershell + # 当你用 cmd.exe 时,则运行: cd %USERPROFILE% + cd ~ + ``` + + +4. 创建目录 `.kube`: + + ```powershell + mkdir .kube + ``` + + +5. 切换到新创建的目录 `.kube` + + ```powershell + cd .kube + ``` + + +6. 配置 kubectl,以接入远程的 Kubernetes 集群: + + ```powershell + New-Item config -type file + ``` + + +{{< note >}} +编辑配置文件,你需要先选择一个文本编辑器,比如 Notepad。 +{{< /note >}} + + +### 作为谷歌云 SDK 的一部分,在 Windows 上安装 {#install-on-windows-as-part-of-the-google-cloud-sdk} + +{{< include "included/install-kubectl-gcloud.md" >}} + + +## 验证 kubectl 配置 {#verify-kubectl-configration} + +{{< include "included/verify-kubectl.md" >}} + + +## kubectl 可选配置 {#optional-kubectl-configurations} + +### 启用 shell 自动补全功能 {#enable-shell-autocompletion} + + +kubectl 为 Bash 和 Zsh 提供自动补全功能,可以减轻许多输入的负担。 + +下面是设置 Zsh 自动补全功能的操作步骤,前提是你在 Windows 上面运行的是 Zsh。 + +{{< include "included/optional-kubectl-configs-zsh.md" >}} + +## {{% heading "whatsnext" %}} + +{{< include "included/kubectl-whats-next.md" >}} \ No newline at end of file