【云原生 从零开始学Kubernetes】九、k8s的node节点选择器与node节点亲和性_nodeselectorrequirements的参数分别对应了什么

指定 pod 节点运行在哪个具体 node 上

#node1和2用docker下载tomcat busybox

[root@k8smaster node]# vim pod-node.yml 
apiVersion: v1 
kind: Pod 
metadata: 
  name: demo-pod
  namespace: default 
  labels: 
    app: myapp 
    env: dev 
spec: 
  nodeName: k8snode
  containers: 
  - name: tomcat-pod-java 
    ports: 
    - containerPort: 8080 
    image: tomcat
    imagePullPolicy: IfNotPresent 
  - name: busybox 
    image: busybox:latest 
    command: 
    - "/bin/sh" 
    - "-c" 
    - "sleep 3600" 
 
[root@k8smaster node]# kubectl apply -f pod-node.yml 
pod/demo-pod created

#查看 pod 调度到哪个节点 
[root@k8smaster node]# kubectl get pods -o wide 
NAME                          READY   STATUS    RESTARTS   AGE    IP            NODE       NOMINATED NODE  
demo-pod                      2/2     Running   0          35s    10.244.2.18   k8snode    <none>   

2、nodeSelector

指定 pod 调度到具有哪些标签的 node 节点上

#给 node 节点打标签,打个具有 disk=ceph 的标签
[root@k8smaster node]# kubectl describe nodes k8snode2 查看node属性
[root@k8smaster node]# kubectl label nodes k8snode2 disk=ceph
node/k8snode2 labeled
#然后再查看去label哪里就能看到了

#定义 pod 的时候指定要调度到具有 disk=ceph 标签的 node 上 
[root@k8smaster node]# vim pod-1.yaml 
apiVersion: v1 
kind: Pod 
metadata: 
  name: demo-pod-1 
  namespace: default 
  labels: 
    app: myapp 
    env: dev 
spec: 
  nodeSelector: 
    disk: ceph
  containers: 
  - name: tomcat-pod-java 
    ports: 
    - containerPort: 8080 
    image: tomcat
    imagePullPolicy: IfNotPresent 
 
[root@k8smaster node]# kubectl apply -f pod-1.yaml 
pod/demo-pod-1 created

#查看 pod 调度到哪个节点 
[root@k8smaster node]# kubectl get pods -o wide 
NAME                          READY   STATUS    RESTARTS   AGE     IP            NODE       NOMINATED NODE    demo-pod-1                    1/1     Running   0          8s      10.244.1.19   k8snode2   <none>         
#如果标签和nodename都有的话 优先选择好的node。

污点和污点容忍

污点容忍

污点容忍就是某个节点可能被调度,也可能不被调度

node 节点亲和性

node 节点亲和性调度:nodeAffinity 用帮助文档查看亲和性字段下面的东西

[root@k8smaster node]# kubectl explain pods.spec.affinity 
KIND:     Pod
VERSION:  v1

RESOURCE: affinity <Object>

DESCRIPTION:
     If specified, the pod's scheduling constraints

 Affinity is a group of affinity scheduling rules.

FIELDS:
 nodeAffinity <Object>
 Describes node affinity scheduling rules for the pod.

 podAffinity <Object>
 
 podAntiAffinity <Object>
#有node节点亲和性 pode节点亲和性等,然后我们再详细的看看nodeAffinity 。
 
[root@k8smaster node]# kubectl explain pods.spec.affinity.nodeAffinity 
KIND: Pod
VERSION: v1

RESOURCE: nodeAffinity <Object>

DESCRIPTION:
 Describes node affinity scheduling rules for the pod.

 Node affinity is a group of node affinity scheduling rules.

FIELDS:
 preferredDuringSchedulingIgnoredDuringExecution <[]Object>

 requiredDuringSchedulingIgnoredDuringExecution <Object>
 
#prefered 表示有节点尽量满足这个位置定义的亲和性,这不是一个必须的条件,软亲和性,没满足也可能调度。
#require 表示必须有节点满足这个位置定义的亲和性,这是个硬性条件,硬亲和性,没满足不可能调度。

[root@k8smaster node]# kubectl explain pods.spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution
KIND: Pod
VERSION: v1

RESOURCE: requiredDuringSchedulingIgnoredDuringExecution <Object>

DESCRIPTION:
 If the affinity requirements specified by this field are not met at
 scheduling time, the pod will not be scheduled onto the node. If the
 affinity requirements specified by this field cease to be met at some point
 during pod execution (e.g. due to an update), the system may or may not try
 to eventually evict the pod from its node.

 A node selector represents the union of the results of one or more label
 queries over a set of nodes; that is, it represents the OR of the selectors
 represented by the node selector terms.

FIELDS:
 nodeSelectorTerms <[]Object> -required- #必写字段,对象列表
 
[root@k8smaster node]# kubectl explain pods.spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution.nodeSelectorTermsKIND: Pod
VERSION: v1

RESOURCE: nodeSelectorTerms <[]Object>

DESCRIPTION:
 Required. A list of node selector terms. The terms are ORed.

 A null or empty node selector term matches no objects. The requirements of
 them are ANDed. The TopologySelectorTerm type implements a subset of the
 NodeSelectorTerm.

FIELDS:
 matchExpressions <[]Object> #匹配表达式的
 A list of node selector requirements by node's labels.

   matchFields	<[]Object>	#匹配字段的 
     A list of node selector requirements by node's fields.
 
[root@k8smaster node]# kubectl explain pods.spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution.nodeSelectorTerms.matchExpressions
KIND: Pod
VERSION: v1

RESOURCE: matchExpressions <[]Object>

DESCRIPTION:
 A list of node selector requirements by node's labels.

     A node selector requirement is a selector that contains values, a key, and
     an operator that relates the key and values.

FIELDS:
   key	<string> -required-		#检查 label
   
   operator	<string> -required-	#做等值选则还是不等值选则 

   values	<[]string>			#给定的值 
#在做node节点亲和性的时候,values是标签的值,他会通过op匹配相等的key或者不等的key。
 
 
例 1:使用 requiredDuringSchedulingIgnoredDuringExecution 硬亲和性 
#node1 node2都拉nginx
 
[root@k8smaster node]# vim pod-nodeaffinity-demo.yaml


![img](https://img-blog.csdnimg.cn/img_convert/cdc14d024fe5090d866ab841d5c1b40a.png)
![img](https://img-blog.csdnimg.cn/img_convert/eeda484eaef3858b8c8910abd487e6fd.png)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值