Kubernetes Day 13: Guide to Static Pods, Manual Scheduling, Labels, and Selectors

Kubernetes is a powerful container orchestration platform that automates deployment, scaling, and management of containerized applications. While it provides advanced scheduling and deployment mechanisms, there are scenarios where you may need to control scheduling manually or deploy pods in a static, non-scheduled manner.

In this blog, we’ll delve into Static Pods, Manual Scheduling, Labels, and Selectors in Kubernetes. We will go through their usage, configurations, and steps to implement them.

1. Static Pods

Static Pods are directly managed by the Kubelet on a specific node, without being scheduled by the Kubernetes control plane. Unlike regular pods, which are created and managed by a Deployment or ReplicaSet, static pods run directly on a node and are not visible to the Kubernetes API (except for their status).

When to Use Static Pods?

Static Pods are useful when you want to run a pod directly on a specific node without interference from the scheduler, such as for running system-level services (e.g., logging or monitoring agents).

Steps to Create Static Pods

  1. Connect to the Node: Access the node where you want to run the static pod. You can do this via SSH:

     ssh <node-ip>
    
  2. Edit the Kubelet Config: Static Pods are defined using a YAML file placed in a specific directory (/etc/kubernetes/manifests) on the node where the kubelet is running.

  3. Create a Static Pod Definition: Write a YAML file to define the static pod:

     # /etc/kubernetes/manifests/static-nginx.yaml
     apiVersion: v1
     kind: Pod
     metadata:
       name: static-nginx
     spec:
       containers:
         - name: nginx
           image: nginx:1.19
           ports:
             - containerPort: 80
    
  4. Place the YAML in the Manifest Directory: Place the pod definition file in the kubelet's manifest directory:

     sudo cp static-nginx.yaml /etc/kubernetes/manifests/
    
  5. Verify the Pod Creation: After placing the manifest file, the Kubelet will automatically create the pod. You can verify the pod is running on the node using:

     kubectl get pods -A | grep static-nginx
    

    2. Manual Scheduling

    In Kubernetes, pods are scheduled automatically to nodes by the Kubernetes scheduler. However, in some cases, you might want to bypass the scheduler and manually assign a pod to a specific node. This can be done by using the nodeName field in the PodSpec.

    Steps to Perform Manual Scheduling

    1. Define the Pod YAML: In the Pod definition, specify the nodeName field to assign the pod to a particular node.

       apiVersion: v1
       kind: Pod
       metadata:
         name: manually-scheduled-pod
       spec:
         nodeName: <node-name>  # Set the node name here
         containers:
           - name: nginx
             image: nginx:1.19
             ports:
               - containerPort: 80
      
    2. Create the Pod: Apply the pod manifest to the cluster:

       kubectl apply -f manually-scheduled-pod.yaml
      
    3. Verify the Pod is Running on the Desired Node: After the pod is created, you can verify where it is running by checking the pod's assigned node:

       kubectl get pod manually-scheduled-pod -o wide
      

      3. Labels and Selectors

      Labels in Kubernetes are key-value pairs attached to objects such as Pods, Nodes, and Services. They are used to organize and select groups of objects. Selectors enable you to filter resources based on their labels.

      Steps to Use Labels and Selectors

      1. Add Labels to a Pod: You can define labels directly in your Pod manifest under the metadata section:

         apiVersion: v1
         kind: Pod
         metadata:
           name: labeled-pod
           labels:
             app: web
             tier: frontend
         spec:
           containers:
             - name: nginx
               image: nginx:1.19
        
      2. Create a Service Using a Label Selector: After labeling your pods, you can use those labels in a service to expose the pods.

         apiVersion: v1
         kind: Service
         metadata:
           name: frontend-service
         spec:
           selector:
             app: web
             tier: frontend
           ports:
             - protocol: TCP
               port: 80
               targetPort: 80
        
      3. Apply the YAML Files: To create the labeled pod and the service, apply the YAML files:

         kubectl apply -f labeled-pod.yaml
         kubectl apply -f frontend-service.yaml
        
      4. Verify the Service and Pods: List the pods with a specific label:

         kubectl get pods -l app=web
        

        Check if the service is correctly targeting the pods:

         kubectl get endpoints frontend-service
        

Conclusion

In this blog, we explored some key Kubernetes features: Static Pods, Manual Scheduling, Labels, and Selectors. Static Pods allow you to directly run pods on specific nodes without going through the Kubernetes scheduler. Manual scheduling gives you control over where a pod runs by assigning it to a specific node. Labels and Selectors offer a flexible way to group and filter resources, making them essential tools for managing Kubernetes objects.

Understanding and implementing these concepts will give you more control over how applications are deployed and managed in your Kubernetes cluster.

Reference

Video

0
Subscribe to my newsletter

Read articles from Rahul Vadakkiniyil directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rahul Vadakkiniyil
Rahul Vadakkiniyil