Category: Kubernetes

Taints and Tolerations in Kubernetes

Taints are applied to Node’s and Tolerations are applied to Pod’s.

They restrict Pod’s to be allocated to Node’s

Check if any taints are already applied to node

kubectl describe controlplane | grep Taints

Apply taint to a node

//kubectl taint nodes <<node name>> <<key>>=<<value>>:<<effect>>
kubectl taint nodes controlplane app=webapp:NoSchedule

Taint types/effects:-

  • NoSchedule – The pod will not get scheduled to the node without a matching toleration.
  • PreferNoSchedule  – This is a softer version of NoSchedule where the controller will not try to schedule a pod with the tainted node. However, it is not a strict requirement
  • NoExecute – This will immediately evict all the pods without the matching toleration from the node

Add toleration to Pod-

// nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx-app
spec:
  containers:
  - name: nginx
    image: nginx
  tolerations:
  - key: color
    value: blue
    operator: Equal
    effect: NoSchedule

Tolerations Operators-

  • Equal – default value. Checks the value of key with the node taint key and value
  • Exists – checks if the key ecists in the node taint key

Create a new Pod

kubectl create -f nginx-pod.yaml

Check the POD created on which node-

kubectl get pods -o wide

Untaint the node (note “-” after NoSchedule)

kubectl taint nodes controlplane node-role.kubernetes.io/master: NoSchedule-

Reference- https://kubernetes.io/docs/concepts/scheduling-eviction/taint-and-toleration/

Loading

Kubernetes commands for managing Pods

To get the cluster info use-

kubectl cluster-info

To get list of nodes user-

kubectl get nodes

Creating Pod – imperative way

Create a POD with nginx image and name nginx in default namespace

// kubectl run <<pod name>> --image <<image in docker hub>> 
kubectl run nginx --image nginx

Create a POD with nginx image and name nginx in different namespace

// kubectl run <<pod name>> --image <<image in docker hub>> -n <<namespace name>>
kubectl run nginx --image nginx -n production

Get Pods and details

Get a list of POD’s in default namespace

kubectl get pods

Get a list of POD’s in other namespace

//kubectl get pods -n <<namespace name>>
kubectl get pods -n production

Check the node of the Pod it is created-

kubectl get pods -o wide

Create POD using yaml – declarative way

// file name- pod-definition.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod-yaml
spec:
  containers:
    - name: nginx-container-yaml
      image: nginx

Create a pod declarative way-

kubectl create -f pod-definition.yaml

Deleting Pod

Delete a pod in default namespace

// kubectl delete pod <<pod-name>>
kubectl delete pod nginx-pod-yaml

Delete all pods in default namespace

kubectl delete --all pods

Delete a pod in custom namespace

//kubectl delete pod <<pod-name>> -n <<namespace-name>>
kubectl delete pod nginx-pod-yaml -n development

Delete all pods in custom namespace

//kubectl delete --all pods -n <<namespace-name>>
kubectl delete --all pods -n development

Loading