NodeSelector

nodeSelector 允许我们添加有关在特定工作节点中运行 pod 的约束。

用例:

  • 应用程序需要更快的磁盘才能有效运行。
  • 在有SSD的节点上运行AppA。

实践

根据节点类型向节点添加标签:

  • disk: hdd
  • disk: ssd

image-20200122153741513

将三个node分别打上标签,第一个和第三个为hdd,第二个为ssd:

[@BDSZYF000132741:Downloads]$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
ip-192-168-175-56.us-west-2.compute.internal Ready <none> 130d v1.13.8-eks-cd3eb0
ip-192-168-209-134.us-west-2.compute.internal Ready <none> 131d v1.13.8-eks-cd3eb0
ip-192-168-87-237.us-west-2.compute.internal Ready <none> 131d v1.13.8-eks-cd3eb0
[@BDSZYF000132741:Downloads]$ kubectl label node ip-192-168-175-56.us-west-2.compute.internal disk=hdd
node/ip-192-168-175-56.us-west-2.compute.internal labeled
[@BDSZYF000132741:Downloads]$ kubectl label node ip-192-168-209-134.us-west-2.compute.internal disk=ssd
node/ip-192-168-209-134.us-west-2.compute.internal labeled
[@BDSZYF000132741:Downloads]$ kubectl label node ip-192-168-87-237.us-west-2.compute.internal disk=hdd
node/ip-192-168-87-237.us-west-2.compute.internal labeled

创建一个nodeSelector配置,仅在标签为“disk=ssd”的节点上运行pod:

apiVersion: v1
kind: Pod
metadata:
  name: service-pod
spec:
  containers:
    - name: service-pod
      image: nginx
  nodeSelector:
    disk: ssd

kubectl get pods -o wide

service-pod 1/1 Running 0 13s 192.168.197.75 ip-192-168-209-134.us-west-2.compute.internal <none> <none>

是在第二个node192-168-209-134上运行