开源容器编排平台 Kubernetes 彻底改变了容器化应用程序的管理和可扩展性。在 Debian 12 “Bookworm” 上安装 Kubernetes 可以让您利用这项尖端技术的强大功能和灵活性。
在 Debian 12 bookworm 上安装 Kubernetes
第 1 步。在我们安装任何软件之前,通过在终端中运行以下命令来确保您的系统是最新的非常重要:apt
<span class="pln">sudo apt update sudo apt install curl gpg gnupg2 software</span><span class="pun">-</span><span class="pln">properties</span><span class="pun">-</span><span class="pln">common apt</span><span class="pun">-</span><span class="pln">transport</span><span class="pun">-</span><span class="pln">https lsb</span><span class="pun">-</span><span class="pln">release ca</span><span class="pun">-</span><span class="pln">certificates</span>
此命令将刷新存储库,允许您安装最新版本的软件包。
第 2 步。安装 Docker。
Kubernetes 依赖 Docker 作为容器运行时。让我们通过执行以下命令来安装 Docker:
<span class="pln">sudo apt install docker</span><span class="pun">.</span><span class="pln">io sudo systemctl enable </span><span class="pun">--</span><span class="pln">now docker</span>
第 3 步。在 Debian 12 上安装 Kubernetes。
现在我们的环境已经准备好了,让我们继续在 Debian 12 “Bookworm” 上安装 Kubernetes。 我们需要添加官方的 Kubernetes 存储库。执行以下命令:
<span class="pln">curl </span><span class="pun">-</span><span class="pln">s https</span><span class="pun">:</span><span class="com">//packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -</span><span class="pln"> echo </span><span class="str">"deb https://apt.kubernetes.io/ kubernetes-xenial main"</span> <span class="pun">|</span><span class="pln"> sudo tee </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">apt</span><span class="pun">/</span><span class="pln">sources</span><span class="pun">.</span><span class="pln">list</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">kubernetes</span><span class="pun">.</span><span class="pln">list sudo apt update</span>
接下来,使用以下命令安装 Kubernetes 软件包:
<span class="pln">sudo apt install kubelet kubeadm kubectl sudo apt</span><span class="pun">-</span><span class="pln">mark hold kubelet kubeadm kubectl</span>
第 4 步。配置群集。
现在,让我们通过设置主节点和工作节点来配置 Kubernetes 集群。
- A. 主节点设置
在所需的主节点上,通过执行以下命令初始化集群:
<span class="pln">sudo kubeadm init</span>
记下成功初始化时显示的 join 命令,因为我们稍后将需要它将工作节点加入集群。
- B. 工作节点设置
要将工作节点添加到集群,请在每个工作节点上执行从主节点初始化中获得的 join 命令:
<span class="pln">sudo kubeadm </span><span class="kwd">join</span> <span class="pun"><</span><span class="pln">master</span><span class="pun">-</span><span class="pln">node</span><span class="pun">-</span><span class="pln">ip</span><span class="pun">>:<</span><span class="pln">master</span><span class="pun">-</span><span class="pln">node</span><span class="pun">-</span><span class="pln">port</span><span class="pun">></span> <span class="pun">--</span><span class="pln">token </span><span class="str"><token></span> <span class="pun">--</span><span class="pln">discovery</span><span class="pun">-</span><span class="pln">token</span><span class="pun">-</span><span class="pln">ca</span><span class="pun">-</span><span class="pln">cert</span><span class="pun">-</span><span class="pln">hash </span><span class="str"><hash></span>
第5步。验证安装。
完成安装过程后,必须验证 Kubernetes 是否已正确安装。
- 集群验证
在主节点上,运行以下命令以检查集群组件的状态:
<span class="pln">kubectl cluster</span><span class="pun">-</span><span class="pln">info</span>
-
- 节点验证
要确保群集中的所有节点都正常运行,请执行:
<span class="pln">kubectl </span><span class="kwd">get</span><span class="pln"> nodes</span>
第 6 步。设置 Kubernetes 集群
现在我们已经安装了 Kubernetes,让我们通过初始化主节点并加入工作节点来设置集群。
- 初始化 Kubernetes 主节点
在主节点上,使用以下命令初始化集群:
<span class="pln">sudo kubeadm init </span><span class="pun">--</span><span class="pln">apiserver</span><span class="pun">-</span><span class="pln">advertise</span><span class="pun">-</span><span class="pln">address</span><span class="pun">=<</span><span class="pln">master</span><span class="pun">-</span><span class="pln">node</span><span class="pun">-</span><span class="pln">ip</span><span class="pun">></span>
- 将工作线程节点加入群集
在每个工作节点上,使用主节点初始化期间获取的 join 命令将它们加入集群:
<span class="pln">sudo kubeadm </span><span class="kwd">join</span> <span class="pun"><</span><span class="pln">master</span><span class="pun">-</span><span class="pln">node</span><span class="pun">-</span><span class="pln">ip</span><span class="pun">>:<</span><span class="pln">master</span><span class="pun">-</span><span class="pln">node</span><span class="pun">-</span><span class="pln">port</span><span class="pun">></span> <span class="pun">--</span><span class="pln">token </span><span class="str"><token></span> <span class="pun">--</span><span class="pln">discovery</span><span class="pun">-</span><span class="pln">token</span><span class="pun">-</span><span class="pln">ca</span><span class="pun">-</span><span class="pln">cert</span><span class="pun">-</span><span class="pln">hash </span><span class="str"><hash></span>
要确保集群设置正确,请在主节点上运行以下命令:
<span class="pln">kubectl </span><span class="kwd">get</span><span class="pln"> nodes</span>
步骤 7.部署示例应用程序
为了验证 Kubernetes 的成功安装和功能,让我们在集群上部署一个示例应用程序。
- 选择示例应用程序
选择适合您需求的示例应用程序。例如,我们将部署一个基本的 Nginx Web 服务器。
- 编写部署 YAML 文件
创建一个名为的 YAML 文件并定义部署规范,如下所示:nginx-deployment.yaml
<span class="pln">apiVersion</span><span class="pun">:</span><span class="pln"> apps</span><span class="pun">/</span><span class="pln">v1 kind</span><span class="pun">:</span> <span class="typ">Deployment</span><span class="pln"> metadata</span><span class="pun">:</span><span class="pln"> name</span><span class="pun">:</span><span class="pln"> nginx</span><span class="pun">-</span><span class="pln">deployment spec</span><span class="pun">:</span><span class="pln"> replicas</span><span class="pun">:</span> <span class="lit">1</span><span class="pln"> selector</span><span class="pun">:</span><span class="pln"> matchLabels</span><span class="pun">:</span><span class="pln"> app</span><span class="pun">:</span><span class="pln"> nginx </span><span class="kwd">template</span><span class="pun">:</span><span class="pln"> metadata</span><span class="pun">:</span><span class="pln"> labels</span><span class="pun">:</span><span class="pln"> app</span><span class="pun">:</span><span class="pln"> nginx spec</span><span class="pun">:</span><span class="pln"> containers</span><span class="pun">:</span> <span class="pun">-</span><span class="pln"> name</span><span class="pun">:</span><span class="pln"> nginx image</span><span class="pun">:</span><span class="pln"> nginx</span><span class="pun">:</span><span class="pln">latest ports</span><span class="pun">:</span> <span class="pun">-</span><span class="pln"> containerPort</span><span class="pun">:</span> <span class="lit">80</span>
-
- 部署应用程序
通过执行以下命令部署应用程序:
<span class="pln">kubectl apply </span><span class="pun">-</span><span class="pln">f nginx</span><span class="pun">-</span><span class="pln">deployment</span><span class="pun">.</span><span class="pln">yaml</span>
使用以下命令检查已部署应用程序的状态:
<span class="pln">kubectl </span><span class="kwd">get</span><span class="pln"> pods</span>
感谢您使用本教程在 Debian 12 Bookworm 上安装 Kubernetes。如需其他帮助或有用信息,我们建议您查看 Kubernetes 官方网站。