Skip to content

Instantly share code, notes, and snippets.

@devops-school
Last active May 16, 2023 18:34
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save devops-school/3da18faede22b18ac7013c404bc10740 to your computer and use it in GitHub Desktop.
Save devops-school/3da18faede22b18ac7013c404bc10740 to your computer and use it in GitHub Desktop.
Example of nodeSelector and nodeAffinity in Kubernetes
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
---
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
@utsavkesharwani
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment