Merge pull request #27322 from zhiguo-lu/zh-trans-task-install-kubectl-on-linux

[zh] translate task/Install and Set Up kubectl on Linux
This commit is contained in:
Kubernetes Prow Robot 2021-04-02 05:18:12 -07:00 committed by GitHub
commit c45c5746b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 371 additions and 0 deletions

View File

@ -0,0 +1,109 @@
---
title: "Linux 系统中的 bash 自动补全功能"
description: "Linux 系统中 bash 自动补全功能的一些可选配置。"
headless: true
---
<!--
---
title: "bash auto-completion on Linux"
description: "Some optional configuration for bash auto-completion on Linux."
headless: true
---
-->
<!--
### Introduction
-->
### 简介 {#introduction}
<!--
The kubectl completion script for Bash can be generated with the command `kubectl completion bash`. Sourcing the completion script in your shell enables kubectl autocompletion.
However, the completion script depends on [**bash-completion**](https://github.com/scop/bash-completion), which means that you have to install this software first (you can test if you have bash-completion already installed by running `type _init_completion`).
-->
kubectl 的 Bash 补全脚本可以用命令 `kubectl completion bash` 生成。
在 shell 中导入Sourcing补全脚本将启用 kubectl 自动补全功能。
然而,补全脚本依赖于工具 [**bash-completion**](https://github.com/scop/bash-completion)
所以要先安装它(可以用命令 `type _init_completion` 检查 bash-completion 是否已安装)。
<!--
### Install bash-completion
-->
### 安装 bash-completion {#install-bash-comletion}
<!--
bash-completion is provided by many package managers (see [here](https://github.com/scop/bash-completion#installation)). You can install it with `apt-get install bash-completion` or `yum install bash-completion`, etc.
The above commands create `/usr/share/bash-completion/bash_completion`, which is the main script of bash-completion. Depending on your package manager, you have to manually source this file in your `~/.bashrc` file.
To find out, reload your shell and run `type _init_completion`. If the command succeeds, you're already set, otherwise add the following to your `~/.bashrc` file:
-->
很多包管理工具均支持 bash-completion参见[这里](https://github.com/scop/bash-completion#installation))。
可以通过 `apt-get install bash-completion``yum install bash-completion` 等命令来安装它。
上述命令将创建文件 `/usr/share/bash-completion/bash_completion`,它是 bash-completion 的主脚本。
依据包管理工具的实际情况,你需要在 `~/.bashrc` 文件中手工导入此文件。
要查看结果,请重新加载你的 shell并运行命令 `type _init_completion`
如果命令执行成功,则设置完成,否则将下面内容添加到文件 `~/.bashrc` 中:
```bash
source /usr/share/bash-completion/bash_completion
```
<!--
Reload your shell and verify that bash-completion is correctly installed by typing `type _init_completion`.
-->
重新加载 shell再输入命令 `type _init_completion` 来验证 bash-completion 的安装状态。
<!--
### Enable kubectl autocompletion
-->
### 启动 kubectl 自动补全功能 {#enable-kubectl-autocompletion}
<!--
You now need to ensure that the kubectl completion script gets sourced in all your shell sessions. There are two ways in which you can do this:
-->
你现在需要确保一点kubectl 补全脚本已经导入sourced到 shell 会话中。
这里有两种验证方法:
<!--
- Source the completion script in your `~/.bashrc` file:
-->
- 在文件 `~/.bashrc` 中导入source补全脚本
```bash
echo 'source <(kubectl completion bash)' >>~/.bashrc
```
<!--
- Add the completion script to the `/etc/bash_completion.d` directory:
-->
- 将补全脚本添加到目录 `/etc/bash_completion.d` 中:
```bash
kubectl completion bash >/etc/bash_completion.d/kubectl
```
<!--
If you have an alias for kubectl, you can extend shell completion to work with that alias:
-->
如果 kubectl 有关联的别名,你可以扩展 shell 补全来适配此别名:
```bash
echo 'alias k=kubectl' >>~/.bashrc
echo 'complete -F __start_kubectl k' >>~/.bashrc
```
{{< note >}}
<!--
bash-completion sources all completion scripts in `/etc/bash_completion.d`.
-->
bash-completion 负责导入 `/etc/bash_completion.d` 目录中的所有补全脚本。
{{< /note >}}
<!--
Both approaches are equivalent. After reloading your shell, kubectl autocompletion should be working.
-->
两种方式的效果相同。重新加载 shell 后kubectl 自动补全功能即可生效。

View File

@ -0,0 +1,262 @@
---
title: 在 Linux 系统中安装并设置 kubectl
content_type: task
weight: 10
card:
name: tasks
weight: 20
title: 在 Linux 系统中安装 kubectl
---
<!--
---
reviewers:
- mikedanese
title: Install and Set Up kubectl on Linux
content_type: task
weight: 10
card:
name: tasks
weight: 20
title: Install kubectl on Linux
---
-->
## {{% heading "prerequisites" %}}
<!--
You must use a kubectl version that is within one minor version difference of your cluster.
For example, a v1.2 client should work with v1.1, v1.2, and v1.3 master.
Using the latest version of kubectl helps avoid unforeseen issues.
-->
kubectl 版本和集群版本之间的差异必须在一个小版本号内。
例如v1.2 版本的客户端只能与 v1.1、v1.2 和 v1.3 版本的集群一起工作。
用最新版的 kubectl 有助于避免不可预见的问题。
<!--
## Install kubectl on Linux
-->
## 在 Linux 系统中安装 kubectl {#install-kubectl-on-linux}
<!--
The following methods exist for installing kubectl on Linux:
-->
在 Linux 系统中安装 kubectl 有如下几种方法:
- [{{% heading "prerequisites" %}}](#{{% heading "prerequisites" %}})
- [在 Linux 系统中安装 kubectl](#install-kubectl-on-linux)
- [用 curl 在 Linux 系统中安装 kubectl](#install-kubectl-binary-with-curl-on-linux)
- [用原生包管理工具安装](#install-using-native-package-management)
- [用其他包管理工具安装](#install-using-other-package-management)
- [作为谷歌云 SDK 的一部分,在 Linux 中安装](#install-on-linux-as-part-of-the-google-cloud-sdk)
- [验证 kubectl 配置](#verify-kubectl-configration)
- [可选的 kubectl 配置](#optional-kubectl-configurations)
- [启用 shell 自动补全功能](#enable-shell-autocompletion)
- [{{% heading "whatsnext" %}}](#{{% heading "whatsnext" %}})
<!--
### Install kubectl binary with curl on Linux
-->
### 用 curl 在 Linux 系统中安装 kubectl {#install-kubectl-binary-with-curl-on-linux}
<!--
1. Download the latest release with the command:
-->
1. 用以下命令下载最新发行版:
```bash
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
```
{{< note >}}
<!--
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 {{< param "fullversion" >}} on Linux, type:
-->
如需下载某个指定的版本,请用指定版本号替换该命令的这一部分: `$(curl -L -s https://dl.k8s.io/release/stable.txt)`
例如,要在 Linux 中下载 {{< param "fullversion" >}} 版本,请输入:
```bash
curl -LO https://dl.k8s.io/release/{{< param "fullversion" >}}/bin/linux/amd64/kubectl
```
{{< /note >}}
<!--
1. Validate the binary (optional)
Download the kubectl checksum file:
-->
1. 验证该可执行文件(可选步骤)
下载 kubectl 校验和文件:
```bash
curl -LO "https://dl.k8s.io/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl.sha256"
```
<!--
Validate the kubectl binary against the checksum file:
-->
基于校验和文件,验证 kubectl 的可执行文件:
```bash
echo "$(<kubectl.sha256) kubectl" | sha256sum --check
```
<!--
If valid, the output is:
-->
验证通过时,输出为:
```console
kubectl: OK
```
<!--
If the check fails, `sha256` exits with nonzero status and prints output similar to:
-->
验证失败时,`sha256` 将以非零值退出,并打印如下输出:
```bash
kubectl: FAILED
sha256sum: WARNING: 1 computed checksum did NOT match
```
{{< note >}}
<!--
Download the same version of the binary and checksum.
-->
下载的 kubectl 与校验和文件版本必须相同。
{{< /note >}}
<!--
1. Install kubectl
-->
1. 安装 kubectl
```bash
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
```
{{< note >}}
<!--
If you do not have root access on the target system, you can still install kubectl to the `~/.local/bin` directory:
-->
即使你没有目标系统的 root 权限,仍然可以将 kubectl 安装到目录 `~/.local/bin` 中:
```bash
mkdir -p ~/.local/bin/kubectl
mv ./kubectl ~/.local/bin/kubectl
# and then add ~/.local/bin/kubectl to $PATH
```
{{< /note >}}
<!--
1. Test to ensure the version you installed is up-to-date:
-->
1. 执行测试,以保障你安装的版本是最新的:
```bash
kubectl version --client
```
<!--
### Install using native package management
-->
### 用原生包管理工具安装 {#install-using-native-package-management}
{{< tabs name="kubectl_install" >}}
{{< tab name="Ubuntu、Debian 或 HypriotOS" codelang="bash" >}}
sudo apt-get update && sudo apt-get install -y apt-transport-https gnupg2 curl
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee -a /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update
sudo apt-get install -y kubectl
{{< /tab >}}
{{< tab name="CentOS、RHEL 或 Fedora" codelang="bash" >}}cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF
yum install -y kubectl
{{< /tab >}}
{{< /tabs >}}
<!--
### Install using other package management
-->
### 用其他包管理工具安装 {#install-using-other-package-management}
{{< tabs name="other_kubectl_install" >}}
{{% tab name="Snap" %}}
<!--
If you are on Ubuntu or another Linux distribution that support [snap](https://snapcraft.io/docs/core/install) package manager, kubectl is available as a [snap](https://snapcraft.io/) application.
-->
如果你使用的 Ubuntu 或其他 Linux 发行版,内建支持 [snap](https://snapcraft.io/docs/core/install) 包管理工具,
则可用 [snap](https://snapcraft.io/) 命令安装 kubectl。
```shell
snap install kubectl --classic
kubectl version --client
```
{{% /tab %}}
{{% tab name="Homebrew" %}}
<!--
If you are on Linux and using [Homebrew](https://docs.brew.sh/Homebrew-on-Linux) package manager, kubectl is available for [installation](https://docs.brew.sh/Homebrew-on-Linux#install).
-->
如果你使用 Linux 系统,并且装了 [Homebrew](https://docs.brew.sh/Homebrew-on-Linux) 包管理工具,
则可以使用这种方式[安装](https://docs.brew.sh/Homebrew-on-Linux#install) kubectl。
```shell
brew install kubectl
kubectl version --client
```
{{% /tab %}}
{{< /tabs >}}
<!--
### Install on Linux as part of the Google Cloud SDK
-->
### 作为谷歌云 SDK 的一部分,在 Linux 上安装 {#install-on-linux-as-part-of-the-google-cloud-sdk}
{{< include "included/install-kubectl-gcloud.md" >}}
<!--
## Verify kubectl configuration
-->
## 验证 kubectl 配置 {#verify-kubectl-configration}
{{< include "included/verify-kubectl.md" >}}
## kubectl 的可选配置 {#optional-kubectl-configurations}
### 启用 shell 自动补全功能 {#enable-shell-autocompletion}
<!--
kubectl provides autocompletion support for Bash and Zsh, which can save you a lot of typing.
Below are the procedures to set up autocompletion for Bash and Zsh.
-->
kubectl 为 Bash 和 Zsh 提供自动补全功能,可以减轻许多输入的负担。
下面是为 Bash 和 Zsh 设置自动补全功能的操作步骤。
{{< tabs name="kubectl_autocompletion" >}}
{{< tab name="Bash" include="included/optional-kubectl-configs-bash-linux.md" />}}
{{< tab name="Zsh" include="included/optional-kubectl-configs-zsh.md" />}}
{{< /tabs >}}
## {{% heading "whatsnext" %}}
{{< include "included/kubectl-whats-next.md" >}}