더 실용적인 사용 사례
더 실용적인 사용 사례인 AntiAffinity
는 ReplicaSets
,StatefulSets
,Deployments
등과 같이 더 높은 수준의 컬렉션과 함께 사용할 때 훨씬 더 유용 할 수 있습니다. 워크로드 세트가 함께 사용 되도록 쉽게 구성 할 수 있습니다. 파드는 동일하게 정의된 토폴로지(예 : 동일한 노드)에 위치 합니다.
1. 동일 노드에 배포
3개의 노드 클러스터에서 한개의 웹 애플리케이션에는 redis와 같은 인 메모리 캐시가 있습니다. 우리는 웹 서버가 가능한 한 캐시와 함께 배치 되기를 원합니다.
다음은 3개의 Replicaset과 app=store
selector 레이블이 있는 간단한 redis 배포의 YAML 스니펫 입니다. 배포에는 스케줄러가 단일 노드에서 복제본을 함께 배치하지 않도록 구성된 ‘PodAntiAffinity’가 있습니다.
cat <<EoF > ~/environment/redis-with-node-affinity.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis-cache
spec:
selector:
matchLabels:
app: store
replicas: 3
template:
metadata:
labels:
app: store
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- store
topologyKey: "kubernetes.io/hostname"
containers:
- name: redis-server
image: redis:3.2-alpine
EoF
아래의 웹 서버 배포의 YAML 에는 podAntiAffinity
및 podAffinity
가 구성되어 있습니다. 이렇게 하면 스케줄러에 모든 복제본이 selector 레이블 app=store
가 있는 파드와 같은 위치에 있음을 알립니다. 이렇게 하면 각 웹 서버 replica가 단일 노드에 함께 배치 되지 않습니다.
cat <<EoF > ~/environment/web-with-node-affinity.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-server
spec:
selector:
matchLabels:
app: web-store
replicas: 3
template:
metadata:
labels:
app: web-store
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- web-store
topologyKey: "kubernetes.io/hostname"
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- store
topologyKey: "kubernetes.io/hostname"
containers:
- name: web-app
image: nginx:1.12-alpine
EoF
이 배포를 적용 해 보겠습니다.
kubectl apply -f ~/environment/redis-with-node-affinity.yaml
kubectl apply -f ~/environment/web-with-node-affinity.yaml
위의 두 배포를 생성하면 3개의 노드 클러스터가 아래와 같이 보일 것입니다.
node-1 - webserver-1 - cache-1
node-2 - webserver-2 - cache-2
node-3 - webserver-3 - cache-3
보는 것 처럼 웹 서버의 3개 복제본은 모두 예상대로 캐시와 함께 자동으로 배치 됩니다.
# We will use --sort-by to filter by nodes name
kubectl get pods -o wide --sort-by='.spec.nodeName'

Last updated
Was this helpful?