[CKA] nodeAffinity

hope·2023년 11월 1일

CKA

목록 보기
3/8
post-thumbnail

Assigning Pods to Nodes

참고 문서: kubernetes.io/assign-pod-node

보통은 스케줄러가 자동으로 리소스를 파악하고 파드를 배치하지만 특정 노드에 파드를 배포하고 싶거나 다른 두 개의 서비스를 동일한 노드에 배포하고 싶을 경우와 같이 파드 배포에 대한 제어가 필요할 때 사용함

스케줄링 방법

  1. nodeSelector
  2. Affinity/anti-Affinity
  3. nodeName
  4. Pod topology spread constraints

nodeSelector

노드 레이블을 명시하여 특정 노드로 파드를 배포함

nodeAffinity

  • 소프트한 규칙을 지정할 수 있음
  • 소프트한 규칙의 의미는 매치되는 노드를 찾지 못한 경우에도 파드를 배포할 수 있다는 말

규칙 종류

  • requiredDuringSchedulingIgnoredDuringExecution: 규칙에 해당하지 않으면 스케줄 X
  • preferredDuringSchedulingIgnoredDuringExecution: 규칙에 해당하지 않아도 스케줄 O

Operator 값 (논리연산자)

  • In: 조건 값 포함
  • NotIn: 조건 값 제외
  • Exists: 존재 유(참)
  • DoesNotExist: 존재 무(거짓)
  • Gt: > (Greater)
  • Lt: < (Little)

예시 코드

예시 1. 파드 생성 시
pod.yml

  • Name: red
  • Image: nginx
  • NodeAffinity: requiredDuringSchedulingIgnoredDuringExecution
  • Key: color
  • values: blue
apiVersion: v1
kind: Pod
metadata:
  name: red
spec:
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: color
            operator: In
            values:
            - blue
  containers:
  - name: red
    image: nginx

예시 2. 디플로이먼트 생성 시
deployment.yml:

  • Name: red
  • Replicas: 2
  • Image: nginx
  • NodeAffinity: requiredDuringSchedulingIgnoredDuringExecution
  • Key: node-role.kubernetes.io/control-plane
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: red
  name: red
spec:
  replicas: 2
  selector:
    matchLabels:
      app: red
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: red
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: node-role.kubernetes.io/control-plane
                operator: Exists
      containers:
      - image: nginx
        imagePullPolicy: Always
        name: nginx
profile
devops 엔지니어

0개의 댓글