[Kubernetes] Kubernetes Taints, Toleration and affinity 설명과 실습
```
[k8s] Kubernetes Taints, Toleration and affinity 설명과 실습
```
이번 글을 통해 배워갈 내용
- taints, toleration, affinity 이론
- taints 실습
- toleration 실습
- affinity 실습
1. taints, toleration, affinity 이론
3줄 요약
Taint는 이 노드에는 해당 작업만 할 수 있어라고 하는 것
Toleration은 node에 설정된 taint에 대해서 이 작업은 괜찮아 할 수 있어라고 하는 것
affinity 이 파드는 이 노드에서만 실행해 라고 하는 것
2. Taints 실습
node에 taint 설정을 합니다
k get nodes
k taint nodes worker-1 key1=val1:NoSchedule
yaml 로 spec에 세팅도 가능합니다
taints:
- effect: NoSchedule
key: key1
value: val1
노드에 라벨을 생성하고
k label node worker-1 mykey=yellow
해당되는 노드에 할당될 deployment를 만들어줍니다
k create deploy my-sample --image=nginx --dry-run=client -o yaml > hello2.yaml
nano hello2.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: my-sample
name: my-sample
spec:
replicas: 1
selector:
matchLabels:
app: my-sample
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: my-sample
spec:
nodeSelector:
mykey: yellow
containers:
- image: nginx
name: nginx
resources: {}
status: {}
k create -f hello2.yaml
node selector로 인해 woker-1 노드에 schedule을 해야 하는데
node에 taint 값으로 인해 schedule이 안 돼서 pending입니다.
3. toleration 실습
2번 실습을 이어서 합니다.
해당되는 deploy를 지우고
k delete -f hello2.yaml
toleration을 세팅해 줍니다
nano hello2.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: my-sample
name: my-sample
spec:
replicas: 1
selector:
matchLabels:
app: my-sample
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: my-sample
spec:
nodeSelector:
mykey: yellow
containers:
- image: nginx
name: nginx
resources: {}
tolerations:
- key: "key1"
value: "val1"
operator: "Equal"
effect: "NoSchedule"
status: {}
deployment를 실행합니다
k create hello2.yaml
taint 설정이 되어있음에도 toleration 덕분에
pod가 정상적으로 실행되었습니다.
4. affinity 실습
node에 affinity 설정을 합니다.
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: my-sample
name: my-sample
spec:
replicas: 1
selector:
matchLabels:
app: my-sample
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: my-sample
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: mkey
operator: In
values:
- yellow
containers:
- image: nginx
name: nginx
resources: {}
status: {}
requiredDuringSchedulingIgnoredDuringExecution으로 세팅되어서
해당되는 라벨을 가진 woker-1 노드에 할당되어야 하나
taint가 설정되어서 pending을 확인 가능합니다.
아래와 같이
preferredDuringSchedulingIgnoredDuringExecution을 사용하게 되면
선호를 하지만 taint로 막히면 다른 노드에 pod가 할당됩니다.
k delete -f hello2.yaml
nano hello2.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: my-sample
name: my-sample
spec:
replicas: 1
selector:
matchLabels:
app: my-sample
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: my-sample
spec:
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 99
preference:
matchExpressions:
- key: mkey
operator: In
values:
- yellow
containers:
- image: nginx
name: nginx
resources: {}
status: {}
k create -f hello2.yaml
읽어주셔서 감사합니다
무엇인가 얻어가셨기를 바라며
오늘도 즐거운 코딩 하시길 바랍니다 ~ :)
참조 및 인용
'DevOps > Kubernetes' 카테고리의 다른 글
[Kubernetes] Kubernetes etcd backup 과 restore 설명과 실습 (0) | 2024.10.09 |
---|---|
[Kubernetes] Kubernetes CRD 와 CR 설명과 실습 (1) | 2024.09.20 |
[Kubernetes] Kubernetes Configmap 설명과 실습 (0) | 2024.09.19 |
[Kubernetes] Kubernetes Volumes 설명과 실습 (1) | 2024.09.16 |
[Kubernetes] Kubernetes NetworkPolicy 설명과 실습 (0) | 2024.09.15 |