Microservices Deployment Strategies: Custom Scheduling
The nodeSelector: Simple Custom Scheduling with Labels
This is the easiest way to constrain Pods to specific nodes, as it uses a simple label selector. It works according to a simple principle:
- You label your nodes with key-value pairs.
- You specify those key-value pairs in your Pod specification using the
nodeSelectorfield. - Kubernetes will then schedule the Pod only on nodes that have matching labels.
nodeSelector: A Practical Example
To better understand how nodeSelector works, let's take a look at a practical example.
Imagine this scenario: You have an Nginx server running a CDN service, and because it serves static content and has high I/O operations, you want it to run only on nodes with SSD disks. In your cluster, you have nodes with both HDD and SSD disks.
The initial setup could look like this:
kubectl apply -f - <
apiVersion: apps/v1
kind: Deployment
metadata:
name: cdn
spec:
replicas: 3
selector:
matchLabels:
app: cdn
template:
metadata:
labels:
app: cdn
spec:
containers:
- name: cdn
image: nginx
ports:
- containerPort: 80
EOF
To schedule the Nginx Pods only on nodes with SSD disks, you first need to identify the nodes with SSD disks and label them accordingly. For this example, let's assume you label those nodes with disktype=ssd. We have two nodes in this example; the first is SSD-based, while the second is HDD-based.
# Any random node will do for this example
export ssd_node=$(kubectl get nodes -o custom-columns=NAME:.metadata.name --no-headers | head -n 1)
# Label the node
kubectl label node $ssd_node disktype=ssd
# Verify the label was added
kubectl get nodes --show-labels | grep $ssd_node
Alternatively, you can transform the output to show a more readable format:
kubectl get node $ssd_node --show-labels=false \
-o jsonpath='{.metadata.labels}' | jq
You should see a group of key-value pairs representing the labels of the node:
{
"beta.kubernetes.io/arch": "amd64",
"beta.kubernetes.io/instance-type": "s-2vcpu-4gb",
"beta.kubernetes.io/os": "linux",
"disktype": "ssd",
"doks.digitalocean.com/managed": "true",
"doks.digitalocean.com/node-id"Cloud-Native Microservices With Kubernetes - 2nd Edition
A Comprehensive Guide to Building, Scaling, Deploying, Observing, and Managing Highly-Available Microservices in KubernetesEnroll now to unlock all content and receive all future updates for free.
