Skip to content

Commit c45c574

Browse files
authored
Merge pull request #27322 from zhiguo-lu/zh-trans-task-install-kubectl-on-linux
[zh] translate task/Install and Set Up kubectl on Linux
2 parents a806f40 + d439f4d commit c45c574

File tree

2 files changed

+371
-0
lines changed

2 files changed

+371
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
---
2+
title: "Linux 系统中的 bash 自动补全功能"
3+
description: "Linux 系统中 bash 自动补全功能的一些可选配置。"
4+
headless: true
5+
---
6+
<!--
7+
---
8+
title: "bash auto-completion on Linux"
9+
description: "Some optional configuration for bash auto-completion on Linux."
10+
headless: true
11+
---
12+
-->
13+
14+
<!--
15+
### Introduction
16+
-->
17+
### 简介 {#introduction}
18+
19+
<!--
20+
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.
21+
22+
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`).
23+
-->
24+
kubectl 的 Bash 补全脚本可以用命令 `kubectl completion bash` 生成。
25+
在 shell 中导入(Sourcing)补全脚本,将启用 kubectl 自动补全功能。
26+
27+
然而,补全脚本依赖于工具 [**bash-completion**](https://github.com/scop/bash-completion)
28+
所以要先安装它(可以用命令 `type _init_completion` 检查 bash-completion 是否已安装)。
29+
30+
<!--
31+
### Install bash-completion
32+
-->
33+
### 安装 bash-completion {#install-bash-comletion}
34+
35+
<!--
36+
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.
37+
38+
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.
39+
40+
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:
41+
-->
42+
很多包管理工具均支持 bash-completion(参见[这里](https://github.com/scop/bash-completion#installation))。
43+
可以通过 `apt-get install bash-completion``yum install bash-completion` 等命令来安装它。
44+
45+
上述命令将创建文件 `/usr/share/bash-completion/bash_completion`,它是 bash-completion 的主脚本。
46+
依据包管理工具的实际情况,你需要在 `~/.bashrc` 文件中手工导入此文件。
47+
48+
要查看结果,请重新加载你的 shell,并运行命令 `type _init_completion`
49+
如果命令执行成功,则设置完成,否则将下面内容添加到文件 `~/.bashrc` 中:
50+
51+
```bash
52+
source /usr/share/bash-completion/bash_completion
53+
```
54+
55+
<!--
56+
Reload your shell and verify that bash-completion is correctly installed by typing `type _init_completion`.
57+
-->
58+
重新加载 shell,再输入命令 `type _init_completion` 来验证 bash-completion 的安装状态。
59+
60+
<!--
61+
### Enable kubectl autocompletion
62+
-->
63+
### 启动 kubectl 自动补全功能 {#enable-kubectl-autocompletion}
64+
65+
<!--
66+
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:
67+
-->
68+
你现在需要确保一点:kubectl 补全脚本已经导入(sourced)到 shell 会话中。
69+
这里有两种验证方法:
70+
71+
<!--
72+
- Source the completion script in your `~/.bashrc` file:
73+
-->
74+
- 在文件 `~/.bashrc` 中导入(source)补全脚本:
75+
76+
```bash
77+
echo 'source <(kubectl completion bash)' >>~/.bashrc
78+
```
79+
80+
<!--
81+
- Add the completion script to the `/etc/bash_completion.d` directory:
82+
-->
83+
- 将补全脚本添加到目录 `/etc/bash_completion.d` 中:
84+
85+
```bash
86+
kubectl completion bash >/etc/bash_completion.d/kubectl
87+
```
88+
89+
<!--
90+
If you have an alias for kubectl, you can extend shell completion to work with that alias:
91+
-->
92+
如果 kubectl 有关联的别名,你可以扩展 shell 补全来适配此别名:
93+
94+
```bash
95+
echo 'alias k=kubectl' >>~/.bashrc
96+
echo 'complete -F __start_kubectl k' >>~/.bashrc
97+
```
98+
99+
{{< note >}}
100+
<!--
101+
bash-completion sources all completion scripts in `/etc/bash_completion.d`.
102+
-->
103+
bash-completion 负责导入 `/etc/bash_completion.d` 目录中的所有补全脚本。
104+
{{< /note >}}
105+
106+
<!--
107+
Both approaches are equivalent. After reloading your shell, kubectl autocompletion should be working.
108+
-->
109+
两种方式的效果相同。重新加载 shell 后,kubectl 自动补全功能即可生效。
Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
---
2+
title: 在 Linux 系统中安装并设置 kubectl
3+
content_type: task
4+
weight: 10
5+
card:
6+
name: tasks
7+
weight: 20
8+
title: 在 Linux 系统中安装 kubectl
9+
---
10+
<!--
11+
---
12+
reviewers:
13+
- mikedanese
14+
title: Install and Set Up kubectl on Linux
15+
content_type: task
16+
weight: 10
17+
card:
18+
name: tasks
19+
weight: 20
20+
title: Install kubectl on Linux
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 Linux
37+
-->
38+
## 在 Linux 系统中安装 kubectl {#install-kubectl-on-linux}
39+
40+
<!--
41+
The following methods exist for installing kubectl on Linux:
42+
-->
43+
在 Linux 系统中安装 kubectl 有如下几种方法:
44+
45+
- [{{% heading "prerequisites" %}}](#{{% heading "prerequisites" %}})
46+
- [在 Linux 系统中安装 kubectl](#install-kubectl-on-linux)
47+
- [用 curl 在 Linux 系统中安装 kubectl](#install-kubectl-binary-with-curl-on-linux)
48+
- [用原生包管理工具安装](#install-using-native-package-management)
49+
- [用其他包管理工具安装](#install-using-other-package-management)
50+
- [作为谷歌云 SDK 的一部分,在 Linux 中安装](#install-on-linux-as-part-of-the-google-cloud-sdk)
51+
- [验证 kubectl 配置](#verify-kubectl-configration)
52+
- [可选的 kubectl 配置](#optional-kubectl-configurations)
53+
- [启用 shell 自动补全功能](#enable-shell-autocompletion)
54+
- [{{% heading "whatsnext" %}}](#{{% heading "whatsnext" %}})
55+
56+
<!--
57+
### Install kubectl binary with curl on Linux
58+
-->
59+
### 用 curl 在 Linux 系统中安装 kubectl {#install-kubectl-binary-with-curl-on-linux}
60+
61+
<!--
62+
1. Download the latest release with the command:
63+
-->
64+
1. 用以下命令下载最新发行版:
65+
66+
```bash
67+
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
68+
```
69+
70+
{{< note >}}
71+
<!--
72+
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.
73+
74+
For example, to download version {{< param "fullversion" >}} on Linux, type:
75+
-->
76+
如需下载某个指定的版本,请用指定版本号替换该命令的这一部分: `$(curl -L -s https://dl.k8s.io/release/stable.txt)`
77+
78+
例如,要在 Linux 中下载 {{< param "fullversion" >}} 版本,请输入:
79+
80+
```bash
81+
curl -LO https://dl.k8s.io/release/{{< param "fullversion" >}}/bin/linux/amd64/kubectl
82+
```
83+
{{< /note >}}
84+
85+
<!--
86+
1. Validate the binary (optional)
87+
88+
Download the kubectl checksum file:
89+
-->
90+
1. 验证该可执行文件(可选步骤)
91+
92+
下载 kubectl 校验和文件:
93+
94+
```bash
95+
curl -LO "https://dl.k8s.io/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl.sha256"
96+
```
97+
98+
<!--
99+
Validate the kubectl binary against the checksum file:
100+
-->
101+
基于校验和文件,验证 kubectl 的可执行文件:
102+
103+
```bash
104+
echo "$(<kubectl.sha256) kubectl" | sha256sum --check
105+
```
106+
107+
<!--
108+
If valid, the output is:
109+
-->
110+
验证通过时,输出为:
111+
112+
```console
113+
kubectl: OK
114+
```
115+
116+
<!--
117+
If the check fails, `sha256` exits with nonzero status and prints output similar to:
118+
-->
119+
验证失败时,`sha256` 将以非零值退出,并打印如下输出:
120+
121+
```bash
122+
kubectl: FAILED
123+
sha256sum: WARNING: 1 computed checksum did NOT match
124+
```
125+
126+
{{< note >}}
127+
<!--
128+
Download the same version of the binary and checksum.
129+
-->
130+
下载的 kubectl 与校验和文件版本必须相同。
131+
{{< /note >}}
132+
133+
<!--
134+
1. Install kubectl
135+
-->
136+
1. 安装 kubectl
137+
138+
```bash
139+
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
140+
```
141+
142+
{{< note >}}
143+
<!--
144+
If you do not have root access on the target system, you can still install kubectl to the `~/.local/bin` directory:
145+
-->
146+
即使你没有目标系统的 root 权限,仍然可以将 kubectl 安装到目录 `~/.local/bin` 中:
147+
148+
```bash
149+
mkdir -p ~/.local/bin/kubectl
150+
mv ./kubectl ~/.local/bin/kubectl
151+
# and then add ~/.local/bin/kubectl to $PATH
152+
```
153+
154+
{{< /note >}}
155+
156+
<!--
157+
1. Test to ensure the version you installed is up-to-date:
158+
-->
159+
1. 执行测试,以保障你安装的版本是最新的:
160+
161+
```bash
162+
kubectl version --client
163+
```
164+
165+
<!--
166+
### Install using native package management
167+
-->
168+
### 用原生包管理工具安装 {#install-using-native-package-management}
169+
170+
{{< tabs name="kubectl_install" >}}
171+
{{< tab name="Ubuntu、Debian 或 HypriotOS" codelang="bash" >}}
172+
sudo apt-get update && sudo apt-get install -y apt-transport-https gnupg2 curl
173+
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
174+
echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee -a /etc/apt/sources.list.d/kubernetes.list
175+
sudo apt-get update
176+
sudo apt-get install -y kubectl
177+
{{< /tab >}}
178+
179+
{{< tab name="CentOS、RHEL 或 Fedora" codelang="bash" >}}cat <<EOF > /etc/yum.repos.d/kubernetes.repo
180+
[kubernetes]
181+
name=Kubernetes
182+
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
183+
enabled=1
184+
gpgcheck=1
185+
repo_gpgcheck=1
186+
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
187+
EOF
188+
yum install -y kubectl
189+
{{< /tab >}}
190+
{{< /tabs >}}
191+
192+
<!--
193+
### Install using other package management
194+
-->
195+
### 用其他包管理工具安装 {#install-using-other-package-management}
196+
197+
{{< tabs name="other_kubectl_install" >}}
198+
{{% tab name="Snap" %}}
199+
<!--
200+
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.
201+
-->
202+
如果你使用的 Ubuntu 或其他 Linux 发行版,内建支持 [snap](https://snapcraft.io/docs/core/install) 包管理工具,
203+
则可用 [snap](https://snapcraft.io/) 命令安装 kubectl。
204+
205+
```shell
206+
snap install kubectl --classic
207+
kubectl version --client
208+
```
209+
210+
{{% /tab %}}
211+
212+
{{% tab name="Homebrew" %}}
213+
<!--
214+
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).
215+
-->
216+
如果你使用 Linux 系统,并且装了 [Homebrew](https://docs.brew.sh/Homebrew-on-Linux) 包管理工具,
217+
则可以使用这种方式[安装](https://docs.brew.sh/Homebrew-on-Linux#install) kubectl。
218+
219+
```shell
220+
brew install kubectl
221+
kubectl version --client
222+
```
223+
224+
{{% /tab %}}
225+
226+
{{< /tabs >}}
227+
228+
<!--
229+
### Install on Linux as part of the Google Cloud SDK
230+
-->
231+
### 作为谷歌云 SDK 的一部分,在 Linux 上安装 {#install-on-linux-as-part-of-the-google-cloud-sdk}
232+
233+
{{< include "included/install-kubectl-gcloud.md" >}}
234+
235+
<!--
236+
## Verify kubectl configuration
237+
-->
238+
## 验证 kubectl 配置 {#verify-kubectl-configration}
239+
240+
{{< include "included/verify-kubectl.md" >}}
241+
242+
## kubectl 的可选配置 {#optional-kubectl-configurations}
243+
244+
### 启用 shell 自动补全功能 {#enable-shell-autocompletion}
245+
246+
<!--
247+
kubectl provides autocompletion support for Bash and Zsh, which can save you a lot of typing.
248+
249+
Below are the procedures to set up autocompletion for Bash and Zsh.
250+
-->
251+
kubectl 为 Bash 和 Zsh 提供自动补全功能,可以减轻许多输入的负担。
252+
253+
下面是为 Bash 和 Zsh 设置自动补全功能的操作步骤。
254+
255+
{{< tabs name="kubectl_autocompletion" >}}
256+
{{< tab name="Bash" include="included/optional-kubectl-configs-bash-linux.md" />}}
257+
{{< tab name="Zsh" include="included/optional-kubectl-configs-zsh.md" />}}
258+
{{< /tabs >}}
259+
260+
## {{% heading "whatsnext" %}}
261+
262+
{{< include "included/kubectl-whats-next.md" >}}

0 commit comments

Comments
 (0)