|
| 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