kubectl apply: Command Reference for CI/CD
Reconcile cluster state to your manifests in one declarative command.
kubectl apply is the workhorse deploy command in CI: it creates what is missing and patches what changed, computing the diff for you. This reference lists the flags you use most and a CI example that validates before it writes.
Common flags and usage
- -f, --filename: manifest file, directory, or URL (repeatable)
- -k, --kustomize: apply a kustomize directory
- --server-side: server-side apply with field-manager tracking (modern CI default)
- --dry-run=server: validate against the live API without writing
- --prune -l <selector>: delete objects in the selector set no longer in the manifests
- --force-conflicts: take ownership on a server-side apply field conflict
Example
kubectl apply -f k8s/ --dry-run=server # validate against the cluster
kubectl apply -f k8s/ --server-side \
--field-manager=ci-pipeline
kubectl rollout status deploy/web --timeout=120sIn CI
Run apply with --dry-run=server first so a malformed or schema-invalid manifest fails the job before any change reaches the cluster. Use --server-side with a stable --field-manager so ownership is attributed to the pipeline, then gate the real apply on kubectl rollout status. On Latchkey managed runners this whole sequence runs on an ephemeral runner with no shared kubeconfig state between jobs.
Key takeaways
- apply is declarative and idempotent: re-running the same manifests is safe.
- --dry-run=server validates against live schemas; --dry-run=client only checks local syntax.
- Prefer --server-side with a named --field-manager so CI owns its fields cleanly.