如何通过 OrbStack 在 Apple Silicon 平台搭建 K3s x86 集群

公众号关注 「奇妙的 Linux 世界」

设为「星标」,每天带你玩转 Linux !

119b5c69091fd4c8bd8787458d556456.png

如果你对 Apple Silicon、OrbStack、x86 架构或 K3s 集群中的任何一个概念不感兴趣,那么这篇文章可能不适合你。

背景

在我的工作流中,我依赖于 M1 MacBook Pro 和多个高效工具的结合。我使用 OrbStack[1] 创建虚拟机,这个平台通过 Rosetta 进行二进制转换以实现虚拟化[2],不仅虚拟化速度快,还有其他众多优点(更多详情请见 OrbStack 优势[3])。同时,我偏爱使用 K3s[4] 集群因为其轻量级特性,搭配 先前介绍的 k3sup 工具,能够迅速搭建新的集群。

虽然这一系列工具的组合主打速度快和轻便,但当它们结合使用时,也会出现一些技术挑战。例如,在 OrbStack 创建的 x86 虚拟机上安装 K3s 时,我遇到了一个常见的问题,这个问题在社区中已经有了广泛的讨论并看似无解,具体可参见这个 GitHub 讨论[5]

Failed to create pod sandbox: rpc error: code = Unknown desc = failed to generate sanbdox container spec options: failed to generate seccomp spec opts: seccomp is not supported

在我的虚拟环境中,尽管使用 Docker 运行容器没有遇到问题,但在安装 K3s 时却遭遇挑战。值得注意的是,无论是 Docker 还是 K3s,它们都依赖于 Containerd[6] 作为容器运行时。这一点让我怀疑问题可能出在 K3s 内置的 Containerd 配置上。

一个潜在的解决方案是让 K3s 使用 Docker 作为容器运行时。幸运的是,K3s 官方文档中提到,通过在集群创建时添加 --docker 参数,可以实现这一配置,具体说明见 K3s 文档[7]

接下来,我们将测试这一方案,以验证它是否能解决在 x86 虚拟机上安装 K3s 遇到的问题。

验证

安装虚拟机

首先是安装 OrbStack。

brew install orbstack

通过命令行创建一个 x86 的 Ubuntu 22.04 虚拟机。

orb create -a amd64 ubuntu:jammy ubuntu

当然你也可以通过 OrbStack 的 GUI 来操作。

通过命令 ssh ubuntu@orb 可以 ssh 到虚拟机。

安装 Docker

ssh 到虚拟机,在虚拟机上安装 Docker。

开始之前需要安装 gpg

sudo apt update
sudo apt install -y gpg

参考 Docker 官方文档[8] 安装,也可以使用下面的命令。

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
  
sudo apt update
sudo apt -y install docker-ce docker-ce-cli containerd.io
echo ""
newgrp docker
sudo usermod -aG docker $USER

检查 Docker 能否正常工作,从结果来看使用了 amd64 平台的镜像。

docker run --rm hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

容器运行时准备好后,就可以安装 k3s 了。

安装 K3s 集群

安装 k3s 的最新版,禁用 traefiklocal-storage metrics-server 和 servicelb,达到极致的轻量级。当然,别忘记 --docker 参数。

curl -sfL https://get.k3s.io | sh -s - --disable traefik --disable local-storage --disable metrics-server --disable servicelb --write-kubeconfig-mode 644 --write-kubeconfig ~/.kube/config --docker

可以通过之前分享的 k3sup 一键安装脚本[9] 来安装:./setupk3s.sh --docker --mini

--mini 表示禁用一切可以禁用的组件

检查集群的运行情况。

kubectl version
Client Version: v1.29.3+k3s1
Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3
Server Version: v1.29.3+k3s1

kubectl get po -A
NAMESPACE     NAME                      READY   STATUS    RESTARTS   AGE
kube-system   coredns-6799fbcd5-mf5vl   1/1     Running   0          28s

通过查看节点,可以看到使用的是 Docker 容器运行时。

kubectl get no -o wide
NAME      STATUS   ROLES                  AGE    VERSION        INTERNAL-IP      EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION                        CONTAINER-RUNTIME
ubuntu2   Ready    control-plane,master   4m7s   v1.29.3+k3s1   198.19.249.154   <none>        Ubuntu 22.04.4 LTS   6.7.11-orbstack-00143-ge6b82e26cd22   docker://26.1.0

最后记得删除虚拟机。

orb delete ubuntu

参考资料

[1] 

OrbStack: https://orbstack.dev/

[2] 

Rosetta 进行二进制转换以实现虚拟化: https://docs.orbstack.dev/docker/#intel-x86-emulation

[3] 

OrbStack 优势: https://docs.orbstack.dev/#why-orbstack

[4] 

K3s: https://k3s.io/

[5] 

GitHub 讨论: https://github.com/orbstack/orbstack/issues/465

[6] 

Containerd: https://containerd.io/

[7] 

K3s 文档: https://docs.k3s.io/advanced#using-docker-as-the-container-runtime

[8] 

Docker 官方文档: https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository

[9] 

k3sup 一键安装脚本: https://gist.github.com/addozhang/92905325746b7858e3d06117d6b9d0b8#file-setupk3s-sh

本文转载自:「云原生指北」,原文:https://url.hi-linux.com/UBJNk,版权归原作者所有。欢迎投稿,投稿邮箱: editor@hi-linux.com。

39ce88c8f3b66429b3742bbba27c5b0c.gif

🚀 最近,我们建立了一个技术交流微信群。目前群里已加入了不少行业内的大神,有兴趣的同学可以加入和我们一起交流技术,在 「奇妙的 Linux 世界」 公众号直接回复 「加群」 邀请你入群。

📕 关注『奇妙的 Linux 世界』公众号,带你开启有趣新生活!更多好用好玩的软件资源,可访问 https://666666.dev 免费获取。

c7ff0dab737e0067687e57effa1ead0f.png

你可能还喜欢

点击下方图片即可阅读

79474556d0866c4b3a8cad537e013844.png

k8gb: 云原生最佳开源 GSLB 方案

7ad74dbd5416a259c7e933bbe5683002.png
点击上方图片,『美团|饿了么』外卖红包天天免费领

c760714344935ce14d2fc89e2a01da11.png

更多有趣的互联网新鲜事,关注「奇妙的互联网」视频号全了解!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值