kubectl apply: Deploy Manifests in CI
Create or update Kubernetes resources from manifests declaratively.
kubectl apply reconciles cluster state toward the manifests you provide, creating resources that are missing and patching ones that drifted. It is the backbone of GitOps-style and CI-driven deploys. Use --dry-run=server to validate before applying for real.
Common flags
-f file.yaml- apply a manifest or directory-k dir- apply a kustomize overlay-n NAMESPACE- target namespace--dry-run=server- validate against the API server without persisting--prune- delete resources no longer in the manifest set
Example in CI
Apply a directory of manifests to production.
shell
kubectl apply -f k8s/ --namespace prodCommon errors in CI
- the server could not find the requested resource - wrong apiVersion or CRD not installed
- Unable to connect to the server - kubeconfig not set or wrong context
- error validating data: ValidationError - schema mismatch in the manifest
Key takeaways
- Declaratively reconciles cluster state to your manifests.
- Validate with --dry-run=server before applying in CI.
- Most failures are a wrong apiVersion or an unset kubeconfig.
Related guides
kubectl set image: Update Deployment Images in CIUpdate a container image on a Deployment from CI: syntax, a rollout example, and the errors that come from wr…
kubectl rollout status: Gate Deploys in CIWait for a Kubernetes rollout to finish in CI: timeout flags, a gating example, and the errors that mean a st…
kubectl config use-context: Select Clusters in CISwitch the active kubeconfig context in CI: usage, a multi-cluster example, and the errors that mean a missin…