[root@k8s-master mainfests]# kubectl get pods --show-labels #查看pod标签 NAME READY STATUS RESTARTS AGE LABELS pod-demo 2/2 Running 0 25s app=myapp,tier=frontend
[root@k8s-master mainfests]# kubectl get pods -l app #过滤包含app的标签 NAME READY STATUS RESTARTS AGE pod-demo 2/2 Running 0 1m [root@k8s-master mainfests]# kubectl get pods -L app NAME READY STATUS RESTARTS AGE APP pod-demo 2/2 Running 0 1m myapp
[root@k8s-master mainfests]# kubectl label pods pod-demo release=canary #给pod-demo打上标签 pod/pod-demo labeled [root@k8s-master mainfests]# kubectl get pods -l app --show-labels NAME READY STATUS RESTARTS AGE LABELS pod-demo 2/2 Running 0 1m app=myapp,release=canary,tier=frontend
[root@k8s-master mainfests]# kubectl label pods pod-demo release=stable --overwrite #修改标签 pod/pod-demo labeled [root@k8s-master mainfests]# kubectl get pods -l release NAME READY STATUS RESTARTS AGE pod-demo 2/2 Running 0 2m [root@k8s-master mainfests]# kubectl get pods -l release,app NAME READY STATUS RESTARTS AGE pod-demo 2/2 Running 0 2m
标签选择器
等值关系标签选择器:=, == , != (kubectl get pods -l app=test,app=dev)
集合关系标签选择器: KEY in (v1,v2,v3), KEY notin (v1,v2,v3) !KEY (kubectl get pods -l “app in (test,dev)”)
操作符: in notin:Values字段的值必须是非空列表 Exists NotExists: Values字段的值必须是空列表
节点标签选择器
1 2 3 4 5 6 7 8 9 10 11
[root@k8s-master mainfests]# kubectl explain pod.spec nodeName <string> NodeName is a request to schedule this pod onto a specific node. If it is non-empty, the scheduler simply schedules this pod onto that node, assuming that it fits resource requirements.
nodeSelector <map[string]string> NodeSelector is a selector which must be true for the pod to fit on a node. Selector which must match a node's labels for the pod to be scheduled on that node. More info: https://kubernetes.io/docs/concepts/configuration/assign-pod-node/
(3)重新创建pod-demo,可以看到固定调度在k8s-node01节点上 [root@k8s-master mainfests]# kubectl delete -f pod-demo.yaml pod "pod-demo" deleted
[root@k8s-master mainfests]# kubectl create -f pod-demo.yaml pod/pod-demo created [root@k8s-master mainfests]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE pod-demo 2/2 Running 0 20s 10.244.1.13 k8s-node01 [root@k8s-master mainfests]# kubectl describe pod pod-demo ...... Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 42s default-scheduler Successfully assigned default/pod-demo to k8s-node01 ......