What I understood by the documentation is that:
kubectl create
Creates a new k8s resource in the clusterkubectl replace
Updates a resource in the live clusterkubectl apply
If I want to do create + replace (Reference)My questions are
Resource Creation kubectl apply and kubectl create are two different approaches in creating resources or updating resource configurations in the Kubernetes cluster environment. Both these commands accept configuration changes either from the file or stdin, and both accept JSON and YAML file formats.
It's really important to remember that technically there isn't an inverse to kubectl apply . This is because of how k8s works, it converges desired state against current state. By running apply , you are telling the cluster to "make it look like this".
The kubectl create service command creates the configuration for the Service and saves it to /tmp/srv. yaml . The kubectl create --edit command opens the configuration file for editing before it creates the object.
Because of this, there is no way to restart a pod, instead, it should be replaced. There is no kubectl restart [podname] command for use with K8S (with Docker you can use docker restart [container_id] ), so there are a few different ways to achieve a pod 'restart' with kubectl .
Those are two different approaches:
kubectl create
is what we call Imperative Management. On this approach you tell the Kubernetes API what you want to create, replace or delete, not how you want your K8s cluster world to look like.
kubectl apply
is part of the Declarative Management approach, where changes that you may have applied to a live object (i.e. through scale
) are "maintained" even if you apply
other changes to the object.
You can read more about imperative and declarative management in the Kubernetes Object Management documentation.
kubectl create
will error out and kubectl apply
will not error out.When running in a CI script, you will have trouble with imperative commands as create raises an error if the resource already exists.
What you can do is applying (declarative pattern) the output of your imperative command, by using --dry-run=true
and -o yaml
options:
kubectl create whatever --dry-run=true -o yaml | kubectl apply -f -
The command above will not raise an error if the resource already exists (and will update the resource if needed).
This is very useful in some cases where you cannot use the declarative pattern (for instance when creating a docker-registry secret).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With