[kubernetes]9-3 Scheduler --- 玩转pod调度(下)

别来无恙 提交于 2020-02-01 06:02:53

 

9-3 Scheduler --- 玩转pod调度(下)

创建web-dev-pod.yaml 验证pod的亲和性

 

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-demo-pod
  namespace: dev
spec:
  selector:
    matchLabels:
      app: web-demo-pod
  replicas: 1
  template:
    metadata:
      labels:
        app: web-demo-pod
    spec:
      containers:
      - name: web-demo-pod
        image: harbor.pdabc.com/kubernetes/web:v3
        ports:
        - containerPort: 8080
      affinity:
        podAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
# 和app 名为web-demo的pod 运行在一起
              matchExpressions:
              - key: app
                operator: In
                values:
                - web-demo
# 运行的条件 会运行在同一个节点
            topologyKey: kubernetes.io/hostname
# 倾向于和名为web-demo-node 的pod运行在一起
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app
                  operator: In
                  values:
                  - web-demo-node
              topologyKey: kubernetes.io/hostname

 

启动pod 的确是在一起的

kubectl apply -f web-dev-pod.yaml

 

修改matchExpressions的app值为web-demo-2 并启动pod

kubectl apply -f web-dev-pod.yaml

matchExpressions的pod一定要存在 否则创建pod的时候 会一直处于pending

 

pod的反亲和性调度

修改 web-dev-pod.yaml

 

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-demo-pod
  namespace: dev
spec:
  selector:
    matchLabels:
      app: web-demo-pod
  replicas: 1
  template:
    metadata:
      labels:
        app: web-demo-pod
    spec:
      containers:
      - name: web-demo-pod
        image: harbor.pdabc.com/kubernetes/web:v3
        ports:
        - containerPort: 8080
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - web-demo-node
            topologyKey: kubernetes.io/hostname
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app
                  operator: In
                  values:
                  - web-demo-node
              topologyKey: kubernetes.io/hostname

 

启动并查看 发现的确没有和web-demo-node在一台node上

 

修改web-dev-pod.yaml 表示不想和自己在一个pod (类似于多可用区?)

      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - web-demo-pod

 

 

如果这时候修改web-dev.yaml为如下 表示要和自己的pod 运行在一起

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-demo-pod
  namespace: dev
spec:
  selector:
    matchLabels:
      app: web-demo-pod
  replicas: 1
  template:
    metadata:
      labels:
        app: web-demo-pod
    spec:
      containers:
      - name: web-demo-pod
        image: harbor.pdabc.com/kubernetes/web:v3
        ports:
        - containerPort: 8080
      affinity:
        podAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - web-demo-pod
            topologyKey: kubernetes.io/hostname
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app
                  operator: In
                  values:
                  - web-demo-node
              topologyKey: kubernetes.io/hostname

 

启动之后发现新pod 一直处于pending状态

通过查看日志kubectl describe pod -n dev web-demo-pod-856d8bdf89-t6zml发现,是因为目前的规则违背了之前pod的规则,之前不想和web-dev-pod在一起,现在又想在一起了。那之前的pod的规则就不满足了,会有排斥。可以通过删除重建解决

 

 

污点

可以将拥有GPU ssd等设备的node 打上污点,避免正常pod被分配上来。

污点的效果

NoSchedule 不会调度

PreferNoSchedule 最好不要调度

NoExcute 不仅不会调度 如果node上还有pod 会进行驱逐,如果没有设置容忍时间则立刻驱逐

 

给node打上污点

kubectl taint nodes kubernetes-node-01 gpu=true:NoSchedule

 

重新修改web-dev-node.yaml 如下 可以再给node1 添加一个ssd标签 测试

kubectl label node kubernetes-node-01 disktype=ssd

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-demo-node
  namespace: dev
spec:
  selector:
    matchLabels:
      app: web-demo-node
  replicas: 1
  template:
    metadata:
      labels:
        app: web-demo-node
    spec:
      containers:
      - name: web-demo-node
        image: harbor.pdabc.com/kubernetes/web:v3
        ports:
        - containerPort: 8080
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: beta.kubernetes.io/arch
                operator: In
                values:
                - amd64
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 1
            preference:
              matchExpressions:
              - key: disktype
                operator: In
                values:
                - ssd

可以发现pod从有污点的带有ssd标签的node1上跑到node3上了

 

创建新的web-dev-taint.yaml 

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-demo-taint
  namespace: dev
spec:
  selector:
    matchLabels:
      app: web-demo-taint
  replicas: 1
  template:
    metadata:
      labels:
        app: web-demo-taint
    spec:
      containers:
      - name: web-demo-taint
        image: harbor.pdabc.com/kubernetes/web:v3
        ports:
        - containerPort: 8080
# 污点容忍的key
      tolerations:
# 这个要和打污点时一样
      - key: "gpu"
# 还有exists  只要污点存在就行 和值没有关系 
        operator: "Equal"
        value: "true"
# 这个要和打污点时一样
        effect: "NoSchedule"

 

启动pod

第一次运行在node2

第二次 修改replicas=3 测试污点容忍 可以在node1上创建pod

其中有一次写错了 容忍度没配置对 就是不会到有污点的node1上

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!