【Kubernetes 017】pod调度之nodename和nodeselector

和前面学习的Affinity类似的,还可以直接在pod的声明中指定一个node去调度。这种方式简单粗暴,我们一起来学习看。

我是T型人小付,一位坚持终身学习的互联网从业者。喜欢我的博客欢迎在csdn上关注我,如果有问题欢迎在底下的评论区交流,谢谢。

文章目录

    • 实践操作
      • 单个node
      • node标签选择器
    • 总结

实践操作

固定节点因为比较简单粗暴,所以直接上操作。分为两类,一类是指定单个node,另一类是指定node的label。

以下所有yaml文件托管在我的Github仓库

单个node

直接在pod.spec.nodeName字段声明node的名字即可。

通过下面的yaml文件test-nodename.yaml来创建deployment

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: test-nodename
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: deployment-app
    spec:
      nodeName: k8s-node1  
      containers:
        - name: mynginx
          image: mynginx:v2
          ports:
            - containerPort: 80

所有的pod都在node1

[root@k8s-master nodename]# kubectl apply -f test-nodename.yaml
deployment.extensions/test-nodename created
[root@k8s-master nodename]# kubectl get pod -o wide
NAME                             READY   STATUS    RESTARTS   AGE   IP             NODE        NOMINATED NODE   READINESS GATES
test-nodename-6f8c4686f5-4vx9h   1/1     Running   0          5s    10.244.1.143   k8s-node1              
test-nodename-6f8c4686f5-59mq5   1/1     Running   0          5s    10.244.1.142   k8s-node1              
test-nodename-6f8c4686f5-rzs8c   1/1     Running   0          5s    10.244.1.141   k8s-node1              

即使扩容也是

[root@k8s-master nodename]# kubectl scale --replicas=7 deployment/test-nodename
deployment.extensions/test-nodename scaled
[root@k8s-master nodename]# kubectl get pod -o wide
NAME                             READY   STATUS    RESTARTS   AGE    IP             NODE        NOMINATED NODE   READINESS GATES
test-nodename-6f8c4686f5-4vx9h   1/1     Running   0          115s   10.244.1.143   k8s-node1              
test-nodename-6f8c4686f5-59mq5   1/1     Running   0          115s   10.244.1.142   k8s-node1              
test-nodename-6f8c4686f5-82trn   1/1     Running   0          4s     10.244.1.145   k8s-node1              
test-nodename-6f8c4686f5-rzs8c   1/1     Running   0          115s   10.244.1.141   k8s-node1              
test-nodename-6f8c4686f5-sr6q5   1/1     Running   0          4s     10.244.1.146   k8s-node1              
test-nodename-6f8c4686f5-tvx7g   1/1     Running   0          4s     10.244.1.147   k8s-node1              
test-nodename-6f8c4686f5-z7lcf   1/1     Running   0          4s     10.244.1.144   k8s-node1              

测试一下如果对node1加上污点会是啥情况

警告:下面的操作可能会很危险,谨慎尝试

kubectl taint nodes k8s-node1 xiaofu=good:NoExecute

之后这些pod因为污点而被销毁,然后又因为Deployment而被自动创建,然后又被销毁,于是满屏幕都是Terminating

[root@k8s-master nodename]# kubectl get pod -o wide
NAME                             READY   STATUS        RESTARTS   AGE     IP             NODE        NOMINATED NODE   READINESS GATES
test-nodename-6f8c4686f5-22dr7   0/1     Terminating   0          4m1s             k8s-node1              
test-nodename-6f8c4686f5-26548   0/1     Terminating   0          4m18s            k8s-node1              
test-nodename-6f8c4686f5-27wsv   0/1     Terminating   0          4m12s            k8s-node1              
test-nodename-6f8c4686f5-28vpt   0/1     Terminating   0          4m13s            k8s-node1              
test-nodename-6f8c4686f5-29gpl   0/1     Terminating   0          4m28s            k8s-node1              
test-nodename-6f8c4686f5-29lnj   0/1     Terminating   0          4m21s            k8s-node1              
test-nodename-6f8c4686f5-2b5dc   0/1     Terminating   0          4m17s            k8s-node1              
test-nodename-6f8c4686f5-2fqw2   0/1     Terminating   0          4m20s            k8s-node1              
test-nodename-6f8c4686f5-2ksnn   0/1     Terminating   0          4m2s             k8s-node1              
test-nodename-6f8c4686f5-2r2gs   0/1     Terminating   0          4m20s            k8s-node1              
test-nodename-6f8c4686f5-2st4d   0/1     Terminating   0          4m19s            k8s-node1              
test-nodename-6f8c4686f5-2xdvr   0/1     Terminating   0          4m27s            k8s-node1              
test-nodename-6f8c4686f5-2xphz   0/1     Terminating   0          4m20s            k8s-node1              
test-nodename-6f8c4686f5-44tps   0/1     Terminating   0          4m15s            k8s-node1              
test-nodename-6f8c4686f5-4gbpp   0/1     Terminating   0          4m22s            k8s-node1              
test-nodename-6f8c4686f5-4gr6m   0/1     Terminating   0          4m17s            k8s-node1              
test-nodename-6f8c4686f5-4jb6w   0/1     Terminating   0          4m16s            k8s-node1              
test-nodename-6f8c4686f5-4lbms   0/1     Terminating   0          4m6s             k8s-node1              
test-nodename-6f8c4686f5-4m4fm   0/1     Terminating   0          4m25s            k8s-node1              
test-nodename-6f8c4686f5-4mcsk   0/1     Terminating   0          4m17s            k8s-node1              
...
...

赶紧去掉污点,删除deployment,等着几百个pod自己慢慢销毁吧。

node标签选择器

也可以在字段pod.spec.nodeSelector中指定node要满足的label。

通过下面的yaml文件test-nodeselector.yaml创建deployment

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: test-nodeselector
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: deployment-app
    spec:
      nodeSelector:
        level: boss
      containers:
        - name: mynginx
          image: mynginx:v2
          ports:
            - containerPort: 80

这里希望node有level:boss的标签,但是没有一个node有这个标签,于是所有pod处于pending状态

[root@k8s-master nodename]# kubectl apply -f test-nodeselector.yaml
deployment.extensions/test-nodeselector created
[root@k8s-master nodename]# kubectl get pod -o wide
NAME                               READY   STATUS    RESTARTS   AGE   IP       NODE     NOMINATED NODE   READINESS GATES
test-nodeselector-b8fd4ccf-2sc6c   0/1     Pending   0          4s                     
test-nodeselector-b8fd4ccf-8xjtl   0/1     Pending   0          4s                     
test-nodeselector-b8fd4ccf-mcn8l   0/1     Pending   0          4s                     

给node1打一个label之后成功被调度

[root@k8s-master nodename]# kubectl label node k8s-node1 level=boss
node/k8s-node1 labeled
[root@k8s-master nodename]# kubectl get pod -o wide
NAME                               READY   STATUS    RESTARTS   AGE   IP             NODE        NOMINATED NODE   READINESS GATES
test-nodeselector-b8fd4ccf-2sc6c   1/1     Running   0          87s   10.244.1.175   k8s-node1              
test-nodeselector-b8fd4ccf-8xjtl   1/1     Running   0          87s   10.244.1.176   k8s-node1              
test-nodeselector-b8fd4ccf-mcn8l   1/1     Running   0          87s   10.244.1.174   k8s-node1              

删除label

[root@k8s-master nodename]# kubectl label node k8s-node1 level-
node/k8s-node1 labeled

总结

这样三种自定义调度pod的方式就都学习完了

  • Affinity/AntiAffinity
  • Taint/Toleration
  • Nodename/Nodeselector

你可能感兴趣的:(Devops,-,Kubernetes)