008.kubernets的调度系统之标签选择器

一 Kubernetes 调度简介

除了让 kubernetes 集群调度器自动为 pod 资源选择某个节点(默认调度考虑的是资源足够,并且 load 尽量平均),有些情况我们希望能更多地控制 pod 应该如何调度。比如,集群中有些机器的配置更好( SSD,更好的内存等),我们希望比较核心的服务(比如说数据库)运行在上面;或者某两个服务的网络传输很频繁,我们希望它们最好在同一台机器上,或者同一个机房。有一些特殊的应用,我们就希望它们能运行在我们指定的节点上,还有些情况下,我们希望某个应用在所有的节点上都能运行一份。

针对不同的应用场景,kubernetes内置了多种调度方式可供选择。包括标签选择器,daemonsets,节点亲和性,pod亲和性,污点与容忍等。

二 标签选择器

这种方式其实就是我们最常用的使用label的方式,给某一个node打上特定的标签,然后在启动pod的时候,通过nodeSelector指定要调度到的node节点的标签。

2.1  nodeSelector打标签

[[email protected] ingress]# kubectl get nodes

NAME              STATUS   ROLES    AGE     VERSION
192.168.132.131   Ready    master   3d18h   v1.17.0
192.168.132.132   Ready    <none>   3d18h   v1.17.0
192.168.132.133   Ready    <none>   3d18h   v1.17.0

[email protected] ingress]# kubectl get pods  -n ingress-nginx -o wide

NAME                                        READY   STATUS    RESTARTS   AGE   IP                NODE              NOMINATED NODE   READINESS GATES
nginx-ingress-controller-5c6985f9cc-wkngv   1/1     Running   0          22h   192.168.132.132   192.168.132.132   <none>           <none>

[[email protected] ingress]# kubectl get nodes --show-labels

NAME              STATUS   ROLES    AGE     VERSION   LABELS
192.168.132.131   Ready    master   3d18h   v1.17.0   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=192.168.132.131,kubernetes.io/os=linux,node-role.kubernetes.io/master=
192.168.132.132   Ready    <none>   3d18h   v1.17.0   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=192.168.132.132,kubernetes.io/os=linux
192.168.132.133   Ready    <none>   3d18h   v1.17.0   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=192.168.132.133,kubernetes.io/os=linux

默认为节点打的标签:系统版本,节点名称,系统架构

[[email protected] ingress]# vi nginx-controller.yaml

nodeSelector:
        kubernetes.io/os: linux

这个标签对于目前的条件,没有任何意义

2.2 重新添加一个唯一标签

nodeSelector:
        kubernetes.io/os: linux
        kubernetes.io/hostname: 192.168.132.133    #这是一个危矣标签

标签是可以是多个,而且是逻辑与的关系,即当所有的标签都满足,才能成立,如果都不满足,就会pengding

[[email protected] ingress]# kubectl apply -f nginx-controller.yaml

namespace/ingress-nginx unchanged
configmap/nginx-configuration unchanged
configmap/tcp-services unchanged
configmap/udp-services unchanged
serviceaccount/nginx-ingress-serviceaccount unchanged
clusterrole.rbac.authorization.k8s.io/nginx-ingress-clusterrole unchanged
role.rbac.authorization.k8s.io/nginx-ingress-role unchanged
rolebinding.rbac.authorization.k8s.io/nginx-ingress-role-nisa-binding unchanged
clusterrolebinding.rbac.authorization.k8s.io/nginx-ingress-clusterrole-nisa-binding unchanged
deployment.apps/nginx-ingress-controller configured
limitrange/ingress-nginx configured

[[email protected] ingress]# kubectl get pods  -n ingress-nginx -o wide

NAME                                        READY   STATUS              RESTARTS   AGE   IP                NODE              NOMINATED NODE   READINESS GATES
nginx-ingress-controller-5c6985f9cc-wkngv   1/1     Running             0          22h   192.168.132.132   192.168.132.132   <none>           <none>
nginx-ingress-controller-5cffd956bf-dm9qf   0/1     ContainerCreating   0          4s    192.168.132.133   192.168.132.133   <none>           <none>

[[email protected] ingress]# kubectl get pods  -n ingress-nginx -o wide

NAME                                        READY   STATUS    RESTARTS   AGE    IP                NODE              NOMINATED NODE   READINESS GATES
nginx-ingress-controller-5cffd956bf-dm9qf   1/1     Running   0          4m3s   192.168.132.133   192.168.132.133   <none>           <none>

然后就运行在了192.168.132.133上

2.3 无满足标签

apiVersion: apps/v1
kind: Deployment
metadata:
    name: busybox
    namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      name: busybox
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 10%
      maxUnavailable: 0
  template:
    metadata:
      labels:
        name: busybox
    spec:
      nodeSelector:
        aaa: bbb
      containers:
      - name: busybox
        image: busybox
        command:
          - /bin/sh
          - -c
          - "sleep 3600"

[[email protected] deployment]# kubectl apply -f busybox-deployment.yaml

deployment.apps/busybox configured

[[email protected] deployment]# kubectl get pods

busybox-546555c84-2psbb             1/1     Running   13         24h
busybox-674bd96f74-m4fst            0/1     Pending   0          13s

[[email protected] deployment]# kubectl describe pods busybox-674bd96f74-m4fst

Type     Reason            Age        From               Message
  ----     ------            ----       ----               -------
  Warning  FailedScheduling  <unknown>  default-scheduler  0/3 nodes are available: 3 node(s) didn‘t match node selector.
  Warning  FailedScheduling  <unknown>  default-scheduler  0/3 nodes are available: 3 node(s) didn‘t match node selector.

一直pengding状态,是因为没有匹配的标签

2.4 为节点打标签

[[email protected] deployment]# kubectl label node 192.168.132.132 aaa=bbb

node/192.168.132.132 labeled

[[email protected] deployment]# kubectl get pods

NAME                                READY   STATUS              RESTARTS   AGE
busybox-546555c84-2psbb             1/1     Running             13         24h
busybox-674bd96f74-m4fst            0/1     ContainerCreating   0          7m26s

[[email protected] deployment]# kubectl describe pods busybox-674bd96f74-m4fst

Events:
  Type     Reason            Age        From                      Message
  ----     ------            ----       ----                      -------
  Warning  FailedScheduling  <unknown>  default-scheduler         0/3 nodes are available: 3 node(s) didn‘t match node selector.
  Warning  FailedScheduling  <unknown>  default-scheduler         0/3 nodes are available: 3 node(s) didn‘t match node selector.
  Normal   Scheduled         <unknown>  default-scheduler         Successfully assigned default/busybox-674bd96f74-m4fst to 192.168.132.132
  Normal   Pulling           38s        kubelet, 192.168.132.132  Pulling image "busybox"
  Normal   Pulled            34s        kubelet, 192.168.132.132  Successfully pulled image "busybox"
  Normal   Created           34s        kubelet, 192.168.132.132  Created container busybox
  Normal   Started           33s        kubelet, 192.168.132.132  Started container busybox

2.5 删除标签

[[email protected] deployment]# kubectl label node 192.168.132.132  aaa-

删除标签后,已经运行的pod会继续运行

[[email protected] deployment]# kubectl get pods -o wide

NAME                                READY   STATUS    RESTARTS   AGE     IP            NODE              NOMINATED NODE   READINESS GATES
busybox-674bd96f74-m4fst            1/1     Running   0          12m     10.244.1.29   192.168.132.132   <none>           <none>
goproxy                             1/1     Running   1          3d11h   10.244.1.21   192.168.132.132   <none>           <none>

2.6 删除pods

[[email protected] deployment]# kubectl delete pods busybox-674bd96f74-m4fst

pod "busybox-674bd96f74-m4fst" deleted

[email protected] deployment]# kubectl get pods -o wide

NAME                                READY   STATUS    RESTARTS   AGE     IP            NODE              NOMINATED NODE   READINESS GATES
busybox-674bd96f74-8d7ml            0/1     Pending   0          65s     <none>        <none>            <none>           <none>

一直不能启动,是因为没有匹配的标签

2.7 自定义标签

为192.168.132.132 打自定义标签

[[email protected] deployment]# kubectl label node 192.168.132.132  ingress=enable

node/192.168.132.132 labeled

[[email protected] deployment]# vim /yamls/ingress/nginx-controller.yaml

nodeSelector:
        ingress: enable

[[email protected] deployment]# kubectl apply -f /yamls/ingress/nginx-controller.yaml

namespace/ingress-nginx unchanged
configmap/nginx-configuration unchanged
configmap/tcp-services unchanged
configmap/udp-services unchanged
serviceaccount/nginx-ingress-serviceaccount unchanged
clusterrole.rbac.authorization.k8s.io/nginx-ingress-clusterrole unchanged
role.rbac.authorization.k8s.io/nginx-ingress-role unchanged
rolebinding.rbac.authorization.k8s.io/nginx-ingress-role-nisa-binding unchanged
clusterrolebinding.rbac.authorization.k8s.io/nginx-ingress-clusterrole-nisa-binding unchanged
deployment.apps/nginx-ingress-controller configured
limitrange/ingress-nginx configured

[[email protected] deployment]# kubectl get pods -n ingress-nginx -o wide

NAME                                        READY   STATUS        RESTARTS   AGE   IP                NODE              NOMINATED NODE   READINESS GATES
nginx-ingress-controller-5cffd956bf-dm9qf   1/1     Terminating   0          32m   192.168.132.133   192.168.132.133   <none>           <none>
nginx-ingress-controller-79669b846b-nlrxl   1/1     Running       0          16s   192.168.132.132   192.168.132.132   <none>           <none>

[[email protected] deployment]# kubectl get pods -n ingress-nginx -o wide

NAME                                        READY   STATUS    RESTARTS   AGE   IP                NODE              NOMINATED NODE   READINESS GATES
nginx-ingress-controller-79669b846b-nlrxl   1/1     Running   0          95s   192.168.132.132   192.168.132.132   <none>           <none>

另一种,是直接使用nodeName:指定节点IP

[[email protected] deployment]# vim /yamls/ingress/nginx-controller.yaml

hostNetwork: true
      nodeName: "192.168.132.133"    

[[email protected] deployment]# kubectl get nodes

NAME              STATUS   ROLES    AGE     VERSION
192.168.132.131   Ready    master   3d19h   v1.17.0
192.168.132.132   Ready    <none>   3d19h   v1.17.0
192.168.132.133   Ready    <none>   3d19h   v1.17.0

尽量不要指定nodeName,因为当这个节点断掉之后,必须修改新的nodeName,但是使用标签选择器,直接打标签就可以了

可以这么操作,运行节点的副本设为2,然后给其中两个节点打上相同的标签,在使用标签选择器选择标签,这样就可以让ingress运行在固定的节点上



博主声明:本文的内容来源主要来自誉天教育晏威老师,由本人实验完成操作验证,需要的博友请联系誉天教育(http://www.yutianedu.com/),获得官方同意或者晏老师(https://www.cnblogs.com/breezey/)本人同意即可转载,谢谢!

原文地址:https://www.cnblogs.com/zyxnhr/p/12189639.html

时间: 2024-10-09 10:41:29

008.kubernets的调度系统之标签选择器的相关文章

009.kubernets的调度系统之污点和容忍

Taints和Tolerations(污点和容忍) Taint需要与Toleration配合使用,让pod避开那些不合适的node.在node上设置一个或多个Taint后,除非pod明确声明能够容忍这些“污点”,否则无法在这些node上运行.Toleration是pod的属性,让pod能够(注意,只是能够,而非必须)运行在标注了Taint的node上. 默认情况下,所有的应用pod都不会运行在有污点的节点上 [[email protected] deployment]# kubectl get

010.kubernets的调度系统之daemonset

daemonset简单操作使用 Deployment 是 Kubernetes 中用于处理无状态服务的资源,而 StatefulSet 是用于支持有状态服务的资源,这两种不同的资源从状态的角度对服务进行了划分,而 DaemonSet 从不同的维度解决了集群中的问题 — 如何同时在集群中的所有节点上提供基础服务和守护进程. 下面简单学习DaemonSet的一写基本的操作和管理 1 介绍 DaemonSet 可以保证集群中所有的或者部分的节点都能够运行同一份 Pod 副本,每当有新的节点被加入到集群

K8S调度之标签选择器

Kubernetes 调度简介 除了让 kubernetes 集群调度器自动为 pod 资源选择某个节点(默认调度考虑的是资源足够,并且 load 尽量平均),有些情况我们希望能更多地控制 pod 应该如何调度.比如,集群中有些机器的配置更好( SSD,更好的内存等),我们希望比较核心的服务(比如说数据库)运行在上面:或者某两个服务的网络传输很频繁,我们希望它们最好在同一台机器上,或者同一个机房.有一些特殊的应用,我们就希望它们能运行在我们指定的节点上,还有些情况下,我们希望某个应用在所有的节点

4.NFC前台调度系统

使用目的:当前Activity能直接响应NFC标签,而不需要用户在choose所有能处理的Activity. 使用步骤: 第一步:在onCreate()方法中 // NFC前台调度系统 private PendingIntent pendingIntent = null;.........// 初始化PendingIntent,当有NFC设备连接上的时候,就交给当前Activity处理pendingIntent = PendingIntent.getActivity(this, 0, new I

黑马程序员-java-银行业务调度系统《十一》

                   --Java培训.Android培训.iOS培训..Net培训.期待与您交流! -- 1.了解银行业务调度系统的工作流程与原理 银行里有三种窗口,一种是普通窗口,一种是VIP窗口,一种是快速窗口,所有客户都要领号排队,当VIP窗口或快速窗口空闲时,可以为普通窗口服务. 2.银行业务调度系统的项目需求 · 银行内有6个业务窗口,1 - 4号窗口为普通窗口,5号窗口为快速窗口,6号窗口为VIP窗口. · 有三种对应类型的客户:VIP客户,普通客户,快速客户(办理

css标签选择器

<!doctype html><html lang="en"> <head> <meta charset="UTF-8"> <meta name="Generator" content="EditPlus?"> <meta name="Author" content=""> <meta name="K

jQuery easyUI id选择器 类选择器 标签选择器 属性选择器 及DOM对象和jQuery相互之间的转换

首先导入js文件 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"

电梯调度系统(界面由C图形库编绘)

电梯调度系统编程 1.编程题目 电梯调度. 2.结对编程组员 黄冠译,刘畅. 3.编程语言 C语言图形库. 4.题目要求: 5.代码运行及结果调试: ① 运行界面为C++图形库支持,开始运行的初始界面如图,且默认所有电梯初始状态都为1楼,此时不分奇偶层: ② 我设置了鼠标响应事件,左边上下箭头为当前楼层有人要上或下的按钮,可以用鼠标直接点击响应,点击后要输入有多少人在此楼层等待,示例点击5楼,输入15人,如图所示: ③ 输入完毕后,电梯会根据单双层或全部楼层4个电梯的忙碌状态调度一个电梯过去,第

调度系统

---恢复内容开始--- 一.目的和要求 1. 实验目的 (1)加深对作业调度算法的理解: (2)进行程序设计的训练. 2.实验要求 用高级语言编写一个或多个作业调度的模拟程序. 单道批处理系统的作业调度程序.作业一投入运行,它就占有计算机的一切资源直到作业完成为止,因此调度作业时不必考虑它所需要的资源是否得到满足,它所运行的时间等因素. 作业调度算法: 1) 采用先来先服务(FCFS)调度算法,即按作业到达的先后次序进行调度.总是首先调度在系统中等待时间最长的作业. 2) 短作业优先 (SJF