kubernetes (k8s) CKA认证之第二课:亲和性与 Pod 的调度
2019-07-17 16:15 jetwill 阅读(725) 评论(0) 编辑 收藏 举报手动调度一个 pod
// cat manual-schedule.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
run: pod-manual-schedule
name: pod-manual-schedule
namespace: default
spec:
nodeName: "app.centos7.com"
containers:
- name: my-pod
image: nginx:alpine
执行以下命令即可验证。
kubectl create -f manual-schedule.yaml
kubectl get po -owide
kubectl describe po pod-manual-schedule|less
kubectl delete -f manual-schedule.yaml
NodeAffinity 结点的亲和性
kubectl label node app.centos7.com has-eip=yes
kubectl get node --show-labels
vi node-affinity.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
run: node-affinity
name: node-affinity
namespace: default
spec:
containers:
- name: node-affinity
image: nginx:alpine
imagePullPolicy: IfNotPresent
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: has-eip
operator: In
values:
- "yes"
运行以下命令加以验证:
kubectl create -f node-affinity.yaml
kubectl get po -owide
结果:
[root@svn ch02]# kubectl get po -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
node-affinity 1/1 Running 0 5m49s 10.244.2.117 app.centos7.com <none> <none>
[root@svn ch02]#
PodAffinity (保留上一步的 node-affinity 处于 Running 状态 ) “豆荚”的亲和性
vi pod-affinity.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-affinity
labels:
run: pod-affinity
spec:
containers:
- name: pod-affinity
image: nginx:alpine
affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: run
operator: In
values:
- "node-affinity"
topologyKey: kubernetes.io/hostname
运行结果:
[root@svn ch02]# kubectl get po --show-labels -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
node-affinity 1/1 Running 0 129m 10.244.2.117 app.centos7.com <none> <none> run=node-affinity
[root@svn ch02]# kubectl create -f pod-affinity.yaml
pod/pod-affinity created
[root@svn ch02]# kubectl get po --show-labels -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
node-affinity 1/1 Running 0 129m 10.244.2.117 app.centos7.com <none> <none> run=node-affinity
pod-affinity 1/1 Running 0 4s 10.244.2.118 app.centos7.com <none> <none> run=pod-affinity
[root@svn ch02]#
pod-anti-affinity 反亲和性
前提:保留上一步的 node-affinity 和 pod-affinity 处于 Running 状态
vi pod-anti-affinity.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-anti-affinity
labels:
run: pod-anti-affinity
spec:
containers:
- name: pod-affinity
image: nginx:alpine
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: run
operator: In
values:
- "pod-affinity"
topologyKey: kubernetes.io/hostname
由于反亲和性,新生成的 pod-anti-affinity 将会被调度到 app.centos7.com 之外的结点。
运行结果:
[root@svn ch02]# kc create -f pod-anti-affinity.yaml
pod/pod-anti-affinity created
[root@svn ch02]# kc get pod -owide --show-labels
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
node-affinity 1/1 Running 0 153m 10.244.2.117 app.centos7.com <none> <none> run=node-affinity
pod-affinity 1/1 Running 0 24m 10.244.2.118 app.centos7.com <none> <none> run=pod-affinity
pod-anti-affinity 1/1 Running 0 3m18s 10.244.1.9 jks.centos7.com <none> <none> run=pod-anti-affinity
[root@svn ch02]#
Taints 与 Tolerations 污点与容忍
// 为 master 节点配置 taints 禁止普通 pod 调度到 master 上
kubectl taint nodes svn.centos7.com node-role.kubernetes.io/master=:NoSchedule
vi pod-tolerations.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
run: pod-tolerations
name: pod-tolerations
spec:
containers:
- name: my-pod
image: nginx:alpine
tolerations:
- key: gpu
operator: Equal
value: "yes"
effect: NoSchedule
给 node jks.centos7.com 增添对应的污点,如此,pod-tolerations 才可被调度在其之上。
kubectl taint node jks.centos7.com gpu=no:NoSchedule
kubectl taint node jks.centos7.com gpu=yes:NoSchedule --overwrite
运行结果:
[root@svn ch02]# kubectl create -f pod-tolerations.yaml
pod/pod-tolerations created
[root@svn ch02]# kubectl get po -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod-tolerations 1/1 Running 0 9s 10.244.1.18 jks.centos7.com <none> <none>
[root@svn ch02]#