Kubernetes에서 노드 선호도 사용
이 페이지에서
- 전제 조건
- 무엇을 할 것인가?\n
- 노드 선호도 구성\n
- 결론
노드 어피니티는 규칙 집합입니다. 클러스터에서 pod를 배치할 수 있는 위치를 결정하기 위해 스케줄러에서 사용합니다. 규칙은 포드 정의에 지정된 노드의 라벨과 라벨 선택기를 사용하여 정의됩니다. 노드 어피니티를 사용하면 포드가 예약할 수 있는 노드 그룹에 대한 어피니티를 지정할 수 있습니다. 특정 노드에서만 실행할 수 있도록 포드를 제한할 수 있습니다.
nodeSelector는 노드 선택 제약 조건의 가장 간단한 형태입니다. nodeSelector는 PodSpec의 속성입니다. 팟(Pod)이 노드에서 실행될 수 있으려면 노드에 표시된 각 레이블이 있어야 합니다.
노드 어피니티는 개념적으로 nodeSelector와 유사합니다. 이를 통해 노드의 라벨을 기반으로 Pod를 예약할 수 있는 노드를 제한할 수 있습니다.
현재 두 가지 유형의 노드 선호도가 있습니다.
- 필수DuringSchedulingIgnoredDuringExecution 및\n
- preferredDuringSchedulingIgnoredDuringExecution.
- 여기서 포드는 아직 생성되지 않았으며 처음으로 생성됩니다.
- 일반적으로 포드가 생성되면 선호도 규칙이 적용됩니다.\n
- 여기서 포드가 실행되고 있으며 nodeAffinity에 영향을 미치는 환경에서 변경이 이루어집니다.\n
노드 어피니티에 대해 자세히 알아보려면 Kubernetes의 공식 문서인 kubernete.io를 방문하세요.
이 도움말에서는 Kubernetes 클러스터에서 "requiredDuringSchedulingIgnoredDuringExecution" 노드 선호도를 사용하여 특정 노드에 Kubernetes Pod를 할당하는 방법을 살펴봅니다.
전제 조건
- 워커 노드가 1개 이상 있는 Kubernetes 클러스터.
Kubernetes 클러스터를 만드는 방법을 알아보려면 여기를 클릭하세요. 이 안내서는 AWS Ubuntu EC2 인스턴스에서 1개의 마스터와 2개의 노드가 있는 Kubernetes 클러스터를 생성하는 데 도움이 됩니다.\n
어떻게 할까요?
- 노드 선호도 구성\n
노드 선호도 구성
먼저 클러스터에서 사용 가능한 노드 목록을 가져옵니다.
kubectl get nodes #Get all the nodes in the cluster
노드에 Taint가 있는지 확인합니다.
kubectl describe node node01 | grep Taints #Describe the node node01 and grep Taints
kubectl describe node master | grep Taints #Describe the node master and grep Taints
작업자 노드 node01에 레이블을 추가합니다.
kubectl label node node01 app=qa #Add a label
배포 정의 파일을 만들고 다음 정의를 추가합니다.
vim my-deployment-without-affinity.yml
apiVersion: apps/v1 kind: Deployment metadata: name: app-without-affinity spec: replicas: 20 selector: matchLabels: run: nginx template: metadata: labels: run: nginx spec: containers: - image: nginx imagePullPolicy: Always name: nginx
Pod 및 배포 목록을 가져옵니다.
kubectl get pods #Get pods in the default namespace
kubectl get deployment #Get deployments in the default namespace
우리가 만든 정의에서 배포를 만듭니다.
kubectl create -f my-deployment-without-affinity.yml #Create a deployment object
kubectl get deployment #Get deployments in the default namespace
kubectl get pods #Get pods in the default namespace
배포로 생성된 포드의 세부정보를 가져옵니다.
여기에서 포드가 마스터 노드에서도 자리를 차지하는 것을 볼 수 있습니다. 그 이유는 노드에 Taint가 없기 때문에 포드가 사용 가능한 모든 노드에서 위치를 얻을 수 있기 때문입니다.
kubectl get pods -o wide #Get pods in the default namespace with more information about them using -o wide
이제 노드 선호도가 정의된 배포 정의를 만듭니다.
vim my-deployment-with-affinity.yml
apiVersion: apps/v1 kind: Deployment metadata: name: app-with-afiinity spec: replicas: 6 selector: matchLabels: run: nginx template: metadata: labels: run: nginx spec: containers: - image: nginx imagePullPolicy: Always name: nginx affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchExpressions: - key: app operator: In values: - qa
기존 배포 목록을 가져오고 위 단계에서 만든 파일을 사용하여 기본 네임스페이스에서 선호도가 있는 새 배포를 만듭니다.
kubectl get deployments #Get deployments in the default namespace
kubectl create -f my-deployment-with-affinity.yml #Create a deployment object
kubectl get deployments #Get deployments in the default namespace
이제 이번에는 포드가 작업자 노드 node01에만 배치된 것을 볼 수 있습니다. 그 이유는 포드가 정의된 조건/레이블과 일치하는 노드에 배포되도록 하는 배포 정의에서 노드 선호도를 정의했기 때문입니다.
kubectl get pods -o wide | grep app-with-afiinity #Get pods in the default namespace with more information about them using -o wide and grep app-with-afiinity
결론
이 도움말에서는 노드에 라벨을 추가하는 방법을 알아보고 노드 어피니티를 사용하여 필요한 노드에서 예약되도록 pod를 제한하는 방법을 살펴보았습니다. 또한 테인트가 없는 경우 포드가 마스터 노드에 배포될 수도 있음을 확인했습니다.