研发工程师玩转Kubernetes——使用Node特性定向调度Pod

这篇具有很好参考价值的文章主要介绍了研发工程师玩转Kubernetes——使用Node特性定向调度Pod。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

在《研发工程师玩转Kubernetes——使用污点(taint)驱逐Pod》中我们提到亲和性(affinity)中的requiredDuringSchedulingIgnoredDuringExecution,它可以定向调度Pod。本节我们将使用相关特性完成定向调度的介绍。

nodeSelector定向调度

我们先模拟Pod被部署在Master Node上的场景。这个时候我们可以使用nodeSelector对Node的Labels做选择。
首先我们查看当前的Node的情况:

查看Node的Labels

get nodes --show-labels
NAME      STATUS   ROLES    AGE   VERSION   LABELS
ubuntuc   Ready    <none>   16m   v1.26.4   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=ubuntuc,kubernetes.io/os=linux,microk8s.io/cluster=true,node.kubernetes.io/microk8s-worker=microk8s-worker
ubuntue   Ready    <none>   15m   v1.26.4   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=ubuntue,kubernetes.io/os=linux,microk8s.io/cluster=true,node.kubernetes.io/microk8s-worker=microk8s-worker
ubuntud   Ready    <none>   15m   v1.26.4   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=ubuntud,kubernetes.io/os=linux,microk8s.io/cluster=true,node.kubernetes.io/microk8s-worker=microk8s-worker
ubuntub   Ready    <none>   16m   v1.26.4   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=ubuntub,kubernetes.io/os=linux,microk8s.io/cluster=true,node.kubernetes.io/microk8s-worker=microk8s-worker
ubuntua   Ready    <none>   20m   v1.27.2   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=ubuntua,kubernetes.io/os=linux,microk8s.io/cluster=true,node.kubernetes.io/microk8s-controlplane=microk8s-controlplane

可以看到只有Master Node(UbuntuA)的Labels含有node.kubernetes.io/microk8s-controlplane=microk8s-controlplane,我们就用这个去做条件选择。

清单文件指定Label

# nginx_deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx-container
        image: nginx
        ports:
        - containerPort: 80
      nodeSelector:
        node.kubernetes.io/microk8s-controlplane: microk8s-controlplane

注意最后两行,即使用nodeSelector让Pod在Labels含有node.kubernetes.io/microk8s-controlplane: microk8s-controlplane的Node上部署。

NodeSelector map[string]string `json:"nodeSelector,omitempty"`

部署

针对上述文件部署Pod

kubectl create -f nginx_deployment.yaml

deployment.apps/nginx-deployment created

kubectl get pods -o wide
NAME                                READY   STATUS    RESTARTS   AGE   IP           NODE      NOMINATED NODE   READINESS GATES
nginx-deployment-5d5bc8fb96-75c9d   1/1     Running   0          67s   10.1.94.67   ubuntua   <none>           <none>

可以看到Pod被部署到Master Node上了。

requiredDuringSchedulingIgnoredDuringExecution定向调度

requiredDuringSchedulingIgnoredDuringExecution是亲和性(affinity)下的一种类型,它表示:只有在规则被满足的时候才能执行调度。
我们修改下清单文件,让Pod只能被调度到Master Node(UbuntuA)上。

清单文件指定亲和性

# nginx_deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: node.kubernetes.io/microk8s-controlplane
                operator: In
                values:
                - microk8s-controlplane
      containers:
      - name: nginx-container
        image: nginx
        ports:
        - containerPort: 80

nodeSelectorTerms只能是一组matchExpressions。

type NodeAffinity struct {
	// 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 a node label update),
	// the system will try to eventually evict the pod from its node.
	RequiredDuringSchedulingRequiredDuringExecution *NodeSelector  `json:"requiredDuringSchedulingRequiredDuringExecution,omitempty"`
	// 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 a node label update),
	// the system may or may not try to eventually evict the pod from its node.
	RequiredDuringSchedulingIgnoredDuringExecution  *NodeSelector  `json:"requiredDuringSchedulingIgnoredDuringExecution,omitempty"`
	// The scheduler will prefer to schedule pods to nodes that satisfy
	// the affinity expressions specified by this field, but it may choose
	// a node that violates one or more of the expressions. The node that is
	// most preferred is the one with the greatest sum of weights, i.e.
	// for each node that meets all of the scheduling requirements (resource
	// request, RequiredDuringScheduling affinity expressions, etc.),
	// compute a sum by iterating through the elements of this field and adding
	// "weight" to the sum if the node matches the corresponding MatchExpressions; the
	// node(s) with the highest sum are the most preferred.
	PreferredDuringSchedulingIgnoredDuringExecution []PreferredSchedulingTerm  `json:"preferredDuringSchedulingIgnoredDuringExecution,omitempty"`
}

// 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 nodeSelectorTerms.
type NodeSelector struct {
	// nodeSelectorTerms is a list of node selector terms. The terms are ORed.
	NodeSelectorTerms []NodeSelectorTerm `json:"nodeSelectorTerms,omitempty"`
}

// An empty node selector term matches all objects. A null node selector term
// matches no objects.
type NodeSelectorTerm struct {
	// matchExpressions is a list of node selector requirements. The requirements are ANDed.
	MatchExpressions []NodeSelectorRequirement `json:"matchExpressions,omitempty"`
}

// A node selector requirement is a selector that contains values, a key, and an operator
// that relates the key and values.
type NodeSelectorRequirement struct {
	// key is the label key that the selector applies to.
	Key string `json:"key" patchStrategy:"merge" patchMergeKey:"key"`
	// operator represents a key's relationship to a set of values.
	// Valid operators are In, NotIn, Exists, DoesNotExist. Gt, and Lt.
	Operator NodeSelectorOperator `json:"operator"`
	// values is an array of string values. If the operator is In or NotIn,
	// the values array must be non-empty. If the operator is Exists or DoesNotExist,
	// the values array must be empty. If the operator is Gt or Lt, the values
	// array must have a single element, which will be interpreted as an integer.
    // This array is replaced during a strategic merge patch.
	Values []string `json:"values,omitempty"`
}

// A node selector operator is the set of operators that can be used in
// a node selector requirement.
type NodeSelectorOperator string

const (
	NodeSelectorOpIn           NodeSelectorOperator = "In"
	NodeSelectorOpNotIn        NodeSelectorOperator = "NotIn"
	NodeSelectorOpExists       NodeSelectorOperator = "Exists"
	NodeSelectorOpDoesNotExist NodeSelectorOperator = "DoesNotExist"
	NodeSelectorOpGt           NodeSelectorOperator = "Gt"
	NodeSelectorOpLt           NodeSelectorOperator = "Lt"
)

观察

get pod --watch -o wide
NAME                                READY   STATUS    RESTARTS   AGE   IP       NODE     NOMINATED NODE   READINESS GATES
nginx-deployment-549db84c68-gk66n   0/1     Pending             0          0s      <none>        <none>    <none>           <none>
nginx-deployment-549db84c68-gk66n   0/1     Pending             0          0s      <none>        ubuntua   <none>           <none>
nginx-deployment-549db84c68-gk66n   0/1     ContainerCreating   0          0s      <none>        ubuntua   <none>           <none>
nginx-deployment-549db84c68-gk66n   0/1     ContainerCreating   0          1s      <none>        ubuntua   <none>           <none>
nginx-deployment-549db84c68-gk66n   1/1     Running             0          4s      10.1.94.80    ubuntua   <none>           <none>

可以看到Pod是在Worker Node(UbuntuA)上部署着。文章来源地址https://www.toymoban.com/news/detail-477565.html

参考资料

  • https://kubernetes.io/zh-cn/docs/concepts/scheduling-eviction/assign-pod-node/
  • https://github.com/kubernetes/design-proposals-archive/blob/main/scheduling/podaffinity.md

到了这里,关于研发工程师玩转Kubernetes——使用Node特性定向调度Pod的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 研发工程师玩转Kubernetes——启动、存活和就绪探针

    启动(Startup Probe)、存活(Liveness Probe)和就绪探针(Readiness Probe)有其不同的用途和优先级。 启动探针(Startup Probe)用于Pod内程序告诉kubernetes,其准备工作已经做好。这些准备工作主要是指业务运行前的前置条件,比如资源文件下载完毕,内置数据库文件下载完毕等。这步

    2024年02月13日
    浏览(25)
  • 【个人介绍】后端研发工程师

    1. 熟悉JAVA基础、多线程、并发编程、设计模式、数据结构与算法、JVM原理与调优等。 2. 熟悉SpringBoot、SpringCloud、SpringCloud Alibaba、JPA、MyBatis等框架。 3. 熟悉数据库设计、MySQL原理与索引优化、Redis、Memecache、MongoDB等。 4. 熟悉Nginx、主从集群、TCP/IP、NIO编程、Reactor模式、Nett

    2023年04月09日
    浏览(42)
  • 一些研发工程师在Springboot注意点

    1. 正确设计代码目录结构 虽然您有很大的自由度,但有一些基本规则值得遵循来设计您的源代码结构。 避免使用默认包。确保所有内容(包括入口点)都在命名良好的包中,这样您就可以避免与组装和组件扫描相关的意外情况; 将 Application.java(应用程序的入口类)放在顶

    2024年02月08日
    浏览(31)
  • 技术精英求职必备:大数据研发工程师简历模板

    原则 撰写针对大数据研发工程师职位的简历时,关键在于准确展现您在 大数据处理、分析和应用开发方面的综合技能、项目经验和技术成就 。简历应作为您展示跨多个技术栈的 编程能力、构建完整大数据解决方案和有效解决大数据技术问题 的能力的窗口。确保每一项经历

    2024年02月21日
    浏览(39)
  • 《研发效能(DevOps)工程师》课程简介(二)丨IDCF

    为贯彻落实《关于深化人才发展体制机制改革的意见》,推动实施人才强国战略,促进专业技术人员提升职业素养、补充新知识新技能,实现人力资源深度开发,推动经济社会全面发展,根据《中华人民共和国劳动法》有关规定,工业和信息化部教育与考试中心联合有关部门

    2024年02月07日
    浏览(37)
  • 技术精英求职必备:AIGC(图像生成)研发工程师简历指南

    核心原则 撰写针对AIGC(图像生成)研发工程师职位的简历时,关键在于准确展现您在深度学习、计算机视觉和图像生成算法方面的综合技能、项目经验和技术成就。简历应作为您展示跨多个技术栈的编程能力、构建完整的图像生成解决方案和有效解决AIGC技术问题的能力的窗口

    2024年03月25日
    浏览(48)
  • 【社招】【中国电信-天翼云】诚聘高级FPGA研发工程师

    基础架构部门介绍: 天翼云是中国电信旗下云计算品牌,致力于成为领先的云计算服务提供商。 基础架构部作为天翼云的核心部门,负责构建天翼云的整个物理基础设施。打造了包括紫金DPU、物理服务器、物理网络、RDMA网络、操作系统、虚拟化、IDC在内的核心底座产品。基

    2024年02月03日
    浏览(33)
  • 阿里云研发工程师刘睿:阿里云消息生态及最佳实践

    2022 年 9 月 24 日,阿里云用户组(AUG)第 12 期活动在厦门举办。活动现场,阿里云消息中间件研发工程师刘睿,向参会企业代表分享了阿里云消息生态及最佳实践。本文根据演讲内容整理而成。 众所周知,消息中间件作为现代软件体系中的底层基础软件,和数据库一样广泛

    2023年04月09日
    浏览(25)
  • 博冠光电8K事业部招聘研发工程师及产品经理

    一、高级结构工程师 工作职责:1、负责公司产品的机械结构设计、模具开发和量产导入;2、负责机械结构方向的技术预研,包括技术专题研究,技术难题攻关; 3、负责结构组的技术管理工作,包括带新人,材料选型,设计审核;4、领导安排的其他工作。任职要求:1.、本

    2024年02月12日
    浏览(25)
  • 突破职场竞争,引领未来发展:考取《研发效能(DevOps)工程师职业技术认证》

    就业形势堪忧,什么最有保障?考个“国家级”证书傍身吧! 工信部教考中心作为中国领先的行业技能认证机构,其颁发的认证证书不仅代表了个人在信息技术领域的专业能力,更可以录入工业和信息化技术技能人才数据库,这是一个重要的信息资源平台,它可以帮助企业和

    2024年02月05日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包