YAML for k8s (Incomplete)
Deployment YAML file for k8s
Deployment of application in chosen nodes
Using Node Affinity
Label Nodes:
Label your desired nodes. For example:
kubectl label nodes node1 mylabel=selected kubectl label nodes node2 mylabel=selected
Create a Deployment YAML file (
deployment.yaml
):apiVersion: apps/v1 kind: Deployment metadata: name: my-deployment spec: replicas: 2 selector: matchLabels: app: my-app template: metadata: labels: app: my-app spec: affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchExpressions: - key: mylabel operator: In values: - selected containers: - name: my-container image: nginx
Apply the Deployment:
Apply the deployment using the following command:
kubectl apply -f deployment.yaml
Labels: You label the nodes you want to target. In this example, both
node1
andnode2
are labeled withmylabel=selected
.Affinity: The
nodeAffinity
field is used to specify that the pods should be scheduled on nodes with the labelmylabel
set toselected
. TherequiredDuringSchedulingIgnoredDuringExecution
field ensures that the scheduler will only place pods on nodes that match this condition.Deployment Spec: The deployment spec ensures that 2 replicas of the pods are created and scheduled only on the labeled nodes.
Using Node Selector
If you want to target multiple nodes using
nodeSelector
, you can label both nodes with the same label and then specify that label in your pod or deployment spec.Label Nodes:
Label your desired nodes. For example:
kubectl label nodes node1 mylabel=selected kubectl label nodes node2 mylabel=selected
Create a Deployment YAML file (
deployment.yaml
):apiVersion: apps/v1 kind: Deployment metadata: name: my-deployment spec: replicas: 2 selector: matchLabels: app: my-app template: metadata: labels: app: my-app spec: nodeSelector: mylabel: selected containers: - name: my-container image: nginx
Apply the Deployment:
Apply the deployment using the following command:
kubectl apply -f deployment.yaml
Labels: You label the nodes you want to target. In this example, both
node1
andnode2
are labeled withmylabel=selected
.Node Selector: The
nodeSelector
field is used to specify that the pods should be scheduled on nodes with the labelmylabel
set toselected
. This is a straightforward way to ensure that pods are only scheduled on nodes with the matching label.Deployment Spec: The deployment spec ensures that 2 replicas of the pods are created and scheduled only on the labeled nodes.
Using nodeSelector
is simpler but less flexible compared to nodeAffinity
. If your requirements are straightforward and you only need to match pods to nodes with specific labels, nodeSelector
is sufficient. If you need more complex scheduling rules, such as preferred or multiple conditions, then nodeAffinity
is the better choice.