Closed5

k8sのノードによるpodスケジューリング

not75743not75743

nodeselector

# ラベルを付与したノードにのみPodが配置されることを確認

apiVersion: apps/v1
kind: Deployment
metadata:
  name: node-selector-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      nodeSelector:
        app: nginx # 特定のラベルを持つノードに配置
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80

not75743not75743

nodeaffinity

required

# matchExpressionsで設定したラベルのノードにpodが配置されること
# matchExpressionsで設定した誤ったラベルのノードにpodが配置されないこと

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nodeaffinity-require
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: app
                operator: In
                values:
                - nginx
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80

prefer

# app=nginx,role=webのラベルを持つノードにpodが配置されること

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nodeaffinity-prefer
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: app
                operator: In
                values:
                - nginx
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 1
            preference:
              matchExpressions:
              - key: role
                operator: In
                values:
                - web
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80

not75743not75743

podaffinity

podaffinity

# apache podと nginx pod がk3d-test-cluster-agent-1に配置されること
# ただしnginx podを配置後、nodeselectorを変更するとnginxは移動するがapacheは移動しない

---
# nginx
apiVersion: apps/v1
kind: Deployment
metadata:
  name: podaffinity-nginx
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      nodeSelector:
        kubernetes.io/hostname: k3d-test-cluster-agent-1
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80

---
# apache
apiVersion: apps/v1
kind: Deployment
metadata:
  name: podaffinity-apache
  labels:
    app: apache
spec:
  replicas: 1
  selector:
    matchLabels:
      app: apache
  template:
    metadata:
      labels:
        app: apache
    spec:
      affinity:
        podAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - nginx
            topologyKey: "kubernetes.io/hostname"
      containers:
      - name: apache
        image: httpd:latest
        ports:
        - containerPort: 80

podantiaffinity

# apache podと nginx pod が異なるノードに配置されること

---
# nginx
apiVersion: apps/v1
kind: Deployment
metadata:
  name: podantiaffinity-nginx
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      nodeSelector:
        kubernetes.io/hostname: k3d-test-cluster-agent-1
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80

---
# apache
apiVersion: apps/v1
kind: Deployment
metadata:
  name: podantiaffinity-apache
  labels:
    app: apache
spec:
  replicas: 1
  selector:
    matchLabels:
      app: apache
  template:
    metadata:
      labels:
        app: apache
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - nginx
            topologyKey: "kubernetes.io/hostname"
      containers:
      - name: apache
        image: httpd:latest
        ports:
        - containerPort: 80

not75743not75743

taint

taintを付与したノードに配置できない

# kubectl taint nodes k3d-test-cluster-agent-1 special=reserved:NoSchedule
# kubectl taint nodes k3d-test-cluster-agent-1 special=reserved:NoSchedule-

# special=reservedのtaintがついたノード以外にスケジューリングされること
# 確認: kubectl get pod -o custom-columns="NAME:.metadata.name,NODE:.spec.nodeName"

apiVersion: apps/v1
kind: Deployment
metadata:
  name: taint
  labels:
    app: nginx
spec:
  replicas: 30
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80

tolerationでtaintを付与したノードにも配置できるようにする

# kubectl taint nodes k3d-test-cluster-agent-1 special=reserved:NoSchedule
# kubectl taint nodes k3d-test-cluster-agent-1 special=reserved:NoSchedule-

# special=reservedのtaintがついたノードにもスケジューリングされること
# ただしtaintがついたノードにのみスケジューリングされるわけではない
# 確認: kubectl get pod -o custom-columns="NAME:.metadata.name,NODE:.spec.nodeName"

apiVersion: apps/v1
kind: Deployment
metadata:
  name: taint
  labels:
    app: nginx
spec:
  replicas: 30
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      tolerations:
      - key: "special"
        operator: "Equal"
        value: "reserved"
        effect: "NoSchedule"
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80

not75743not75743

topologyspread

均等配置

# 4つのノードに均等に配置されること

apiVersion: apps/v1
kind: Deployment
metadata:
  name: topologyspread
spec:
  replicas: 8
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      topologySpreadConstraints:
      - maxSkew: 2
        topologyKey: kubernetes.io/hostname
        whenUnsatisfiable: DoNotSchedule
        labelSelector:
          matchLabels:
            app: nginx
      containers:
      - name: topologyspread
        image: nginx:latest

antiaffinityを使ってノード数以上のpodはpendingされる

# 4つのノードに均等に配置され、余ったPodはスケジューリングされないこと

apiVersion: apps/v1
kind: Deployment
metadata:
  name: topologyspread
spec:
  replicas: 5
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      topologySpreadConstraints:
      - maxSkew: 1
        topologyKey: kubernetes.io/hostname
        whenUnsatisfiable: DoNotSchedule
        labelSelector:
          matchLabels:
            app: nginx
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchLabels:
                app: nginx
            topologyKey: kubernetes.io/hostname
      containers:
      - name: topologyspread
        image: nginx:latest

このスクラップは28日前にクローズされました