K8S入门学习笔记第六天,nodeSelector,nodeAffinity,Taint,DaemonSet控制器
nodeSelector和nodeAffinity作用分别是什么?nodeSelector:节点选择器用于将Pod调度到匹配label的node上,如果没有匹配的标签会调度失败。完全匹配节点标签,固定到pod的特定节点。nodeAffinity:节点亲和器...
·
nodeSelector和nodeAffinity作用分别是什么?
nodeSelector:节点选择器
用于将Pod调度到匹配label的node上,如果没有匹配的标签会调度失败。
完全匹配节点标签,固定到pod的特定节点。
nodeAffinity:节点亲和器
具体用法如下
1.创建标签
kubectl label node cka-node disktype=gpu
#给 cka-node这个节点打上一个 disktype=ssd的标签
2.检查标签
kubectl get node --show-labels #查看所有标签
kubectl get node -l disktype=gpu #指定key查找标签
3.在pod.yaml中指定标签
至此只顶你标签创建 pod完成
nodeAffinity:节点亲和类型去nodeSelector,可以根据节点上的标签来约束pod调度到哪些节点
特点:
匹配有更多的逻辑组合,不只是字符串的完全相等
调度分为软策略和硬策略,而不是硬性要求
required:必须满足
preferred:尝试满足,但不保证
Taint(污点)
Taints :避免pod调度到特定node上
设置污点
kubectl taint node [node] key=value:[effect]
[effect]有三个可选值
1.Noschedule:一定不能被调度
2.PreferNoSchedule:尽量不要被调度
3.NoExecute:不仅不会被调度,还会驱逐Node上已有的Pod
Taint用法
1.打污点Taint
kubectl taint node cka-node gpu=yes:NoSchedule
2.查看污点
kubectl describe node | grep Taint
3.测试
[root@CKA-master ~]# cat pod.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
run: nginx
name: nginx-gpu
spec:
nodeSelector:
porttype: "ssd"
containers:
- image: nginx
name: nginx-ssd
resources: {}
dnsPolicy: ClusterFirst
kubectl apply -f pod.yaml
kubectl describe pod nginx-gpu#检查原因
发现节点有污点无法容忍,所以无法正常启动
4.更新pod
容忍这个污点
apiVersion: v1
kind: Pod
metadata:
labels:
run: nginx
name: nginx-tolerations
spec:
tolerations:
- key: "gpu" #key等于gpu
operator: "Equal" #相等
value: "yes" #value=yes
effect: "NoSchedule" #污点标签
nodeSelector:
disktype: "gpu" #指定标签
containers:
- image: nginx
name: nginx-ssd
resources: {}
dnsPolicy: ClusterFirst
5.去掉污点
kubectl taint node cka-node gpu:NoSchedule-
6.忽略污点精确分配
增加 nodeName: "cka-node"的配置
apiVersion: v1
kind: Pod
metadata:
labels:
run: nginx
name: nginx-node
spec:
nodeName: "cka-node"
containers:
- image: nginx
name: nginx-ssd
resources: {}
dnsPolicy: ClusterFirst
7.DaemonSet
DaemonSet功能:
1.在每个Node上运行一个Pod
2.新加入的Node也同样会自动运行一个Pod
应用场景:网络插件,监控Agent,日志Agent
7.1创建daemonset.yaml文件
apiVersion: apps/v1
kind: DaemonSet
metadata:
labels:
app: nginx
name: nginx
spec:
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
tolerations: #容忍master节点的污点
- effect: NoSchedule #容忍master节点的污点
operator: Exists #容忍master节点的污点
containers:
- image: nginx
name: nginx
resources: {}
#启动 kubectl apply -f daemonset.yaml
7.2验证是不是每个节点都部署了nginx
kubectl get pods -o wide
启动完成,每个节点会生成一个nginx容器
更多推荐
已为社区贡献16条内容
所有评论(0)