pod调度

定向调度
通过标签选择器定向调度到node上
此调度方式是硬性现在 如果匹配不上 会调度失败

#在node上加标签
k label nodes dev4-worker zone=north
#查看
k get node dev4-worker --show-labels

创建pod的yaml文件

#通过标签选择器 定向调度到指定node
apiVersion: v1
kind: ReplicationController
metadata:
 name: redis-master
 labels:
  name: redis-master
spec:
 replicas: 1
 selector: 
  name: redis-master
 template:
  metadata:
   labels:
    name: redis-master
  spec:
   containers:
   - name: master
     image: kubeguide/redis-master
     ports:
     - containerPort: 6379
   nodeSelector: #标签选择器 选择要调度到带此标签的node上
    zone: north

验证

k get pod -o wide

通过node的亲和与反亲和选择调度到node节点

#通过node亲和性选择节点
apiVersion: v1
kind: Pod
metadata:
 name: with-node-affinity
spec:
 affinity:
  nodeAffinity:
   requiredDuringSchedulingIgnoredDuringExecution:  #Node节点必须满足指定的所有规则才可以,相当于硬限制
    nodeSelectorTerms: # 节点选择列表
    - matchExpressions:   #标签选择器 可以选择符合的标签列表(推荐)
      - key: kubernetes.io/arch   #键
        operator: In #匹配标签的关系符 支持Exists, DoesNotExist, In, NotIn, Gt, Lt
        values: #值
        - amd64  
   preferredDuringSchedulingIgnoredDuringExecution: #优先调度到满足指定的规则的Node,相当于软限制 (倾向)     
   - weight: 1 #倾向权重,在范围1-100。
     preference:   #一个节点选择器项,与相应的权重相关联
      matchExpressions:   
      - key: disk-type # 键
        operator: In #关系符 支持In, NotIn, Exists, DoesNotExist, Gt, Lt  
        values:  # 值
        - ssd
 containers:
 - name: with-node-affinity
   image: busybox       
   

关系符的使用说明:

- matchExpressions:
    - key: nodeenv # 匹配存在标签的key为nodeenv的节点
      operator: Exists   
    - key: nodeenv # 匹配标签的key为nodeenv,且value是"xxx"或"yyy"的节点
      operator: In    
      values: ["xxx","yyy"]
    - key: nodeenv # 匹配标签的key为nodeenv,且value不是"xxx"或"yyy"的节点
      operator: NotIn
      values: ["xxx","yyy"]  
    - key: nodeenv # 匹配标签的key为nodeenv,且value大于"xxx"的节点
      operator: Gt   
      values: "xxx"

pod亲和性与反亲和调度
pod的亲和与反亲和调度 会限制在拓扑域中例如:在一个机架 一个主机 或者一个区域
创建参照目标pod

#测试pod的亲和与反亲和 参照目标pod
apiVersion: v1
kind: Pod
metadata:
 name: pod-flag
 labels:
  security: "S1"
  app: "nginx"
spec:
 containers:
 - name: nginx-flag
   image: nginx  

pod亲和性调度

#pod亲和性调度
apiVersion: v1
kind: Pod
metadata:
 name: pod-affinity
spec:
 affinity:
  podAffinity:
   requiredDuringSchedulingIgnoredDuringExecution:
   - labelSelector:
      matchExpressions:
      - key: security
        operator: In
        values:
        - S1
     topologyKey: kubernetes.io/hostname #对应的目标拓扑域内 进制亲和与反亲和调度
 containers:
 - name: with-pod-affinity
   image: nginx      

反亲和调度
这个新pod必须与security=S1的pod在一个zong的拓扑里 ,但是不与app=nginx 在一个node节点上

#反亲和性
apiVersion: v1
kind: Pod
metadata:
 name: pod-anti-affinity
spec:
 affinity:
  podAffinity:
   requiredDuringSchedulingIgnoredDuringExecution:
   - labelSelector:
      matchExpressions:
      - key: security
        operator: In
        values:
        - S1
     topologyKey: topology.kubernetes.io/zone #这个新pod必须与security=S1的pod在一个zong的拓扑里 
  podAntiAffinity:
   requiredDuringSchedulingIgnoredDuringExecution:
   - labelSelector:
      matchExpressions:
      - key: app
        operator: In
        values:
        - nginx
     topologyKey: kubernetes.io/hostname #但是不与app=nginx 在一个node节点上
 containers:
 - name: with-pod-anti-affinity
   image: nginx  

你可能感兴趣的:(kubernetes)