MacOS使用Multipass快速搭建轻量级k3s集群

发布于:2025-07-14 ⋅ 阅读:(21) ⋅ 点赞:(0)

使用Homebrew安装Multipass(macOS系统)

brew install multipass --cask

创建一个名为k3s的Multipass虚拟机,分配2G内存、10G硬盘和1个CPU核心

multipass launch --name k3s --mem 2G --disk 10G --cpus 1

进入虚拟机的shell环境

multipass shell k3s

使用国内镜像源安装k3s(Rancher中国镜像站)

curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh -

停止k3s服务以便修改配置

sudo systemctl stop k3s

确保进入当前用户home目录

cd

创建.kube配置目录

mkdir .kube

复制k3s配置文件到用户目录

sudo cp /etc/rancher/k3s/k3s.yaml .kube/config

将配置文件所有权改为当前用户

sudo chown (id−u):(id -u):(idu):(id -g) .kube/config

查看k3s服务状态(主要是为了找到服务配置文件路径)

systemctl status k3s

编辑k3s服务配置文件

sudo nano /etc/systemd/system/k3s.service

在ExecStart行末尾追加以下参数(注意转义符和空格):

–write-kubeconfig=/home/ubuntu/.kube/config \
–write-kubeconfig-mode=644 \

重新加载systemd配置

sudo systemctl daemon-reload

启动k3s服务

sudo systemctl start k3s

验证节点状态

kubectl get node

配置镜像仓库源

cat > /etc/rancher/k3s/registries.yaml <<EOF
mirrors:
docker.io:
endpoint:
- “https://registry.cn-hangzhou.aliyuncs.com/”
quay.io:
endpoint:
- “https://quay.tencentcloudcr.com/”
registry.k8s.io:
endpoint:
- “https://registry.aliyuncs.com/v2/google_containers”
gcr.io:
endpoint:
- “https://gcr.m.daocloud.io/”
k8s.gcr.io:
endpoint:
- “https://registry.aliyuncs.com/google_containers”
ghcr.io:
endpoint:
- “https://ghcr.m.daocloud.io/”
EOF

重新启动k3s服务

sudo systemctl restart k3s

停止虚拟机实例

multipass stop k3s

启动虚拟机实例

multipass start k3s

删除虚拟机实例

multipass delete -p k3s

查看虚拟机实例列表

multipass list

参考

https://zhuanlan.zhihu.com/p/499810063
https://juejin.cn/post/7189945206085517367


网站公告

今日签到

点亮在社区的每一天
去签到