What are static pods in Kubernetes?
Let's first discuss the pods which we have created as of now using kubectl
command. As we know, kubectl
is a client used to interact with k8s cluster's components. When we deploy pod using kubectl
it sends the request to kube-api
server, and kube-api
server further send request to etcd
server to validate the request. Validation result is send back to kube-api
server and kube-api
server sends instruction to kubelet
agent to deploy pod the in a node. These pods are called non-static pods.
Static pods are those pods which are directly created/managed by kubelet
agent in a particular node with no intervention of kube-api
server.
How to create static pod?
Static pods can be created in two different way:
- File Hosted Approach
- Web Hosted Approach
In this block we will discuss on file hosted approach only.
Files Hosted Approach
Creating a static pods in file hosted approach is a way where we create a pod's manifest file in the local directory of the corresponding nodes and then need to pass the location of yaml file to the kubelet
configuration. Let's flow below steps to create static pod in a node and deploy springboot image we have build in previous blogs.
Step 1: Get nodes
kubectl get nodes -o wide
Result:
As you see, we have one master and three worker kind nodes. we can deploy pods master and worker both but we will go with worker node. kind-worker
.
Step 2: ssh into the kind-worker
node. As we discuss in kind installation and cluster creation section, kind nodes are running in docker containers. So we will be using below command to ssh our node.
docker exec -it kind-worker bash
You can see we are inside kind-worker
node.
Step 2: Locate the config file of kubelet agent to identify the static pod directory
.
ps aux | grep kubelet
Let's copy the --config
directory /var/lib/kubelet/config.yaml
which is shown in the above image. In the next step use the use below command.
cat /var/lib/kubelet/config.yaml | grep -i staticpodpath
Result:
You can see We got staticPodPath. Now we need to initialize the manifest file static-pod.yaml
using vi
or vim
or any other text editor tools and provide the definition for the pod. I am using below definition to deploy my springbootapp:v2
.
apiVersion: v1
kind: Pod
metadata:
name: mydemo-app
namespace: qa
labels:
appname: demoapp
type: frontend
spec:
containers:
- name: mydemoapp
image: jeewangautam02261995/springbootapp:v2
Now save the file using :wq
. Open the new terminal and apply below command to view the pods. As my pod is deployed in qa
namespace I am using -n qa
in below command.
kubectl get pods -n qa
As you see, mydemo-app-kind-worker
static pod is created and up and running where pod's name combined with node's name.
The beauty of this is that we didn’t run kubectl apply -f
to create static pod as we would normally do when creating pods. This was created automatically by kubelet
agent and if anything happens and it by any chance gets destroyed. It gets created again automatically. The ability to be recreated and started automatically in case any failure occurs is a unique feature of static pods and as such we do not need to take any further steps as we have met the requirements.
Let's delete pod using below command.
kubectl delete pod mydemo-app-kind-worker -n qa
Monitor continuously in the new terminal using below command.
kubectl get pods -w -n qa
Result:
As you seen, first pod was in terminating status as we deleted. But kubelet
automatically create it again and it's in running status now. Unless, you deleted manifest filestatic-pod.yaml
from the local directory of node you will not able to delete the pod. Once you deleted the file pod will automatically deleted.
Top comments (2)
Nice Jeewan good job, well written.
Thank you 😊 !