Openshift:可靠的Kubernetes发行版

玩转Openshift中Pod调度

2018-05-21  本文已影响417人  潘晓华Michael
Pod调度通过Scheduler组件实现

Deployment/RC:全自动调度

Deployment/RC主要是自动部署应用的多个副本,并持续监控,以维持副本的数量。默认是使用系统Master的Scheduler经过一系列算法计算来调度,用户无法干预调度过程与结果。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  replicas: 1
  template:
    metadata:
      name: nginx
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        imagePullPolicy: IfNotPresent
      restartPolicy: Always
  selector:
    matchLabels:
      app: nginx

NodeSelector:定向调度

通过Node的标签和Pod的nodeSelector属性相匹配,可以达到将pod调度到指定的一些Node上。

oc label nodes <node-name> env=dev
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  replicas: 1
  template:
    metadata:
      name: nginx
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        imagePullPolicy: IfNotPresent
      restartPolicy: Always
nodeSelector:
    env: 'dev'  

NodeAffinity:Node亲和性调度

apiVersion: v1
kind: Pod
metadata:
  name: nginx 
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx
    imagePullPolicy: IfNotPresent
  restartPolicy: Always
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
          - matchExpressions:
              - key: env
                operator: In
                values:
                  - dev
      preferredDuringSchedulingIgnoredDuringExecution:
        - preference:
           weight: 1
            matchExpressions:
              - key: disk-type
                operator: In
                values:
                  - ssd

PodAffinity:Pod亲和与互斥调度策略

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx
    imagePullPolicy: IfNotPresent
  restartPolicy: Always
  affinity:
    podAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        - topologyKey: failure-domain.beta.kubernetes.io/zone
          labelSelector:
            matchExpressions:
              - key: app
                operator: In
                values:
                  - test
    podAntiAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        - topologyKey: kubernetes.io/hostname
          labelSelector:
            matchExpressions:
              - key: app
                operator: In
                values:
                  - nginx

Taints与Tolerations(污点与容忍)

为node添加一个Taint,效果是NoSchedule(除了NoSchedule还可以取值PreferNoSchedule/NoExecute)。意味着除非Pod明确声明可以容忍这个Taint,否则不会被调度到该Node上。如果Pod无法容忍NoExecute的Taint,则上面已经运行的Pod会被驱逐。

oc taint nodes <node-name> key1=value1:NoSchedule
oc taint nodes <node-name> key1=value1:NoExecute
oc taint nodes <node-name> key2=value2:NoSchedule
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx
    imagePullPolicy: IfNotPresent
  restartPolicy: Always
  tolerations:
    - key: "key1"
      operator: "Exists"
      effect: "NoSchedule"
   - key: "key1"
     operatior: "Equal"
     value: "value1"
     effect: "NoExecute"
     tolerationSeconds: 3600

DaemonSet:在每个Node上调度一个Pod

apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
  labels:
    component: fluentd
  name: logging-fluentd
  namespace: logging
spec:
  selector:
    matchLabels:
      component: fluentd
  template:
    metadata:
      labels:
        component: fluentd
      name: fluentd-elasticsearch
    spec:
      containers:
        image: docker.io/openshift/origin-logging-fluentd:latest
        imagePullPolicy: IfNotPresent
        name: fluentd-elasticsearch
      nodeSelector:
        logging-infra-fluentd: "true"

Job:批处理调度

kind: Job
apiVersion: batch/v1
metadata:
  name: process-itme
  labels:
    jobgroup: jobexample
spec:
  template:
    metadata:
      name: jobexample
      labels:
        jobgroup: jobexample
    spec:
      containers:
        - name: c
          image: busybox
          command: ["sh", "-c", "echo jobexample && sleep 5"]
      restartPolicy: Never

Cronjob:定时任务

kind: CronJob
apiVersion: batch/v2alpha1
metadata:
  name: hello
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
            - name: hello
              image: busybox
              command: ["sh", "-c", "echo Hello && sleep 5"]
          restartPolicy: OnFailure

参考书目:《Kubernetes权威指南》

上一篇 下一篇

猜你喜欢

热点阅读