Last active
May 16, 2023 18:34
-
-
Save devops-school/3da18faede22b18ac7013c404bc10740 to your computer and use it in GitHub Desktop.
Example of nodeSelector and nodeAffinity in Kubernetes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apiVersion: v1 | |
kind: Pod | |
metadata: | |
name: with-node-affinity | |
spec: | |
affinity: | |
nodeAffinity: | |
requiredDuringSchedulingIgnoredDuringExecution: | |
nodeSelectorTerms: | |
- matchExpressions: | |
- key: kubernetes.io/e2e-az-name | |
operator: In | |
values: | |
- e2e-az1 | |
- e2e-az2 | |
preferredDuringSchedulingIgnoredDuringExecution: | |
- weight: 1 | |
preference: | |
matchExpressions: | |
- key: another-node-label-key | |
operator: In | |
values: | |
- another-node-label-value | |
containers: | |
- name: with-node-affinity | |
image: k8s.gcr.io/pause:2.0 | |
TODO: Debug this example | |
--- | |
apiVersion: v1 | |
kind: Pod | |
metadata: | |
name: nginx | |
labels: | |
nginx: yes | |
annotations: | |
scheduler.alpha.kubernetes.io/affinity: > | |
{ | |
"nodeAffinity": { | |
"requiredDuringSchedulingIgnoredDuringExecution": [ | |
{ | |
"labelSelector": { | |
"matchExpressions": [ | |
{ | |
"key": "x-web", | |
"operator": "In", | |
"values": ["yes", "true"] | |
} | |
] | |
} | |
} | |
] | |
}, | |
"podAntiAffinity": { | |
"requiredDuringSchedulingIgnoredDuringExecution": [ | |
{ | |
"labelSelector": { | |
"matchExpressions": [ | |
{ | |
"key": "httpd", | |
"operator": "Exists" | |
"values": ["yes", "true"] | |
} | |
] | |
} | |
} | |
] | |
} | |
} | |
spec: | |
containers: | |
- name: nginx | |
image: nginx | |
--- | |
apiVersion: v1 | |
kind: Pod | |
metadata: | |
name: httpd | |
labels: | |
nginx: yes | |
annotations: | |
scheduler.alpha.kubernetes.io/affinity: > | |
{ | |
"nodeAffinity": { | |
"preferredDuringSchedulingIgnoredDuringExecution": [ | |
{ | |
"labelSelector": { | |
"matchExpressions": [ | |
{ | |
"key": "y-web", | |
"operator": "In", | |
"values": ["yes", "true"] | |
} | |
] | |
} | |
} | |
] | |
} | |
} | |
spec: | |
containers: | |
- name: httpd | |
image: httpd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
apiVersion: v1 | |
kind: Pod | |
metadata: | |
name: nginx | |
labels: | |
env: test | |
spec: | |
containers: | |
- name: nginx | |
image: nginx | |
imagePullPolicy: IfNotPresent | |
nodeSelector: | |
key1: value1 | |
key2: value2 | |
... | |
--- | |
apiVersion: v1 | |
kind: Pod | |
metadata: | |
name: nginx | |
labels: | |
env: test | |
spec: | |
containers: | |
- name: nginx | |
image: nginx | |
imagePullPolicy: IfNotPresent | |
nodeSelector: | |
disktype: ssd | |
... | |
--- | |
apiVersion: v1 | |
kind: Pod | |
metadata: | |
name: nginx | |
labels: | |
env: test | |
spec: | |
containers: | |
- name: nginx | |
image: nginx | |
nodeSelector: | |
beta.kubernetes.io/instance-type: m3.medium | |
example.com/load-balancer: true | |
--- | |
apiVersion: v1 | |
kind: Pod | |
metadata: | |
name: httpd | |
labels: | |
env: test | |
spec: | |
containers: | |
- name: httpd | |
image: httpd | |
nodeSelector: | |
failure-domain.beta.kubernetes.io/region: us-west-2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/devops-school/3da18faede22b18ac7013c404bc10740#file-example-nodeaffinity-kubernetes-yaml-L81
The label for httpd pod should be
httpd
, correct?