以nginx部署为例,指定部署到disktype为ssd的节点上。

Deployment File

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      imagePullSecrets: 
      - name: kleven-registry
      containers:
        - name: nginx
          image: kleven-repository:8083/nginx
      nodeSelector:
        disktype: ssd

假如文件名为 nginx-node-selector.yaml:

kubectl create -f nginx-node-selector.yaml

当没有节点匹配时

没有节点匹配时,pod无法被调度,会一直处于Pending状态。

kubectl get pod

NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-6f54d5c657-tp8kr   0/1     Pending   0          2m17s

通过describe可以查看到原因为所有节点(2个节点)都没有和node selector 匹配上。

kubectl describe pod nginx-deployment-6f54d5c657-tp8kr 

Events:
  Type     Reason            Age   From               Message
  ----     ------            ----  ----               -------
  Warning  FailedScheduling  34s   default-scheduler  0/2 nodes are available: 2 node(s) didn't match Pod's node affinity/selector.

当有节点匹配到时

我们给其中一个节点打上标签

kubectl label node i-jgdj1gg0 disktype=ssd
node/i-jgdj1gg0 labeled

再去看node,就会发现已经被成功调度,状态已经变成Running。

kubectl get pod
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-6f54d5c657-tp8kr   1/1     Running   0          5m48s

我们同样可以通过describe命令查看到完成的Events,并且可以看到container已经成功started。

kubectl describe pod nginx-deployment-6f54d5c657-tp8kr

Events:
  Type     Reason            Age                   From               Message
  ----     ------            ----                  ----               -------
  Warning  FailedScheduling  3m6s (x5 over 7m58s)  default-scheduler  0/2 nodes are available: 2 node(s) didn't match Pod's node affinity/selector.
  Normal   Scheduled         2m47s                 default-scheduler  Successfully assigned default/nginx-deployment-6f54d5c657-tp8kr to i-jgdj1gg0
  Normal   Pulling           2m46s                 kubelet            Pulling image "kleven-repository:8083/nginx"
  Normal   Pulled            2m38s                 kubelet            Successfully pulled image "kleven-repository:8083/nginx" in 7.441114609s
  Normal   Created           2m38s                 kubelet            Created container nginx
  Normal   Started           2m38s                 kubelet            Started container nginx
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐