Create, apply and delete safely
Choose a small change with a clear owner
A ConfigMap is an API object that stores non-secret configuration; a later chapter covers consuming its data in containers. For now, use a single synthetic greeting. create -f submits a new object; when that name already exists in the same scope it normally fails. apply -f creates an absent object or reconciles fields of an existing object with a configuration file. Applying is not equivalent to replacing every field or to confirming an application has loaded the value. Review the same file and keep its identity stable. Offline: compare settings.yaml and settings-updated.yaml without running kubectl. Optional lab requires the explicitly authorized context and namespace from the previous lesson, a checked free name and ownership of the object. No existing resource may be adopted casually.
apiVersion: v1kind: ConfigMapmetadata: name: workflow-settings namespace: book-lab labels: app: catalog lab: workflowdata: greeting: "Welcome"The name, namespace and labels identify this example. The data value is a string. Save the manifest in a dedicated directory and use no real secrets.
Preview before committing
Client dry-run avoids submitting the proposed write; depending on command and version, discovery or validation can still need server access. Server dry-run sends a request for server processing without persisting the proposed object; authorization, admission and validation still matter. It does not test controller behavior or application readiness. diff compares the live object with a proposed applied result and may require write-like API permissions for dry-run. Stop on unexpected scope or errors.
kubectl --kubeconfig ./lab.kubeconfig --context bytemind-lab -n book-lab apply --dry-run=client -f settings.yaml -o yamlkubectl --kubeconfig ./lab.kubeconfig --context bytemind-lab -n book-lab apply --dry-run=server -f settings.yaml -o yamlkubectl --kubeconfig ./lab.kubeconfig --context bytemind-lab -n book-lab diff -f settings.yamlInspect the proposed namespace, name and greeting. Diff exit code 0 means no change, 1 means differences, and values greater than 1 mean failure. Do not put an unconditional apply after diff or treat all nonzero exit codes as the same.
Apply one reviewed object
In the optional lab only, first confirm the object name is free by inventorying ConfigMaps in book-lab. If the name exists and is not your recorded fixture, stop. A single operator should own this isolated fixture. After explicit review and approval, use apply, then verify the actual data through get. For the second change, review settings-updated.yaml and its diff before applying. Do not switch management strategies silently; this example uses ordinary client-side apply throughout. Advanced field ownership is a later lesson.
kubectl --kubeconfig ./lab.kubeconfig --context bytemind-lab -n book-lab apply -f settings.yamlkubectl --kubeconfig ./lab.kubeconfig --context bytemind-lab -n book-lab get configmap/workflow-settings -o jsonpath='{.data.greeting}'Expected greeting in this controlled scenario: Welcome. The command may omit a trailing newline; a real API response and controller behavior have not been tested.
kubectl --kubeconfig ./lab.kubeconfig --context bytemind-lab -n book-lab diff -f settings-updated.yaml# Only after inspecting the diff and approving the change:kubectl --kubeconfig ./lab.kubeconfig --context bytemind-lab -n book-lab apply -f settings-updated.yamlkubectl --kubeconfig ./lab.kubeconfig --context bytemind-lab -n book-lab get configmap/workflow-settings -o jsonpath='{.data.greeting}'Expected data value after successful update: Hello again. This verifies the API object value, not that any application reloaded it. Run the lines individually rather than as an unattended script.
Remove only the resource you own
Deleting a resource can trigger further effects and may wait for finalizers. A name can be reused by another object, so verify the current object identity and ownership just before cleanup. In the approved isolated lab with no competing writers, remove only the ConfigMap you created. Do not force delete, prune, delete by broad selector or delete the namespace. If ownership is uncertain, stop rather than clean up another learner’s work.
kubectl --kubeconfig ./lab.kubeconfig --context bytemind-lab -n book-lab get configmap/workflow-settings -o yaml# After verifying lab ownership and receiving approval:kubectl --kubeconfig ./lab.kubeconfig --context bytemind-lab -n book-lab delete configmap/workflow-settings --wait=true --timeout=30skubectl --kubeconfig ./lab.kubeconfig --context bytemind-lab -n book-lab get configmapsThe final inventory should no longer contain the owned object after successful deletion. An authorization or connection error is not proof of absence. If cleanup times out, inspect state and report the remaining resource instead of forcing removal.
Practice and next step
Open the two YAML fixtures and the diff-cases.json table. Identify exactly which field changed, which identity did not change, and what to do for each diff exit code. This is a reasoning exercise; no live changes are required. Next, use selectors to narrow a list without modifying the objects.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Create, apply and delete safely — knowledge check
Apply the lesson concepts to original offline scenarios.
A ConfigMap already exists under the same name and namespace. What does create normally do?
What does a successful server dry-run establish?
What does diff exit code 1 normally mean?
Which is the right cleanup scope?
After get shows Hello again, what is proven?