schedule 命令全解析:定时备份的创建、查询与底层实现)
VeleroArkschedule 命令全解析定时备份的创建、查询与底层实现【免费下载链接】veleroBackup and migrate Kubernetes applications and their persistent volumes项目地址: https://gitcode.com/GitHub_Trending/ve/veleroark schedule是 Velero 前身 Heptio Arkv0.7.0 时代命令行工具名为ark即当前仓库 site/content/docs/v0.7.0/cli-reference/ark_schedule.md 所记录的版本中用于管理**定时备份Schedule**的 CLI 命令族。通过本文你将掌握schedule命令的全局选项与四个核心子命令create/get/describe/delete的完整用法、cron 表达式与every语法的编写规则并从当前仓库源码层面理解 Schedule 是如何被创建、持久化并最终由控制器触发为真实 Backup 的。命令总览ark schedule在 CLI 体系中的位置ark schedule的定位是Work with schedules即围绕 Schedule 这一自定义资源提供一组操作子命令。在 v0.7.0 的 CLI 参考文档中该命令没有独立的行为参数仅提供-h, --help帮助选项真正的能力全部收敛到它的子命令中-h, --help help for schedule从文档的 SEE ALSO 一节可以看到完整的命令树ark — 顶层命令Back up and restore Kubernetes cluster resources.ark schedule create — 创建一个定时备份ark schedule delete — 删除一个定时备份ark schedule describe — 查看定时备份的详细信息ark schedule get — 列出定时备份这一命令树的组织方式与当前仓库源码完全一致在 pkg/cmd/cli/schedule/schedule.go 中NewCommand通过 Cobra 将create、get、describe、delete注册为子命令。从源码结构看该命令族在后续版本中还扩展出了pause暂停与unpause恢复两个子命令以满足临时停用定时任务而不删除的运维场景。全局选项所有 schedule 子命令共享的父级参数ark schedule及其全部子命令都继承一组来自父命令的全局参数主要用于控制日志输出与 Kubernetes 集群访问方式--alsologtostderr log to standard error as well as files --kubeconfig string Path to the kubeconfig file to use to talk to the Kubernetes apiserver. If unset, try the environment variable KUBECONFIG, as well as in-cluster configuration --log_backtrace_at traceLocation when logging hits line file:N, emit a stack trace (default :0) --log_dir string If non-empty, write log files in this directory --logtostderr log to standard error instead of files -n, --namespace string The namespace in which Ark should operate (default heptio-ark) --stderrthreshold severity logs at or above this threshold go to stderr (default 2) -v, --v Level log level for V logs --vmodule moduleSpec comma-separated list of patternN settings for file-filtered logging其中几个参数值得重点说明--kubeconfig指定用于连接 Kubernetes apiserver 的 kubeconfig 文件路径。若不指定CLI 会依次尝试环境变量KUBECONFIG与集群内in-cluster配置。这与 pkg/client 中基于 controller-runtime 构建客户端时的配置解析逻辑相对应。-n, --namespaceArk 操作所用的命名空间默认值为heptio-ark。所有 Schedule 资源都会创建在这个命名空间下详见下文源码分析。-v, --v与--vmodule继承自 Kubernetes 社区通用的 glog 风格日志体系--v设置整体日志级别--vmodule按patternN格式对指定文件做细粒度日志过滤。--logtostderr/--alsologtostderr/--log_dir/--log_backtrace_at/--stderrthreshold控制日志的输出目标文件或标准错误、回溯信息与错误阈值适合排查 CLI 与集群交互异常时使用。核心实操ark schedule create创建定时备份是 schedule 命令族中最常用的操作其语法为ark schedule create NAME [flags]NAME是必填参数要求恰好一个位置参数对应源码中cobra.ExactArgs(1)的校验--schedule标志必填——在 pkg/cmd/cli/schedule/create.go 的Validate中若--schedule为空会直接返回--schedule is required错误。支持的调度表达式cron 与every--schedule接受两种表达式。第一种是标准 cron 表达式共 5 个字段时区为 UTC字段位置字段含义可取值1分钟Minute0-59, *2小时Hour0-23, *3日Day of Month1-31, *4月Month1-12, *5星期Day of Week0-6, *第二种是every duration语法duration 可由秒s、分m、小时h自由组合例如every 2h30m。该说明同样完整保留在 create.go 的 Long 描述中。完整的创建参数表参数说明默认值--schedule string指定该备份的 cron 表达式无必填--include-namespaces stringArray要纳入备份的命名空间*表示全部*--exclude-namespaces stringArray要从备份中排除的命名空间无--include-resources stringArray要纳入备份的资源格式为resource.group如storageclasses.storage.k8s.io*表示全部资源无--exclude-resources stringArray要从备份中排除的资源格式同上无--include-cluster-resources optionalBool[true]是否包含集群级资源true--snapshot-volumes optionalBool[true]是否对 PersistentVolume 创建快照true-l, --selector labelSelector仅备份匹配该标签选择器的资源none--labels mapStringString应用到备份对象上的标签无--label-columns stringArray指定作为表格展示列的标签列表逗号分隔无--show-labels在最后一列展示标签false--ttl duration备份在垃圾回收前可保留的时长720h0m0s30 天-o, --output string输出格式对 create 命令仅展示对象而不真正发送到服务端合法值为table、json、yaml无其中optionalBool类型的三态布尔true/false/unset与--ttl默认 30 天这两个细节在源码中分别体现为BackupOptions的三态指针字段与metav1.Duration类型见 create.go 中 Run 方法对 ScheduleSpec 的组装。--ttl到期后备份会被 Velero 的垃圾回收控制器清理这是控制存储成本的关键参数。实战示例结合源码中的Example段以下是可直接落地的典型用法# 每 6 小时备份一次cron 表达式UTC ark schedule create NAME --schedule0 */6 * * * # 等价写法使用 every 语法 ark schedule create NAME --scheduleevery 6h # 每天备份 web 命名空间 ark schedule create NAME --scheduleevery 24h --include-namespaces web # 每周备份一次且每个备份保留 90 天2160 小时 ark schedule create NAME --scheduleevery 168h --ttl 2160h0m0s # 只查看生成的 Schedule 对象JSON 格式不真正提交到集群 ark schedule create NAME --scheduleevery 6h -o json命令执行成功后CLI 会输出Schedule NAME created successfully.。底层Schedule 是怎么被持久化的从 create.go 的 Run 方法可以看到ark schedule create的本质是构造一个api.Schedule对象并调用crClient.Create写入 Kubernetes。该对象的关键字段包括Spec.Schedulecron 或every表达式Spec.Template一个完整的BackupSpec模板--include-namespaces、--snapshot-volumes、--ttl等备份参数全部沉淀在这里——每次触发时控制器都会以该模板为准创建一次 BackupObjectMeta.Namespace取自f.Namespace()即父级-n/--namespace参数默认heptio-ark。Schedule 对应的 CRD 定义位于 config/crd/v1/bases/velero.io_schedules.yaml这也是kubectl get schedules等原生 Kubernetes 操作可以查看它的原因。查看与检索ark schedule getark schedule get用于列出当前命名空间下的所有定时备份语法为ark schedule get [flags]其选项如下-h, --help help for get --label-columns stringArray a comma-separated list of labels to be displayed as columns -o, --output string Output display format. For create commands, display the object but do not send it to the server. Valid formats are table, json, and yaml. (default table) -l, --selector string only show items matching this label selector --show-labels show labels in the last column与create不同get的默认输出格式是table便于日常巡检。从 pkg/cmd/cli/schedule/get.go 的实现看当带名字参数时它会逐个Get指定 Schedule不带参数时则通过labels.Parse解析--selector并List全部 Schedule。无论哪种路径最终都会经由output.PrintWithFormat以 table/json/yaml 格式渲染。# 以表格形式列出全部 schedule ark schedule get # 按标签选择器过滤 ark schedule get -l appweb # 以 JSON 格式输出原始对象 ark schedule get -o json # 额外展示 app 标签列 ark schedule get --label-columns app --show-labels深度查看ark schedule describeark schedule describe用于查看单个或多个 schedule 的详细描述信息语法支持一次传入多个名字ark schedule describe [NAME1] [NAME2] [NAME...] [flags]选项仅有两个-h, --help help for describe -l, --selector string only show items matching this label selector该命令底层通过output.DescribeSchedule生成人类可读的详细描述见 pkg/cmd/cli/schedule/describe.go多个 schedule 之间以空行分隔输出。相比get的表格视图describe会展示调度表达式、TTL、包含/排除规则等完整配置是排查为什么备份没按预期触发时的第一选择。# 查看单个 schedule 详情 ark schedule describe daily-web-backup # 同时查看多个 ark schedule describe daily-web-backup weekly-db-backup清理与下线ark schedule deleteark schedule delete用于删除一个已存在的定时备份语法为ark schedule delete NAME [flags]它同样只提供-h, --help选项。需要注意删除 schedule 只会移除定时触发规则不会自动删除其已经触发生成的 Backup 对象若要连同历史备份一起清理需要配合ark backup delete等备份删除命令这一点在规划备份留存策略时务必留意。# 删除名为 daily-web-backup 的定时备份 ark schedule delete daily-web-backup触发机制从 Schedule 到 Backup 的控制器流水线CLI 只负责 Schedule 对象的读写真正让定时备份生效的是服务端的 schedule 控制器。当前仓库中对应实现为 pkg/controller/schedule_controller.go其核心逻辑可以总结为事件过滤与暂停处理控制器的SetupWithManager通过 predicate 过滤事件——当schedule.Spec.Paused为 true 时直接跳过该 schedule对应 schedule_controller.go 的事件谓词这正是后续版本引入pause/unpause子命令的语义基础。周期同步控制器每scheduleSyncPeriod1 分钟轮询一次解析 cron 表达式并判断是否到达触发时刻源码中scheduleSyncPeriod time.Minute。触发即创建 Backup到达触发点时控制器基于Schedule.Spec.Template即 create 命令写入的 BackupSpec 模板实例化一个 Backup 对象交由 backup 控制器执行实际的资源备份与卷快照工作。因此一条完整的定时备份链路是ark schedule create写入 Schedule CR→ schedule 控制器解析 cron、到点触发→ 依据 Template 创建 Backup CR → backup 控制器执行备份。理解这条链路后即使get/describe显示 schedule 状态正常若 Backup 未产生排查方向也应回到控制器日志与集群时钟上。版本演进说明本页 CLI 参考文档属于 v0.7.0 版本彼时工具名为arkHeptio Ark默认命名空间为heptio-ark。项目后续更名为 Velero命令由ark变为velero默认命名空间也相应调整从当前仓库源码看schedule 命令族 已在此基础上扩展了pause、unpause子命令并支持--use-owner-references-in-backup、--paused等新参数见 create.go 的 BindFlags。在阅读旧版本文档或迁移到新版 Velero 时只需将ark替换为velero其余核心语义保持一致。【免费下载链接】veleroBackup and migrate Kubernetes applications and their persistent volumes项目地址: https://gitcode.com/GitHub_Trending/ve/velero创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考