0x00 需求
公有云K8s部署,如果是采用手动维护node的方式进行部署。肯定会面临一个问题:
如何把特定的pod 调度到指定的node上运行,而且希望这个node 只运行指定的pod。
0x01 关于node的配置
1. node 的label操作基础
创建node时,指定label,也可以在创建后手动维护 label:
# 给node k8s-node02 添加一个label: ns=sit
kubectl label nodes k8s-node02 ns=sit
# 查看
kubectl describe node k8s-node02
# 删除
kubectl label nodes k8s-node02 ns-
2.如何禁止其他Pod调度到当前node ?
给node 打上一个污点,可以解决上述需求。
例:
# 设置污点
kubectl taint nodes k8s-node02 app=test:NoSchedule
#查看污点
kubectl describe node k8s-node02 | grep Taints
#删除污点
kubectl taint nodes k8s-node2 app=test:NoSchedule-
这里,打上一个 NoSchedule
污点,表示这个node对后续pod不提供调度能力。
也可以理解为:有污点的node,无法随意执行pod了。
注意:如果打污点之前node上已经有pod了,这些pod不受影响。
0x02.如何把Pod 调度到上述 node
这里有两个问题:
1、如何选择node
使用nodeSelector, 后面加一个 node 的 label.
表示pod会选择指定 label的node进行调度。
2、如何解除node上污点的限制
问题1解决了,但是node有污点,一般是无法调度成功的。
这里就需要用到容忍度了:
来看一个完整的deployment 样例:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-test
labels:
app: "nginx-test"
spec:
replicas: 1
template:
metadata:
name: nginx
labels:
app: "nginx-test"
spec:
containers:
- name: "nginx-test"
image: nginx:latest
imagePullPolicy: IfNotPresent
tolerations:
- key: "app"
operator: "Equal"
value: "test"
effect: NoSchedule
restartPolicy: Always
nodeSelector:
ns: sit
selector:
matchLabels:
app: "nginx-test"
-
nodeSelector
比较好理解。 -
tolerations
在配置时,需要和node 的污点信息保持一致。
这样就可以实现开篇的需求了。
0x03 小结
-
操作对象
node: 污点
pod: 容忍度、nodeSelector -
应用场景
aws eks 中跨az 会产生流量费用和性能影响。
我们就可以考虑结合使用上述能力,让pod 都部署在同一个az中。
网友评论