文章目录1 先在主机上安装 VirtaulBox 并创建一个 RockyLinux10 的虚拟机2 虚拟机 rockylinux10-1 上的准备——第一部分2.1 设置 /etc/hosts2.2 设置 hotstname 主机名2.3 关闭防火墙2.4 关闭 selinux2.5 关闭 swap2.6 VirtualBox 生成个快照 rockylinux10-init3 虚拟机 rockylinux10-1 上的准备——第二部分3.1 安装 docker3.2 设置时间同步3.3 VirtualBox 生成个快照 rockylinux10-installed-docker4 虚拟机 rockylinux10-1 上安装 k8s4.1 设置 k8s 的 repo 源4.2 安装 kubelet, kubeadm 和 kubectl4.3 设置 docker 安装后 自带的 containerd4.3.1 编辑配置文件4.3.2 重启 containerd 服务4.3.3 与 containerd 相关一些命令4.4 初始化 rockylinux10-1 节点4.4.1 先创建 /root/kubeadm-config.yaml 文件4.4.2 手动拉取阿里云的 pause 镜像并打上官方标签4.4.3 执行初始化命令4.4.4 k8s 初始化失败❗️如何清理现场方便再次尝试❓4.4.5 k8s 初始化成功✌️4.5 VirtualBox 生成个快照 rockylinux10-k8s-init-success感谢阅读1 先在主机上安装 VirtaulBox 并创建一个 RockyLinux10 的虚拟机可以参考我的另一篇文章 《VirtualBox安装RockyLinux并使用ssh访问》。这篇文章虽然是 Windows 10 上的 VirtualBox但是 Win11、Linux 和 Mac 上的 VirtualBox 同理另外它虽然是安装 RockyLinux 9.2 的 dvd iso但是 RockyLinux 10 也同理。⚠️需要注意的是《VirtualBox安装RockyLinux并使用ssh访问》里面我给虚拟机分了 8G 内存、4核CPU、60GB磁盘但是本文我们不得不分得少一些。因为我们需要3个虚拟机但主机资源有限不能给每个虚拟机太高的配置。所以我们创建虚拟机的时候分 4GB 内存、2核CPU、50GB磁盘就行。✅我们可以参考《VirtualBox安装RockyLinux并使用ssh访问》操作只要确保做到如下几点在主机上安装好了 VirtualBox 最新版下载好了 RockyLinux 10 最新版本目前最新是 10.2 的 dvd.iso给新建的虚拟机命名为rockylinux-10-1给新建的虚拟机配置好了 4GB 内存、2核CPU、50GB磁盘不预先分配全部空间给虚拟机配置双网卡网络地址转换(NAT)仅主机(Host-Only)网络⚠️注意‼️不要勾选它的DHCP服务因为后面我们要设置静态IP)启动虚拟机安装好 RockyLinux 10给虚拟机设置好端口转发 ssh 22Rocky Linux 防火墙放行 22 端口Rocky Linux 安装 sship addr 查看虚拟机有没有 192.168.56.xxx 这样的 ip如果没有的话就 使用nmtui配置网卡记得设置静态 IP如下图。设置好之后尝试在主机上ping 192.168.56.101要保证能 ping 通在虚拟机中尝试ping www.baidu.com要保证能 ping 通。期间有什么问题可以问 DeepSeek用阿里的源替换 RockyLinux 的默认源2 虚拟机 rockylinux10-1 上的准备——第一部分2.1 设置 /etc/hostsvim/etc/hosts# 在文件末尾添加如下配置192.168.56.101 rockylinux10-1192.168.56.102 rockylinux10-2192.168.56.103 rockylinux10-32.2 设置 hotstname 主机名hostnamectl set-hostname rockylinux10-12.3 关闭防火墙systemctl stop firewalld systemctl disable firewalld做了这一步其实前面安装 RockyLinux 时做的防火墙放行 22 端口VirtualBox 做端口映射就没什么用了可以去除掉。2.4 关闭 selinuxsetenforce0sed-is/SELINUXenforcing/SELINUXdisabled/g/etc/selinux/config2.5 关闭 swapswapoff-ayes|cp/etc/fstab /etc/fstab_bakvim/etc/fstab#注释与 swap 有关的一行比如#UUIDb154ad02-c85b-4301-a28a-7074e24c39e6 none swap defaults 0 02.6 VirtualBox 生成个快照 rockylinux10-init快照的名称是rockylinux10-init描述如下rockylinux 10 内存4G CPU2个 50GB磁盘 配置好了阿里的repo源 配置好了双网卡hostOnly 和 NAT 设置好了静态ip 192.168.56.101/24 设置好了网关 192.168.56.1 主机可以访问虚拟机 虚拟机可以访问外网 设置好了 /etc/hosts 设置好了 hotstname 主机名为 rockylinux10-1 关闭了防火墙 firewalld 关闭了 selinux 关闭了 swap3 虚拟机 rockylinux10-1 上的准备——第二部分3.1 安装 docker按照 https://docs.docker.com/engine/install/rhel/ 官网的说明分别执行以下命令# 1 Set up the repositorydnf-yinstalldnf-plugins-core dnf config-manager --add-repo https://download.docker.com/linux/rhel/docker-ce.repo# 2 Install Docker Enginednfinstalldocker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin# 3 Start Docker Enginesystemctlenable--nowdocker然后设置/etc/docker/daemon.json设置 cgroup 的 driver 是 systemd设置 dockerhub 的国内镜像https://docker.m.daocloud.io{exec-opts:[native.cgroupdriversystemd],registry-mirrors:[https://docker.m.daocloud.io]}3.2 设置时间同步# 查看服务状态应该是 active 的状态并且开机自启systemctl status chronyd# 一般 chronyd 是已安装的并且是开机自动启动的如果没有安装则安装它dnfinstallchrony# 安装之后设置开机自动启动systemctlenable--nowchronyd# 查看详细的同步信息关注 System time 和 Last offset 等字段chronyc tracking# 查看当前可用的NTP时间源chronyc sources-v#### 使用国内的 ntp 服务器vim/etc/chrony.conf#### 在文件中找到 pool 或 server 开头的行这些是当前使用的 NTP 源。注释掉在行首加 #它们然后添加国内常用的 NTP 服务器。如下# pool 2.rocky.pool.ntp.org iburst# 使用国内 NTP 服务器server ntp.aliyun.com iburst server ntp.tencent.com iburst server ntp.ntsc.ac.cn iburst#### 其他配置不变#### 重启 chronyd 服务并检查状态systemctl restart chronyd chronyc tracking chronyc sources-v#### 验证时间同步确保输出 System clock synchronized: yestimedatectl3.3 VirtualBox 生成个快照 rockylinux10-installed-docker快照的名称是rockylinux10-installed-docker描述如下rockylinux 10 安装了 docker 并且自启动 配置了 /etc/docker/daemon.json 设置了时间同步 chronyd 用国内 ntp4 虚拟机 rockylinux10-1 上安装 k8s4.1 设置 k8s 的 repo 源使用华为的源注意我这里的 k8s 版本是 v1.36catEOF|sudotee/etc/yum.repos.d/kubernetes.repo[kubernetes] nameKubernetes baseurlhttps://mirrors.huaweicloud.com/kubernetes-new/core:/stable:/v1.36/rpm/ enabled1 gpgcheck1 gpgkeyhttps://mirrors.huaweicloud.com/kubernetes-new/core:/stable:/v1.36/rpm/repodata/repomd.xml.key excludekubelet kubeadm kubectl cri-tools kubernetes-cni EOF为什么不用阿里的源原因是阿里的 k8s repo 源https://mirrors.aliyun.com/kubernetes-new/内容没有华为的全。当然RockyLinux 的 repo 源也可以用华为的命令如下也可以还继续用阿里的sed-es|^mirrorlist|#mirrorlist|g\-es|^#baseurlhttp://dl.rockylinux.org/$contentdir|baseurlhttps://mirrors.huaweicloud.com/rockylinux/|g\-i.bak\/etc/yum.repos.d/rocky*.repo# 然后执行dnf makecache4.2 安装 kubelet, kubeadm 和 kubectl通过dnf --version命令来看自己的 linux 是用的 DNF 还是 DNF5如下很明显是 DNF 4的版本也就是 DNF。[rootrockylinux10-1 ~]# dnf --version4.20.0 Installed: dnf-0:4.20.0-22.el10_2.rocky.0.1.noarch at Mon27Jul202609:54:53 AM GMT Built:Rocky Linux Build Systemrelengrockylinux.orgat Tue19May202602:06:23 PM GMT Installed: rpm-0:4.19.1.1-23.el10.aarch64 at Mon27Jul202609:54:53 AM GMT Built:Rocky Linux Build Systemrelengrockylinux.orgat Sat 07 Feb202602:50:24 PM GMT根据 k8s 官方文档 https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/ 开始安装命令如下# For systems with DNF:# 我们是 DNF4 也就是 DNF所以执行下面的命令dnfinstall-ykubelet kubeadm kubectl--disableexcludeskubernetes# For systems with DNF5:# 我们是 DNF4 不是5所以不用下面的命令dnfinstall-ykubelet kubeadm kubectl--setoptdisable_excludeskubernetes4.3 设置 docker 安装后 自带的 containerd4.3.1 编辑配置文件vim/etc/containerd/config.toml找到disabled_plugins [cri]这一行修改成下面的样子也就是把 cri 删除掉# disabled_plugins [cri]disabled_plugins[]然后在文件末尾添加以下完整内容:# 启用 CRI 插件如果存在 disabled_plugins确保没有 cri[plugins.io.containerd.grpc.v1.cri]# 启用 CRI 服务[plugins.io.containerd.grpc.v1.cri.containerd]# 默认使用 runc[plugins.io.containerd.grpc.v1.cri.containerd.runtimes][plugins.io.containerd.grpc.v1.cri.containerd.runtimes.runc]runtime_typeio.containerd.runc.v2[plugins.io.containerd.grpc.v1.cri.containerd.runtimes.runc.options]SystemdCgrouptrue# 关键与 kubelet 的 cgroup 驱动保持一致4.3.2 重启 containerd 服务systemctl restart containerd# 查看版本号验证重启是否成功ctr-nk8s.io version4.3.3 与 containerd 相关一些命令# 查看 containerd 版本号ctr-nk8s.io version# 查看 k8s 里面有哪些镜像ctr-nk8s.io images list# k8s 拉取镜像ctr-nk8s.io images pull xxx-image:xxx-tag# k8s 给镜像打 tagctr-nk8s.io images tag source-image:aaa-tag target-image:bbb-tag# 查看 k8s 里面有哪些正在运行的容器ctr-nk8s.io containers list4.4 初始化 rockylinux10-1 节点4.4.1 先创建 /root/kubeadm-config.yaml 文件内容如下apiVersion:kubeadm.k8s.io/v1beta3kind:InitConfigurationlocalAPIEndpoint:advertiseAddress:192.168.56.101bindPort:6443---apiVersion:kubeadm.k8s.io/v1beta3kind:ClusterConfigurationkubernetesVersion:v1.36.3# 用阿里云的镜像imageRepository:registry.aliyuncs.com/google_containers# 对应 --service-cidrserviceSubnet:10.1.0.0/16# 对应 --pod-network-cidrpodSubnet:10.244.0.0/16---apiVersion:kubelet.config.k8s.io/v1beta1kind:KubeletConfiguration# 统一 cgroup 驱动与 containerd 的 systemd 保持一致cgroupDriver:systemd# 关键指定 pause 镜像地址阿里云镜像源中 pause 的版本可能为 3.10podSandboxImage:registry.aliyuncs.com/google_containers/pause:3.104.4.2 手动拉取阿里云的 pause 镜像并打上官方标签为什么要手动拉取因为上面的 kubeadm 一直要从 k8s 官方源 registry.k8s.io 拉取镜像 pause:3.10.1即便是配置了上面的kubeadm-config.yaml还是要访问 registry.k8s.io。但是国内访问不了 registry.k8s.io所以只能手动拉取然后打 tag命令如下# 拉取阿里云上的 pause版本 3.10与 3.10.1 基本一致sudoctr-nk8s.io images pull registry.aliyuncs.com/google_containers/pause:3.10# 给这个镜像打上 kubelet 期望的标签这样 kubelet 拉取时会直接命中本地镜像sudoctr-nk8s.io images tag registry.aliyuncs.com/google_containers/pause:3.10 registry.k8s.io/pause:3.10.1# 验证镜像已存在ctr-nk8s.io images list|greppause# 应该有一行是 registry.k8s.io/pause:3.10.14.4.3 执行初始化命令kubeadm init--configkubeadm-config.yaml4.4.4 k8s 初始化失败❗️如何清理现场方便再次尝试❓kubeadm reset-f\rm-rf/etc/kubernetes/\rm-rf/var/lib/kubelet/\systemctl restart containerd\systemctl restart kubelet4.4.5 k8s 初始化成功✌️可以看到如下结果Your Kubernetes control-plane has initialized successfully!To start using your cluster, you need to run the following as a regular user:mkdir-p$HOME/.kubesudocp-i/etc/kubernetes/admin.conf$HOME/.kube/configsudochown$(id-u):$(id-g)$HOME/.kube/config Alternatively,ifyou are the root user, you can run:exportKUBECONFIG/etc/kubernetes/admin.conf You should now deploy a pod network to the cluster. Runkubectl apply -f [podnetwork].yamlwith one of the options listed at: https://kubernetes.io/docs/concepts/cluster-administration/addons/ Then you canjoinany number of worker nodes by running the following on each as root: kubeadmjoin192.168.56.101:6443--tokensa8kol.9i8mk2zjxkxlwhnr\--discovery-token-ca-cert-hash sha256:3ea305e20b3bbabe2bccf3a0de7ce4b56a9aabb5150051a9631821614a9fad9f4.5 VirtualBox 生成个快照 rockylinux10-k8s-init-success快照的名称是rockylinux10-k8s-init-success描述如下rockylinux 10 设置了 k8s 华为 repo 源 安装了 kubelet, kubeadm 和 kubectl 设置了 containerd 的 /etc/containerd/config.toml 使用 kubeadm 初始化成功感谢阅读