Master Kubernetes InitContainers, Static Pods, and Node Scheduling with Real‑World Examples
This tutorial walks through Kubernetes initContainers, demonstrates how to create and manage static Pods, and explains pod scheduling strategies—including node labeling and nodeSelector—using step‑by‑step YAML examples and kubectl commands to illustrate each concept in practice.
InitContainers (Initialization Containers)
Kubernetes introduced init containers in version 1.3 to run one or more containers before the main application container starts, allowing you to set up the environment or perform prerequisite tasks.
init1 → init2 → … → all init containers finish → app container starts
<code># initContainer.yml
apiVersion: v1
kind: Pod
metadata:
name: initpod02
spec:
containers:
- name: initpod02
image: nginx
imagePullPolicy: Never
initContainers:
- name: initc
image: centos
imagePullPolicy: Never
securityContext:
privileged: true
command: ["ls"]
dnsPolicy: ClusterFirst
restartPolicy: Always
</code>Applying this file with
kubectl apply -f initContainer.ymlcreates the pod; the init container runs first. If the init container succeeds, the main nginx container starts. Changing the command to an invalid one (e.g.,
"lssssssss") causes the init container to fail, the pod enters
Init:CrashLoopBackOff, and the application container never starts.
<code># kubectl apply -f initContainer.yml
pod/initpod02 created
# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
initpod02 0/1 Init:CrashLoopBackOff 1 3s 10.244.xxx.xxx node1
</code>When the init container succeeds, you can verify the side‑effect (e.g., a file created in a shared volume) from the main container:
<code># kubectl exec -it initpod02 -- cat /test/initfile
123
</code>Static Pod
A static pod is defined by placing a pod manifest file directly in the kubelet’s
--pod-manifest-pathdirectory (default
/etc/kubernetes/manifests). The kubelet watches this directory and automatically creates or deletes the pod when the file appears or disappears, without using
kubectl.
<code># Example static pod manifest (nginx.yaml)
apiVersion: v1
kind: Pod
metadata:
name: static-nginx
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
</code>Copy the file to
/etc/kubernetes/manifestson a node, and the pod appears in
kubectl get pods -n kube-system. Deleting the file removes the pod automatically.
Pod Scheduling (Assigning Pods to Specific Nodes)
Kubernetes schedules pods to nodes based on resource availability and other criteria. You can influence placement by labeling nodes and using
nodeSelectorin the pod spec.
<code># Label a node
kubectl label nodes node2 cloud=666
</code>Create a pod manifest that selects the labeled node:
<code># label.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod07
labels:
cloud: "666"
spec:
containers:
- name: pod07
image: nginx
imagePullPolicy: IfNotPresent
nodeSelector:
cloud: "666"
</code> <code># Apply the pod
kubectl apply -f label.yaml
pod/pod07 created
# Verify placement
kubectl get pods -o wide | grep pod07
pod07 1/1 Running 0 10.244.xxx.xxx node2
</code>Removing the label from the node (
kubectl label nodes node2 cloud-) disables the selector.
Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.