K8S自己动手系列 - 1.3 - Taint & Affinity

本文涉及的产品
容器服务 Serverless 版 ACK Serverless,952元额度 多规格
容器服务 Serverless 版 ACK Serverless,317元额度 多规格
简介: 本文主要描述节点污点及亲和性相关操作

Taint

please refer https://kubernetes.io/docs/concepts/configuration/taint-and-toleration/
三种taint:

  • NoSchedule
  • PreferNoSchedule
  • NoExecute

为节点打Taint

  ~ kubectl taint node worker02 role=nginx:NoSchedule
node/worker02 tainted
  ~ kubectl describe node worker02
Name:               worker02
...
Taints:             role=nginx:NoSchedule

  ~ kubectl get pod -o wide
NAME                     READY   STATUS    RESTARTS   AGE     IP            NODE       NOMINATED NODE   READINESS GATES
nginx-7cffb9df96-748j6   1/1     Running   0          5m21s   10.244.0.23   worker01   <none>           <none>
nginx-7cffb9df96-d2rt5   1/1     Running   0          5m22s   10.244.1.35   worker02   <none>           <none>

通过查看pod,发现已经运行的pod并未受影响
扩容pod,看下效果

  ~ kubectl scale --replicas=5 deployment/nginx
deployment.extensions/nginx scaled
  ~ kubectl get pod -o wide
NAME                     READY   STATUS              RESTARTS   AGE    IP            NODE       NOMINATED NODE   READINESS GATES
nginx-7cffb9df96-4rjhf   0/1     ContainerCreating   0          1s     <none>        worker01   <none>           <none>
nginx-7cffb9df96-748j6   1/1     Running             0          6m8s   10.244.0.23   worker01   <none>           <none>
nginx-7cffb9df96-89ngr   0/1     ContainerCreating   0          1s     <none>        worker01   <none>           <none>
nginx-7cffb9df96-d2rt5   1/1     Running             0          6m9s   10.244.1.35   worker02   <none>           <none>
nginx-7cffb9df96-zsgmd   0/1     ContainerCreating   0          1s     <none>        worker01   <none>           <none>

通过实验发现由于taint NoSchedule的影响,新pod不会调度到含有污点的节点上

移除节点Taint

  ~ kubectl taint node worker02 role:NoSchedule-
node/worker02 untainted

再次扩容,查看效果

  ~ kubectl scale --replicas=10 deployment/nginx
deployment.extensions/nginx scaled
  ~ kubectl get pod -o wide
NAME                     READY   STATUS    RESTARTS   AGE     IP            NODE       NOMINATED NODE   READINESS GATES
nginx-7cffb9df96-2wlln   1/1     Running   0          4s      10.244.1.38   worker02   <none>           <none>
nginx-7cffb9df96-4rjhf   1/1     Running   0          3m10s   10.244.0.26   worker01   <none>           <none>
nginx-7cffb9df96-748j6   1/1     Running   0          9m17s   10.244.0.23   worker01   <none>           <none>
nginx-7cffb9df96-89ngr   1/1     Running   0          3m10s   10.244.0.24   worker01   <none>           <none>
nginx-7cffb9df96-9vbdt   1/1     Running   0          4s      10.244.1.36   worker02   <none>           <none>
nginx-7cffb9df96-d2rt5   1/1     Running   0          9m18s   10.244.1.35   worker02   <none>           <none>
nginx-7cffb9df96-hqvfh   1/1     Running   0          4s      10.244.1.37   worker02   <none>           <none>
nginx-7cffb9df96-mhxmn   1/1     Running   0          4s      10.244.1.40   worker02   <none>           <none>
nginx-7cffb9df96-xdnzc   1/1     Running   0          4s      10.244.1.39   worker02   <none>           <none>
nginx-7cffb9df96-zsgmd   1/1     Running   0          3m10s   10.244.0.25   worker01   <none>           <none>

发现新增加的pod已经可以调度到worker02上了

容忍节点Taint

重新为节点增加污点

  ~ kubectl taint node worker02 role=nginx:NoSchedule
node/worker02 tainted

然后在deploy定义中增加:

tolerations:
- key: "role"
  operator: "Equal"
  value: "nginx"
  effect: "NoSchedule"

通过kubectl diff -f xxx.yaml可以查看变化情况

  lab02 kubectl diff -f nginx-deploy.yaml 
diff -u -N /tmp/LIVE-021075426/extensions.v1beta1.Deployment.default.nginx /tmp/MERGED-503027673/extensions.v1beta1.Deployment.default.nginx
--- /tmp/LIVE-021075426/extensions.v1beta1.Deployment.default.nginx    2019-06-09 15:53:54.120907334 +0800
+++ /tmp/MERGED-503027673/extensions.v1beta1.Deployment.default.nginx    2019-06-09 15:53:54.128907426 +0800
@@ -4,7 +4,7 @@
   annotations:
     deployment.kubernetes.io/revision: "8"
   creationTimestamp: "2019-06-08T04:27:19Z"
-  generation: 23
+  generation: 24
   labels:
     app: nginx
   name: nginx
@@ -14,7 +14,7 @@
   uid: b43106be-89a5-11e9-8ec2-080027a62701
 spec:
   progressDeadlineSeconds: 2147483647
-  replicas: 1
+  replicas: 2
   revisionHistoryLimit: 2147483647
   selector:
     matchLabels:
@@ -42,6 +42,11 @@
       schedulerName: default-scheduler
       securityContext: {}
       terminationGracePeriodSeconds: 30
+      tolerations:
+      - effect: NoSchedule
+        key: role
+        operator: Equal
+        value: nginx
 status:
   availableReplicas: 1
   conditions:
exit status 1

再次扩容,查看效果

  lab02 kubectl get pods -o wide
NAME                     READY   STATUS    RESTARTS   AGE   IP            NODE       NOMINATED NODE   READINESS GATES
nginx-859959cc7f-7sx2p   1/1     Running   0          4s    10.244.0.38   worker01   <none>           <none>
nginx-859959cc7f-8tpw7   1/1     Running   0          4s    10.244.0.39   worker01   <none>           <none>
nginx-859959cc7f-ffh7v   1/1     Running   0          4s    10.244.1.49   worker02   <none>           <none>
nginx-859959cc7f-j2xp7   1/1     Running   0          4s    10.244.0.37   worker01   <none>           <none>
nginx-859959cc7f-lht8b   1/1     Running   0          4s    10.244.1.47   worker02   <none>           <none>
nginx-859959cc7f-mcth6   1/1     Running   0          17s   10.244.0.36   worker01   <none>           <none>
nginx-859959cc7f-sx5ln   1/1     Running   0          4s    10.244.1.48   worker02   <none>           <none>
nginx-859959cc7f-tlxk5   1/1     Running   0          4s    10.244.1.46   worker02   <none>           <none>
nginx-859959cc7f-xkjtq   1/1     Running   0          4s    10.244.1.45   worker02   <none>           <none>
nginx-859959cc7f-xltkz   1/1     Running   0          17s   10.244.1.44   worker02   <none>           <none>

Affinity

上面通过节点污点的方式,我们可以避免pod被调度到含有污点的节点,同时也可以使某些pod容忍节点的污点,配合下面要说的affinity,可以实现特定节点为特定pod专用的目的

NodeSelector

在pod定义中增加如下:

      nodeSelector:
        kubernetes.io/hostname: worker02

执行查看效果

  lab02 kubectl apply -f nginx-deploy-toleration-nodeselector.yaml
deployment.extensions/nginx configured
  lab02 kubectl get pod -o wide
NAME                     READY   STATUS    RESTARTS   AGE   IP            NODE       NOMINATED NODE   READINESS GATES
nginx-564c745dcd-6dtz7   1/1     Running   0          25s   10.244.1.56   worker02   <none>           <none>
nginx-564c745dcd-fw8kt   1/1     Running   0          25s   10.244.1.55   worker02   <none>           <none>
nginx-564c745dcd-l8k7m   1/1     Running   0          22s   10.244.1.58   worker02   <none>           <none>
nginx-564c745dcd-mvqjd   1/1     Running   0          28s   10.244.1.53   worker02   <none>           <none>
nginx-564c745dcd-qn6r9   1/1     Running   0          29s   10.244.1.52   worker02   <none>           <none>
nginx-564c745dcd-rbl57   1/1     Running   0          27s   10.244.1.54   worker02   <none>           <none>
nginx-564c745dcd-s2jbm   1/1     Running   0          19s   10.244.1.59   worker02   <none>           <none>
nginx-564c745dcd-wnd45   1/1     Running   0          30s   10.244.1.50   worker02   <none>           <none>
nginx-564c745dcd-zlnfg   1/1     Running   0          30s   10.244.1.51   worker02   <none>           <none>
nginx-564c745dcd-zm7qx   1/1     Running   0          22s   10.244.1.57   worker02   <none>           <none>

NodeAffinity

NodeAffinity与NodeSelector有相似的作用,但是相比之下有更加灵活的表达语义
please refer: https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#affinity-and-anti-affinity

相关实践学习
通过Ingress进行灰度发布
本场景您将运行一个简单的应用,部署一个新的应用用于新的发布,并通过Ingress能力实现灰度发布。
容器应用与集群管理
欢迎来到《容器应用与集群管理》课程,本课程是“云原生容器Clouder认证“系列中的第二阶段。课程将向您介绍与容器集群相关的概念和技术,这些概念和技术可以帮助您了解阿里云容器服务ACK/ACK Serverless的使用。同时,本课程也会向您介绍可以采取的工具、方法和可操作步骤,以帮助您了解如何基于容器服务ACK Serverless构建和管理企业级应用。 学习完本课程后,您将能够: 掌握容器集群、容器编排的基本概念 掌握Kubernetes的基础概念及核心思想 掌握阿里云容器服务ACK/ACK Serverless概念及使用方法 基于容器服务ACK Serverless搭建和管理企业级网站应用
目录
相关文章
|
5月前
【CKA模拟题】Ingress新手必看,全面了解Ingress的基础操作
【CKA模拟题】Ingress新手必看,全面了解Ingress的基础操作
166 1
|
5月前
|
编解码 测试技术
【自己动手画CPU】计算机数据表示
【自己动手画CPU】计算机数据表示
231 0
|
存储 Kubernetes 调度
【K8S系列】第二讲:Pod入门
【K8S系列】第二讲:Pod入门
119 0
|
10月前
|
存储 Kubernetes NoSQL
k8s 学习九,pod 知识点 上
k8s 学习九,pod 知识点 上
211 0
|
Kubernetes 安全 Linux
Pod必备知识: SecurityContexts
Security Context主要用于限制容器的行为,从而保障系统和其他容器的安全。这一块的能力不是 Kubernetes 或者容器 runtime 本身的能力,而是 Kubernetes 和 runtime 通过用户的配置,最后下传到内核里,再通过内核的机制让 SecurityContext 来生效。所以这里介绍的内容,会比较简单或者说比较抽象一点。 1.容器级别的Security Context:仅对指定容器生效 2.Pod级别的Security Context:对指定Pod中的所有容器生效 3.Pod Security Policies(PSP):对集群内所有Pod生效
1612 0
Pod必备知识: SecurityContexts
|
2月前
|
Linux 调度
Linux源码阅读笔记05-进程优先级与调度策略-实战分析
Linux源码阅读笔记05-进程优先级与调度策略-实战分析
|
2月前
|
算法 Linux 调度
Linux源码阅读笔记03-调度器及CFS调度器
Linux源码阅读笔记03-调度器及CFS调度器
|
存储 Kubernetes NoSQL
【k8s 系列】k8s 学习九,pod 知识点 上
在 K8S 中, pod 是一个非常关键的存在,我们一起来看看 pod 具体是个什么?
160 0
|
10月前
|
Kubernetes API 调度
pod 知识点 下
pod 知识点 下
|
存储 Kubernetes API
【k8s 系列】k8s 学习十,pod 知识点 下
上一篇分享了 pod 的基本知识点,有 K8S 环境的小伙伴还是可以用起来的,还对比较简单,知道了 pod 的 yaml 文件结构,标识,基本的创建 pod 和删除 pod 的用法等等,我们继续
121 0