6. Pod的节点分配

6.1 nodeName

1
2
3
4
5
6
7
8
9
10
# 编写Yaml文件
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx
nodeName: node01

6.2 nodeSelector

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 设置节点的Labels
kubectl label nodes node01 disktype=ssd

# 编写Yaml文件
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
env: test
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
nodeSelector:
disktype: ssd

6.3 Affinity and anti-affinity

6.3.1 Node affinity

硬性要求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 编写Yaml文件
apiVersion: v1
kind: Pod
metadata:
name: with-node-affinity
spec:
affinity:
nodeAffinity: # 使用node亲和进行判定
requiredDuringSchedulingIgnoredDuringExecution: # 硬性要求
nodeSelectorTerms: # 节点选择器的条件
- matchExpressions: # 定义匹配的表达式
- key: app # label的key
operator: In # 定义判断方式多种:In,NotIn,Exists,DoesNotexist,Gt,Lt。
values: # label的values
- nginx1
containers:
- name: with-node-affinity
image: nginx

软性要求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 编写Yaml文件
apiVersion: v1
kind: Pod
metadata:
name: with-node-affinity
spec:
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution: # 软性要求
- weight: 1 # 设置权重,分数越高优先级越高
preference:
matchExpressions:
- key: disktype
operator: In
values:
- ssd
containers:
- name: with-node-affinity
image: nginx

偏好要求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 设置节点的Labels
kubectl label nodes node02 disktype=ssd
kubectl label nodes node01 app=nginx1
kubectl label nodes node02 app=nginx2

# 编写Yaml文件
apiVersion: v1
kind: Pod
metadata:
name: with-node-affinity
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: app
operator: In
values:
- nginx1
- nginx2
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1
preference:
matchExpressions:
- key: disktype
operator: In
values:
- ssd
containers:
- name: with-node-affinity
image: nginx

6.3.2 Inter-pod affinity and anti-affinity

创建redis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 编写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

创建web-server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 编写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