From 22f104c6c329dca97901ec1b453da0cba6147504 Mon Sep 17 00:00:00 2001 From: luzg Date: Wed, 10 Mar 2021 11:14:58 +0800 Subject: [PATCH] [zh] translate task/install kubectl on macOS --- .../tools/included/install-kubectl-gcloud.md | 37 +++ .../tools/included/kubectl-whats-next.md | 27 ++ .../optional-kubectl-configs-bash-mac.md | 173 ++++++++++++ .../included/optional-kubectl-configs-zsh.md | 50 ++++ .../tasks/tools/included/verify-kubectl.md | 62 +++++ .../docs/tasks/tools/install-kubectl-macos.md | 263 ++++++++++++++++++ 6 files changed, 612 insertions(+) create mode 100644 content/zh/docs/tasks/tools/included/install-kubectl-gcloud.md create mode 100644 content/zh/docs/tasks/tools/included/kubectl-whats-next.md create mode 100644 content/zh/docs/tasks/tools/included/optional-kubectl-configs-bash-mac.md create mode 100644 content/zh/docs/tasks/tools/included/optional-kubectl-configs-zsh.md create mode 100644 content/zh/docs/tasks/tools/included/verify-kubectl.md create mode 100644 content/zh/docs/tasks/tools/install-kubectl-macos.md diff --git a/content/zh/docs/tasks/tools/included/install-kubectl-gcloud.md b/content/zh/docs/tasks/tools/included/install-kubectl-gcloud.md new file mode 100644 index 0000000000..e73932851d --- /dev/null +++ b/content/zh/docs/tasks/tools/included/install-kubectl-gcloud.md @@ -0,0 +1,37 @@ +--- +title: "通过 gcloud 安装 kubectl" +description: "用各个特定操作系统标签页中包含的 gcloud 指令片段安装 kubectl。" +headless: true +--- + + + +kubectl 可以作为 Google Cloud SDK 的一部分被安装。 + + +1. 安装 [Google Cloud SDK](https://cloud.google.com/sdk/)。 +1. 运行安装 `kubectl` 的命令: + + ```shell + gcloud components install kubectl + ``` + + +1. 验证一下,确保安装的是最新的版本: + + ```shell + kubectl version --client + ``` \ No newline at end of file diff --git a/content/zh/docs/tasks/tools/included/kubectl-whats-next.md b/content/zh/docs/tasks/tools/included/kubectl-whats-next.md new file mode 100644 index 0000000000..3990922e0e --- /dev/null +++ b/content/zh/docs/tasks/tools/included/kubectl-whats-next.md @@ -0,0 +1,27 @@ +--- +title: "后续内容" +description: "安装 kubectl 之后,还可以做些什么?" +headless: true +--- + + + +* [安装 Minikube](https://minikube.sigs.k8s.io/docs/start/) +* 有关创建集群的更多信息,请参阅[入门指南](/zh/docs/setup/). +* [学习如何启动并对外公开你的应用程序。](/zh/docs/tasks/access-application-cluster/service-access-application-cluster/) +* 如果你需要访问其他人创建的集群,请参阅 + [共享集群接入文档](/zh/docs/tasks/access-application-cluster/configure-access-multiple-clusters/). +* 阅读 [kubectl 参考文档](/zh/docs/reference/kubectl/kubectl/) diff --git a/content/zh/docs/tasks/tools/included/optional-kubectl-configs-bash-mac.md b/content/zh/docs/tasks/tools/included/optional-kubectl-configs-bash-mac.md new file mode 100644 index 0000000000..d92055f6a3 --- /dev/null +++ b/content/zh/docs/tasks/tools/included/optional-kubectl-configs-bash-mac.md @@ -0,0 +1,173 @@ +--- +title: "macOS 系统上的 bash 自动补全" +description: "在 macOS 上实现 Bash 自动补全的一些可选配置。" +headless: true +--- + + + +### 简介 + + +kubectl 的 Bash 补全脚本可以通过 `kubectl completion bash` 命令生成。 +在你的 shell 中导入(Sourcing)这个脚本即可启用补全功能。 + +此外,kubectl 补全脚本依赖于工具 [**bash-completion**](https://github.com/scop/bash-completion), +所以你必须先安装它。 + +{{< warning>}} + +bash-completion 有两个版本:v1 和 v2。v1 对应 Bash3.2(也是 macOS 的默认安装版本),v2 对应 Bash 4.1+。 +kubectl 的补全脚本**无法适配** bash-completion v1 和 Bash 3.2。 +必须为它配备 **bash-completion v2** 和 **Bash 4.1+**。 +有鉴于此,为了在 macOS 上使用 kubectl 补全功能,你必须要安装和使用 Bash 4.1+ +([*说明*](https://itnext.io/upgrading-bash-on-macos-7138bd1066ba))。 +后续说明假定你用的是 Bash 4.1+(也就是 Bash 4.1 或更新的版本) +{{< /warning >}} + + +### 升级 Bash + + +后续说明假定你已使用 Bash 4.1+。你可以运行以下命令检查 Bash 版本: + +```bash +echo $BASH_VERSION +``` + + +如果版本太旧,可以用 Homebrew 安装/升级: + +```bash +brew install bash +``` + + +重新加载 shell,并验证所需的版本已经生效: + +```bash +echo $BASH_VERSION $SHELL +``` + + +Homebrew 通常把它安装为 `/usr/local/bin/bash`。 + + +### 安装 bash-completion + + +{{< note >}} + +如前所述,本说明假定你使用的 Bash 版本为 4.1+,这意味着你要安装 bash-completion v2 +(不同于 Bash 3.2 和 bash-completion v1,kubectl 的补全功能在该场景下无法工作)。 +{{< /note >}} + + +你可以用命令 `type _init_completion` 测试 bash-completion v2 是否已经安装。 +如未安装,用 Homebrew 来安装它: + +```bash +brew install bash-completion@2 +``` + + +如命令的输出信息所显示的,将如下内容添加到文件 `~/.bash_profile` 中: + +```bash +export BASH_COMPLETION_COMPAT_DIR="/usr/local/etc/bash_completion.d" +[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh" +``` + + +重新加载 shell,并用命令 `type _init_completion` 验证 bash-completion v2 已经恰当的安装。 + + +### 启用 kubectl 自动补全功能 + + +你现在需要确保在所有的 shell 环境中均已导入(sourced) kubectl 的补全脚本, +有若干种方法可以实现这一点: + +- 在文件 `~/.bash_profile` 中导入(Source)补全脚本: + + ```bash + echo 'source <(kubectl completion bash)' >>~/.bash_profile + ``` + + +- 将补全脚本添加到目录 `/usr/local/etc/bash_completion.d` 中: + + ```bash + kubectl completion bash >/usr/local/etc/bash_completion.d/kubectl + ``` + + +- 如果你为 kubectl 定义了别名,则可以扩展 shell 补全来兼容该别名: + + ```bash + echo 'alias k=kubectl' >>~/.bash_profile + echo 'complete -F __start_kubectl k' >>~/.bash_profile + ``` + + +- 如果你是用 Homebrew 安装的 kubectl([如上所述](#install-with-homebrew-on-macos)), + 那么 kubectl 补全脚本应该已经安装到目录 `/usr/local/etc/bash_completion.d/kubectl` 中了。 + 这种情况下,你什么都不需要做。 + + {{< note >}} + + 用 Hommbrew 安装的 bash-completion v2 会初始化 目录 `BASH_COMPLETION_COMPAT_DIR` 中的所有文件,这就是后两种方法能正常工作的原因。 + {{< /note >}} + + +总之,重新加载 shell 之后,kubectl 补全功能将立即生效。 diff --git a/content/zh/docs/tasks/tools/included/optional-kubectl-configs-zsh.md b/content/zh/docs/tasks/tools/included/optional-kubectl-configs-zsh.md new file mode 100644 index 0000000000..0f32593d55 --- /dev/null +++ b/content/zh/docs/tasks/tools/included/optional-kubectl-configs-zsh.md @@ -0,0 +1,50 @@ +--- +title: "zsh 自动补全" +description: "zsh 自动补全的一些可选配置" +headless: true +--- + + + +kubectl 通过命令 `kubectl completion zsh` 生成 Zsh 自动补全脚本。 +在 shell 中导入(Sourcing)该自动补全脚本,将启动 kubectl 自动补全功能。 + +为了在所有的 shell 会话中实现此功能,请将下面内容加入到文件 `~/.zshrc` 中。 + +```zsh +source <(kubectl completion zsh) +``` + + +如果你为 kubectl 定义了别名,可以扩展脚本补全,以兼容该别名。 + +```zsh +echo 'alias k=kubectl' >>~/.zshrc +echo 'complete -F __start_kubectl k' >>~/.zshrc +``` + + +重新加载 shell 后,kubectl 自动补全功能将立即生效。 + +如果你收到 `complete:13: command not found: compdef` 这样的错误提示,那请将下面内容添加到 `~/.zshrc` 文件的开头: + +```zsh +autoload -Uz compinit +compinit +``` diff --git a/content/zh/docs/tasks/tools/included/verify-kubectl.md b/content/zh/docs/tasks/tools/included/verify-kubectl.md new file mode 100644 index 0000000000..a587e26cd0 --- /dev/null +++ b/content/zh/docs/tasks/tools/included/verify-kubectl.md @@ -0,0 +1,62 @@ +--- +title: "验证 kubectl 的安装效果" +description: "如何验证 kubectl。" +headless: true +--- + + + +为了让 kubectl 能发现并访问 Kubernetes 集群,你需要一个 +[kubeconfig 文件](/docs/zh/concepts/configuration/organize-cluster-access-kubeconfig/), +该文件在 +[kube-up.sh](https://github.com/kubernetes/kubernetes/blob/master/cluster/kube-up.sh) +创建集群时,或成功部署一个 Miniube 集群时,均会自动生成。 +通常,kubectl 的配置信息存放于文件 `~/.kube/config` 中。 + +通过获取集群状态的方法,检查是否已恰当的配置了 kubectl: + +```shell +kubectl cluster-info +``` + + +如果返回一个 URL,则意味着 kubectl 成功的访问到了你的集群。 + +如果你看到如下所示的消息,则代表 kubectl 配置出了问题,或无法连接到 Kubernetes 集群。 + +``` +The connection to the server was refused - did you specify the right host or port? +(访问 被拒绝 - 你指定的主机和端口是否有误?) +``` + + +例如,如果你想在自己的笔记本上(本地)运行 Kubernetes 集群,你需要先安装一个 Minikube 这样的工具,然后再重新运行上面的命令。 + +如果命令 `kubectl cluster-info` 返回了 url,但你还不能访问集群,那可以用以下命令来检查配置是否妥当: + +```shell +kubectl cluster-info dump +``` \ No newline at end of file diff --git a/content/zh/docs/tasks/tools/install-kubectl-macos.md b/content/zh/docs/tasks/tools/install-kubectl-macos.md new file mode 100644 index 0000000000..bd5e670e86 --- /dev/null +++ b/content/zh/docs/tasks/tools/install-kubectl-macos.md @@ -0,0 +1,263 @@ +--- +title: 在 macOS 系统上安装和设置 kubectl +content_type: task +weight: 10 +card: + name: tasks + weight: 20 + title: 在 macOS 系统上安装 kubectl +--- + + +## {{% heading "prerequisites" %}} + + +kubectl 版本和集群之间的差异必须在一个小版本号之内。 +例如:v1.2 版本的客户端只能与 v1.1、v1.2 和 v1.3 版本的集群一起工作。 +用最新版本的 kubectl 有助于避免不可预见的问题。 + + +## 在 macOS 系统上安装 kubectl {#install-kubectl-on-macos} + + +在 macOS 系统上安装 kubectl 有如下方法: + +- [{{% heading "prerequisites" %}}](#{{% heading "prerequisites" %}}) +- [在 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) + - [作为谷歌云 SDK 的一部分,在 macOS 上安装](#install-on-macos-as-part-of-the-google-cloud-sdk) +- [验证 kubectl 配置](#verify-kubectl-configuration) +- [可选的 kubectl 配置](#optional-kubectl-configurations) + - [启用 shell 自动补全功能](#enable-shell-autocompletion) +- [{{% heading "whatsnext" %}}](#{{% heading "whatsnext" %}}) + + +### 用 curl 在 macOS 系统上安装 kubectl {#install-kubectl-binary-with-curl-on-macos} + + +1. 下载最新的发行版: + + ```bash + curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/darwin/amd64/kubectl" + ``` + + {{< note >}} + + 如果需要下载某个指定的版本,用该指定版本号替换掉命令的这个部分:`$(curl -L -s https://dl.k8s.io/release/stable.txt)`。 + 例如:要在 macOS 系统中下载 {{< param "fullversion" >}} 版本,则输入: + + ```bash + curl -LO https://dl.k8s.io/release/{{< param "fullversion" >}}/bin/darwin/amd64/kubectl + ``` + + {{< /note >}} + + +1. 验证可执行文件(可选操作) + + 下载 kubectl 的校验和文件: + + ```bash + curl -LO "https://dl.k8s.io/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/darwin/amd64/kubectl.sha256" + ``` + + + 根据校验和文件,验证 kubectl: + + ```bash + echo "$( + 验证通过时,输出如下: + + ```console + kubectl: OK + ``` + + + 验证失败时,`shasum` 将以非零值退出,并打印如下输出: + + ``` + kubectl: FAILED + shasum: WARNING: 1 computed checksum did NOT match + ``` + + {{< note >}} + + 下载的 kubectl 与校验和文件版本要相同。 + {{< /note >}} + + +1. 将 kubectl 置为可执行文件: + + ```bash + chmod +x ./kubectl + ``` + + +1. 将可执行文件 kubectl 移动到系统可寻址路径 `PATH` 内的一个位置: + + ```bash + sudo mv ./kubectl /usr/local/bin/kubectl + sudo chown root: /usr/local/bin/kubectl + ``` + + +1. 测试一下,确保你安装的是最新的版本: + + ```bash + kubectl version --client + ``` + + +### 用 Homebrew 在 macOS 系统上安装 {#install-with-homebrew-on-macos} + + +如果你是 macOS 系统,且用的是 [Homebrew](https://brew.sh/) 包管理工具, +则可以用 Homebrew 安装 kubectl。 + + +1. 运行安装命令: + + ```bash + brew install kubectl + ``` + + 或 + + ```bash + brew install kubernetes-cli + ``` + + +1. 测试一下,确保你安装的是最新的版本: + + ```bash + kubectl version --client + ``` + + +### 用 Macports 在 macOS 上安装 {#install-with-macports-on-macos} + + +如果你用的是 macOS,且用 [Macports](https://macports.org/) 包管理工具,则你可以用 Macports 安装kubectl。 + + +1. 运行安装命令: + + ```bash + sudo port selfupdate + sudo port install kubectl + ``` + + +1. 测试一下,确保你安装的是最新的版本: + + ```bash + kubectl version --client + ``` + + +### 作为谷歌云 SDK 的一部分,在 macOS 上安装 {#install-on-macos-as-part-of-the-google-cloud-sdk} + +{{< include "included/install-kubectl-gcloud.md" >}} + + +## 验证 kubectl 配置 {#verify-kubectl-configuration} + +{{< include "included/verify-kubectl.md" >}} + + +## 可选的 kubectl 配置 {#optional-kubectl-configurations} + +### 启用 shell 自动补全功能 {#enable-shell-autocompletion} + + +kubectl 为 Bash 和 Zsh 提供自动补全功能,这可以节省许多输入的麻烦。 + +下面是为 Bash 和 Zsh 设置自动补全功能的操作步骤。 + +{{< tabs name="kubectl_autocompletion" >}} +{{< tab name="Bash" include="included/optional-kubectl-configs-bash-mac.md" />}} +{{< tab name="Zsh" include="included/optional-kubectl-configs-zsh.md" />}} +{{< /tabs >}} + +## {{% heading "whatsnext" %}} + +{{< include "included/kubectl-whats-next.md" >}} \ No newline at end of file