【云原生 | 从零开始学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 节点的时候匹配的条件


相关实践学习
通过Ingress进行灰度发布
本场景您将运行一个简单的应用,部署一个新的应用用于新的发布,并通过Ingress能力实现灰度发布。
容器应用与集群管理
欢迎来到《容器应用与集群管理》课程,本课程是“云原生容器Clouder认证“系列中的第二阶段。课程将向您介绍与容器集群相关的概念和技术,这些概念和技术可以帮助您了解阿里云容器服务ACK/ACK Serverless的使用。同时,本课程也会向您介绍可以采取的工具、方法和可操作步骤,以帮助您了解如何基于容器服务ACK Serverless构建和管理企业级应用。 学习完本课程后,您将能够: 掌握容器集群、容器编排的基本概念 掌握Kubernetes的基础概念及核心思想 掌握阿里云容器服务ACK/ACK Serverless概念及使用方法 基于容器服务ACK Serverless搭建和管理企业级网站应用
目录
相关文章
|
2天前
|
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停止抓包。
27 12
|
14天前
|
监控 Kubernetes Cloud Native
基于阿里云容器服务Kubernetes版(ACK)的微服务架构设计与实践
本文介绍了如何基于阿里云容器服务Kubernetes版(ACK)设计和实现微服务架构。首先概述了微服务架构的优势与挑战,如模块化、可扩展性及技术多样性。接着详细描述了ACK的核心功能,包括集群管理、应用管理、网络与安全、监控与日志等。在设计基于ACK的微服务架构时,需考虑服务拆分、通信、发现与负载均衡、配置管理、监控与日志以及CI/CD等方面。通过一个电商应用案例,展示了用户服务、商品服务、订单服务和支付服务的具体部署步骤。最后总结了ACK为微服务架构提供的强大支持,帮助应对各种挑战,构建高效可靠的云原生应用。
|
2月前
|
存储 运维 Kubernetes
正式开源,Doris Operator 支持高效 Kubernetes 容器化部署方案
飞轮科技推出了 Doris 的 Kubernetes Operator 开源项目(简称:Doris Operator),并捐赠给 Apache 基金会。该工具集成了原生 Kubernetes 资源的复杂管理能力,并融合了 Doris 组件间的分布式协同、用户集群形态的按需定制等经验,为用户提供了一个更简洁、高效、易用的容器化部署方案。
正式开源,Doris Operator 支持高效 Kubernetes 容器化部署方案
|
3月前
|
存储 Kubernetes Docker
Kubernetes(k8s)和Docker Compose本质区别
理解它们的区别和各自的优势,有助于选择合适的工具来满足特定的项目需求。
310 19
|
3月前
|
Kubernetes 应用服务中间件 nginx
二进制安装Kubernetes(k8s)v1.32.0
本指南提供了一个详细的步骤,用于在Linux系统上通过二进制文件安装Kubernetes(k8s)v1.32.0,支持IPv4+IPv6双栈。具体步骤包括环境准备、系统配置、组件安装和配置等。
1073 11
|
1月前
|
弹性计算 JavaScript 前端开发
一键安装!阿里云新功能部署Nodejs环境到ECS竟然如此简单!
Node.js 是一种高效的 JavaScript 运行环境,基于 Chrome V8 引擎,支持在服务器端运行 JavaScript 代码。本文介绍如何在阿里云上一键部署 Node.js 环境,无需繁琐配置,轻松上手。前提条件包括 ECS 实例运行中且操作系统为 CentOS、Ubuntu 等。功能特点为一键安装和稳定性好,支持常用 LTS 版本。安装步骤简单:登录阿里云控制台,选择扩展程序管理页面,安装 Node.js 扩展,选择实例和版本,等待创建完成并验证安装成功。通过阿里云的公共扩展,初学者和经验丰富的开发者都能快速进入开发状态,开启高效开发之旅。
|
13天前
|
JavaScript 前端开发 数据可视化
【01】Cocos游戏开发引擎从0开发一款游戏-cocos环境搭建以及配置-Cocos Creator软件系统下载安装-node环境-优雅草卓伊凡
【01】Cocos游戏开发引擎从0开发一款游戏-cocos环境搭建以及配置-Cocos Creator软件系统下载安装-node环境-优雅草卓伊凡
27 2
【01】Cocos游戏开发引擎从0开发一款游戏-cocos环境搭建以及配置-Cocos Creator软件系统下载安装-node环境-优雅草卓伊凡
|
1月前
|
弹性计算 JavaScript 前端开发
一键安装!阿里云新功能部署Nodejs环境到ECS竟然如此简单!
一键安装!阿里云新功能部署Nodejs环境到ECS竟然如此简单!
一键安装!阿里云新功能部署Nodejs环境到ECS竟然如此简单!
|
2月前
|
JavaScript
nodejs安装之npm ERR! code CERT_HAS_EXPIREDnpm ERR! errno CERT_HAS_EXPIRED reason: certificate has expired-证书错误通用问题解决方案-优雅草央千澈
nodejs安装之npm ERR! code CERT_HAS_EXPIREDnpm ERR! errno CERT_HAS_EXPIRED reason: certificate has expired-证书错误通用问题解决方案-优雅草央千澈
377 26
|
4月前
|
Web App开发 JavaScript 前端开发
2024年5月node.js安装(winmac系统)保姆级教程
本篇博客为2024年5月版Node.js安装教程,适用于Windows和Mac系统。作者是一名熟悉JavaScript与Vue的大一学生,分享了Node.js的基本介绍、下载链接及简单安装步骤。安装完成后,通过终端命令`node -v`验证版本即可确认安装成功。欢迎关注作者,获取更多技术文章。
64 2
2024年5月node.js安装(winmac系统)保姆级教程

热门文章

最新文章