Newsletter
TechAnV Blog
Get updates on security engineering, Rust, eBPF, and DevSecOps. No spam, unsubscribe anytime.
Check your inbox and click the confirmation link to complete your subscription.
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:
1apiVersion: v12kind: Pod3metadata:4 name: manually-scheduled-pod5spec:6 nodeName: <your-node-name>7 containers:8 - name: nginx9 image: nginxReplace <your-node-name> with the name of the node where you want to schedule the pod. To apply this configuration:
1kubectl apply -f manually-scheduled-pod.yamlVerify the pod’s placement:
1kubectl get pod manually-scheduled-pod -o wideYou 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:
- SSH into your control plane node.
- Navigate to the static pod manifest directory:
1cd /etc/kubernetes/manifests/- 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:
1sudo mv kube-apiserver.yaml kube-apiserver.yaml.bak2sudo mv kube-apiserver.yaml.bak kube-apiserver.yamlThis 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:
1---2apiVersion: v13kind: Pod4metadata:5 name: pod16 labels:7 env: test8spec:9 containers:10 - name: nginx11 image: nginx12---13apiVersion: v114kind: Pod15metadata:16 name: pod217 labels:18 env: dev19spec:20 containers:21 - name: nginx22 image: nginx23---24apiVersion: v125kind: Pod26metadata:27 name: pod328 labels:29 env: prod30spec:31 containers:32 - name: nginx33 image: nginxSave this as labeled-pods.yaml and apply it:
1kubectl apply -f labeled-pods.yaml4. 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:
1kubectl get pods -l env=devTo get pods with the prod label:
1kubectl get pods -l env=prodTo get pods with either dev or prod labels:
1kubectl 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!