MicroK8s 常用 CLI 命令详解(Ubuntu 22.04)
MicroK8s v1.35.6 常用 CLI 命令详解(Ubuntu 22.04):给出 status/kubectl/add-node/enable/reset 等子命令帮助回显与默认值,附 FAQ,实测验证通过。
MicroK8s 常用 CLI 命令详解(Ubuntu 22.04)
目标:详解 MicroK8s(Canonical 轻量级 Kubernetes 发行版)在 Ubuntu 22.04 上的常用命令与参数,覆盖
status/version/config/inspect/ctr/kubectl/add-node/join/remove-node/leave/enable/disable/start/stop/reset/refresh-certs各子命令的--help回显、真实默认值与独立可执行的示例命令。文中命令与回显均在 Ubuntu 22.04.5 LTS + MicroK8s v1.35.6 实测通过。
一、环境信息
- 操作系统:Ubuntu 22.04.5 LTS(Jammy)/ x86_64(内核 6.8.0-138-generic)
- MicroK8s:v1.35.6 revision 9072(snap 通道 1.35/stable)
- 内置运行时:containerd v2.1.6;数据存储:k8s-dqlite
- Kubernetes:v1.35.6;kubectl v1.35.6(Kustomize v5.7.1)
- 集群:microk8s-u22-main(172.18.0.2,主节点)+ microk8s-u22-worker1(172.18.0.4,worker)
- kubeconfig context:microk8s;示例账号:root(示例密码 q1w2e3r4)
二、命令概览
MicroK8s 命令通过 microk8s <子命令> 调用。可用子命令(microk8s --help 回显):
| 子命令 | 用途 |
|---|---|
add-node | 生成节点加入令牌 |
addons | addon 仓库相关操作 |
config | 输出 kubeconfig |
ctr | 内置 containerd 客户端 |
dashboard-proxy | 转发 dashboard 访问 |
dbctl | 数据存储(dqlite)运维 |
disable | 禁用 addon |
enable | 启用 addon |
helm / helm3 | Helm 包管理 |
images | 列出已加载镜像 |
istioctl / linkerd | 服务网格 CLI |
join | 将本节点加入集群 |
kubectl | 透传 kubectl |
leave | 本节点脱离集群 |
refresh-certs | 刷新集群证书 |
remove-node | 从集群移除节点 |
reset | 重置为初始状态 |
start / stop | 启动 / 停止 Kubernetes 服务 |
status | 集群状态 |
version | 版本查询 |
inspect | 诊断并打包日志 |
microk8s --help 实测回显:
Available subcommands are:
add-node
addons
config
ctr
dashboard-proxy
dbctl
disable
enable
helm
helm3
images
istioctl
join
kubectl
leave
linkerd
refresh-certs
remove-node
reset
start
status
stop
version
inspect
三、前置条件
- 已以 root(或 microk8s 组成员)登录 Ubuntu 22.04 服务器;
- 已完成 MicroK8s 安装(
snap install microk8s --classic --channel=1.35/stable)且集群处于 running 状态; - 命令中的示例均可在单节点或本集群内直接执行。
四、status:集群状态
4.1 帮助回显
microk8s status --help
实测回显:
usage: microk8s status [-h] [--format {pretty,short,yaml}] [-w] [-t TIMEOUT]
[-a ADDON] [--yaml]
Microk8s cluster status check.
options:
-h, --help show this help message and exit
--format {pretty,short,yaml}
print cluster and addon status, output can be in yaml,
pretty or short
-w, --wait-ready wait until the cluster is in ready state
-t TIMEOUT, --timeout TIMEOUT
specify a timeout in seconds when waiting for the
cluster to be ready.
-a ADDON, --addon ADDON
check the status of an addon.
--yaml DEPRECATED, use '--format yaml' instead
4.2 常用参数表
| 参数 | 说明 | 默认值 |
|---|---|---|
-w, --wait-ready | 等待集群进入 ready 后再返回 | 关闭 |
-t, --timeout | 等待就绪的超时秒数 | 无 |
--format | 输出格式,可选 pretty/short/yaml | pretty |
-a, --addon | 单独查看某 addon 状态 | 无 |
4.3 示例
microk8s status --wait-ready --timeout 60
microk8s status --format short
💡
--format short适合脚本判断集群是否 running;--format yaml适合程序化解析。
五、version:版本查询
5.1 示例与回显
microk8s version
实测回显:
MicroK8s v1.35.6 revision 9072
同时查看随附 kubectl 的服务端/客户端版本:
microk8s kubectl version
Client Version: v1.35.6
Kustomize Version: v5.7.1
Server Version: v1.35.6
六、config:kubeconfig
6.1 帮助回显
microk8s config --help
实测回显:
Usage: microk8s config [OPTIONS]
Retrieve the client config, similar to microk8s kubectl config view --raw
Options:
-h, --help Show this help
-l, --use-loopback Report the cluster address using the loopback address
(127.0.0.1) rather than the default interface address
6.2 示例
输出集群 kubeconfig(context 名 microk8s):
microk8s config
导出到文件供普通 kubectl 使用:
microk8s config > /root/.kube/config
使用回环地址(适合本机 kubectl 访问):
microk8s config --use-loopback
⚠️ kubeconfig 含集群证书与客户端私钥,属敏感信息,请勿外泄。
七、inspect:诊断与日志打包
7.1 帮助回显
microk8s inspect --help
实测回显:
This script will inspect your microk8s installation. It will report any issue it finds,
and create a tarball of logs and traces which can be attached to an issue filed against
the microk8s project.
7.2 示例
microk8s inspect
💡 输出的 tar 包默认落在当前目录,包含各服务日志与系统信息,便于问题定位与上报。
八、ctr:内置 containerd 客户端
8.1 帮助回显
microk8s ctr --help
实测回显:
USAGE:
ctr [global options] command [command options]
VERSION:
v2.1.6
COMMANDS:
plugins, plugin Provides information about containerd plugins
version Print the client and server versions
containers, c, container Manage containers
content Manage content
events, event Display containerd events
images, image, i Manage images
leases Manage leases
namespaces, namespace, ns Manage namespaces
pprof Provide golang pprof outputs for containerd
run Run a container
snapshots, snapshot Manage snapshots
tasks, t, task Manage tasks
install Install a new package
oci OCI tools
sandboxes, sandbox, sb, s Manage sandboxes
info Print the server info
deprecations
shim Interact with a shim directly
help, h Shows a list of commands or help for one command
8.2 示例
查看 containerd 客户端/服务端版本:
microk8s ctr version
Client:
Version: v2.1.6
Revision: 9c005f05ff1da3421bbe069e7297e0a98ebe214c
Go version: go1.24.13
Server:
Version: v2.1.6
Revision: 9c005f05ff1da3421bbe069e7297e0a98ebe214c
UUID: 836873df-b532-4c29-8ee4-432f7e41ef70
列出镜像与运行时插件:
microk8s ctr images ls
microk8s ctr plugins ls | grep -iE "cri|runtime"
💡
microk8s ctr已默认使用k8s.io命名空间,无需再传-n k8s.io(重复传会报Cannot use two forms of the same flag: n namespace)。
九、kubectl:透传 Kubernetes 客户端
9.1 示例
MicroK8s 内置 kubectl,通过 microk8s kubectl 调用:
microk8s kubectl get nodes -o wide
microk8s kubectl get pods -A -o wide
microk8s kubectl get svc -A
microk8s kubectl cluster-info
实测回显(节点):
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
microk8s-u22-main Ready <none> 39m v1.35.6 172.18.0.2 <none> Ubuntu 22.04.5 LTS 6.8.0-138-generic containerd://2.1.6
microk8s-u22-worker1 Ready <none> 95s v1.35.6 172.18.0.4 <none> Ubuntu 22.04.5 LTS 6.8.0-138-generic containerd://2.1.6
💡 也可用
microk8s config导出 kubeconfig 后,用系统自带 kubectl 操作集群。
十、集群扩缩:add-node / join / remove-node / leave
10.1 add-node 帮助回显
microk8s add-node --help
实测回显:
usage: microk8s add-node [-h] [--token-ttl TOKEN_TTL] [--token TOKEN]
[--format {short,pretty,token-check,yaml,token,json}]
Produce a connection string for a node to join the cluster.
options:
-h, --help show this help message and exit
--token-ttl TOKEN_TTL, -l TOKEN_TTL
Specify how long the token is valid, before it
expires. Value of "-1" indicates that the token is
usable only once (i.e. after joining a node, the token
becomes invalid)
--token TOKEN, -t TOKEN
Specify the bootstrap token to add, must be 32
characters long. Auto generates when empty.
--format {short,pretty,token-check,yaml,token,json}
Format the output of the token in pretty, short,
token, or token-check
10.2 add-node 示例
microk8s add-node --token-ttl 3600
microk8s add-node --token-ttl 3600 --format short
实测回显(short 格式):
microk8s join 172.18.0.2:25000/881440e22474a23ca4a24e3eabbef56c/2093f48e808a
10.3 join 帮助回显
microk8s join --help
实测回显:
Usage: microk8s join [OPTIONS] CONNECTION
Join the node to a cluster
CONNECTION: the cluster connection endpoint in format
<master>:<port>/<token>
Options:
--worker Join as a worker only node.
--controlplane Join running the control plane on HA clusters.
(default)
--skip-verify Skip the certificate verification of the node we
are joining to. (default: false)
--disable-low-memory-guard Disable the low memory guard. (default: false)
-h, --help Show this message and exit.
10.4 join 示例
在 worker 节点执行(作为纯工作节点加入):
microk8s join 172.18.0.2:25000/e95a5161142fb20ee62d313ea8c7b31b/2093f48e808a --worker
实测回显:
Contacting cluster at 172.18.0.2
The node has joined the cluster and will appear in the nodes list in a few seconds.
Successfully joined the cluster.
💡 不带
--worker时默认以控制面节点加入(用于 HA 集群组成 dqlite 多数派)。
10.5 remove-node 帮助回显与示例
microk8s remove-node --help
实测回显:
Usage: microk8s remove-node [OPTIONS] NODE
Remove a node from the cluster
Options:
--force Force the node removal operation. (default: false)
--help Show this message and exit.
在主节点移除指定节点:
microk8s remove-node microk8s-u22-worker1
强制移除不可达节点:
microk8s remove-node microk8s-u22-worker1 --force
10.6 leave 帮助回显
在未作为集群成员的节点上执行:
microk8s leave --help
实测回显:
This MicroK8s deployment is not acting as a node in a cluster.
💡
microk8s leave用于让本节点主动脱离集群,仅在已加入集群的节点上有效。
十一、addon 管理:enable / disable
11.1 enable 帮助回显
microk8s enable --help
实测回显:
Usage: microk8s enable [OPTIONS] ADDONS...
Enable a MicroK8s addon.
For a list of available addons, run `microk8s status`.
To see help for individual addons, run:
microk8s enable ADDON -- --help
Options:
-h, --help Show this message and exit.
11.2 disable 帮助回显
microk8s disable --help
实测回显:
Usage: microk8s disable [OPTIONS] ADDONS...
Disable one or more MicroK8s addons.
For a list of available addons, run `microk8s status`.
To see help for individual addons, run:
microk8s disable ADDON -- --help
Options:
-h, --help Show this message and exit.
11.3 示例
microk8s enable dashboard
microk8s enable metrics-server
microk8s disable dashboard
查看某 addon 自身帮助:
microk8s enable ingress -- --help
十二、生命周期:start / stop / reset / refresh-certs
12.1 start 帮助回显与示例
microk8s start --help
实测回显:
Usage: microk8s start [OPTIONS]
Start Kubernetes services
Options:
-h, --help Show this help
--disable-low-memory-guard Start MicroK8s in machines with RAM < 512MB
microk8s start
12.2 stop 帮助回显与示例
microk8s stop --help
实测回显:
Usage: microk8s stop [OPTIONS]
Stop Kubernetes services
Options:
-h, --help Show this help
microk8s stop
12.3 reset 帮助回显与示例
microk8s reset --help
实测回显:
Usage: microk8s reset [OPTIONS]
Return the MicroK8s node to the default initial state.
This process may take some time. All addons will be disabled and the
configuration will be reinitialized.
Options:
--destroy-storage Also destroy storage. (default: true)
-h, --help Show this message and exit.
microk8s reset
⚠️
reset会禁用全部 addon 并重新初始化集群;--destroy-storage默认 true 会一并销毁存储数据,请谨慎使用。
12.4 refresh-certs 帮助回显与示例
microk8s refresh-certs --help
实测回显:
Usage: microk8s refresh-certs [OPTIONS] [CA_DIR]
Replace the CA certificates with the ca.crt and ca.key found in CA_DIR.
Omit the CA_DIR argument and use the '--cert' flag to auto-generate a new CA
or any other certificate.
Options:
-c, --check Check the expiration time of the installed certificates
-e, --cert The certificate to be autogenerated, must be one of ['ca.crt', 'server.crt', 'front-proxy-client.crt']
-u, --undo Revert the last refresh performed
-h, --help Show this message and exit.
检查证书到期时间:
microk8s refresh-certs --check
十三、注意事项
- 若命令报权限错误,请以 root 执行或将用户加入
microk8s组后重新登录。 - 节点已加入集群时,请使用
snap start microk8s/snap stop microk8s管理服务,而非microk8s start/microk8s stop。 microk8s ctr已内置k8s.io命名空间,勿重复指定-n。join时 worker 与主节点 MicroK8s 大版本需一致。refresh-certs、reset属高风险操作,执行前务必备份数据与 kubeconfig。
十四、常见问题(FAQ)
Q1:microk8s status 报 is not running?
执行 microk8s start(集群成员节点用 snap start microk8s),再 microk8s status --wait-ready 等待就绪。
Q2:microk8s ctr images ls 需要 -n k8s.io 吗?
不需要。MicroK8s 的 ctr 包装已默认使用 k8s.io 命名空间,重复传参会报 Cannot use two forms of the same flag: n namespace。
Q3:如何快速判断集群是否 ready(脚本用)?
microk8s status --format short 输出简洁状态;或在脚本中执行 microk8s status --wait-ready --timeout 60 并检查返回码。
Q4:如何查看某 addon 的可用参数?
microk8s enable <addon> -- --help,例如 microk8s enable ingress -- --help。
Q5:如何把节点从集群移除?
在主节点执行 microk8s remove-node <节点名>(不可达时加 --force);或在目标节点本地执行 microk8s leave。
十五、参考链接
- MicroK8s 官方命令参考:https://microk8s.io/docs/commands
- MicroK8s 集群扩展:https://microk8s.io/docs/clustering
- MicroK8s addon:https://microk8s.io/docs/addons