Kubernetes objects can be created, updated, and deleted by using the
command-line tool along with an object configuration file written in YAML or JSON.
This document explains how to define and manage objects using configuration files.
You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. If you do not already have a cluster, you can create one by using Minikube, or you can use one of these Kubernetes playgrounds:
To check the version, enter
kubectl tool supports three kinds of object management:
See Kubernetes Object Management for a discussion of the advantages and disadvantage of each kind of object management.
You can use
kubectl create -f to create an object from a configuration file.
Refer to the kubernetes API reference
kubectl create -f <filename|url>
Warning: Updating objects with the
replacecommand drops all parts of the spec not specified in the configuration file. This should not be used with objects whose specs are partially managed by the cluster, such as Services of type
LoadBalancer, where the
externalIPsfield is managed independently from the configuration file. Independently managed fields must be copied to the configuration file to prevent
replacefrom dropping them.
You can use
kubectl replace -f to update a live object according to a
kubectl replace -f <filename|url>
You can use
kubectl delete -f to delete an object that is described in a
kubectl delete -f <filename|url>
You can use
kubectl get -f to view information about an object that is
described in a configuration file.
kubectl get -f <filename|url> -o yaml
-o yaml flag specifies that the full object configuration is printed.
kubectl get -h to see a list of options.
delete commands work well when each object’s
configuration is fully defined and recorded in its configuration
file. However when a live object is updated, and the updates are not merged
into its configuration file, the updates will be lost the next time a
is executed. This can happen if a controller, such as
a HorizontalPodAutoscaler, makes updates directly to a live object. Here’s
If you need to support multiple writers to the same object, you can use
kubectl apply to manage the object.
Suppose you have the URL of an object configuration file. You can use
kubectl create --edit to make changes to the configuration before the
object is created. This is particularly useful for tutorials and tasks
that point to a configuration file that could be modified by the reader.
kubectl create -f <url> --edit
Migrating from imperative commands to imperative object configuration involves several manual steps.
kubectl get <kind>/<name> -o yaml > <kind>_<name>.yaml
Manually remove the status field from the object configuration file.
For subsequent object management, use
kubectl replace -f <kind>_<name>.yaml
Warning: Updating selectors on controllers is strongly discouraged.
The recommended approach is to define a single, immutable PodTemplate label used only by the controller selector with no other semantic meaning.
selector: matchLabels: controller-selector: "extensions/v1beta1/deployment/nginx" template: metadata: labels: controller-selector: "extensions/v1beta1/deployment/nginx"
Was this page helpful?
Thanks for the feedback. If you have a specific, answerable question about how to use Kubernetes, ask it on Stack Overflow. Open an issue in the GitHub repo if you want to report a problem or suggest an improvement.