Day 5: Kubernetes (Quick Bytes)
21. Selectors
Selectors are used in Kubernetes to select a subset of objects based on their labels. Selectors are used with services, deployments, replica sets, and other controllers to identify which pods to target.
Real-world example: Suppose you have a service that needs to target only the pods with the label 'app: frontend'. You can define the selector for the service using the following YAML definition:
----------------------------------
apiVersion: v1
kind: Service
metadata:
name: frontend-service
spec:
selector:
app: frontend
ports:
- name: http
port: 80
targetPort: 8080
-----------------------------------
22. Taints & Tolerations in K8s
Taints and tolerations are used in Kubernetes to control which nodes can run which pods. A taint is a property that is applied to a node, and a toleration is a property that is applied to a pod. If a pod has a toleration that matches the taint on a node, the pod can be scheduled on that node.
Real-world example:
Suppose you have a node in your Kubernetes cluster that is running a database, and you want to ensure that only pods that are suitable for running a database are scheduled on that node. You can apply a taint to the node using the following command:
> kubectl taint nodes <node-name> db=required:NoSchedule
This taint specifies that the node requires a database and that no pods should be scheduled on the node unless they have a toleration for the db=required taint. To allow a pod to be scheduled on the node, you can add a toleration to the pod using the following YAML definition:
------------------------------------------------------------
apiVersion: v1
kind: Pod
metadata:
name: database-pod
spec:
tolerations:
- key: "db"
operator: "Equal"
value: "required"
effect: "NoSchedule"
containers:
- name: database
image: mysql
-----------------------------------------------------------
23. Node Affinity in K8s
Node affinity is a feature in Kubernetes that allows you to specify which nodes should run which pods based on node labels. Node affinity can be used to ensure that certain pods are always scheduled on specific nodes, or to spread pods across different nodes based on their properties.
Real-world example:
Suppose you have a set of pods that require GPUs to run, and you have some nodes in your cluster that have GPUs attached. You can use node affinity to ensure that the pods are scheduled on the nodes with GPUs. You can define node affinity using the following YAML definition:
------------------------------------------------------------
apiVersion: apps/v1
kind: Deployment
metadata:
name: gpu-app
spec:
replicas: 3
selector:
matchLabels:
app: gpu
template:
metadata:
labels:
app: gpu
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: gpu
operator: Exists
containers:
- name: gpu-container
image: gpu-image
————————————————————————
This YAML definition specifies that the pods should be scheduled on nodes with the label 'gpu', which indicates that the node has a GPU attached.