kubernets通过给node设置标签实现pod指定node部署

1、添加标签ssd,模拟Node2节点是ssd硬盘

[[email protected] ~]$ kubectl get node --show-labels 
NAME STATUS ROLES AGE VERSION LABELS
k8s-master Ready master 52d v1.14.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master,kubernetes.io/os=linux,node-role.kubernetes.io/master=
k8s-node1 Ready <none> 52d v1.14.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node1,kubernetes.io/os=linux
k8s-node2 Ready <none> 52d v1.14.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node2,kubernetes.io/os=linux
k8s-node3 Ready <none> 52d v1.14.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node3,kubernetes.io/os=linux
[[email protected] ~]$ kubectl label node k8s-node2 disktype=ssd
node/k8s-node2 labeled
[[email protected] ~]$ kubectl get node --show-labels 
NAME STATUS ROLES AGE VERSION LABELS
k8s-master Ready master 52d v1.14.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master,kubernetes.io/os=linux,node-role.kubernetes.io/master=
k8s-node1 Ready <none> 52d v1.14.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node1,kubernetes.io/os=linux
k8s-node2 Ready <none> 52d v1.14.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disktype=ssd,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node2,kubernetes.io/os=linux
k8s-node3 Ready <none> 52d v1.14.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node3,kubernetes.io/os=linux

制定nginx调度到ssd标签的node

[[email protected] ~]$ cp nginx.yml nginx_ssd.yml
[[email protected] ~]$ vim nginx_ssd.yml 
[[email protected] ~]$ diff nginx.yml nginx_ssd.yml 
15a16,17
> nodeSelector:
> disktype: ssd
[[email protected] ~]$ cat nginx_ssd.yml 
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 5
template:
metadata:
labels:
app: web_server
spec:
containers:
- name: nginx
image: nginx:1.7.9
nodeSelector:
disktype: ssd
[[email protected] ~]$

重新部署配置文件

[[email protected] ~]$ kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
http-app-5b7568bf86-bnszg 1/1 Running 0 137m 10.244.1.20 k8s-node1 <none> <none>
http-app-5b7568bf86-vwtl6 1/1 Running 0 137m 10.244.3.14 k8s-node3 <none> <none>
myjob-4gbwc 0/1 Completed 0 52d 10.244.3.12 k8s-node3 <none> <none>
myjob-bpw87 0/1 Completed 0 52d 10.244.3.13 k8s-node3 <none> <none>
myjob-lmbbv 0/1 Completed 0 52d 10.244.1.19 k8s-node1 <none> <none>
myjob-zz9fx 0/1 Completed 0 52d 10.244.1.18 k8s-node1 <none> <none>
nginx-deployment-656d6ccbd5-76cbf 1/1 Running 0 13s 10.244.3.19 k8s-node3 <none> <none>
nginx-deployment-656d6ccbd5-ch24p 1/1 Running 0 13s 10.244.3.20 k8s-node3 <none> <none>
nginx-deployment-656d6ccbd5-pgg86 1/1 Running 0 13s 10.244.1.26 k8s-node1 <none> <none>
nginx-deployment-656d6ccbd5-r2kpf 1/1 Running 0 13s 10.244.2.18 k8s-node2 <none> <none>
nginx-deployment-656d6ccbd5-swmrs 1/1 Running 0 13s 10.244.2.17 k8s-node2 <none> <none>
[[email protected] ~]$ kubectl apply -f nginx_ssd.yml 
deployment.extensions/nginx-deployment configured

可以看到k8s正在自动调整中

[[email protected] ~]$ kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
http-app-5b7568bf86-bnszg 1/1 Running 0 138m 10.244.1.20 k8s-node1 <none> <none>
http-app-5b7568bf86-vwtl6 1/1 Running 0 138m 10.244.3.14 k8s-node3 <none> <none>
myjob-4gbwc 0/1 Completed 0 52d 10.244.3.12 k8s-node3 <none> <none>
myjob-bpw87 0/1 Completed 0 52d 10.244.3.13 k8s-node3 <none> <none>
myjob-lmbbv 0/1 Completed 0 52d 10.244.1.19 k8s-node1 <none> <none>
myjob-zz9fx 0/1 Completed 0 52d 10.244.1.18 k8s-node1 <none> <none>
nginx-deployment-656d6ccbd5-76cbf 0/1 Terminating 0 28s <none> k8s-node3 <none> <none>
nginx-deployment-656d6ccbd5-pgg86 0/1 Terminating 0 28s 10.244.1.26 k8s-node1 <none> <none>
nginx-deployment-656d6ccbd5-r2kpf 1/1 Terminating 0 28s 10.244.2.18 k8s-node2 <none> <none>
nginx-deployment-656d6ccbd5-swmrs 1/1 Running 0 28s 10.244.2.17 k8s-node2 <none> <none>
nginx-deployment-6bb686dbcf-6scnc 1/1 Running 0 4s 10.244.2.20 k8s-node2 <none> <none>
nginx-deployment-6bb686dbcf-f25ds 0/1 ContainerCreating 0 2s <none> k8s-node2 <none> <none>
nginx-deployment-6bb686dbcf-f4gjf 1/1 Running 0 2s 10.244.2.21 k8s-node2 <none> <none>
nginx-deployment-6bb686dbcf-g9zq2 1/1 Running 0 4s 10.244.2.19 k8s-node2 <none> <none>
nginx-deployment-6bb686dbcf-scpsv 0/1 ContainerCreating 0 1s <none> k8s-node2 <none> <none>

[[email protected] ~]$ kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
http-app-5b7568bf86-bnszg 1/1 Running 0 138m 10.244.1.20 k8s-node1 <none> <none>
http-app-5b7568bf86-vwtl6 1/1 Running 0 138m 10.244.3.14 k8s-node3 <none> <none>
myjob-4gbwc 0/1 Completed 0 52d 10.244.3.12 k8s-node3 <none> <none>
myjob-bpw87 0/1 Completed 0 52d 10.244.3.13 k8s-node3 <none> <none>
myjob-lmbbv 0/1 Completed 0 52d 10.244.1.19 k8s-node1 <none> <none>
myjob-zz9fx 0/1 Completed 0 52d 10.244.1.18 k8s-node1 <none> <none>
nginx-deployment-656d6ccbd5-pgg86 0/1 Terminating 0 34s 10.244.1.26 k8s-node1 <none> <none>
nginx-deployment-656d6ccbd5-swmrs 0/1 Terminating 0 34s 10.244.2.17 k8s-node2 <none> <none>
nginx-deployment-6bb686dbcf-6scnc 1/1 Running 0 10s 10.244.2.20 k8s-node2 <none> <none>
nginx-deployment-6bb686dbcf-f25ds 1/1 Running 0 8s 10.244.2.22 k8s-node2 <none> <none>
nginx-deployment-6bb686dbcf-f4gjf 1/1 Running 0 8s 10.244.2.21 k8s-node2 <none> <none>
nginx-deployment-6bb686dbcf-g9zq2 1/1 Running 0 10s 10.244.2.19 k8s-node2 <none> <none>
nginx-deployment-6bb686dbcf-scpsv 1/1 Running 0 7s 10.244.2.23 k8s-node2 <none> <none>
[[email protected] ~]$

调整完毕,全部到了node2节点

[[email protected] ~]$ kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
http-app-5b7568bf86-bnszg 1/1 Running 0 139m 10.244.1.20 k8s-node1 <none> <none>
http-app-5b7568bf86-vwtl6 1/1 Running 0 139m 10.244.3.14 k8s-node3 <none> <none>
myjob-4gbwc 0/1 Completed 0 52d 10.244.3.12 k8s-node3 <none> <none>
myjob-bpw87 0/1 Completed 0 52d 10.244.3.13 k8s-node3 <none> <none>
myjob-lmbbv 0/1 Completed 0 52d 10.244.1.19 k8s-node1 <none> <none>
myjob-zz9fx 0/1 Completed 0 52d 10.244.1.18 k8s-node1 <none> <none>
nginx-deployment-6bb686dbcf-6scnc 1/1 Running 0 55s 10.244.2.20 k8s-node2 <none> <none>
nginx-deployment-6bb686dbcf-f25ds 1/1 Running 0 53s 10.244.2.22 k8s-node2 <none> <none>
nginx-deployment-6bb686dbcf-f4gjf 1/1 Running 0 53s 10.244.2.21 k8s-node2 <none> <none>
nginx-deployment-6bb686dbcf-g9zq2 1/1 Running 0 55s 10.244.2.19 k8s-node2 <none> <none>
nginx-deployment-6bb686dbcf-scpsv 1/1 Running 0 52s 10.244.2.23 k8s-node2 <none> <none>

删除标签

[[email protected] ~]$ kubectl label node k8s-node2 disktype-
node/k8s-node2 labeled
[[email protected] ~]$ kubectl get node --show-labels 
NAME STATUS ROLES AGE VERSION LABELS
k8s-master Ready master 52d v1.14.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master,kubernetes.io/os=linux,node-role.kubernetes.io/master=
k8s-node1 Ready <none> 52d v1.14.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node1,kubernetes.io/os=linux
k8s-node2 Ready <none> 52d v1.14.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node2,kubernetes.io/os=linux
k8s-node3 Ready <none> 52d v1.14.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node3,kubernetes.io/os=linux

但是Pod不会自动调整

[[email protected] ~]$ kubectl get pod
NAME READY STATUS RESTARTS AGE
http-app-5b7568bf86-bnszg 1/1 Running 0 143m
http-app-5b7568bf86-vwtl6 1/1 Running 0 143m
myjob-4gbwc 0/1 Completed 0 52d
myjob-bpw87 0/1 Completed 0 52d
myjob-lmbbv 0/1 Completed 0 52d
myjob-zz9fx 0/1 Completed 0 52d
nginx-deployment-6bb686dbcf-6scnc 1/1 Running 0 5m14s
nginx-deployment-6bb686dbcf-f25ds 1/1 Running 0 5m12s
nginx-deployment-6bb686dbcf-f4gjf 1/1 Running 0 5m12s
nginx-deployment-6bb686dbcf-g9zq2 1/1 Running 0 5m14s
nginx-deployment-6bb686dbcf-scpsv 1/1 Running 0 5m11s
[[email protected] ~]$

手动调整pod,选择之前的配置文件

[[email protected] ~]$ kubectl apply -f nginx.yml 
deployment.extensions/nginx-deployment configured

pod开始自动调整

[[email protected] ~]$ kubectl get pod
NAME READY STATUS RESTARTS AGE
http-app-5b7568bf86-bnszg 1/1 Running 0 143m
http-app-5b7568bf86-vwtl6 1/1 Running 0 143m
myjob-4gbwc 0/1 Completed 0 52d
myjob-bpw87 0/1 Completed 0 52d
myjob-lmbbv 0/1 Completed 0 52d
myjob-zz9fx 0/1 Completed 0 52d
nginx-deployment-656d6ccbd5-g9htz 1/1 Running 0 3s
nginx-deployment-656d6ccbd5-njfvh 1/1 Running 0 2s
nginx-deployment-656d6ccbd5-p2ckr 1/1 Running 0 3s
nginx-deployment-656d6ccbd5-rv9nd 0/1 ContainerCreating 0 1s
nginx-deployment-656d6ccbd5-sxlg6 1/1 Running 0 1s
nginx-deployment-6bb686dbcf-6scnc 1/1 Terminating 0 5m28s
nginx-deployment-6bb686dbcf-f25ds 0/1 Terminating 0 5m26s
nginx-deployment-6bb686dbcf-f4gjf 1/1 Terminating 0 5m26s
nginx-deployment-6bb686dbcf-g9zq2 1/1 Terminating 0 5m28s

稍等片刻,pod调整完毕

[[email protected] ~]$ kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
http-app-5b7568bf86-bnszg 1/1 Running 0 145m 10.244.1.20 k8s-node1 <none> <none>
http-app-5b7568bf86-vwtl6 1/1 Running 0 145m 10.244.3.14 k8s-node3 <none> <none>
myjob-4gbwc 0/1 Completed 0 52d 10.244.3.12 k8s-node3 <none> <none>
myjob-bpw87 0/1 Completed 0 52d 10.244.3.13 k8s-node3 <none> <none>
myjob-lmbbv 0/1 Completed 0 52d 10.244.1.19 k8s-node1 <none> <none>
myjob-zz9fx 0/1 Completed 0 52d 10.244.1.18 k8s-node1 <none> <none>
nginx-deployment-656d6ccbd5-g9htz 1/1 Running 0 109s 10.244.3.21 k8s-node3 <none> <none>
nginx-deployment-656d6ccbd5-njfvh 1/1 Running 0 108s 10.244.2.24 k8s-node2 <none> <none>
nginx-deployment-656d6ccbd5-p2ckr 1/1 Running 0 109s 10.244.1.27 k8s-node1 <none> <none>
nginx-deployment-656d6ccbd5-rv9nd 1/1 Running 0 107s 10.244.1.28 k8s-node1 <none> <none>
nginx-deployment-656d6ccbd5-sxlg6 1/1 Running 0 107s 10.244.3.22 k8s-node3 <none> <none>
此条目发表在kubernetes分类目录,贴了标签。将固定链接加入收藏夹。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注