A Static Pod in Kubernetes is a pod that runs directly on a specific node without being managed by the Kubernetes API server. Unlike regular pods created through Deployment or ReplicaSet controllers, static pods are manually managed. They are defined in files located on the node’s file system. This makes them ideal for critical tasks such as monitoring agents or essential services that need to run on specific nodes.
In this tutorial, we’ll cover the basic concepts, configuration, and steps required to create and manage static pods effectively.
Table of Contents
Find Static Pod Location
First, you need to find the staticPodPath parameter configured with the kubelet to create a static pod. The default staticPodPath location is /etc/kubernetes/manifests.
Execute the following command to find the kubelet config file location:
# ps -aux | grep kubelet | grep config.yaml
This command will display the process details of the kubelet, including the path to the config.yaml file.
kubelet 1234 1 0 /usr/bin/kubelet --config=/var/lib/kubelet/config.yaml ...
Once you have the location of the config.yaml file, open it to verify the staticPodPath parameter.
# nano /var/lib/kubelet/config.yaml
Look for the staticPodPath parameter in the configuration file. By default, it should be set to /etc/kubernetes/manifests.
staticPodPath: /etc/kubernetes/manifests
...
View Static Pods
Static pods won’t appear in the usual pod list when you use kubectl get pods. To view static pods, you need to check the node directly or use specific commands to filter them out.
You can use the kubectl to view the static pod:
# kubectl get pods --all-namespaces -o wide | grep static-pod
Output:
NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
default static-pod-example 1/1 Running 0 5m 10.1.0.1 node-name
Create a Static Pod
First, you need to access the node where you want to create the static pod. You can do this using the kubectl exec command.
# kubectl exec -it pod_name -- /bin/bash
Note: Replace pod_name with the actual name of your pod.
Navigate to the directory where the kubelet watches for static pod configurations. By default, this is /etc/kubernetes/manifests.
# cd /etc/kubernetes/manifests
Now, create a YAML file for your static pod, such as static-pod.yaml.
# nano static-pod.yaml
In the static-pod.yaml file, define your pod. Here’s an example of a simple static pod configuration:
apiVersion: v1
kind: Pod
metadata:
name: static-pod-example
labels:
app: static-pod
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
Save the configuration file. The kubelet should automatically pick it up and start the pod. You can verify the pod is running by checking the node directly:
# docker ps | grep static-pod-example
Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
abcd1234 nginx "nginx" 5 minutes ago Up 5 minutes 0.0.0.0:80->80/tcp static-pod-example
You can also use kubectl to check the pod status. But remember that static pods won’t appear in the usual pod list. Instead, use:
# kubectl get pods --all-namespaces -o wide | grep static-pod-example
Output:
NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
default static-pod-example 1/1 Running 0 5m 10.1.0.1 node-name
Delete a Static Pod
To delete a static pod, remove the configuration file:
# rm /etc/kubernetes/manifests/static-pod.yaml
The kubelet will automatically stop and remove the pod.
Conclusion
In this article, we explored static pods in Kubernetes, highlighting their unique characteristics and use cases. We walked through the process of finding the static pod location, viewing, creating, and deleting a static pod.
FAQs
1. Where is the default location for static pod configuration files?
The default location for static pod configuration files is typically /etc/kubernetes/manifests, as defined by the staticPodPath parameter in the kubelet configuration file.
2. How can I create a static pod in Kubernetes?
To create a static pod, define a pod configuration in a YAML file and place it in the directory specified by the staticPodPath. The kubelet will automatically start the pod from this file.
3. How do I delete a static pod in Kubernetes?
To delete a static pod, simply remove its configuration file from the directory specified by the staticPodPath. The kubelet will automatically stop and remove the pod.
4. Can static pods be managed by Kubernetes controllers?
No, static pods are managed solely by the kubelet and are not controlled by Kubernetes controllers like Deployments or ReplicaSets. They run independently of the cluster's control plane.