Kubectl apply: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(13 intermediate revisions by the same user not shown)
Line 2: Line 2:


* [[Kubectl#Commands|kubectl]]
* [[Kubectl#Commands|kubectl]]
* [[kubectl create]]
* [[kubectl delete]]
* [[Kubernetes_Manifests#Overview|Kubernetes Manifests]]
* [[Kubernetes_Concepts#Declarative_versus_Imperative_Approach|Declarative versus Imperative Approach]]


=Overview=
=Overview=


<tt>kubectl apply</tt> supports [[Kubernetes Strategic Merge Patch|strategic merge patch]].
Apply a configuration to a resource by filename or stdin:
 
<syntaxhighlight lang='bash'>
kubectl apply -f <manifest.yaml>
</syntaxhighlight>
<syntaxhighlight lang='bash'>
cat <manifest.yaml> | kubectl apply -f -
</syntaxhighlight>
 
To apply all manifests from a directory:
<syntaxhighlight lang='bash'>
kubectl apply -f ./dir
</syntaxhighlight>
 
In-line:
<syntaxhighlight lang='bash'>
cat <<EOF | kubectl -n ${namespace} apply -f -
apiVersion: v1
kind: Service
metadata:
  name: something
  namespace: ${namespace}
spec:
  ...
EOF
</syntaxhighlight>
 
The command can be used to both create a new resource or update the state of an existing resource. When updating the state, the command supports [[Kubernetes Strategic Merge Patch|strategic merge patch]].
 
Also see: {{Internal|kubectl create|kubectl create}}
 
=Namespace Handling=
The resources specified by the manifest can be deployed in an arbitrary namespace with the -n <namespace> option, unless the manifest already specifies a different namespace in the metadata section. In that case, an error will ensue:
<syntaxhighlight lang='text'>
error: the namespace from the provided object "default" does not match the namespace "prometheus". You must pass '--namespace=default' to perform this operation.
</syntaxhighlight>
 
=Options=
 
==-f|--filename==
 
The filename that contains the configuration to apply. "-" means it will take stdin and apply it.
 
==-k|--kustomize==
 
Process a kustomization directory.
 
==-k==

Latest revision as of 02:20, 26 February 2022

Internal

Overview

Apply a configuration to a resource by filename or stdin:

kubectl apply -f <manifest.yaml>
cat <manifest.yaml> | kubectl apply -f -

To apply all manifests from a directory:

kubectl apply -f ./dir

In-line:

cat <<EOF | kubectl -n ${namespace} apply -f - 
apiVersion: v1
kind: Service
metadata:
  name: something
  namespace: ${namespace}
spec:
  ...
EOF

The command can be used to both create a new resource or update the state of an existing resource. When updating the state, the command supports strategic merge patch.

Also see:

kubectl create

Namespace Handling

The resources specified by the manifest can be deployed in an arbitrary namespace with the -n <namespace> option, unless the manifest already specifies a different namespace in the metadata section. In that case, an error will ensue:

error: the namespace from the provided object "default" does not match the namespace "prometheus". You must pass '--namespace=default' to perform this operation.

Options

-f|--filename

The filename that contains the configuration to apply. "-" means it will take stdin and apply it.

-k|--kustomize

Process a kustomization directory.

-k