How to create a pod on a specific node in kubernetes?

To create a Pod on a specific Node in Kubernetes, you can use either the NodeSelector field or NodeAffinity rules to specify the desired Node for the Pod. Here are examples of both methods:

  1. Utilizing the NodeSelector field:
  2. In the description file of the Pod, add a nodeSelector field and specify the labels of the Node to be used. For example, if you want to create a Pod on a Node with the label node-type: worker, you can add the following content to the Pod description file:
    apiVersion: v1
    kind: Pod
    metadata:
    name: my-pod
    spec:
    containers:
    – name: my-container
    image: my-image
    nodeSelector:
    node-type: worker
  3. Next, use the kubectl command to create a Pod by running:
    kubectl create -f pod.yaml
  4. This will create a Pod named my-pod on a Node with the label node-type: worker.
  5. Utilizing NodeAffinity rules:
  6. Add an affinity field to the description file of the Pod and specify the nodeAffinity rule. Here is an example:

    apiVersion: v1
    kind: Pod
    metadata:
    name: my-pod
    spec:
    containers:
    – name: my-container
    image: my-image
    affinity:
    nodeAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
    nodeSelectorTerms:
    – matchExpressions:
    – key: node-type
    operator: In
    values:
    – worker

  7. Next, use the kubectl command to create a Pod by running:
    kubectl create -f pod.yaml
  8. This will create a Pod named my-pod on a Node labeled node-type: worker.

Please choose one of the methods based on your needs, and modify the sample files accordingly according to your cluster configuration and label settings.

bannerAds