Skip to content

Commit f7dc13e

Browse files
authored
Merge pull request #26980 from zhiguo-lu/zh-trans-task-install-kubectl-on-mac
[zh] translate task/Install and Set Up kubectl on macOS
2 parents 07d6f33 + 22f104c commit f7dc13e

File tree

6 files changed

+612
-0
lines changed

6 files changed

+612
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
title: "通过 gcloud 安装 kubectl"
3+
description: "用各个特定操作系统标签页中包含的 gcloud 指令片段安装 kubectl。"
4+
headless: true
5+
---
6+
<!--
7+
---
8+
title: "gcloud kubectl install"
9+
description: "How to install kubectl with gcloud snippet for inclusion in each OS-specific tab."
10+
headless: true
11+
---
12+
-->
13+
14+
<!--
15+
You can install kubectl as part of the Google Cloud SDK.
16+
-->
17+
kubectl 可以作为 Google Cloud SDK 的一部分被安装。
18+
19+
<!--
20+
1. Install the [Google Cloud SDK](https://cloud.google.com/sdk/).
21+
1. Run the `kubectl` installation command:
22+
-->
23+
1. 安装 [Google Cloud SDK](https://cloud.google.com/sdk/)
24+
1. 运行安装 `kubectl` 的命令:
25+
26+
```shell
27+
gcloud components install kubectl
28+
```
29+
30+
<!--
31+
1. Test to ensure the version you installed is up-to-date:
32+
-->
33+
1. 验证一下,确保安装的是最新的版本:
34+
35+
```shell
36+
kubectl version --client
37+
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
title: "后续内容"
3+
description: "安装 kubectl 之后,还可以做些什么?"
4+
headless: true
5+
---
6+
<!--
7+
---
8+
title: "What's next?"
9+
description: "What's next after installing kubectl."
10+
headless: true
11+
---
12+
-->
13+
14+
<!--
15+
* [Install Minikube](https://minikube.sigs.k8s.io/docs/start/)
16+
* See the [getting started guides](/docs/setup/) for more about creating clusters.
17+
* [Learn how to launch and expose your application.](/docs/tasks/access-application-cluster/service-access-application-cluster/)
18+
* If you need access to a cluster you didn't create, see the
19+
[Sharing Cluster Access document](/docs/tasks/access-application-cluster/configure-access-multiple-clusters/).
20+
* Read the [kubectl reference docs](/docs/reference/kubectl/kubectl/)
21+
-->
22+
* [安装 Minikube](https://minikube.sigs.k8s.io/docs/start/)
23+
* 有关创建集群的更多信息,请参阅[入门指南](/zh/docs/setup/).
24+
* [学习如何启动并对外公开你的应用程序。](/zh/docs/tasks/access-application-cluster/service-access-application-cluster/)
25+
* 如果你需要访问其他人创建的集群,请参阅
26+
[共享集群接入文档](/zh/docs/tasks/access-application-cluster/configure-access-multiple-clusters/).
27+
* 阅读 [kubectl 参考文档](/zh/docs/reference/kubectl/kubectl/)
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
---
2+
title: "macOS 系统上的 bash 自动补全"
3+
description: "在 macOS 上实现 Bash 自动补全的一些可选配置。"
4+
headless: true
5+
---
6+
<!--
7+
---
8+
title: "bash auto-completion on macOS"
9+
description: "Some optional configuration for bash auto-completion on macOS."
10+
headless: true
11+
---
12+
-->
13+
14+
<!--
15+
### Introduction
16+
-->
17+
### 简介
18+
19+
<!--
20+
The kubectl completion script for Bash can be generated with `kubectl completion bash`. Sourcing this script in your shell enables kubectl completion.
21+
22+
However, the kubectl completion script depends on [**bash-completion**](https://github.com/scop/bash-completion) which you thus have to previously install.
23+
-->
24+
kubectl 的 Bash 补全脚本可以通过 `kubectl completion bash` 命令生成。
25+
在你的 shell 中导入(Sourcing)这个脚本即可启用补全功能。
26+
27+
此外,kubectl 补全脚本依赖于工具 [**bash-completion**](https://github.com/scop/bash-completion)
28+
所以你必须先安装它。
29+
30+
{{< warning>}}
31+
<!--
32+
There are two versions of bash-completion, v1 and v2. V1 is for Bash 3.2 (which is the default on macOS), and v2 is for Bash 4.1+. The kubectl completion script **doesn't work** correctly with bash-completion v1 and Bash 3.2. It requires **bash-completion v2** and **Bash 4.1+**. Thus, to be able to correctly use kubectl completion on macOS, you have to install and use Bash 4.1+ ([*instructions*](https://itnext.io/upgrading-bash-on-macos-7138bd1066ba)). The following instructions assume that you use Bash 4.1+ (that is, any Bash version of 4.1 or newer).
33+
-->
34+
bash-completion 有两个版本:v1 和 v2。v1 对应 Bash3.2(也是 macOS 的默认安装版本),v2 对应 Bash 4.1+。
35+
kubectl 的补全脚本**无法适配** bash-completion v1 和 Bash 3.2。
36+
必须为它配备 **bash-completion v2****Bash 4.1+**
37+
有鉴于此,为了在 macOS 上使用 kubectl 补全功能,你必须要安装和使用 Bash 4.1+
38+
([*说明*](https://itnext.io/upgrading-bash-on-macos-7138bd1066ba))。
39+
后续说明假定你用的是 Bash 4.1+(也就是 Bash 4.1 或更新的版本)
40+
{{< /warning >}}
41+
42+
<!--
43+
### Upgrade Bash
44+
-->
45+
### 升级 Bash
46+
47+
<!--
48+
The instructions here assume you use Bash 4.1+. You can check your Bash's version by running:
49+
-->
50+
后续说明假定你已使用 Bash 4.1+。你可以运行以下命令检查 Bash 版本:
51+
52+
```bash
53+
echo $BASH_VERSION
54+
```
55+
56+
<!--
57+
If it is too old, you can install/upgrade it using Homebrew:
58+
-->
59+
如果版本太旧,可以用 Homebrew 安装/升级:
60+
61+
```bash
62+
brew install bash
63+
```
64+
65+
<!--
66+
Reload your shell and verify that the desired version is being used:
67+
-->
68+
重新加载 shell,并验证所需的版本已经生效:
69+
70+
```bash
71+
echo $BASH_VERSION $SHELL
72+
```
73+
74+
<!--
75+
Homebrew usually installs it at `/usr/local/bin/bash`.
76+
-->
77+
Homebrew 通常把它安装为 `/usr/local/bin/bash`
78+
79+
<!--
80+
### Install bash-completion
81+
-->
82+
### 安装 bash-completion
83+
84+
85+
{{< note >}}
86+
<!--
87+
As mentioned, these instructions assume you use Bash 4.1+, which means you will install bash-completion v2 (in contrast to Bash 3.2 and bash-completion v1, in which case kubectl completion won't work).
88+
-->
89+
如前所述,本说明假定你使用的 Bash 版本为 4.1+,这意味着你要安装 bash-completion v2
90+
(不同于 Bash 3.2 和 bash-completion v1,kubectl 的补全功能在该场景下无法工作)。
91+
{{< /note >}}
92+
93+
<!--
94+
You can test if you have bash-completion v2 already installed with `type _init_completion`. If not, you can install it with Homebrew:
95+
-->
96+
你可以用命令 `type _init_completion` 测试 bash-completion v2 是否已经安装。
97+
如未安装,用 Homebrew 来安装它:
98+
99+
```bash
100+
brew install bash-completion@2
101+
```
102+
103+
<!--
104+
As stated in the output of this command, add the following to your `~/.bash_profile` file:
105+
-->
106+
如命令的输出信息所显示的,将如下内容添加到文件 `~/.bash_profile` 中:
107+
108+
```bash
109+
export BASH_COMPLETION_COMPAT_DIR="/usr/local/etc/bash_completion.d"
110+
[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"
111+
```
112+
113+
<!--
114+
Reload your shell and verify that bash-completion v2 is correctly installed with `type _init_completion`.
115+
-->
116+
重新加载 shell,并用命令 `type _init_completion` 验证 bash-completion v2 已经恰当的安装。
117+
118+
<!--
119+
### Enable kubectl autocompletion
120+
-->
121+
### 启用 kubectl 自动补全功能
122+
123+
<!--
124+
You now have to ensure that the kubectl completion script gets sourced in all your shell sessions. There are multiple ways to achieve this:
125+
126+
- Source the completion script in your `~/.bash_profile` file:
127+
-->
128+
你现在需要确保在所有的 shell 环境中均已导入(sourced) kubectl 的补全脚本,
129+
有若干种方法可以实现这一点:
130+
131+
- 在文件 `~/.bash_profile` 中导入(Source)补全脚本:
132+
133+
```bash
134+
echo 'source <(kubectl completion bash)' >>~/.bash_profile
135+
```
136+
137+
<!--
138+
- Add the completion script to the `/usr/local/etc/bash_completion.d` directory:
139+
-->
140+
- 将补全脚本添加到目录 `/usr/local/etc/bash_completion.d` 中:
141+
142+
```bash
143+
kubectl completion bash >/usr/local/etc/bash_completion.d/kubectl
144+
```
145+
146+
<!--
147+
- If you have an alias for kubectl, you can extend shell completion to work with that alias:
148+
-->
149+
- 如果你为 kubectl 定义了别名,则可以扩展 shell 补全来兼容该别名:
150+
151+
```bash
152+
echo 'alias k=kubectl' >>~/.bash_profile
153+
echo 'complete -F __start_kubectl k' >>~/.bash_profile
154+
```
155+
156+
<!--
157+
- If you installed kubectl with Homebrew (as explained [above](#install-with-homebrew-on-macos)), then the kubectl completion script should already be in `/usr/local/etc/bash_completion.d/kubectl`. In that case, you don't need to do anything.
158+
-->
159+
- 如果你是用 Homebrew 安装的 kubectl([如上所述](#install-with-homebrew-on-macos)),
160+
那么 kubectl 补全脚本应该已经安装到目录 `/usr/local/etc/bash_completion.d/kubectl` 中了。
161+
这种情况下,你什么都不需要做。
162+
163+
{{< note >}}
164+
<!--
165+
The Homebrew installation of bash-completion v2 sources all the files in the `BASH_COMPLETION_COMPAT_DIR` directory, that's why the latter two methods work.
166+
-->
167+
用 Hommbrew 安装的 bash-completion v2 会初始化 目录 `BASH_COMPLETION_COMPAT_DIR` 中的所有文件,这就是后两种方法能正常工作的原因。
168+
{{< /note >}}
169+
170+
<!--
171+
In any case, after reloading your shell, kubectl completion should be working.
172+
-->
173+
总之,重新加载 shell 之后,kubectl 补全功能将立即生效。
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
title: "zsh 自动补全"
3+
description: "zsh 自动补全的一些可选配置"
4+
headless: true
5+
---
6+
<!--
7+
---
8+
title: "zsh auto-completion"
9+
description: "Some optional configuration for zsh auto-completion."
10+
headless: true
11+
---
12+
-->
13+
14+
<!--
15+
The kubectl completion script for Zsh can be generated with the command `kubectl completion zsh`. Sourcing the completion script in your shell enables kubectl autocompletion.
16+
17+
To do so in all your shell sessions, add the following to your `~/.zshrc` file:
18+
-->
19+
kubectl 通过命令 `kubectl completion zsh` 生成 Zsh 自动补全脚本。
20+
在 shell 中导入(Sourcing)该自动补全脚本,将启动 kubectl 自动补全功能。
21+
22+
为了在所有的 shell 会话中实现此功能,请将下面内容加入到文件 `~/.zshrc` 中。
23+
24+
```zsh
25+
source <(kubectl completion zsh)
26+
```
27+
28+
<!--
29+
If you have an alias for kubectl, you can extend shell completion to work with that alias:
30+
-->
31+
如果你为 kubectl 定义了别名,可以扩展脚本补全,以兼容该别名。
32+
33+
```zsh
34+
echo 'alias k=kubectl' >>~/.zshrc
35+
echo 'complete -F __start_kubectl k' >>~/.zshrc
36+
```
37+
38+
<!--
39+
After reloading your shell, kubectl autocompletion should be working.
40+
41+
If you get an error like `complete:13: command not found: compdef`, then add the following to the beginning of your `~/.zshrc` file:
42+
-->
43+
重新加载 shell 后,kubectl 自动补全功能将立即生效。
44+
45+
如果你收到 `complete:13: command not found: compdef` 这样的错误提示,那请将下面内容添加到 `~/.zshrc` 文件的开头:
46+
47+
```zsh
48+
autoload -Uz compinit
49+
compinit
50+
```
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
title: "验证 kubectl 的安装效果"
3+
description: "如何验证 kubectl。"
4+
headless: true
5+
---
6+
<!--
7+
---
8+
title: "verify kubectl install"
9+
description: "How to verify kubectl."
10+
headless: true
11+
---
12+
-->
13+
14+
<!--
15+
In order for kubectl to find and access a Kubernetes cluster, it needs a
16+
[kubeconfig file](/docs/concepts/configuration/organize-cluster-access-kubeconfig/),
17+
which is created automatically when you create a cluster using
18+
[kube-up.sh](https://github.com/kubernetes/kubernetes/blob/master/cluster/kube-up.sh)
19+
or successfully deploy a Minikube cluster.
20+
By default, kubectl configuration is located at `~/.kube/config`.
21+
22+
Check that kubectl is properly configured by getting the cluster state:
23+
-->
24+
为了让 kubectl 能发现并访问 Kubernetes 集群,你需要一个
25+
[kubeconfig 文件](/docs/zh/concepts/configuration/organize-cluster-access-kubeconfig/)
26+
该文件在
27+
[kube-up.sh](https://github.com/kubernetes/kubernetes/blob/master/cluster/kube-up.sh)
28+
创建集群时,或成功部署一个 Miniube 集群时,均会自动生成。
29+
通常,kubectl 的配置信息存放于文件 `~/.kube/config` 中。
30+
31+
通过获取集群状态的方法,检查是否已恰当的配置了 kubectl:
32+
33+
```shell
34+
kubectl cluster-info
35+
```
36+
37+
<!--
38+
If you see a URL response, kubectl is correctly configured to access your cluster.
39+
40+
If you see a message similar to the following, kubectl is not configured correctly or is not able to connect to a Kubernetes cluster.
41+
-->
42+
如果返回一个 URL,则意味着 kubectl 成功的访问到了你的集群。
43+
44+
如果你看到如下所示的消息,则代表 kubectl 配置出了问题,或无法连接到 Kubernetes 集群。
45+
46+
```
47+
The connection to the server <server-name:port> was refused - did you specify the right host or port?
48+
(访问 <server-name:port> 被拒绝 - 你指定的主机和端口是否有误?)
49+
```
50+
51+
<!--
52+
For example, if you are intending to run a Kubernetes cluster on your laptop (locally), you will need a tool like Minikube to be installed first and then re-run the commands stated above.
53+
54+
If kubectl cluster-info returns the url response but you can't access your cluster, to check whether it is configured properly, use:
55+
-->
56+
例如,如果你想在自己的笔记本上(本地)运行 Kubernetes 集群,你需要先安装一个 Minikube 这样的工具,然后再重新运行上面的命令。
57+
58+
如果命令 `kubectl cluster-info` 返回了 url,但你还不能访问集群,那可以用以下命令来检查配置是否妥当:
59+
60+
```shell
61+
kubectl cluster-info dump
62+
```

0 commit comments

Comments
 (0)