Kubectl is the one of the key tools which help you to perform most of the kubernetes operations like deploy apps, inspect, manage, and troubleshoot. With that when we use two of its operations — apply and create — often have confused us. What is that? And when we need to use it? In this post, lets discuss about that in detail.
Before starting, the key difference between kubectl apply and create is that apply creates Kubernetes objects through a declarative syntax, while the create command is imperative. The command set kubectl apply is used at a terminal’s command-line window to create or modify Kubernetes resources defined in a manifest file. This is called a declarative usage. The state of the resource is declared in the manifest file, then kubectl apply is used to implement that state. Check the infographic from the DigitalOcean for more details.
Kubectl create is the command you use to create a Kubernetes resource directly at the command line. You can also use kubectl create against a manifest file to create a new instance of the resource. However, if the resource already exists, you will get an error.
kubectl apply
Let’s explore the details of both kubectl usages. First, let’s look at kubectl apply. Listing 1 below is a manifest file that describes a Kubernetes deployment that has two replicas of our internal tool foxy
apiVersion: apps/v1
kind: Deployment
metadata:
name: testdeploy
labels:
app: foxapp2
spec:
replicas: 2
selector:
matchLabels:
app: foxapp2
template:
metadata:
labels:
app: foxapp2
spec:
containers:
- name: foxapp2
image: foxytool:latest
ports:
- containerPort: 80
The name of the deployment manifest file as testdeploy.yaml. If you run the command below, it will create a deployment according to the contents of this manifest file.
# kubectl apply -f testdeploy.yaml
deployment/testdeploy created
When you run the command kubectl get deployment, you’ll get the following output:
NAME READY UP-TO-DATE AVAILABLE AGE
testdeploy 3/3 3 3 2m50s
Here, we’ve created the deployment named testdeploy, and it is running its two pods.
Continue read on Kubectl apply vs Kubectl create — FoxuTech