天天看点

k8s 节点的 NodeAffinity 使用

在 k8s 中,pod 会通过 kube-scheduler 按照节占先有的资源平均的调度到这些节点上,但有时候,我们需要将某个应用的pod调度到特定的节点上,

比如:两个应用需要频繁的进行通讯,那么我们希望将它们部署到同一个节点。或者希望访问一些类似需要ssd这样特殊资源的节点等应用场景。

最简单的方法是使用

nodeSelector

,但它比较简单粗暴,使用起来不能灵活调度,这个在后续版本中也会慢慢过实,所以我们一般用

nodeAffinity

来实现这些需求。

具体演示如下:

# 列出集群所有节点
-[[email protected] ~]$ kubectl get no
NAME           STATUS    ROLES     AGE       VERSION
.   Ready     <none>    d       v1.
.   Ready     <none>    d       v1.
.   Ready     <none>    d       v1.
           
# 删除之前打过的标签 tester=chenqiang
-[[email protected] ~]$ kubectl label no . tester-   
node "10.130.14.67" labeled

# 重新打标签 tester=chenqiang
-[[email protected] ~]$ kubectl label no . tester=chenqiang
node "10.130.14.67" labeled

# 查看所有标签
-[[email protected] ~]$ kubectl get no --show-labels                 
NAME           STATUS    ROLES     AGE       VERSION   LABELS
.   Ready     <none>    d       v1.    beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,ingress-nginx=true,kubernetes.io/hostname=.,tester=chenqiang
.   Ready     <none>    d       v1.    beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,ingress-nginx=true,kubernetes.io/hostname=.
.   Ready     <none>    d       v1.    beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=.

# 通过特定标签进行过滤节点
-[[email protected] ~]$ kubectl get no -l tester=chenqiang
NAME           STATUS    ROLES     AGE       VERSION
.   Ready     <none>    d       v1.
           

如果我不希望pod调度到打了

tester=chenqiang

这个标签的 node,那么需要使用

NotIn

这个介词,否则使用

In

假设不希望调度,则添加如下的 nodeAffinity:

spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: tester
                operator: NotIn
                values:
                - chenqiang
           

一个完整的例子如下:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-hello-deployment
  namespace: chenqiang-ns1
  labels:
    app: nginx-hello
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx-hello
  template:
    metadata:
      labels:
        app: nginx-hello
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: tester
                operator: NotIn
                values:
                - chenqiang
      containers:
      - name: nginx-hello
        image: docker-registry.saicstack.com/chenqiang/nginx-hello:v2.0
        ports:
        - containerPort: 
      imagePullSecrets:
      - name: regcred
           

从测试结果看,果然没有调度到

10.130.14.67

这个节点。

-[[email protected] ~]$ kubectl -n chenqiang-ns1 get po -o wide    
NAME                                     READY     STATUS    RESTARTS   AGE       IP               NODE
nginx-hello-deployment-f99df9d-fvz   /       Running             m       .     .
nginx-hello-deployment-f99df9d-mj6xs   /       Running             m       .   .
           

继续阅读