Manmohan Mittal
3 min readMay 17, 2023

Kubernetes — Run Applications on Specific Nodes

Kubernetes Clusters are used to run different kind of application: Stateless , Stateful , Batch , Machine Learning , Event Streaming etc. These applications have different type of functional & non functional requirements in terms of availability , performance , latency etc.

Hence each type of application demands for specific compute ( cpu , memory & storage) to run which required specific nodes. So, to run specific applications on specific nodes , Kubernetes provides options of taints and tolerations which are used to control which applications can be scheduled on which nodes.

Taints are applied to nodes, marking them with certain attributes , while tolerations are added to application deployments, indicating which taints the workloads can tolerate.

Here’s an example of how to apply taints and tolerations to nodes and application deployments:

Step 1: Apply a Taint to a Node
To apply a taint to a node, you can use the kubectl taint command. Here’s an example:

kubectl taint nodes <node-name> key=value:taint-effect

For instance, to apply a taint with the key “app” and value “production” to a node named “node-1” with the taint effect of “NoSchedule”, you would run:

kubectl taint nodes node-1 app=production:NoSchedule

Step 2: Create an Application Deployment with Tolerations
When creating a deployment, you can specify tolerations in the pod template to allow pods to be scheduled on nodes with specific taints. Here’s an example deployment YAML:

apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
tolerations:
- key: "app"
operator: "Equal"
value: "production"
effect: "NoSchedule"
containers:
- name: myapp-container
image: myapp-image
ports:
- containerPort: 8080

In this example, the `tolerations` field is specified under `spec.template.spec` to define the tolerations for the pods. The toleration indicates that the pods can tolerate a taint with the key “app”, value “production”, and effect “NoSchedule”.

Step 3: Apply the Workload Deployment
After saving the deployment YAML file, you can apply it using the `kubectl apply` command:

kubectl apply -f deployment.yaml

This will create the deployment with the specified tolerations.

Now, when the Kubernetes (K8s) scheduler assigns pods to nodes, it will take into account the taints on the nodes and the tolerations specified in the pod template. So, Pods with matching tolerations will be scheduled on nodes with the corresponding taints.

Note that there are different taint effects available, such as “NoSchedule”, “PreferNoSchedule”, and “NoExecute”, each with different behaviour as below:

  1. NoSchedule: This taint effect prevents non tolerated new pods from being scheduled onto the tainted node. Existing non tolerated pods continue to run unless they are evicted for other reasons. It is the strongest taint effect and ensures that no new pods are scheduled unless they have a matching toleration.
  2. PreferNoSchedule: This taint effect functions similarly to “NoSchedule”, but it gives the scheduler a preference to avoid scheduling new non tolerated pods on the tainted node . The scheduler will try to avoid the node unless there are no other suitable options available.
  3. NoExecute: This taint effect not only prevents new pods from being scheduled onto the node unless they have tolerations, but also evicts existing pods that do not tolerate the taint. It is used for more critical situations where running pods on the tainted node could be harmful or against policies.

Make sure to adjust above described taint effects according to your application requirements.

Manmohan Mittal

Passionate about K8s ,Cloud and learning new technologies.