Course contentsShow
← Docker, Kubernetes & CI/CD
Lesson 225 of 8023. Docker Orchestration and StorageFree lesson

Managing Service Placement Constraints and Preferences

Learners will control container scheduling by assigning specific workloads to nodes based on hardware labels, roles, or resource availability.

In this lesson, you will learn how to direct specific containers to run on designated nodes within your cluster using placement constraints and preferences.

Why Control Placement?

In a multi-node cluster, not all servers are identical. Some nodes might have high-performance SSDs, others might be equipped with GPUs for machine learning, and some might be located in specific geographic regions. By using placement constraints, you ensure that your workloads are scheduled exactly where they belong.

Placement Constraints

A constraint acts as a hard filter. If you tell a service to run only on nodes with a specific label, the scheduler will strictly refuse to deploy that container anywhere else. This is ideal for workloads that have specific hardware dependencies.

To use this, you first label your node:

docker node update --label-add gpu=true worker-node-1

Then, you define the constraint in your service deployment:

services:
  ai-model:
    image: my-gpu-app
    deploy:
      placement:
        constraints:
          - node.labels.gpu == true

Placement Preferences

A preference is a "soft" rule. Instead of forcing a hard requirement, a preference tells the scheduler to try its best to place replicas in a specific way, such as spreading them across different zones or rack labels for high availability. If the preferred nodes are full, the scheduler will still deploy the service to another node rather than letting the deployment fail.

services:
  web-app:
    image: nginx
    deploy:
      placement:
        preferences:
          - spread: node.labels.zone

By combining these, you gain fine-grained control: use constraints to ensure a container has the hardware it needs to function, and use preferences to ensure your replicas are distributed across the cluster for maximum availability. This dual approach ensures your infrastructure is both reliable and optimized for the specific requirements of each application.

Key Takeaway: Constraints act as strict requirements to ensure specialized hardware usage, while preferences act as soft guidelines to optimize cluster availability and distribution.