1. NodeSelector
nodeSelector 예시
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: mycontainer
image: nginx
nodeSelector:
kubernetes.io/hostname: k8s-worker1
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: mycontainer
image: nginx
nodeSelector:
disktype: ssd
kubectl label nodes <node name> <key>=<value>
2. nodeAffinity
-
NodeSelector와 비슷하지만 조금 더 유연한 조건 설정이 가능. 특정 노드에 파드를 배치하거나 배치하지 않을때 사용
-
requiredDuringSchedulingIgnoredDuringExecution : 파드가 특정 조건을 만족하는 노드에만 스케쥴링 되도록 함
-
preferredDuringSchedulingIgnoredDuringExecution : 파드가 특정 조건을 만족하는 노드에 배치되기를 선호하지만, 반드시 그 노드에 배치해야 하는 것은 아님. 조건을 만족하지 않은 노드에도 파드를 배치 할 수 있음
-
requiredDuringSchedulingIgnoredDuringExecution 와 preferredDuringSchedulingIgnoredDuringExecution를 동시에 사용하면 파드를 배치할 노드의 우선순위를 정하는 것. 즉, requiredDuringSchedulingIgnoredDuringExecution에 포함되는 노드에 배치해야 하며, preferredDuringSchedulingIgnoredDuringExecution에 포함되는 노드에 우선적으로 배치 함
requiredDuringSchedulingIgnoredDuringExecution + preferredDuringSchedulingIgnoredDuringExecution
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms: # 파드를 배치할 노드를 선택하는 조건을 정의
- matchExpressions: # 조건을 정의하는 표현식
- key: disktype # 노드의 레이블 키
operator: In # 조건을 정의하는 연산자 (In , NotIn , Exists , DoseNotExits)
values: # 조건에 맞는 값들의 리스트 (operator가 In 혹은 NotIn 일때 사용)
- ssd
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1 # 조건의 우선순위를 나타내는 값
preference:
matchExpressions: # 조건을 정의하는 표현식
- key: another-node-label-key # 노드의 레이블 키
operator: In # 조건을 정의하는 연산자
values:
- another-node-label-value # 조건에 맞는 값들의 리스트
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
3. podAffinity / podAntiAffinity
특정 파드들이 같은 노드에 배치 되도록(podAffinity), 혹은 특정 파드들이 같은 노드에 배치 되지 않도록 하는 방법 (podAntiAffinity)
podAffinify
apiVersion: apps/v1
kind: Deployment
metadata:
name: pod-affinity-example
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- myapp
topologyKey: "kubernetes.io/hostname"
containers:
- name: myapp-container
image: myapp-image
podAntiAffinity
apiVersion: apps/v1
kind: Deployment
metadata:
name: anti-affinity-example
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- myapp
topologyKey: "kubernetes.io/hostname"
containers:
- name: myapp-container
image: myapp-image