【云原生 | 从零开始学Kubernetes】九、k8s的node节点选择器与node节点亲和性

简介: 我们在创建 pod 资源的时候,pod 会根据 schduler 进行调度,那么默认会调度到随机的一个工作节点,如果我们想要 pod 调度到指定节点或者调度到一些具有相同特点的 node 节点,怎么办呢? 可以使用 pod 中的 nodeName 或者 nodeSelector 字段指定要调度到的 node 节点

node 节点选择器


我们在创建 pod 资源的时候,pod 会根据 schduler 进行调度,那么默认会调度到随机的一个工作节点,如果我们想要 pod 调度到指定节点或者调度到一些具有相同特点的 node 节点,怎么办呢? 可以使用 pod 中的 nodeName 或者 nodeSelector 字段指定要调度到的 node 节点


1、nodeName


指定 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
apiVersion: v1
kind: Pod
metadata:
        name: pod-node-affinity-demo
        namespace: default
        labels:
            app: myapp
            tier: frontend
spec:
    containers:
    - name: myapp
      image: nginx
    affinity:
         nodeAffinity:
            requiredDuringSchedulingIgnoredDuringExecution:
                   nodeSelectorTerms:
                   - matchExpressions:
                     - key: zone
                       operator: In
                       values:
                       - foo
                       - bar


affinity:亲和性,下面的node是node亲和性,然后requ硬亲和性,nodeselect是对象列表,我们用-链接,然后match也是对象列表,同上,key是zone,然后等值关系,值是foo和bar。


这个yaml意思是:我们检查当前节点中有任意一个节点拥有 zone 标签的值是 foo 或者 bar,就可以把 pod 调度到这个 node 节点的 foo 或者 bar 标签上的节点上,现在找不到,因为没打标签!


[root@k8smaster node]# kubectl apply -f pod-nodeaffinity-demo.yaml 
pod/pod-node-affinity-demo created
[root@k8smaster node]# kubectl get pods -o wide | grep pod-node 
pod-node-affinity-demo        0/1     Pending   0          11s    <none>        <none>     <none>          
# status 的状态是 pending,上面说明没有完成调度,因为没有一个拥有 zone 的标签的值是 foo 或者 bar,而且使用的是硬亲和性,必须满足条件才能完成调度。
[root@k8smaster node]# kubectl label nodes k8snode zone=foo
node/k8snode labeled
#给这个节点打上标签 zone=foo,在查看 
[root@k8smaster node]# kubectl get pods -o wide 显示running了
pod-node-affinity-demo        1/1     Running   0          4m4s   10.244.2.19   k8snode    <none>           
例 2:使用 preferredDuringSchedulingIgnoredDuringExecution 软亲和性 
[root@k8smaster node]# vim pod-nodeaffinity-demo-2.yaml 
apiVersion: v1
kind: Pod
metadata:
        name: pod-node-affinity-demo-2
        namespace: default
        labels:
            app: myapp
            tier: frontend
spec:
    containers:
    - name: myapp
      image: nginx
    affinity:
        nodeAffinity:
            preferredDuringSchedulingIgnoredDuringExecution:
            - preference:
               matchExpressions:
               - key: zone1
                 operator: In
                 values:
                 - foo1
                 - bar1
              weight: 60
#用的还是node亲和性,然后是软亲和性,如果所有的工作节点都没有这个标签,pod还是会调度
[root@k8smaster node]# kubectl apply -f pod-nodeaffinity-demo-2.yaml 
pod/pod-node-affinity-demo-2 created
[root@k8smaster node]# kubectl get pods -o wide |grep demo-2
pod-node-affinity-demo-2      1/1     Running   0          29s     10.244.1.20   k8snode2   <none>          
#上面说明软亲和性是可以运行这个 pod 的,尽管没有运行这个 pod 的节点定义的 zone1 标签 
Node 节点亲和性针对的是 pod 和 node 的关系,Pod 调度到 node 节点的时候匹配的条件


相关实践学习
深入解析Docker容器化技术
Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。Docker是世界领先的软件容器平台。开发人员利用Docker可以消除协作编码时“在我的机器上可正常工作”的问题。运维人员利用Docker可以在隔离容器中并行运行和管理应用,获得更好的计算密度。企业利用Docker可以构建敏捷的软件交付管道,以更快的速度、更高的安全性和可靠的信誉为Linux和Windows Server应用发布新功能。 在本套课程中,我们将全面的讲解Docker技术栈,从环境安装到容器、镜像操作以及生产环境如何部署开发的微服务应用。本课程由黑马程序员提供。 &nbsp; &nbsp; 相关的阿里云产品:容器服务 ACK 容器服务 Kubernetes 版(简称 ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情: https://www.aliyun.com/product/kubernetes
目录
相关文章
|
存储 Kubernetes 网络安全
关于阿里云 Kubernetes 容器服务(ACK)添加镜像仓库的快速说明
本文介绍了在中国大陆地区因网络限制无法正常拉取 Docker 镜像的解决方案。作者所在的阿里云 Kubernetes 集群使用的是较旧版本的 containerd(1.2x),且无法直接通过 SSH 修改节点配置,因此采用了一种无需更改 Kubernetes 配置文件的方法。通过为 `docker.io` 添加 containerd 的镜像源,并使用脚本自动修改 containerd 配置文件中的路径错误(将错误的 `cert.d` 改为 `certs.d`),最终实现了通过多个镜像站点拉取镜像。作者还提供了一个可重复运行的脚本,用于动态配置镜像源。虽然该方案能缓解镜像拉取问题,
1433 3
|
存储 Kubernetes 监控
K8s集群实战:使用kubeadm和kuboard部署Kubernetes集群
总之,使用kubeadm和kuboard部署K8s集群就像回归童年一样,简单又有趣。不要忘记,技术是为人服务的,用K8s集群操控云端资源,我们不过是想在复杂的世界找寻简单。尽管部署过程可能遇到困难,但朝着简化复杂的目标,我们就能找到意义和乐趣。希望你也能利用这些工具,找到你的乐趣,满足你的需求。
1472 33
|
Kubernetes API 网络安全
当node节点kubectl 命令无法连接到 Kubernetes API 服务器
当Node节点上的 `kubectl`无法连接到Kubernetes API服务器时,可以通过以上步骤逐步排查和解决问题。首先确保网络连接正常,验证 `kubeconfig`文件配置正确,检查API服务器和Node节点的状态,最后排除防火墙或网络策略的干扰,并通过重启服务恢复正常连接。通过这些措施,可以有效解决与Kubernetes API服务器通信的常见问题,从而保障集群的正常运行。
1460 17
|
存储 人工智能 Kubernetes
ACK Gateway with AI Extension:面向Kubernetes大模型推理的智能路由实践
本文介绍了如何利用阿里云容器服务ACK推出的ACK Gateway with AI Extension组件,在Kubernetes环境中为大语言模型(LLM)推理服务提供智能路由和负载均衡能力。文章以部署和优化QwQ-32B模型为例,详细展示了从环境准备到性能测试的完整实践过程。
|
Kubernetes Shell Windows
【Azure K8S | AKS】在AKS的节点中抓取目标POD的网络包方法分享
在AKS中遇到复杂网络问题时,可通过以下步骤进入特定POD抓取网络包进行分析:1. 使用`kubectl get pods`确认Pod所在Node;2. 通过`kubectl node-shell`登录Node;3. 使用`crictl ps`找到Pod的Container ID;4. 获取PID并使用`nsenter`进入Pod的网络空间;5. 在`/var/tmp`目录下使用`tcpdump`抓包。完成后按Ctrl+C停止抓包。
696 12
|
存储 运维 Kubernetes
容器数据保护:基于容器服务 Kubernetes 版(ACK)备份中心实现K8s存储卷一键备份与恢复
阿里云ACK备份中心提供一站式容器化业务灾备及迁移方案,减少数据丢失风险,确保业务稳定运行。
|
存储 Cloud Native 数据处理
从嵌入式状态管理到云原生架构:Apache Flink 的演进与下一代增量计算范式
本文整理自阿里云资深技术专家、Apache Flink PMC 成员梅源在 Flink Forward Asia 新加坡 2025上的分享,深入解析 Flink 状态管理系统的发展历程,从核心设计到 Flink 2.0 存算分离架构,并展望未来基于流批一体的通用增量计算方向。
900 0
从嵌入式状态管理到云原生架构:Apache Flink 的演进与下一代增量计算范式
|
运维 监控 Cloud Native
从本土到全球,云原生架构护航灵犀互娱游戏出海
本文内容整理自「 2025 中企出海大会·游戏与互娱出海分论坛」,灵犀互娱基础架构负责人朱晓靖的演讲内容,从技术层面分享云原生架构护航灵犀互娱游戏出海经验。
1167 15
|
运维 监控 Cloud Native
从本土到全球,云原生架构护航灵犀互娱游戏出海
内容整理自「 2025 中企出海大会·游戏与互娱出海分论坛」,灵犀互娱基础架构负责人朱晓靖的演讲内容,从技术层面分享云原生架构护航灵犀互娱游戏出海经验。
|
Kubernetes Cloud Native 微服务
探索云原生技术:容器化与微服务架构的融合之旅
本文将带领读者深入了解云原生技术的核心概念,特别是容器化和微服务架构如何相辅相成,共同构建现代软件系统。我们将通过实际代码示例,探讨如何在云平台上部署和管理微服务,以及如何使用容器编排工具来自动化这一过程。文章旨在为开发者和技术决策者提供实用的指导,帮助他们在云原生时代中更好地设计、部署和维护应用。
794 59

推荐镜像

更多