八 pod:pod的调度——将 Pod 指派给节点( 三 )

可以看到pod运行在k8scloude3节点
[root@k8scloude1 pod]# kubectl get pod -o wideNAMEREADYSTATUSRESTARTSAGEIPNODENOMINATED NODEREADINESS GATESpod11/1Running07s10.244.251.209k8scloude3<none><none>[root@k8scloude1 pod]# kubectl delete pod pod1 --forcewarning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.[root@k8scloude1 pod]# kubectl get podsNo resources found in pod namespace.创建pod,nodeName: k8scloude1让pod运行在k8scloude1节点
[root@k8scloude1 pod]# vim schedulepod3.yaml #kind: Pod表示资源类型为Podlabels指定pod标签metadata下面的name指定pod名字containers下面全是容器的定义#image指定镜像名字imagePullPolicy指定镜像下载策略containers下面的name指定容器名#resources指定容器资源(CPU,内存等)env指定容器里的环境变量dnsPolicy指定DNS策略#restartPolicy容器重启策略ports指定容器端口containerPort容器端口hostPort节点上的端口#nodeName: k8scloude1让pod运行在k8scloude1节点[root@k8scloude1 pod]# cat schedulepod3.yamlapiVersion: v1kind: Podmetadata:creationTimestamp: nulllabels:run: pod1name: pod1namespace: podspec:nodeName: k8scloude1containers:- image: nginximagePullPolicy: IfNotPresentname: pod1resources: {}ports:- name: httpcontainerPort: 80protocol: TCPhostPort: 80dnsPolicy: ClusterFirstrestartPolicy: Alwaysstatus: {}[root@k8scloude1 pod]# kubectl apply -f schedulepod3.yamlpod/pod1 created可以看到pod运行在k8scloude1,注意k8scloude1是master节点,master节点一般不运行应用pod,并且k8scloude1有污点,一般来说,pod是不运行在有污点的主机上的,如果强制调度上去的话,pod的状态应该是pending,但是通过nodeName可以把一个pod调度到有污点的主机上正常运行的,比如nodeName指定pod运行在master上
[root@k8scloude1 pod]# kubectl get pods -o wideNAMEREADYSTATUSRESTARTSAGEIPNODENOMINATED NODEREADINESS GATESpod11/1Running047s10.244.158.81k8scloude1<none><none>[root@k8scloude1 pod]# kubectl delete pod pod1 --forcewarning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.pod "pod1" force deleted3.4 使用节点标签nodeSelector指定pod运行在哪个节点与很多其他 Kubernetes 对象类似,节点也有标签 。你可以手动地添加标签 。Kubernetes 也会为集群中所有节点添加一些标准的标签 。
通过为节点添加标签,你可以准备让 Pod 调度到特定节点或节点组上 。你可以使用这个功能来确保特定的 Pod 只能运行在具有一定隔离性,安全性或监管属性的节点上 。nodeSelector 是节点选择约束的最简单推荐形式 。你可以将 nodeSelector 字段添加到 Pod 的规约中设置你希望目标节点所具有的节点标签 。Kubernetes 只会将 Pod 调度到拥有你所指定的每个标签的节点上 。nodeSelector 提供了一种最简单的方法来将 Pod 约束到具有特定标签的节点上 。
3.4.1 查看标签【八 pod:pod的调度——将 Pod 指派给节点】查看节点node的标签,标签的格式:键值对:xxxx/yyyy.aaaa=456123,xxxx1/yyyy1.aaaa=456123,--show-labels参数显示标签
[root@k8scloude1 pod]# kubectl get nodes --show-labelsNAMESTATUSROLESAGEVERSIONLABELSk8scloude1Readycontrol-plane,master7d1hv1.21.0beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8scloude1,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node-role.kubernetes.io/master=,node.kubernetes.io/exclude-from-external-load-balancers=k8scloude2Ready<none>7dv1.21.0beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8scloude2,kubernetes.io/os=linuxk8scloude3Ready<none>7dv1.21.0beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8scloude3,kubernetes.io/os=linux

经验总结扩展阅读