Skip to content

Kubernetes: Create, generate & apply YAML

10 Kubernetes commands: Create, generate & apply YAML. Click a command to see what each part does.

Index
10 items
Click a command to see what each part does.

Create, generate & apply YAML10

  • kubectl run <name> --image=<image> --port=<port> --dry-run=client -o yaml > pod.yamlGenerate a pod manifest

    What each part does

    1. kubectl run <name> --image=<image>a single pod (the old --generator flag is gone)
    2. --dry-run=client -o yamlprint YAML instead of creating
    3. > pod.yamlsave to edit
  • kubectl expose deploy <deployment> --port=<port> --target-port=<container-port> --dry-run=client -o yaml > svc.yamlGenerate a service manifest

    What each part does

    1. expose deploy <deployment>service selecting the deployment's pods
    2. --port / --target-portservice port / container port
    3. --dry-run=client -o yamlYAML only
  • kubectl create job <job-name> --from=cronjob/<cronjob> -n <namespace>Create a job from a cronjob (run it now)

    What each part does

    1. create job --from=cronjob/<cronjob>one-off Job using the CronJob's template
    2. -n <namespace>in this namespace (default: the context's namespace; -A for all)
  • kubectl create cronjob <name> --image=<image> --schedule='*/5 * * * *' -- <command>Create a cronjob

    What each part does

    1. --schedule='...'cron schedule
    2. -- <command>what the container runs
  • kubectl get deploy <deployment> -n <namespace> -o yaml > deploy.yamlExport an existing object as clean YAML

    What each part does

    1. -o yamlthe full object; remove status, metadata.uid, resourceVersion, managedFields before re-applying (--export no longer exists; the kubectl-neat plugin strips them)
  • kubectl apply -R -f <dir>Apply a directory recursively

    What each part does

    1. -Rrecurse into subdirectories
    2. -f <dir>every manifest in it
  • kubectl apply -k <overlay-dir>Apply a Kustomize overlay

    What each part does

    1. -k <dir>render kustomization.yaml and apply; kubectl kustomize <dir> just prints it
  • kubectl apply --server-side -f <file>Server-side apply

    What each part does

    1. --server-sidethe API server merges and tracks field ownership; needed for large CRDs
  • kubectl replace --force -f <file>Replace an object (delete + recreate)Caution

    What each part does

    1. replace --forcedelete and recreate: for fields that can't be changed in place
  • kubectl wait --for=condition=Ready pod -l app=<app> --timeout=120s -n <namespace>Wait until pods are ready

    What each part does

    1. kubectl wait --for=condition=Readyblock until the condition is true (also --for=delete, --for=jsonpath=...)
    2. pod -l app=<app>which objects
    3. --timeout=120sfail after 2 minutes

My DevOps Playbook by Angelo John Calleja

New runbook

GitHub not connected

Saving is handled by the site: the GitHub token is stored in Cloudflare, not in your browser. Enter the editor password (the EDITOR_PASSWORD secret) to unlock saving in this browser.

Saving commits to gelogrammer/My_DevOps_Playbook. Create a fine-grained token with access to only this repo andContents: Read and write, then paste it here. It is stored only in this browser.

src/content/docs/…