Pod 调度策略
基于节点的调度
[root@master ~]# vim myhttp.yaml
---
kind: Pod
apiVersion: v1
metadata:
name: myhttp
spec:
nodeName: node-0001 # 基于节点名称进行调度
containers:
- name: apache
image: myos:httpd
[root@master ~]# kubectl apply -f myhttp.yaml
pod/myhttp created
[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
myhttp 1/1 Running 0 3s 10.244.1.6 node-0001
标签管理
查询标签
[root@master ~]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
myhttp 1/1 Running 0 2m34s <none>
[root@master ~]# kubectl get namespaces --show-labels
NAME STATUS AGE LABELS
default Active 3h44m kubernetes.io/metadata.name=default
kube-node-lease Active 3h44m kubernetes.io/metadata.name=kube-node-lease
kube-public Active 3h44m kubernetes.io/metadata.name=kube-public
kube-system Active 3h44m kubernetes.io/metadata.name=kube-system
[root@master ~]# kubectl get nodes --show-labels
NAME STATUS ROLES VERSION LABELS
master Ready control-plane v1.26.0 kubernetes.io/hostname=master
node-0001 Ready <none> v1.26.0 kubernetes.io/hostname=node-0001
node-0002 Ready <none> v1.26.0 kubernetes.io/hostname=node-0002
node-0003 Ready <none> v1.26.0 kubernetes.io/hostname=node-0003
node-0004 Ready <none> v1.26.0 kubernetes.io/hostname=node-0004
node-0005 Ready <none> v1.26.0 kubernetes.io/hostname=node-0005
使用标签过滤
# 使用标签过滤资源对象
[root@master ~]# kubectl get nodes -l kubernetes.io/hostname=master
NAME STATUS ROLES AGE VERSION
master Ready control-plane 3h38m v1.26.0
添加标签
[root@master ~]# kubectl label pod myhttp app=apache
pod/myhttp labeled
[root@master ~]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
myhttp 1/1 Running 0 14m app=apache
删除标签
[root@master ~]# kubectl label pod myhttp app-
pod/myhttp labeled
[root@master ~]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
myhttp 1/1 Running 0 14m <none>
资源文件标签
[root@master ~]# vim myhttp.yaml
---
kind: Pod
apiVersion: v1
metadata:
name: myhttp
labels: # 声明标签
app: apache # 标签键值对
spec:
containers:
- name: apache
image: myos:httpd
[root@master ~]# kubectl delete pods myhttp
pod "myhttp" deleted
[root@master ~]# kubectl apply -f myhttp.yaml
pod/myhttp created
[root@master ~]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
myhttp 1/1 Running 0 14m app=apache
基于标签的调度
[root@master ~]# kubectl get nodes node-0002 --show-labels
NAME STATUS ROLES VERSION LABELS
node-0002 Ready <none> v1.26.0 kubernetes.io/hostname=node-0002 ...
[root@master ~]# vim myhttp.yaml
---
kind: Pod
apiVersion: v1
metadata:
name: myhttp
labels:
app: apache
spec:
nodeSelector: # 基于节点标签进行调度
kubernetes.io/hostname: node-0002 # 节点