尧图网站设计 尧图网站设计YAOTU DESIGN
ARTICLE DETAIL

资讯详情

深耕网站设计与一线实操的经验洞察。

Ark backup create 命令完全指南:Kubernetes 备份创建参数详解与源码级解析

Ark backup create 命令完全指南:Kubernetes 备份创建参数详解与源码级解析 Ark backup create 命令完全指南Kubernetes 备份创建参数详解与源码级解析【免费下载链接】veleroBackup and migrate Kubernetes applications and their persistent volumes项目地址: https://gitcode.com/GitHub_Trending/ve/velero本篇技术指南以 Heptio ArkVelero 的前身v0.7.0 文档中的ark backup create命令参考为核心系统讲解创建 Kubernetes 备份的完整命令语法、全部筛选/快照/存储参数、标签选择器用法与输出格式并结合当前仓库源码pkg/cmd/cli/backup/create.go与单元测试深入剖析命令的参数校验、对象构建与执行流程。读完本文你将能熟练使用该命令完成全量备份、命名空间/资源筛选备份、按标签备份、不拍快照的文件级备份以及基于调度模板的备份并理解每个参数在底层如何影响最终的 Backup 自定义资源对象。命令概览从 Ark 到 Velero 的备份入口ark backup create是 Ark 命令行客户端中创建备份的核心命令。ArkHeptio Ark是 Velero 的前身在 v0.7.0 时代命令名为ark默认工作命名空间为heptio-ark当前仓库已将项目更名为 Velero命令相应变为velero backup create。尽管名字变化命令结构、参数体系与底层实现一脉相承这正是本文以 v0.7.0 文档为主体、以当前源码为佐证的原因。命令的完整语法为ark backup create NAME [flags]在源码中该命令由 pkg/cmd/cli/backup/backup.go 注册NewCommand将create作为backup的子命令挂载同时挂载的还有get、logs、describe、download、delete等命令。而 create.go 中的NewCreateCommand定义了命令的Use为create NAME、Short描述为 Create a backup并声明了命令的校验规则与示例用法# Create a backup containing all resources. velero backup create backup1 # Create a backup including only the nginx namespace. velero backup create nginx-backup --include-namespaces nginx # Create a backup excluding the velero and default namespaces. velero backup create backup2 --exclude-namespaces velero,default # Create a backup based on a schedule named daily-backup. velero backup create --from-schedule daily-backup # View the YAML for a backup that doesnt snapshot volumes, without sending it to the server. velero backup create backup3 --snapshot-volumesfalse -o yaml # Wait for a backup to complete before returning from the command. velero backup create backup4 --wait其中 Create a backup containing all resources 即 v0.7.0 文档快速开始中的典型用法参见 site/content/docs/v0.7.0/_index.md。命令参数详解Optionsark backup create的参数可分为内容筛选快照与存储标签与选择器输出与展示生命周期五类v0.7.0 文档完整列出的参数如下--exclude-namespaces stringArray namespaces to exclude from the backup --exclude-resources stringArray resources to exclude from the backup, formatted as resource.group, such as storageclasses.storage.k8s.io -h, --help help for create --include-cluster-resources optionalBool[true] include cluster-scoped resources in the backup --include-namespaces stringArray namespaces to include in the backup (use * for all namespaces) (default *) --include-resources stringArray resources to include in the backup, formatted as resource.group, such as storageclasses.storage.k8s.io (use * for all resources) --label-columns stringArray a comma-separated list of labels to be displayed as columns --labels mapStringString labels to apply to the backup -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. -l, --selector labelSelector only back up resources matching this label selector (default none) --show-labels show labels in the last column --snapshot-volumes optionalBool[true] take snapshots of PersistentVolumes as part of the backup --ttl duration how long before the backup can be garbage collected (default 720h0m0s)内容筛选命名空间与资源--include-namespaces stringArray指定要包含进备份的命名空间使用*表示所有命名空间默认值为*。参数可重复传入例如--include-namespaces nginx --include-namespaces default也可用逗号分隔--include-namespaces nginx,default在 create_test.go 中即用app1,app2验证了逗号分隔解析。对应源码NewCreateOptions中以flag.NewStringArray(*)初始化。--exclude-namespaces stringArray指定要从备份中排除的命名空间与 include 相反。--include-resources stringArray指定要包含的资源类型格式为resource.group例如storageclasses.storage.k8s.io使用*表示所有资源。这是 Kubernetes 的 Group/Version/Resource 命名体系的体现可精确到 API 分组。--exclude-resources stringArray指定要排除的资源类型格式同上。从源码看create.go 的Validate方法会调用collections.ValidateNamespaceIncludesExcludes校验 include 与 exclude 命名空间列表不能同时出现同一命名空间而 include/exclude 资源列表的参数帮助文本明确说明include-resources、exclude-resources与include-cluster-resources属于旧版筛选参数不能与include-cluster-scoped-resources、exclude-cluster-scoped-resources、include-namespace-scoped-resources、exclude-namespace-scoped-resources这一组新版筛选参数混用源码通过oldAndNewFilterParametersUsedTogether检测并返回错误create_test.go 中的TestCreateCommand完整覆盖了这一校验。快照与生命周期--snapshot-volumes optionalBool[true]是否在备份时对 PersistentVolume 打快照默认 true。该参数是optionalBool类型——只需写--snapshot-volumes即等价于--snapshot-volumestrue源码中通过f.NoOptDefVal cmd.TRUE实现见 create.go。设--snapshot-volumesfalse可跳过卷快照仅备份 Kubernetes 资源清单配合-o yaml常用于预览。--include-cluster-resources optionalBool[true]是否包含集群级别cluster-scoped资源默认 true。同样支持裸写即 true的简写方式。--ttl duration备份在被垃圾回收GC前可保留的时长默认 720h0m0s30 天。这一默认值与服务器端配置一致在 pkg/cmd/server/config/config.go 中defaultBackupTTL 30 * 24 * time.Hour服务器通过--default-backup-ttl标志可调整。备份创建后TTL 决定status.expiration时间过期备份由 pkg/controller/backup_controller.go 结合垃圾回收逻辑删除。标签与选择器--labels mapStringString为 Backup 对象附加一组标签格式为key1value1,key2value2。这些标签会写入 Backup 自定义资源的metadata.labels便于用--label-columns/--show-labels展示或后续筛选。对应源码flag.NewMap()类型的Labels字段。-l, --selector labelSelector只备份匹配该标签选择器的资源默认none即不筛选。它最终被解析为 Kubernetes 标准的LabelSelector写入 Backup 的spec.labelSelector。快速开始文档中的经典用法ark backup create nginx-backup --selector appnginx即只备份打了appnginx标签的对象见 site/content/docs/v0.7.0/_index.md。输出与展示-o, --output string输出显示格式。对 create 命令而言仅展示对象而不会真正发送到服务器dry-run 语义可选table、json、yaml。这是预览备份配置的利器例如ark backup create backup3 --snapshot-volumesfalse -o yaml可直接看到即将创建的 Backup 对象完整 YAML。--label-columns stringArray将指定标签以逗号分隔列表的形式显示为表格列便于在ark backup get场景中查看关键标签。--show-labels在最后一列显示标签。继承自父命令的参数Options inherited from parent commands所有ark子命令共享以下继承参数用于控制客户端与 Kubernetes API Server 的连接及日志行为--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 stringkubeconfig 文件路径若未指定则依次尝试环境变量KUBECONFIG与集群内in-cluster配置。-n, --namespace stringArk 的工作命名空间默认heptio-ark。Backup 等自定义资源均在此命名空间内创建与查询在当前的 Velero 版本中该默认值已演进为velero。源码视角参数如何落到 Backup 对象上ark backup create的执行链路在 create.go 中由三个环节构成Run中的cmd.CheckError(o.Complete(...))→o.Validate(...)→o.Run(...)Complete从命令行参数中取出备份名o.Name args[0]并建立与 API Server 的KubebuilderWatchClient连接。Validate依次校验输出格式标志、selector与or-selector不能同时指定、from-schedule非空、备份名符合 DNS-1123 子域名规范validation.IsDNS1123Subdomain、命名空间 include/exclude 不冲突、新旧筛选参数不混用。若指定了--storage-location或--volume-snapshot-locations还会预先从集群中读取对应的 BackupStorageLocation / VolumeSnapshotLocation 对象不存在则直接报错测试用例TestCreateCommand验证了 backupstoragelocations.velero.io not found 的错误路径。BuildBackup Run将选项组装为velerov1api.Backup对象见 pkg/apis/velero 的 API 定义并通过output.PrintWithFormat决定是否只打印不发送随后调用o.client.Create提交到集群打印Backup request xxx submitted successfully.。BuildBackup是参数落地的核心create.go 将IncludeNamespaces、ExcludeNamespaces、IncludeResources、ExcludeResources、LabelSelector、TTL、StorageLocation、VolumeSnapshotLocations、SnapshotVolumes、IncludeClusterResources等逐一映射到BackupSpec对应字段。测试 create_test.go 的TestCreateOptions_BuildBackup精确断言了映射结果例如labels写入metadata.labels、ttl写入spec.ttl、选择器写入spec.labelSelector。此外命令还支持--wait-w参数提交后启动 SharedInformer 监听同名 Backup 对象的状态变化直到进入Completed、PartiallyFailed、Failed或FailedValidation终态才返回并提示用velero backup describe/velero backup logs进一步排查。备份产物理解 create 之后发生什么ark backup create提交的 Backup 对象最终由服务器端控制器执行产物是一个 gzip 压缩的 tar 包文件名与 Backup 资源的metadata.name一致。对象存储中每个备份位于独立子目录并额外包含一个ark-backup.json清单文件完整记录 Backup 资源信息含默认值与status.version输出格式版本号目录结构示意rootBucket/ backup1234/ ark-backup.json backup1234.tar.gz解压后的备份目录按资源类型与命名空间组织resources/ persistentvolumes/ cluster/ pv01.json ... configmaps/ namespaces/ namespace1/ myconfigmap.json ...以上细节详见 site/content/docs/v0.7.0/output-file-format.md。理解产物结构有助于在ark backup create之后用ark backup describe、ark backup logs、ark backup download等命令见 site/content/docs/v0.7.0/cli-reference/ark_backup.md进行校验与排障。实战从全量备份到精细化筛选结合文档与源码给出几条可直接落地的典型用法# 1. 全量备份所有命名空间、所有资源、含 PV 快照TTL 30 天 ark backup create full-backup # 2. 只备份 nginx 命名空间其余命名空间自动排除 ark backup create nginx-backup --include-namespaces nginx # 3. 排除多个命名空间 ark backup create backup2 --exclude-namespaces velero,default # 4. 按资源类型筛选只备份 Deployment 与 Service格式 resource.group ark backup create app-backup --include-resources deployments.apps,services # 5. 按标签选择器备份只备份打了 appnginx 标签的对象 ark backup create nginx-backup --selector appnginx # 6. 不拍卷快照、仅预览对象 YAMLdry-run不提交到服务器 ark backup create backup3 --snapshot-volumesfalse -o yaml # 7. 附加自定义标签并延长保留时间 ark backup create weekly-backup --labels envprod,ownerteam-a --ttl 168h # 8. 基于既有调度模板创建一次性备份所有其他筛选参数被忽略 ark backup create --from-schedule daily-backup # 9. 提交后阻塞等待备份完成 ark backup create backup4 --wait注意第 8 种用法当指定--from-schedule时备份规格完全继承调度的模板其他筛选参数被忽略源码会打印 Creating backup from schedule, all other filters are ignored.若此时不提供备份名将自动生成调度名 14 位时间戳形式的名称create.go 中调用schedule.TimestampedName校验逻辑还会确保调度名长度不超过 238 字符以为时间戳后缀留出空间见TestCreateOptions_Validate。常见校验错误与排查从源码Validate与测试用例可归纳出高频报错便于自查场景报错说明未给备份名且未用--from-schedulea backup name is required, unless you are creating based on a schedule备份名必填基于调度时除外备份名非法invalid backup name xxx: ...必须符合 DNS-1123 子域名规范不能含大写、下划线等新旧筛选参数混用include-resources, exclude-resources and include-cluster-resources are old filter parameters...两组资源筛选参数二选一存储位置不存在backupstoragelocations.velero.io xxx not found--storage-location指定的 BSL 必须在集群中存在快照位置不存在volumesnapshotlocations.velero.io xxx not found--volume-snapshot-locations指定的 VSL 必须存在--from-schedule为空值flag must have a non-empty value: --from-schedule标志已声明但值为空含纯空格测试TestCreateOptions_ValidateFromScheduleFlag覆盖相关命令导航ark backup create隶属于ark backup命令组相关文档位于 site/content/docs/v0.7.0/cli-reference/可继续查阅ark backup备份命令组的入口create/delete/describe/download/get/logsark backup describe查看备份详情与状态ark backup get列出备份并配合--label-columns/--show-labels展示标签ark backup logs获取备份执行日志ark restore create基于备份执行恢复site/content/docs/v0.7.0/config-definition.md理解备份存储与持久卷提供者的底层配置site/content/docs/v0.7.0/output-file-format.md理解备份产物在对象存储中的组织方式【免费下载链接】veleroBackup and migrate Kubernetes applications and their persistent volumes项目地址: https://gitcode.com/GitHub_Trending/ve/velero创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表