节点选择器:NodeSelector
如果我们期望把Pod调度到某一个特定的节点上,可以通过设定Pod.spec.nodeName给定node名称实现。我们可以给一部分node打上特有标签,在pod.spec.nodeSelector中匹配这些标签
# 查看node的label命令
kubectl get nodes --show-labels
# 给一个节点增加标签
kubectl label node k8s-node-1 effect=mysql
之后我们可以采用软亲和或者硬亲和的策略将pod调度到刚才标签的node上
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution: # 硬策略
nodeSelectorTerms:
- matchExpressions:
- key: effect
operator: NotIn
values:
- mysql
preferredDuringSchedulingIgnoredDuringExecution: # 软策略
- weight: 1
preference:
matchExpressions:
- key: effect
operator: In
values:
- mysql
实际案例
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-muban
namespace: mysql
labels:
app: mysql-muban
spec:
replicas: 1
revisionHistoryLimit: 3
selector:
matchLabels:
app: mysql-muban
template:
metadata:
labels:
app: mysql-muban
spec:
containers:
- name: mysql-muban
image: mysql:v5.7.22
volumeMounts:
- mountPath: /var/lib/mysql
name: mysqldb-muban
- mountPath: /etc/mysql
name: mysqlconf-muban
env:
- name: TZ
value: Asia/Shanghai
- name: MYSQL_ROOT_PASSWORD
value: Root123+
ports:
- containerPort: 3306
name: mysql-muban
resources:
requests:
cpu: 1
memory: 1G
limits:
cpu: 4
memory: 4G
volumes:
- name: mysqldb-muban
hostPath:
path: /data/mysql/muban/mysqldb
type: Directory
- name: mysqlconf-muban
hostPath:
path: /data/mysql/muban/conf
type: Directory
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: effect
operator: In
values:
- mysql
tolerations:
- key: "key"
operator: "Equal"
value: "value"
effect: NoExecute
---
apiVersion: v1
kind: Service
metadata:
name: mysql-muban
namespace: mysql
labels:
app: mysql-muban
spec:
type: NodePort
selector:
app: mysql-muban
ports:
- port: 3306
nodePort: 33307