Kubernetes Pod Scheduling This section describes two scheduling methods

Method 1: Use NodeName for POD scheduling:

Pod.spec.nodeName is used to enforce a scheduling constraint on a Node, but a Pod with nodeName skips Scheduler’s scheduling logic and writes to the PodList.

[root@k8s-master ~]# vim nginxrc.yaml apiVersion: v1 kind: ReplicationController metadata: name: my-nginx spec: Replicas: 1 template: metadata: labels: app: nginx spec: nodeName: k8S-Node-1 # Specifies the scheduling nodes as k8S-Node-1 containers: - name: nginx image: daocloud.io/library/nginx:latest ports: - containerPort: 80Copy the code

Create the rc:

slightlyCopy the code

Check which node pod is running on:

[root@k8s-master ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE my-nginx-dwp13 1/1 Running 0 21m 10.0.6.2 k8s - node - 1Copy the code

Method 2: Use NodeSelector for POD scheduling

Pod.spec.nodeSelector selects nodes through the label-selector mechanism of Kubernetes. The scheduler scheduling policy MatchNodeSelector matches labels and schedules pods to target nodes. The matching rule is a mandatory constraint. To enable a node selector, perform the following steps:

1. Add the label to the Node

Marking rules: Kubectl label nodes <node-name> <label-key>=<label-value> #kubectl label nodes k8s.node1 cloudnil.com/role=dev [email protected]:~# kubectl get nodes k8s.node1 --show-labels NAME STATUS AGE LABELS k8s.node1 Ready 29d beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,cloudnil.com/role=dev,kubernetes.io/hostname=k8s.node1Copy the code

2. Add nodeSelector to the Pod definition

[root@k8s-master ~]# vim nginxrc.yaml apiVersion: v1 kind: ReplicationController metadata: name: my-nginx spec: replicas: 1 template: metadata: labels: app: nginx spec: nodeSelector: cloudnil.com/role: Dev# specify the scheduling nodes as nodes with the label: cloudnil.com/role=dev. daocloud.io/library/nginx:latest ports: - containerPort: 80Copy the code

Create the rc:

slightlyCopy the code

Check which node pod is running on:

[root@k8s-master ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE my-nginx-zttrj 1/1 Running 0 5s 10.0.6.2 k8s - node - 1Copy the code

Ignore this sentence below: first is the key to the spec. The selector and the spec. The template. The metadata. Labels, these two fields must be the same, otherwise, the next step to create the RC will fail. (can also don’t write specs. The selector, so the default with the spec. The template. The metadata. The labels the same)