mirror of
https://github.com/marcel-dempers/docker-development-youtube-series.git
synced 2025-06-06 17:01:30 +00:00
affinity updates
This commit is contained in:
parent
f925d502f4
commit
f5a6234ef4
@ -22,7 +22,7 @@ demo-worker3 Ready <none> 35s v1.28.0
|
||||
|
||||
## Node Affinity
|
||||
|
||||
[Node Affinity](https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#node-affinity) is similar to `nodeSelector` however you can define more complex expressions
|
||||
[Node Affinity](https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#node-affinity) is similar to `nodeSelector` however you can define more complex expressions. "Like my pods must run on SSD nodes or preffer SSD nodes"
|
||||
|
||||
For example:
|
||||
* Node selector is a hard and fast rule meaning a pod will not be scheduled if the selection is not satisfied
|
||||
@ -59,5 +59,34 @@ kubectl taint nodes demo-worker type=ssd:NoSchedule-
|
||||
kubectl taint nodes demo-worker3 type=ssd:NoSchedule-
|
||||
```
|
||||
|
||||
## Pod Affinity
|
||||
|
||||
|
||||
```
|
||||
kubectl apply -f app-disk --replicas 3
|
||||
kubectl apply -f web-disk --replicas 3
|
||||
```
|
||||
|
||||
## Pod Anti-Affinity
|
||||
|
||||
Let's say we observe our `app-disk` application disk usage is quite intense, and we would like to prevent `app-disk` pods from running together. </br>
|
||||
This is where anti-affinity comes in:
|
||||
|
||||
```
|
||||
podAntiAffinity:
|
||||
requiredDuringSchedulingIgnoredDuringExecution:
|
||||
- labelSelector:
|
||||
matchExpressions:
|
||||
- key: app
|
||||
operator: In
|
||||
values:
|
||||
- app-disk
|
||||
topologyKey: "kubernetes.io/hostname"
|
||||
```
|
||||
|
||||
After applying the above, we can roll it out:
|
||||
|
||||
```
|
||||
kubectl apply -f node-affinity.yaml
|
||||
```
|
||||
|
||||
|
@ -2,17 +2,17 @@ kind: Cluster
|
||||
apiVersion: kind.x-k8s.io/v1alpha4
|
||||
nodes:
|
||||
- role: control-plane
|
||||
- role: worker
|
||||
- role: worker #demo-worker
|
||||
labels:
|
||||
zone: us-east
|
||||
type: ssd
|
||||
- role: worker
|
||||
- role: worker #demo-worker2
|
||||
labels:
|
||||
zone: us-west
|
||||
type: ssd
|
||||
- role: worker
|
||||
- role: worker #demo-worker3
|
||||
labels:
|
||||
zone: us-east
|
||||
- role: worker
|
||||
- role: worker #demo-worker4
|
||||
labels:
|
||||
zone: us-west
|
||||
|
@ -19,6 +19,15 @@ spec:
|
||||
- name: app-disk
|
||||
image: nginx:latest
|
||||
affinity:
|
||||
podAntiAffinity:
|
||||
requiredDuringSchedulingIgnoredDuringExecution:
|
||||
- labelSelector:
|
||||
matchExpressions:
|
||||
- key: app
|
||||
operator: In
|
||||
values:
|
||||
- app-disk
|
||||
topologyKey: "kubernetes.io/hostname"
|
||||
nodeAffinity:
|
||||
requiredDuringSchedulingIgnoredDuringExecution:
|
||||
nodeSelectorTerms:
|
||||
|
@ -2,29 +2,29 @@
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: app-disk2
|
||||
name: web-disk
|
||||
labels:
|
||||
app: app-disk2
|
||||
app: web-disk
|
||||
spec:
|
||||
selector:
|
||||
matchLabels:
|
||||
app: app-disk2
|
||||
app: web-disk
|
||||
replicas: 1
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: app-disk2
|
||||
app: web-disk
|
||||
spec:
|
||||
containers:
|
||||
- name: app-disk2
|
||||
- name: web-disk
|
||||
image: nginx:latest
|
||||
affinity:
|
||||
podAffinity:
|
||||
requiredDuringSchedulingIgnoredDuringExecution:
|
||||
- labelSelector:
|
||||
matchExpressions:
|
||||
- key: zone
|
||||
- key: app
|
||||
operator: In
|
||||
values:
|
||||
- us-east
|
||||
topologyKey: zone
|
||||
- app-disk
|
||||
topologyKey: "kubernetes.io/hostname"
|
Loading…
x
Reference in New Issue
Block a user