Skip to content

Hands-On with Kubernetes - Manual Scheduling, Static Pods, and Label Selectors

Published: at 07:30 AM

Kubernetes offers powerful features for pod scheduling, management, and organization. In this hands-on guide, we’ll explore manual pod scheduling, static pods, and the use of labels and selectors. Let’s dive into some practical exercises to deepen our understanding of these concepts.

1. Manual Pod Scheduling

Kubernetes typically handles pod scheduling automatically, but sometimes you might need to assign a pod to a specific node. Here’s how to do it:

apiVersion: v1
kind: Pod
metadata:
  name: manually-scheduled-pod
spec:
  nodeName: <your-node-name>
  containers:
    - name: nginx
      image: nginx

Replace <your-node-name> with the name of the node where you want to schedule the pod. To apply this configuration:

kubectl apply -f manually-scheduled-pod.yaml

Verify the pod’s placement:

kubectl get pod manually-scheduled-pod -o wide

You should see the pod running on the specified node.

2. Working with Static Pods

Static pods are managed directly by the kubelet on a specific node. They’re typically used for control plane components. Let’s explore how to work with them:

  1. SSH into your control plane node.
  2. Navigate to the static pod manifest directory:
cd /etc/kubernetes/manifests/
  1. Here, you’ll find YAML files for control plane components like kube-apiserver.yaml, kube-controller-manager.yaml, etc.

To restart a control plane component, you can modify its YAML file. For example, to restart the API server:

sudo mv kube-apiserver.yaml kube-apiserver.yaml.bak
sudo mv kube-apiserver.yaml.bak kube-apiserver.yaml

This effectively removes and re-adds the manifest, causing kubelet to restart the component.

Note: Be cautious when modifying control plane components, as it can affect cluster stability.

3. Creating Pods with Different Labels

Labels are key-value pairs attached to Kubernetes objects. Let’s create three pods with different environment labels:

---
apiVersion: v1
kind: Pod
metadata:
  name: pod1
  labels:
    env: test
spec:
  containers:
    - name: nginx
      image: nginx
---
apiVersion: v1
kind: Pod
metadata:
  name: pod2
  labels:
    env: dev
spec:
  containers:
    - name: nginx
      image: nginx
---
apiVersion: v1
kind: Pod
metadata:
  name: pod3
  labels:
    env: prod
spec:
  containers:
    - name: nginx
      image: nginx

Save this as labeled-pods.yaml and apply it:

kubectl apply -f labeled-pods.yaml

4. Filtering Pods Using Label Selectors

Now that we have pods with different labels, let’s use selectors to filter them:

To get pods with the dev label:

kubectl get pods -l env=dev

To get pods with the prod label:

kubectl get pods -l env=prod

To get pods with either dev or prod labels:

kubectl get pods -l 'env in (dev,prod)'

Conclusion

These exercises demonstrate the flexibility and power of Kubernetes in managing and organizing pods. Manual scheduling allows for precise control over pod placement, while static pods are crucial for managing core cluster components. Labels and selectors provide a robust system for organizing and querying your Kubernetes resources.

As you continue your Kubernetes journey, experiment with these features to understand how they can be applied in real-world scenarios. Remember, with great power comes great responsibility – especially when working with control plane components!

Happy Kuberneting!