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

简介: 本文主要描述节点污点及亲和性相关操作

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

相关实践学习
容器服务Serverless版ACK Serverless 快速入门:在线魔方应用部署和监控
通过本实验,您将了解到容器服务Serverless版ACK Serverless 的基本产品能力,即可以实现快速部署一个在线魔方应用,并借助阿里云容器服务成熟的产品生态,实现在线应用的企业级监控,提升应用稳定性。
云原生实践公开课
课程大纲 开篇:如何学习并实践云原生技术 基础篇: 5 步上手 Kubernetes 进阶篇:生产环境下的 K8s 实践 相关的阿里云产品:容器服务&nbsp;ACK 容器服务&nbsp;Kubernetes&nbsp;版(简称&nbsp;ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情:&nbsp;https://www.aliyun.com/product/kubernetes
目录
相关文章
|
2月前
|
编解码 测试技术
【自己动手画CPU】计算机数据表示
【自己动手画CPU】计算机数据表示
105 0
|
6月前
|
存储 Kubernetes 调度
【K8S系列】第二讲:Pod入门
【K8S系列】第二讲:Pod入门
74 0
|
4月前
|
存储 Kubernetes NoSQL
k8s 学习九,pod 知识点 上
k8s 学习九,pod 知识点 上
146 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生效
1520 0
Pod必备知识: SecurityContexts
|
9月前
|
存储 Kubernetes NoSQL
【k8s 系列】k8s 学习九,pod 知识点 上
在 K8S 中, pod 是一个非常关键的存在,我们一起来看看 pod 具体是个什么?
126 0
|
4月前
|
Kubernetes API 调度
pod 知识点 下
pod 知识点 下
|
9月前
|
存储 Kubernetes API
【k8s 系列】k8s 学习十,pod 知识点 下
上一篇分享了 pod 的基本知识点,有 K8S 环境的小伙伴还是可以用起来的,还对比较简单,知道了 pod 的 yaml 文件结构,标识,基本的创建 pod 和删除 pod 的用法等等,我们继续
|
资源调度 Kubernetes 固态存储
k8s-Pod调度策略(入门攻略)
k8s-Pod调度策略(入门攻略)
303 1
|
存储 Kubernetes 调度
【K8S系列】Pod入门
Pod是Kubernetes创建或部署的最小/最简单的基本单位,一个Pod代表集群上正在运行的一个进程。一个Pod封装一个应用容器(也可以有多个容器),存储资源、一个独立的网络IP以及管理控制容器运行方式的策略选项。Pod代表部署的一个单位:Kubernetes中单个应用的实例,它可能由单个容器或多个容器共享组成的资源。Docker是Kubernetes Pod中最常见的runtime ,Pods也支持其他容器runtimes。...
337 0
【K8S系列】Pod入门
|
存储 Kubernetes 应用服务中间件
k8s之Pod基础概念 (下)
Pod是kubernetes中最小的资源管理组件,Pod也是最小化运行容器化应用的资源对象。一个Pod代表着集群中运行的一个进程。kubernetes中其他大多数组件都是围绕着Pod来进行支撑和扩展Pod功能的,例如,用于管理Pod运行的StatefulSet和Deployment等控制器对象,用于暴露Pod应用的Service和Ingress对象,为Pod提供存储的PersistentVolume存储资源对象等
k8s之Pod基础概念 (下)

热门文章

最新文章