Argo CD `argocd app sync` 命令详解:按应用、源与资源粒度选择性同步

发布时间:2026/11/9 9:09:54

Argo CD `argocd app sync` 命令详解:按应用、源与资源粒度选择性同步 Argo CDargocd app sync命令详解按应用、源与资源粒度选择性同步【免费下载链接】argo-cdDeclarative Continuous Deployment for Kubernetes项目地址: https://gitcode.com/GitHub_Trending/ar/argo-cd本文以 Argo CD 官方命令参考 argocd_app_sync.md 为核心完整覆盖argocd app sync的全部选项与用法示例并结合 CLI 实现源码 NewApplicationSyncCommand 深入解析其参数校验、资源过滤、同步策略与本地同步等底层行为。读完后你将能够正确选择要同步的应用按名称、标签选择器或项目、对多源应用指定特定源与修订版本、按GROUP:KIND:NAME精确圈定同步资源、并通过重试、策略与预览类标志控制同步全过程。命令概述argocd app sync将应用同步到其目标状态target state。它支持三种互斥的应用选择方式argocd app sync [APPNAME... | -l selector | --project project-name] [flags]位置参数APPNAME...可以传入一个或多个应用名逐个同步-l selector按标签选择应用支持、、!、in、notin、存在与不存在判断匹配的应用必须同时满足所有标签约束--project同步属于指定项目的应用可重复指定。从源码的参数校验逻辑cmd/argocd/commands/app.go#L1769-L1801可以看到三条硬性规则必须提供应用名、-l selector或--project三者之一否则打印帮助并以退出码 1 终止位置参数与-l selector不能混用Cannot use selector option when application name(s) passed as argument(s)--revisions、--source-positions、--source-names要求恰好传入1 个应用名且--source-positions与--source-names二者只能指定其一--revisions与它们的取值个数必须一一对应--source-positions的计数从 1 开始、不允许 0 或负数。若通过 selector 或 project 查询后没有任何匹配应用命令会以No matching apps found for filter:报错终止与argocd app list静默返回空列表不同同步属于破坏性操作未匹配到目标时必须显式失败见 cmd/argocd/commands/app.go#L1835-L1845。官方示例全集以下是命令参考中给出的全部示例可直接复制运行# Sync an app argocd app sync my-app # Sync multiples apps argocd app sync my-app other-app # Sync apps by label, in this example we sync apps that are children of another app (aka app-of-apps) argocd app sync -l app.kubernetes.io/instancemy-app argocd app sync -l app.kubernetes.io/instance!my-app argocd app sync -l app.kubernetes.io/instance argocd app sync -l !app.kubernetes.io/instance argocd app sync -l app.kubernetes.io/instance notin (my-app,other-app) # Sync a multi-source application for specific revision of specific sources argocd app sync my-app --revisions 0.0.1 --source-positions 1 --revisions 0.0.2 --source-positions 2 argocd app sync my-app --revisions 0.0.1 --source-names my-chart --revisions 0.0.2 --source-names my-values # Sync a specific resource # Resource should be formatted as GROUP:KIND:NAME. If no GROUP is specified then :KIND:NAME argocd app sync my-app --resource :Service:my-service argocd app sync my-app --resource argoproj.io:Rollout:my-rollout argocd app sync my-app --resource !apps:Deployment:my-service argocd app sync my-app --resource apps:Deployment:my-service --resource :Service:my-service argocd app sync my-app --resource !*:Service:* # Specify namespace if the application has resources with the same name in different namespaces argocd app sync my-app --resource argoproj.io:Rollout:my-namespace/my-rollout完整选项清单以下为参考文档列出的全部命令级选项-N, --app-namespace string Only sync an application in namespace --apply-out-of-sync-only Sync only out-of-sync resources --assumeYes Assume yes as answer for all user queries or prompts --async Do not wait for application to sync before continuing --dry-run Preview apply without affecting cluster --force Use a force apply -h, --help help for sync --ignore-normalizer-jq-execution-timeout duration Set ignore normalizer JQ execution timeout (default 1s) --info stringArray A list of key-value pairs during sync process. These infos will be persisted in app. --label stringArray Sync only specific resources with a label. This option may be specified repeatedly. --local string Path to a local directory. When this flag is present no git queries will be made --local-repo-root string Path to the repository root. Used together with --local allows setting the repository root (default /) -o, --output string Output format. One of: json|yaml|wide|tree|treedetailed (default wide) --preview-changes Preview difference against the target and live state before syncing app and wait for user confirmation --project stringArray Sync apps that belong to the specified projects. This option may be specified repeatedly. --prune Allow deleting unexpected resources --replace Use a kubectl create/replace instead apply --resource stringArray Sync only specific resources as GROUP:KIND:NAME or !GROUP:KIND:NAME. Fields may be blank and * can be used. This option may be specified repeatedly --retry-backoff-duration duration Retry backoff base duration. Input needs to be a duration (e.g. 2m, 1h) (default 5s) --retry-backoff-factor int Factor multiplies the base duration after each failed retry (default 2) --retry-backoff-max-duration duration Max retry backoff duration. Input needs to be a duration (e.g. 2m, 1h) (default 3m0s) --retry-limit int Max number of allowed sync retries --retry-refresh Indicates if the latest revision should be used on retry instead of the initial one --revision string Sync to a specific revision. Preserves parameter overrides --revisions stringArray Show manifests at specific revisions for source position in source-positions -l, --selector string Sync apps that match this label. Supports , , !, in, notin, exists not exists. Matching apps must satisfy all of the specified label constraints. --server-side Use server-side apply while syncing the application --server-side-diff-concurrency int Max concurrent batches for server-side diff. -1 unlimited, 1 sequential, 2 concurrent (0 invalid) (default -1) --server-side-diff-max-batch-kb int Max batch size in KB for server-side diff. Smaller values are safer for proxies (default 250) --source-names stringArray List of source names. Default is an empty array. --source-positions int64Slice List of source positions. Default is empty array. Counting start at 1. (default []) --strategy string Sync strategy (one of: apply|hook) --timeout uint Time out after this many seconds下面按功能域分组详解各选项的用途与源码中的落点。应用选择标签选择器与项目过滤-l/--selector走的是ApplicationQuery.Selector的服务端过滤cmd/argocd/commands/app.go#L1827-L1850因此可以一次同步成百上千个应用。典型场景是同步 app-of-apps应用的子应用集合子应用由 Application of Applications 自动打上app.kubernetes.io/instance父应用名标签用argocd app sync -l app.kubernetes.io/instancemy-app即可批量同步。--project可重复传入多个项目名与-N/--app-namespace组合可把同步范围限定到某个 Argo CD 实例命名空间内的指定项目。注意-N还会影响位置参数中省略命名空间时的解析未带/的应用名会被自动补全为app-namespace/appnamecmd/argocd/commands/app.go#L1873-L1878。多源应用--revisions/--source-positions/--source-names多源multi-source应用由 Helm 与 Kustomization/目录等多个 source 拼装而成。此时不能再用--revision源码中显式拦截对多源应用使用--revision会报argocd cli does not work on multi-source app with --revision flag. Use --revisions and --source-positions instead.对多源应用使用--local同样被拒绝cmd/argocd/commands/app.go#L1924-L1934。--revisions与--source-positions从 1 计数的源位置数组或--source-names源名称数组经 getSourceNameToPositionMap 换算成位置一一配对表示「为指定源取该修订版本下的清单」。这组参数最终写入ApplicationSyncRequest.Revisions与ApplicationSyncRequest.SourcePositionscmd/argocd/commands/app.go#L1980-L1992由仓库服务器按源生成对应清单。资源级同步--resource与--label--resource按GROUP:KIND:NAME圈定要同步的资源三个字段均可留空并使用*通配前缀!表示排除。解析逻辑在 parseSelectedResources省略 GROUP 时写作:KIND:NAME如:Service:my-service省略 KIND/NAME 时留空即可因此*:Service:*表示全部 Service当应用内同名资源分布在多个命名空间时NAME 字段可写为NAMESPACE/NAME如argoproj.io:Rollout:my-namespace/my-rollout源码中通过resourceFieldNamespaceDelimiter /拆分命名空间与名称可重复传入多个--resource实现「包含某些、排除某些」的组合圈定。解析后的过滤器与应用的Status.Resources做交集filterAppResources 遍历应用已知资源用argo.IncludeResource判断是否命中至少一个过滤器排除式条目用于从命中集中剔除。若最终没有任何资源匹配--resource/--label命令会直接报No matching app resources found for resource filter而不会发起空同步cmd/argocd/commands/app.go#L1936-L1942。--label则是另一条更直观的资源筛选路径先向 API Server 请求该应用的 manifestsGetManifests逐个解析为对象凡是对象 label 与给定keyvalue匹配的资源被自动转换为GROUP:KIND:NAME追加到--resource过滤器中cmd/argocd/commands/app.go#L1880-L1913。找不到匹配资源时同样报错退出。典型用途只同步同一部署中打了tierfrontend标签的那部分资源。同步策略与执行行为--strategy决定同步采用哪种策略源码中的映射为cmd/argocd/commands/app.go#L1994-L2003applykubectl apply 风格的原地应用--force透传为SyncStrategyApply.Forcehook或留空默认执行 Argo CD 生命周期 Hook--force透传为SyncStrategyHook.Force传入其他值会报Unknown sync strategy。--replace、--server-side、--apply-out-of-sync-only三个布尔标志会被打包进SyncOptions.Items字符串化选项列表随同步请求下发见 syncOptionsFactory--replace以 kubectl create/replace 语义代替 apply--server-side同步时启用 server-side apply--apply-out-of-sync-only只同步处于 out-of-sync 状态的资源已同步的资源不再触发 apply可减少无谓的变更扰动。其余行为控制标志--prune允许删除「意外资源」目标中已不存在的资源。同步完成后 CLI 还会检查是否需要剪枝若操作成功但最终状态非 Synced 且存在待剪枝资源会以N resources require pruning提示终止cmd/argocd/commands/app.go#L2048-L2057--dry-run预览 apply 而不影响集群--async发起同步后立即返回不等待结果不加该标志时 CLI 会调用waitOnApplicationStatus阻塞等待操作完成--timeout控制等待上限单位秒--revision同步到指定修订版本保留参数覆盖仅适用于单源应用--infokeyvalue形式的键值对列表经 getInfos 解析后持久化到应用的同步操作信息中可用于在操作历史里标注「本次同步的原因」-o/--output输出格式支持json|yaml|wide|tree|treedetailed默认wide。重试策略--retry-limit与退避参数只要--retry-limit非 0CLI 就会把整组退避参数组装为RetryStrategy随请求下发cmd/argocd/commands/app.go#L2004-L2014标志含义默认值--retry-limit允许的最大同步重试次数0不启用重试--retry-backoff-duration退避基础时长5s--retry-backoff-factor每次失败后乘以基础时长的系数2--retry-backoff-max-duration退避时长上限3m--retry-refresh重试时是否刷新到最新修订版本而非沿用初始版本false默认值对应argoappv1.DefaultSyncRetryDuration、DefaultSyncRetryFactor、DefaultSyncRetryMaxDuration三个 API 默认常量cmd/argocd/commands/app.go#L2070-L2074。注意--retry-limit与其余退避标志是组合生效的不设置--retry-limit时退避参数不会随请求发送。本地同步--local与--local-repo-root--local指定一个本地目录路径CLI 直接在本地生成清单不做任何 git 查询配合--local-repo-root默认/设置仓库根路径。这条路径主要服务于离线/本地开发场景。源码中有两个明确限制cmd/argocd/commands/app.go#L1945-L1957应用若启用了自动同步策略Automated Sync则禁止本地同步除非同时加--dry-run否则报Cannot use local sync when Automatic Sync Policy is enabled except with --dry-run多源应用不支持--local见上文多源一节。同步前预览--preview-changes与--assumeYes--preview-changes会在真正同步之前调用ManagedResources拉取应用的实时与期望状态并打印差异Previewing differences between live and desired state of application ...若未发现差异打印No Differences found后直接返回不发起同步若发现差异会交互式询问Do you want to continue the sync process? (y/n)回答n则以退出码 0 结束加上--assumeYes可跳过所有交互确认适合 CI 管道等无人值守场景。该流程实现于 cmd/argocd/commands/app.go#L2015-L2040。另外若应用带有ServerSideDifftrue比较选项注解预览会自动切换到 server-side diff 路径--server-side-diff-concurrency默认-1即不限制并发批次与--server-side-diff-max-batch-kb默认250KB值越小对代理转发越安全用于控制这一过程的并发与批大小--ignore-normalizer-jq-execution-timeout默认1s则控制忽略差异 JQ 归一化脚本的执行超时。继承自父命令的通用选项argocd app sync继承 argocd 根命令的全部客户端连接选项摘录如下完整列表见参考文档--argocd-context string The name of the Argo-CD server context to use --auth-token string Authentication token; set this or the ARGOCD_AUTH_TOKEN environment variable --client-crt string Client certificate file --client-crt-key string Client certificate key file --config string Path to Argo CD config (default /home/user/.config/argocd/config) --controller-name string Name of the Argo CD Application controller (default argocd-application-controller) --core If set to true then CLI talks directly to Kubernetes instead of talking to Argo CD API server --grpc-web Enables gRPC-web protocol. Useful if Argo CD server is behind proxy which does not support HTTP2. --grpc-web-root-path string Enables gRPC-web protocol. Set web root. -H, --header strings Sets additional header to all requests made by Argo CD CLI. --http-retry-max int Maximum number of retries to establish http connection to Argo CD server --insecure Skip server certificate and domain verification --kube-context string Directs the command to the given kube-context --logformat string Set the logging format. One of: json|text (default json) --loglevel string Set the logging level. One of: debug|info|warn|error (default info) --plaintext Disable TLS --port-forward Connect to a random argocd-server port using port forwarding --port-forward-namespace string Namespace name which should be used for port forwarding --prompts-enabled Force optional interactive prompts to be enabled or disabled, overriding local configuration. --redis-compress string Enable this if the application controller is configured with redis compression enabled. (default gzip) --redis-haproxy-name string Name of the Redis HA Proxy (default argocd-redis-ha-haproxy) --redis-name string Name of the Redis deployment (default argocd-redis) --repo-server-name string Name of the Argo CD Repo server (default argocd-repo-server) --server string Argo CD server address --server-crt string Server certificate file --server-name string Name of the Argo CD API server (default argocd-server)这些选项决定 CLI 如何连接到 Argo CD API Server--server、TLS 证书、gRPC-web、端口转发等在执行任何同步操作前应先确认登录态与服务器地址配置正确。小结与相关命令选应用位置参数 /-l selector/--project三选一selector 与位置参数互斥选源多源应用用--revisions配对--source-positions或--source-names选资源--resourceGROUP:KIND:NAME支持!排除、*通配、NAMESPACE/NAME与--label自动转换为资源过滤器控行为--strategy、--force、--replace、--server-side、--apply-out-of-sync-only、--prune、--dry-run、--async控韧性--retry-limit 三个退避参数 --retry-refresh控确认--preview-changes预览差异 --assumeYes跳过交互。相关命令可参考同一目录下的 argocd app管理应用的父命令以及argocd_app_wait.md、argocd_app_rollback.md、argocd_app_diff.md等姊妹命令文档。本文所有源码路径均基于当前仓库行号对应 cmd/argocd/commands/app.go 的实现版本命令行为可能随版本演进实际使用时请以argocd app sync --help输出为准。【免费下载链接】argo-cdDeclarative Continuous Deployment for Kubernetes项目地址: https://gitcode.com/GitHub_Trending/ar/argo-cd创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
延伸阅读

更多相关文章

2026/11/9 9:09:21

电工证有几级呀网上查询怎么避坑全攻略

电工证有几级呀网上查询怎么避坑全攻略 不知道去哪报名怕被中介坑?电工证有几级呀?网上查询怎么选正规机构?作为干了10年特种作业培训的我,见过太多人因为选错证、报错班、被中介忽悠,最后花了冤枉钱还拿不到证。今天我就从济源劳务班组负责人的角度,…

2026/11/9 9:09:21

高压电工作业运行证怎么转异地?真实经历告诉你怎么应对

高压电工作业运行证怎么转异地?真实经历告诉你怎么应对 工地太忙根本没时间复习考试,这是我去年在开封某工地做电工时的真实经历。当时手里已经拿到了低压电工证,但项目部突然要求持高压电工作业运行证上岗,我一下傻眼了,不仅没接触过高压作业,连考试题…

2026/11/9 9:09:21

电工证测量培训机构怎么选 通过率揭秘

电工证测量培训机构怎么选 通过率揭秘 工地太忙根本没时间复习考试,很多人只能靠培训机构“包过”,但市场上培训机构良莠不齐, 通过率揭秘 就成了大家最关心的问题。尤其是在 电工证测量…

2026/11/9 9:09:21

新津区电工证考点实操考试心里没底怕挂科?保姆级教程来了

新津区电工证考点实操考试心里没底怕挂科?保姆级教程来了 实操考试心里没底怕挂科,这是大多数特种作业考试学员的真实写照。特别是在新津区电工证考点,很多学员因为缺乏实战经验,面对实际操作设备时容易手忙脚乱。如果你正在为电工证考试发愁,那就跟着这…

2026/11/9 9:04:20

高压电工证考多少题全国通用吗怎么转过来

高压电工证考多少题全国通用吗怎么转过来 之前考的证在外省能不能转过来?别急,你不是一个人。高压电工证全国通用,但跨省转证需要走流程,今天就帮你把这事说清楚。 ### 高压电工证考多少题?考试内容全解析…

2026/11/9 0:37:55

电工证丢失还能补吗?千万别踩坑,丢了证等于失业

电工证丢失还能补吗?千万别踩坑,丢了证等于失业 你正在找工作,但手里的电工证突然丢了,这下怎么办?别急,先别慌, 电工证丢失还能补吗 ?答案是肯定的,但 千万别踩坑 ,补办流程如果不了解,可能会耽误你上岗、甚至影响到你的收入。…

2026/11/9 0:03:09

建设厅的特种工有哪些含金量高?别交完钱考不过

建设厅的特种工有哪些含金量高?别交完钱考不过 怕考不过白交培训费?别急,先搞清楚建设厅的特种工有哪些含金量高的证,再决定是不是值得投入时间和金钱。 建设厅的特种工证是建筑行业里最基础、也最实用的证书之一,涉及多个作业类别,每种证的 含金量…

2026/11/9 0:03:09

荆门市电工证报名怎么考才靠谱 全国通用证别交冤枉钱

荆门市电工证报名怎么考才靠谱 全国通用证别交冤枉钱 怕考不过白交培训费?荆门市电工证报名前必须知道这些避坑指南。别看网上一堆培训机构广告,真要报个电工证,很多人被坑得一塌糊涂。特别是那些打着“包过”旗号的,最后不是考试不过,就是证不是全国通…

2026/11/9 0:03:09

高空电工作业过程通过率揭秘:怎么选靠谱机构不被坑

高空电工作业过程通过率揭秘:怎么选靠谱机构不被坑 不知道去哪报名怕被中介坑?你不是一个人。现在特种作业证报名渠道五花八门,从线下培训机构到线上代理,真假难辨。尤其是像高空电工作业这类高风险、高门槛的作业类型,证书含金量和考试通过率直接影响你…

2026/11/9 6:30:27

面向复杂光照与角度的宠物面部识别鲁棒性增强策略

在智慧养宠时代,基于视觉的宠物身份识别技术正逐步从实验室走向家庭应用。然而,与配合度高的人类不同,宠物是典型的“非合作生物识别对象”——它们不会主动配合摄像头调整姿态,更无法控制光照环境。实际应用中,我们常…

2026/11/7 14:50:12

SSM电子书店源码全解析:结构、原理、部署与排错实战

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

还想了解更多?直接咨询顾问

免费诊断 + 免费方案 + 透明报价。

全国咨询热线400-8866-253
免费获取方案
☎咨询二维码 ☎ ↑