Sub topic 10
Declarative configuration with kubectl apply
This chapter assumes you have a running kind or minikube cluster, a working kubectl context, and have created a Deployment with a command such as kubectl create deployment. You should also be able to read YAML.
Imperative and declarative
An imperative command tells the cluster what to do: kubectl create deployment web --image=nginx:1.27. A declarative workflow describes the state you want in a file and lets kubectl work out the steps. kubectl apply -f takes a manifest and makes the cluster match it. If the object doesn't exist, apply creates it. If it exists and differs, apply updates it. If nothing changed, apply reports unchanged.
This is why most teams keep manifests in version control. The file is the record of what should be running.
Applying a manifest
This web.yaml defines a Deployment and a Service. A line with --- separates the two objects in one file.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 2
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.27
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: web
spec:
type: NodePort
selector:
app: web
ports:
- port: 80
targetPort: 80
nodePort: 30080Apply it and wait for the rollout:
kubectl apply -f web.yaml
kubectl rollout status deployment/web
kubectl get pods -o wideThe first run prints deployment.apps/web created and service/web created. The Service is covered in the next chapter, so for now treat it as part of the file.
Changing the desired state
Edit the file, for example set replicas: 3 or change the image to nginx:1.27-alpine. Then run kubectl apply -f web.yaml again. Only the changed fields are patched. An image change triggers a rolling update, which replaces pods gradually.
Before you apply, you can preview the change:
kubectl diff -f web.yaml
kubectl apply -f web.yaml --dry-run=serverkubectl diff shows a patch-style comparison with the live object. It exits with status 1 when differences exist, which matters if you use it in a script. --dry-run=server sends the manifest to the API server for validation, but nothing is saved.
Common errors
AlreadyExistsfromkubectl create.createonly makes new objects. Useapplyfor files you will edit and re-run.- Selector and labels don't match. The Deployment's
spec.selector.matchLabelsmust matchspec.template.metadata.labels. Otherwise the API server rejects the manifest. field is immutable. Some fields, such as a Deployment's selector, can't change after creation. Delete the object and apply again.- YAML indentation mistakes. Use spaces, never tabs.
error converting YAML to JSONusually points to the bad line. - Mixing methods. Apply tracks the fields it set in an annotation. If you scale with
kubectl scaleand then apply a file with a differentreplicas, the file wins.
Exercise
You change replicas: 2 to replicas: 5 in web.yaml. How can you see what apply will change before running it, and which command makes the change?
Show answer
Run kubectl diff -f web.yaml to preview the change, then kubectl apply -f web.yaml to make it. You can check afterward with kubectl rollout status deployment/web and kubectl get pods.