Colima配置
Colima使用了 YAML 格式的配置文件:
~/.colima/_templates目录下有一个default.yaml配置文件是所有配置的起点,也就是当创建实例时会从这个模版文件复制出来进行定制~/.colima/default/colima.yaml是实例配置文件,修订这个配置并保存,Colima会自动重启虚拟机以应用新的硬件规格,之前在虚拟机中安装的二进制文件、容器和本地镜像都会完好无损地保留下来
当使用 colima start 启动了虚拟机,就会形成 ~/.colima/default/colima.yaml 实例配置,此时需要修订该配置来完成修改。修改模版 ~/.colima/_template/default.yaml 只会影响之后创建的虚拟机。
备注
~/.colima/_templates/default.yaml 中有详细注释,所以可以比较清晰找到对应设置。本文仅选择部分关注点进行记录和实践
参数解析
VM规格调整
在 Colima快速起步 实践中,根据本地host主机硬件可以调整CPU和Memory的规格,由于 MacBook Pro 15" Late 2013 硬件有限,所以我调整为 2c4g 规格
修改RunTime
默认 容器运行时(Container Runtimes) 是 Docker ,但是考虑到轻量级和 Kubernetes 平滑一致,我将默认runtime修订为containerd
修改默认OS(diskImage)
Colima底层是 Lima: Linux Machines ,完整继承了Lima对多发行版的支持(底层实际上是采用了 Lima模版和配置 ),可以通过命令行参数或配置文件,将虚拟机替换为各种主流Linux发行版。
检查 ~/.colima/_templates/default.yaml 可以看到如下配置,其配置方法应该是沿用 Lima模版和配置 :
# Specify a custom disk image for the virtual machine.
# When not specified, Colima downloads an appropriate disk image from Github at
# https://github.com/abiosoft/colima-core/releases.
# The file path to a custom disk image can be specified to override the behaviour.
#
# Default: ""
# 这里我参考 https://github.com/lima-vm/lima/blob/master/templates/_images/debian-13.yaml
diskImage: "https://cloud.debian.org/images/cloud/trixie/20260413-2447/debian-13-genericcloud-amd64-20260413-2447.qcow2"
根据注释可以看到默认是从github获取Ubntu的镜像,所以参考 Lima模版和配置 应该可以指定其他Linux发行版。后续我准备改成采用 Debian 来运行Colima
设置主机名
hostname 变量可以设置模版默认的主机名,如没有设置默认是 colima
启用Kubernetes
提供了一个启用 K3s - 轻量级Kubernetes 运行kubernetes模拟集群,方便快速开发和测试,并且提供了一些可调整设置,其中包含了可以指定绑定网卡接口(对于多网卡)
Network
Network 段落设置提供了调整 macOS 环境(Linux忽略)的 shared 或 bridged 模式
vm类型
vmType 默认使用 QEMU ,对于macOS 13以上可以指定 vz 类型,性能更好
mountType 对于 vz 虚拟机默认使用 virtiofs ,对于 qemu 虚拟机默认使用 sshfs 。需要注意 virtiofs 对依赖的vz必须是Apple Silicon硬件,所以我在 MacBook Pro 15" Late 2013 只能使用qemu,所以要提升IO性能,改为采用 9p ,这是比sshfs性能更好的qemu内置IO支持
备注
9p 协议(九号计划文件系统协议):它是 QEMU 原生支持的进程间共享内存文件系统。由于它直接在 QEMU 的内存映射(Memory Mapping)中进行数据交换,省去了 sshfs 极其消耗老 CPU 的 SSH 握手和加解密损耗。对于老款 Intel 芯片来说,它的 IO 响应速度和吞吐量显著好于 sshfs 。
9p 的缺点是在极端高并发的大文件频繁读写时(比如上千个小文件的并发编译),可能会偶发文件指针锁死。但对于日常的全栈基础开发,它的性能体感相比 sshfs 好很多。
警告
由于 mountType(卷挂载驱动)在 Colima 的底层逻辑中属于一旦创建就无法热修改的元数据。直接执行 colima start 是不会生效的,所以需要在修订 ~/.colima/_template/default.yaml 之后,通过以下命令重建VM生效:
9pcolima delete
colima start
上述操作会销毁虚拟机导致所有数据丢失,所以务必在操作前备份数据 特别是已经花费了大量时间的 Colima镜像 :
# 备份已经创建的容器镜像
sudo nerdctl save -o debian-ssh-tini.tar debian-ssh-tini:latest
sudo nerdctl save -o debian-dev.tar debian-dev:latest
# 等Colima VM重建以后,再通过以下命令恢复镜像
sudo nerdctl load -i debian-ssh-tini.tar
sudo nerdctl load -i debian-dev.tar
最终配置
default.yaml# Number of CPUs to be allocated to the virtual machine.
# Default: 2
cpu: 2
# Size of the disk in GiB to be allocated to the virtual machine for container data.
# NOTE: value can only be increased after virtual machine has been created.
#
# Default: 100
disk: 100
# Size of the memory in GiB to be allocated to the virtual machine.
# Default: 2
memory: 4
# Architecture of the virtual machine (x86_64, aarch64, host).
#
# NOTE: value cannot be changed after virtual machine is created.
# Default: host
arch: host
# Container runtime to be used (docker, containerd).
#
# NOTE: value cannot be changed after virtual machine is created.
# Default: docker
runtime: containerd
...
# Virtual Machine type (krunkit, qemu, vz)
# NOTE: this is macOS 13 only. For Linux and macOS <13.0, qemu is always used.
#
# vz is macOS virtualization framework and requires macOS 13.
# krunkit runs super‑light VMs on macOS/ARM64 with a focus on GPU access. It is experimental.
#
# NOTE: value cannot be changed after virtual machine is created.
# Default: qemu
# 只有Apple Silicon才能够使用vz
vmType: qemu
...
# Volume mount driver for the virtual machine (virtiofs, 9p, sshfs).
#
# virtiofs is limited to macOS and vmType `vz`. It is the fastest of the options.
#
# 9p is the recommended and the most stable option for vmType `qemu`.
#
# sshfs is faster than 9p but the least reliable of the options (when there are lots
# of concurrent reads or writes).
#
# NOTE: value cannot be changed after virtual machine is created.
# Default: virtiofs (for vz), sshfs (for qemu)
# 只有Apple silicon才能够使用vz并支持virtiofs
mountType: 9p
...
# Specify a custom disk image for the virtual machine.
# When not specified, Colima downloads an appropriate disk image from Github at
# https://github.com/abiosoft/colima-core/releases.
# The file path to a custom disk image can be specified to override the behaviour.
#
# Default: ""
diskImage: "https://cloud.debian.org/images/cloud/trixie/20260413-2447/debian-13-genericcloud-amd64-20260413-2447.qcow2"
...
# [start:env-proxy]
env:
# curl / git 能够完美识别 http_proxy、https_proxy 或 ALL_PROXY,优先级是小写变量最高
# ALL_PROXY 是一个全协议兜底变量 它不仅管 HTTP(S),还管 ftp://、git://、甚至是内部未明确分类的其他 TCP 流量
http_proxy: socks5h://127.0.0.1:1080
https_proxy: socks5h://127.0.0.1:1080
all_proxy: socks5h://127.0.0.1:1080
no_proxy: localhost,127.0.0.1,192.168.0.0/16,10.0.0.0/8,*.baidu.com
# 大写变量兜底防止某些应用忽略大小写
HTTP_PROXY: socks5h://127.0.0.1:1080
HTTPS_PROXY: socks5h://127.0.0.1:1080
ALL_PROXY: socks5h://127.0.0.1:1080
NO_PROXY: localhost,127.0.0.1,192.168.0.0/16,10.0.0.0/8,*.baidu.com
# [end:env-proxy]
备注
每次修订 ~/.colima/_template/default.yaml ,只有再次创建虚拟机才会读取这个默认模版,之前已经创建的vm不受影响。所以如果要修订已经创建的虚拟机,需要执行删除再启动:
colima delete
colima start
创建容器运行环境
备注
注意在国内访问github存在障碍,需要配置 Colima 代理
修订
~/.colima/default/colima.yaml添加socks代理
env:
# 也可以使用 ALL_PROXY 进行设置
HTTP_PROXY: socks5h://127.0.0.1:1080
HTTPS_PROXY: socks5h://127.0.0.1:1080
NO_PROXY: localhost,127.0.0.1,*.baidu.com,192.168.0.0/16,10.0.0.0/8
警告
实践发现,在 ~/.colima/default/colima.yaml 配置 env 使用 ALL_PROXY 对于colima自身下载镜像没有作用,需要配置 http_proxy 和 https_proxy 才能生效。看来底层是采用 curl ,其优先级是小写的 http_proxy 和 https_proxy 优先级最高
执行启动:
colima start
启动不带任何参数,colima就会从 ~/.colima/_tempalate/default.yaml 读取配置并形成 ~/.colima/default/colima.yaml 配置。例如上文设置了模版中的代理配置,也会带入到默认配置中