Pods can be scheduled to run on the desired node according to requirements, or not run on a certain node.
1 Check whether the existing node operating environment has labels
kubectl get nodes --show-labels
2 Add tags
#给节点node1打上标签env=uat
kubectl label node node1 env=uat
#给节点node2打上标签env=prod
kubectl label node node2 env=prod
#查看生成的标签
kubectl get node --show-labels
3 Specify the label to distribute the pod
Case 1: Specify the scheduling node when creating a pod
Add the following parameters to the yaml configuration
nodeSelector: #添加此行
env: uat #指定标签
1) Edit yaml file
$ cat nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-node1
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
nodeSelector: #添加此行
env: uat #指定标签
containers:
- name: nginx-node1
image: nginx:latest
ports:
- containerPort: 80
2) Execute to generate pod
$ kubectl apply -f nginx.yaml
3) Check which node the pod is distributed on
$ kubectl get pods -o wide
Case 2: Modify the generated pod and schedule it to the specified node
1) The current node pod has been restarted multiple times and cannot run normally. Try scheduling to other nodes.
$ kubectl get pod -n drugs-erp -o wide
2) Check the current label situation
$ kubectl get node --show-labels
3) Schedule to other nodes by modifying the deployment controller
$ kubectl get deploy -n drugs-erp
4) Modify control parameters
$ kubectl edit deploy drugs-erp-store -n drugs-erp -o yaml
#Also add the following parameters
nodeSelector: #添加此行
mmp: myj #指定标签
5) Re-schedule the new node and check that the pod is serving normally
$ kubectl get pod -n drugs-erp -o wide
5 Delete tags
#删除节点1标签env=uat
kubectl label node node1 env-
#删除节点2标签env=prod
kubectl label node node2 env-