什么是 Kubernetes?
Kubernetes(或 K8s)是一个开源容器编排系统,用于自动化计算机应用程序的部署、扩展和管理。 Kubernetes 在众多主机上管理和运行 Docker 容器。 该项目由谷歌发起,得到了许多公司的支持,包括微软、RedHat、IBM。
Kubernetes 有两个主要版本,一个生产环境和一个学习环境。 以下说明用于安装简单的 Kubernetes 学习框架。 用于部署此类环境的工具是 Minikube 和 Kubectl。
什么是 Minikube?
Minikube 是一个允许我们在虚拟机中本地运行 Kubernetes 设置的工具。 它让我们可以在个人计算机上运行单节点 Kubernetes 集群来尝试 Kubernetes 或基本的开发工作。
什么是 Kubectl?
Kubectl 是 Kubernetes 的一个命令行工具,它允许我们在 Kubernetes 集群上运行命令。 我们还可以使用 kubectl 来部署应用,检查和管理集群资源,查看日志。
在 Ubuntu 20 上安装 Kubectl
在 Linux 系统上正式安装 kubectl 有两种选择。
- 第一个选项是使用安装 kubectl 二进制文件 curl 任何版本的 Linux 上的命令。
- 第二个选项使用 Ubuntu 中的内置包管理器 apt。 作为最佳实践,建议使用第二个选项。
与往常一样,我们更新所有包并安装 https 传输协议。
root@host:~$ apt-get update && apt-get install -y apt-transport-https
安装 GPG 存储库安全密钥
首先,下载存储库的 gpg 安全密钥。
root@host:~$ curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
OK
root@host:~$
现在,我们添加密钥并应用它。
root@host:~$ echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | tee -a /etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
root@host:~$
最后,再次更新包管理器存储库。
root@host:~$ apt-get update
安装 Kubectl
现在我们可以使用以下命令安装 kubectl。
root@host:~$ apt-get install -y kubectl
接下来,我们将使用 version 命令验证它是否已安装。
root@host:~$ kubectl version --client
Client Version: version.Info{Major:"1", Minor:"19", GitVersion:"v1.19.1", GitCommit:"206bcadf021e76c27513500ca24182692aabd17e", GitTreeState:"clean", BuildDate:"2020-09-09T11:26:42Z", GoVersion:"go1.15", Compiler:"gc", Platform:"linux/amd64"}
root@host:~$
额外的 Kubectl 配置
安装 Shell 自动完成
Kubectl 支持 Bash 和 Zsh 中的自动完成输入(自动完成用于完成部分键入的命令)。 在输入长命令时,此功能可以节省我们的时间。
我们需要安装 bash-completion 包,以便我们可以添加 kubectl 自动完成脚本。 使用此命令安装它。
root@host:~$ apt-get install -y bash-completion
Reading package lists... Done
Building dependency tree
Reading state information... Done
bash-completion is already the newest version (1:2.10-1ubuntu1).
bash-completion set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
root@host:~$
要使 ~ /.bashrc 中的命令起作用,请在终端中使用以下命令来实现它。
root@host:~$ source /usr/share/bash-completion/bash_completion
root@host:~$
要验证我们是否正确安装了软件包,请运行以下命令。
root@host:~$ type _init_completion
启用 Shell 自动完成
现在,我们使用以下命令将脚本添加到 ~/.bashrc 文件中:
root@host:~$ echo 'source <(kubectl completion bash)' >>~/.bashrc
root@host:~$
接下来,将脚本添加到目录 /etc/bash_completion.d
root@host:~$ kubectl completion bash >/etc/bash_completion.d/kubectl
root@host:~$
重新启动操作系统后,您应该对 kubectl 有一个 AutoComplete 输入。
在 macOS 上安装 Kubectl
我们将在 macOS 上使用 Homebrew 来安装 kubectl。
安装 Kubectl
在终端中使用以下安装命令。
admin@MacBook-Pro-4 ~ % brew install kubectl
验证安装
要检查安装是否成功,请运行以下命令。
admin@MacBook-Pro-4 ~ % kubectl version --client
注意:在 macOS 上为 kubectl 设置自动完成脚本时,有一个关键因素。 该方案与Linux系统非常相似,但有两个版本 bash-完成。 第一个 (v1) 用于 Bash 3.2(这是 macOS 上的默认设置)。 第二个 (v2) 适用于 Bash 4.1+。 kubectl 插件脚本无法正常工作 bash-完成 v1 和 Bash 3.2。 需要 Bash-completion v2 和 Bash 4.1 +,因此我们需要同时更新两者 bash 和 bash-完成。
我们可以检查我们的版本 bash 使用这个命令。
admin@MacBook-Pro-4 ~ % bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin19)
Copyright (C) 2007 Free Software Foundation, Inc.
admin@MacBook-Pro-4 ~ %
更新 Bash
现在我们将更新 bash 通过运行以下命令使用 Homebrew。
admin@MacBook-Pro-4 ~ % brew install bash
验证更新
检查它是否已使用此命令更新。
admin@MacBook-Pro-4 ~ % /usr/local/bin/bash --version
GNU bash, version 5.0.18(1)-release (x86_64-apple-darwin19.5.0)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
admin@MacBook-Pro-4 ~ %
安装 Bash 完成
安装 bash- 使用此命令完成。
admin@MacBook-Pro-4 ~ % brew install bash-completion@2
现在,将以下行添加到 ~/.bashrc 文件。
admin@MacBook-Pro-4 ~ % 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"
admin@MacBook-Pro-4 ~ %
接下来,将脚本添加到目录中。
admin@MacBook-Pro-4 ~ % kubectl completion bash >/usr/local/etc/bash_completion.d/kubectl
admin@MacBook-Pro-4 ~ %
macOS 上 kubectl 的自动完成功能现在可以使用了。
在 Windows Server 2019 上安装 Kubectl
Windows 上有多个安装选项。 最好的选择是在 Powershell 中使用 Chocolatey 进行安装。 我们将使用第三方包管理器 Chocolatey。 通过右键单击开始并选择 Windows PowerShell (Admin) 来启动 Windows PowerShell (Admin)。 通过在 PowerShell 中输入以下命令来安装 Chocolatey (choco)。
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
接下来,使用此命令检查安装是否成功。
PS C:UsersAdministrator> choco
Chocolatey v0.10.15
Please run 'choco -?' or 'choco <command> -?' for help menu.
PS C:UsersAdministrator>
现在我们使用以下命令在 Windows 上安装 kubectl。 同意安装时选择“Y”。
PS C:UsersAdministrator> choco install kubernetes-cli
Enter 用于验证所有内容都已安装的版本命令
PS C:UsersAdministrator> kubectl version --client
Client Version: version.Info{Major:"1", Minor:"19", GitVersion:"v1.19.1", GitCommit:"206bcadf021e76c27513500ca24182692aabd17e", GitTreeState:"clean", BuildDate:"2020-09-09T11:26:42Z", GoVersion:"go1.15", Compiler:"gc", Platform:"windows/amd64"}
PS C:UsersAdministrator>
您已在 Windows Server 2019 上成功安装 kubectl。
安装 Minikube
安装 kubectl 后,我们需要安装 Minikube。 Minikube 是一个工具,可以让我们在虚拟机本地快速运行 K8s 的单节点版本。
Ubuntu 20
首先,我们需要检查我们的电脑是否支持 VT-x / AMD-v 使用此命令进行虚拟化。
root@host:~$ grep -E --color 'vmx|svm' /proc/cpuinfo
每个人在输出中都会有不同的信息(基于您的 CPU 类型),但我们应该验证我们是否看到了 vmx 或 svm 标志。 如果是这样,CPU 能够运行硬件虚拟化。
接下来,我们安装Virtualbox作为hypervisor(Virtualbox是一个用于虚拟化OS Windows、Linux、FreeBSD、macOS、Solaris等的软件产品)。用这个命令安装Virtualbox。
root@host:~$ apt install virtualbox virtualbox-ext-pack
现在,我们使用直接链接下载 Minikube,并使用 chmod 命令使 Minikube 可执行。
root@host:~$ curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 && chmod +x minikube
然后,我们将文件移动到 bin 目录。
root@host:~$ cp minikube-linux-amd64 /usr/local/bin/minikube
要验证我们是否已安装所有内容,请输入以下命令:
root@host:~$ minikube version
minikube version: v1.13.0
commit: 0c5e9de4ca6f9c55147ae7f90af97eff5befef5f-dirty
root@host:~$
在 macOS 上安装 Minikube
使用以下命令验证虚拟化的可能性:
admin@MacBook-Pro-4 ~ % sysctl -a | grep -E --color 'machdep.cpu.features|VMX'
您应该有类似于以下输出的内容。
然后安装 HyperKit。 这是一个流行的虚拟机管理程序。 它用于轻量级虚拟机和容器部署。 我们使用 brew 安装它。
admin@MacBook-Pro-4 ~ % brew install hyperkit
要验证安装,请使用 version 命令。
admin@MacBook-Pro-4 ~ % hyperkit -v
hyperkit: 0.20200224
Homepage: https://github.com/docker/hyperkit
License: BSD
admin@MacBook-Pro-4 ~ %
我们也可以使用 brew 安装 minikube。
admin@MacBook-Pro-4 ~ % brew install minikube
Updating Homebrew...
==> Downloading https://homebrew.bintray.com/bottles/minikube-1.13.0.catalina.bo
==> Pouring minikube-1.13.0.catalina.bottle.1.tar.gz
==> Caveats
Bash completion has been installed to:
/usr/local/etc/bash_completion.d
zsh completions have been installed to:
/usr/local/share/zsh/site-functions
==> Summary
? /usr/local/Cellar/minikube/1.13.0: 8 files, 62.2MB
admin@MacBook-Pro-4 ~ %
使用 version 命令检查是否已安装 minikube:
admin@MacBook-Pro-4 ~ % minikube version
minikube version: v1.13.0
commit: 0c5e9de4ca6f9c55147ae7f90af97eff5befef5f
admin@MacBook-Pro-4 ~ %
在 Windows Server 2019 上安装 Minikube
首先,我们需要检查服务器是否支持虚拟化。 Enter 以下命令:
PS C:UsersAdministrator> systeminfo
Host Name: WIN-SG77QKRNOA6
OS Name: Microsoft Windows Server 2019 Standard
OS Version: 10.0.17763 N/A Build 17763
OS Manufacturer: Microsoft Corporation
OS Configuration: Standalone Server
OS Build Type: Multiprocessor Free
Registered Owner: Windows User
Registered Organization:
Product ID: 00429-70000-00000-AA065
Original Install Date: 9/13/2020, 6:53:39 AM
System Boot Time: 9/13/2020, 7:31:43 AM
System Manufacturer: innotek GmbH
System Model: VirtualBox
System Type: x64-based PC
Processor(s): 1 Processor(s) Installed.
[01]: AMD64 Family 23 Model 113 Stepping 0 AuthenticAMD ~3593 Mhz
BIOS Version: innotek GmbH VirtualBox, 12/1/2006
Windows Directory: C:Windows
System Directory: C:Windowssystem32
Boot Device: DeviceHarddiskVolume1
System Locale: en-us;English (United States)
Input Locale: en-us;English (United States)
Time Zone: (UTC-08:00) Pacific Time (US & Canada)
Total Physical Memory: 2,048 MB
Available Physical Memory: 1,242 MB
Virtual Memory: Max Size: 3,200 MB
Virtual Memory: Available: 2,468 MB
Virtual Memory: In Use: 732 MB
Page File Location(s): C:pagefile.sys
Domain: WORKGROUP
Logon Server: WIN-SG77QKRNOA6
Network Card(s): 1 NIC(s) Installed.
[01]: Intel(R) PRO/1000 MT Desktop Adapter
Connection Name: Ethernet
DHCP Enabled: Yes
DHCP Server: 192.168.50.1
IP address(es)
[01]: 192.168.50.167
[02]: fe80::b115:cdee:3a22:1b65
Hyper-V Requirements: VM Monitor Mode Extensions: Yes
Virtualization Enabled In Firmware: Yes
Second Level Address Translation: No
Data Execution Prevention Available: Yes
PS C:UsersAdministrator>
请注意以下几行:
Hyper-V Requirements:
VM Monitor Mode Extensions: Yes
Virtualization Enabled In Firmware: Yes
Second Level Address Translation: No
Data Execution Prevention Available: Yes
现在使用 choco 安装 Hypervisor。
PS C:UsersAdministrator> choco install virtualbox
使用 choco 安装 minikube。
PS C:UsersAdministrator> choco install minikube
使用 version 命令检查 Minikube 是否已安装。
PS C:UsersAdministrator> minikube version
minikube version: v1.13.0
commit: 0c5e9de4ca6f9c55147ae7f90af97eff5befef5f-dirty
PS C:UsersAdministrator>
这样就完成了 Windows Server 上的 minikube 安装。
结论
在本文中,我们讨论了如何在 Linux、macOS 和 Windows 上安装和配置 minikube 和 kubectl。 我们提供了命令行说明来帮助用户完成 minikube 和 kubectl 的本地安装,允许用户测试本地安装的 Kubernetes 版本。
今天就开始吧!
我们以成为 Hosting™ 中最有帮助的人而自豪!
我们经验丰富的托管解决方案顾问随时可以向您展示如何利用这些技术!
我们全年 365 天、每周 7 天、每天 24 小时提供服务,通过我们的票务系统 [email protected]、电话(800-580-4986)或 在线聊天 或您喜欢的任何方法。