前言
在公司里,我确实接触过 Kubernetes,但实际办公场景并不多,更多是维护、偶尔改配置、偶尔排问题,而不是从零搭建或深度理解它的工作机制。
我自己也用过:
- minikube
- kubeadm 快速部署
- 各种一键脚本
包括也看了很多视频,觉得"懂了",可过一段时间都忘的七七八八:
- etcd 是干嘛的?
- apiserver 和 controller 怎么协作?
- 网络是怎么通的?
- kubelet 具体干了什么?
面试问到原理时,总是:
"大概是这样......但细节我记不清了。"
且公司与 Kubernetes 相关 业务/系统 太少了,没什么学习、锻炼机会,所以,这次,我决定换一种学习方式:
不用任何自动化工具,不用 kubeadm,不用 Helm,不用一键脚本。
要做的是:
- 从二进制开始部署
- 手动生成证书
- 删除证书、删除etcd、拔掉节点等
实现目标
- 理解 Kubernetes 的完整工作流
- 理解各组件的边界
- 理解网络模型
- 理解存储抽象
- 理解控制面是怎么维持状态一致性的
- 能定位问题
- 能恢复系统
一、参考资料
本次学习主要参考:
Kubernetes The Hard Way 官方项目:
Kelsey Hightower 的公开视频教程:
- Kubernetes The Hard Way by Kelsey Hightower | Build Your Own Cluster From Scratch!
- aghilish / kubernetes-the-hard-way
个人建议最好先看 rockylinux 的官方教程,自己理解并做一遍
本人是结合两者去做的
相较于官方教程,本博文或多做一些原理说明
二、环境准备
为了避免在 VMware 里做嵌套虚拟化,我直接准备了一台实体 PC:
尽可能一台实体机完成所有部署以及"破坏式"实验
| 项目 | 配置 |
|---|---|
| 内存 | 16GB |
| 硬盘 | 1TB |
| CPU | i5 |
| 系统 | Ubuntu 24.04 LTS |
因为要用到虚拟机脚本(懒得手敲),所以下 aghilish 的仓库,此外没什么其他区别:
git clone https://github.com/aghilish/kubernetes-the-hard-way.git -b youtube-tutorial
按文档(官方)说的,准备四台机:
| Name | Description | CPU | RAM | Storage |
|---|---|---|---|---|
| jumpbox | Administration host | 1 | 512MB | 10GB |
| server | Kubernetes server | 1 | 2GB | 20GB |
| node-0 | Kubernetes worker node | 1 | 2GB | 20GB |
| node-1 | Kubernetes worker node | 1 | 2GB | 20GB |
文档里让我执行一个 launch.sh 脚本:
bash
# launch.sh
multipass launch --name jumpbox --cpus 1 --memory 512M --disk 10G --cloud-init configs/debian-cloud-init.yaml
multipass launch --name server --cpus 1 --memory 2G --disk 20G --cloud-init configs/debian-cloud-init.yaml
multipass launch --name node-0 --cpus 1 --memory 2G --disk 20G --cloud-init configs/debian-cloud-init.yaml
multipass launch --name node-1 --cpus 1 --memory 2G --disk 20G --cloud-init configs/debian-cloud-init.yaml
打字就是使用 multipass 来生成四台虚拟机
三、Multipass 原理
multipass 我也没用过(以往用的都是vmware),查了一下,原理大概如下:
它是一个极简命令行虚拟机管理器。
很多人会误以为它是容器,其实不是。
| 工具 | 本质 |
|---|---|
| Docker | 容器 |
| LXC | 系统容器 |
| VirtualBox | GUI 虚拟机 |
| VMware | 企业级虚拟化 |
| Multipass | 命令行 VM 管理器 |
它创建的是:
真正的虚拟机,不是容器。
底层结构
Multipass 本身并不实现虚拟化,它只是一个控制层:
你
↓
multipass CLI
↓
multipass daemon
↓
系统虚拟化引擎(KVM / Hyper-V / Hypervisor.framework)
在 Linux 上,它使用:
KVM + QEMU
也就是说,它依赖:
- CPU 虚拟化指令集(VT-x / AMD-V)
- /dev/kvm
- 内核虚拟化模块
这也是为什么:
你不能在 VMware 虚拟机里再用 Multipass 开虚拟机(默认情况下)。
这是典型的:
Nested Virtualization 问题
命令解释
bash
multipass launch --name jumpbox --cpus 1 --memory 512M --disk 10G --cloud-init configs/debian-cloud-init.yaml
multipass launch 用于创建并启动一台新的虚拟机
--cloud-init 用于在虚拟机启动时注入初始化配置,例如:
- 用户
- 密码
- SSH
- 镜像源
- 基础工具
详细可以到 configs/debian-cloud-init.yaml 文件里面去看
四、启动虚拟机并验证
bash
snap install multipass
sh ./launch.sh
验证:
bash
multipass list
multipass shell jumpbox
uname -mov
五、部署说明
上面准备好虚拟机,这里简单说明一下,为什么要四台机:
- Jumpbox
- master
- node-1
- node-1,
后面三个都很好理解(组 kubernetes 集群)。
主要说一下 Jumpbox ,官方说明如下:
Think of the jumpbox as the administration machine you will use as a home base when setting up your Kubernetes cluster from the ground up. One thing you need to do before you get started is to install a few command line utilities and clone the Kubernetes The Hard Way git repository, which contains some additional configuration files that you will use to configure various Kubernetes components throughout this tutorial.
可把它理解为,是一个 专门用来"操作集群"的管理节点 。
它的职责是:
-
不运行任何 Kubernetes 组件
-
不承载业务
-
只做三件事:
- 分发配置
- 分发证书
- 执行管理命令
和 JumpServer 之类的是类似的。
六、Jumpbox
官方教程用的是 Rocky Linux(dnf),我这里用的是 Ubuntu / Debian 系(虚拟机),命令会有点不一样。
所以安装软件都是使用 apt install
1. 进入 jumpbox 并安装基础工具
bash
multipass shell jumpbox
sudo apt-get -y install wget curl vim openssl git
2. 下载对应文件
下载配套教程文件
bash
git clone --depth 1 \
https://github.com/wsoyinka/kubernetes-the-hard-way.git
cd kubernetes-the-hard-way
下载二进制文件(避免 kubernetes 节点多次下载相同文件)
bash
wget -q --show-progress \
--https-only \
--timestamping \
-P downloads \
-i downloads.txt
ls -loh downloads
3. 安装 kubectl
官方说明:
In this section, you will install the kubectl, the official Kubernetes client command line tool, on the jumpbox machine. You will use kubectl to interact with the Kubernetes control plane after the provisioning of your cluster completes later in this tutorial.
jumpbox 是未来所有集群管理操作的入口,所以这里需要提前安装 kubectl。
bash
chmod +x downloads/kubectl
cp downloads/kubectl /usr/local/bin/
验证:
bash
kubectl version --client
七、hosts 配置
官方教程 hosts 部分比较啰嗦,如果大家明白它做什么用的手动复制上去都可以
在宿主机上,查看所有虚拟机 IP:
bash
multipass list
# 输出如下:
Name State IPv4 Image
jumpbox Running 10.182.70.218 Ubuntu 24.04 LTS
node-0 Running 10.182.70.221 Ubuntu 24.04 LTS
node-1 Running 10.182.70.234 Ubuntu 24.04 LTS
server Running 10.182.70.26 Ubuntu 24.04 LTS
在宿主机上追加 hosts
bash
vim /etc/hosts
# 追加
10.182.70.218 jumpbox
10.182.70.26 server.kubernetes.local server
10.182.70.221 node-0.kubernetes.local node-0 10.200.0.0/24
10.182.70.234 node-1.kubernetes.local node-1 10.200.1.0/24
新建一份文件并追加给虚拟机的 hosts
bash
cat <<'EOF' > hosts.append
10.182.70.218 jumpbox
10.182.70.26 server.kubernetes.local server
10.182.70.221 node-0.kubernetes.local node-0 10.200.0.0/24
10.182.70.234 node-1.kubernetes.local node-1 10.200.1.0/24
EOF
for vm in $(multipass list --format csv | tail -n +2 | cut -d, -f1); do
echo "Processing $vm"
multipass exec "$vm" -- sudo tee -a /etc/hosts < hosts.append > /dev/null
done
# 验证
multipass exec node-0 -- tail -n 10 /etc/hosts
八、SSH 配置
目的是实现 jumpbox 可以 ssh 免密登录到另外3台虚拟机
1. 开启 SSH
在宿主机上操作:
批量配置所有虚拟机 SSH(开启 Root 账号登录)
bash
for vm in $(multipass list --format csv | tail -n +2 | cut -d, -f1); do multipass exec "$vm" -- sudo sed -i 's/^#PermitRootLogin.*/PermitRootLogin yes/' /etc/ssh/sshd_config && systemctl daemon-reload && systemctl restart ssh; done
批量配置 Root 账号密码
bash
# 一句话命令改密码
# echo "root:YourPassword" | sudo chpasswd
# multipass exec node-1 -- bash -c "echo 'root:123' | sudo chpasswd && sudo passwd -u root"
# 批量修改为 "123"
for vm in $(multipass list --format csv | tail -n +2 | cut -d, -f1); do
echo "Processing $vm"
multipass exec "$vm" -- bash -c 'echo "root:123" | sudo chpasswd && sudo passwd -u root && sudo systemctl restart ssh'
done
镜像 config 里面其实配了 root 密码,但似乎没生效,所以重配了
ssh也得重启后才生效
2. 免密登录配置
在宿主机上操作:
拷一份之前创建的 hosts 到 jumpbox
bash
scp /root/kubernetes-the-hard-way/docs/hosts.append root@jumpbox:/root/kubernetes-the-hard-way
在 jumebox 上操作:
创建公钥私钥
bash
ssh-keygen
配置免密
bash
while read IP FQDN HOST SUBNET; do
ssh-copy-id root@${IP}
done < hosts.append
这里可用 ssh 测试另外3节点是不是免密了
再改下 hosts
bash
while read IP FQDN HOST SUBNET; do
ssh -n root@${IP} "echo '127.0.0.1 $FQDN' >> /etc/hosts
done < hosts.append
while read IP FQDN HOST SUBNET; do
ssh -n root@${IP} "echo '127.0.0.1 ${FQDN}' >> /etc/hosts"
done < hosts.append
hosts 前面追加过内容,这里不用改动太多,改完上去查看验证下就行。
hostname 就不改了,对应就是虚拟机名(默认)
且 Kubernetes 中,kubelet 默认使用系统 hostname 作为 Node 名。如果不显式指定 --hostname-override(官方操作这块会有点问题,后面会导致 Node 节点加不上)
总结
没那么多实体机,创建太多 VM 虚拟机也不太好管理(配地址、清理之类的),
测试环境就用 multipass,创建删除管理都方便。
现在前期准备了,下篇会正式开始部署 Kubernetes。