K8s之调度约束

注意此篇文章接上篇:K8s之Pod进阶
https://blog.51cto.com/14464303/2472123

原理:

kubernetes通过watch的机制进行每个组件的协作,每个组件之间的设计实现了解耦

K8s之调度约束

调度方式:

nodeName用于将Pod调度到指定的Node名称上(跳过调度器直接分配)

nodeSelector用于将Pod调度到匹配Label的Node上(前提是node要有标签)

图解:

左上角的运维人员往节点中创建一个nginx资源

(1)API Server和etcd和Scheduler是master

(2)Kubelet和Docker是node节点

API Server做为唯一入口,接受create创建资源的属性信息写入到etcd中(属性信息:名称,镜像名称,限制条件),etcd完善发现机制(watch)给Scheduler调度器(查看那个节点适合),然后绑定相关pod的网络信息,反馈给API Server,收到信息后api写入etcd中,此时etcd存储了pod的网络信息(IP),node1、中的kubelet会管理pod资源,会触发容器的创建命令,安装完成后docker就会反馈状态信息给API Server,当API Server收到状态信息写入到etcd中

总结:

apiserver相当于是平台中的书记(负责记录)

etcd相当于书记的记事本(内容写在其中)

结论:API Server如果挂了,什么都玩不了


示例1: nodeName

指定创建到某一台节点服务器上

[ demo]# vim pod5.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod-example
  labels:
    app: nginx
spec:
  nodeName: 192.168.18.148      #指定给node1节点
  containers:
  - name: nginx
    image: nginx:1.15
#验证就可以到node1上看是否创建了nginx

`创建`
[ demo]# kubectl create -f pod5.yaml
pod/pod-example created

[ demo]# kubectl get pods
NAME                                READY   STATUS             RESTARTS   AGE
pod-example                         1/1     Running            0          37s

`查看详细事件(发现未经过调度器)`
[ demo]# kubectl describe pod pod-example
Events:
  Type    Reason   Age   From                      Message
  ----    ------   ----  ----                      -------
  Normal  Pulled   37s   kubelet, 192.168.18.148  Container image "nginx:1.15" already present on machine       #此处未经过Scheduled调度器直接拉取镜像
  Normal  Created  37s   kubelet, 192.168.18.148  Created container
  Normal  Started  37s   kubelet, 192.168.18.148  Started container

`清空pod资源`
[ demo]# kubectl delete -f .
[ demo]# kubectl get pods
No resources found.

示例2:nodeSelector

`获取标签帮助`

`需要获取node上的NAME名称`
[ demo]# kubectl get node
NAME             STATUS   ROLES    AGE   VERSION
192.168.18.145   Ready    <none>   14d   v1.12.3
192.168.18.148   Ready    <none>   14d   v1.12.3

`给对应的node设置标签分别为ky=a和ky=b`
[ demo]# kubectl label nodes 192.168.18.148 ky=a
node/192.168.18.148 labeled
[ demo]# kubectl label nodes 192.168.18.145 ky=b
node/192.168.18.145 labeled

`查看标签`
[ demo]# kubectl get nodes --show-labels
NAME             STATUS   ROLES    AGE   VERSION   LABELS
192.168.18.145   Ready    <none>   14d   v1.12.3   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=192.168.18.145,ky=b
192.168.18.148   Ready    <none>   14d   v1.12.3   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=192.168.18.148,ky=a

`pod5.yaml文件做如下修改`
[ demo]# vim pod5.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod-example
  labels:
    app: nginx
spec:
  nodeSelector:     #修改处
    ky: a           #修改处
  containers:
  - name: nginx
    image: nginx:1.15
#修改完成后按Esc退出插入模式,输入:wq保存退出

`创建新资源`
[ demo]# kubectl create -f pod5.yaml
pod/pod-example created
[ demo]# kubectl get pods
NAME                                READY   STATUS             RESTARTS   AGE
pod-example                         1/1     Running            0          3s

`查看详细事件(通过事件可以观察经过调度器分配)`
[ demo]# kubectl describe pod pod-example
Events:
  Type    Reason     Age   From                     Message
  ----    ------     ----  ----                     -------
  Normal  Scheduled  85s   default-scheduler        Successfully assigned default/pod-example to 192.168.18.148     #此时经过了Scheduled调度器
  Normal  Pulled     85s   kubelet, 192.168.18.148  Container image "nginx:1.15" already present on machine
  Normal  Created    85s   kubelet, 192.168.18.148  Created container
  Normal  Started    84s   kubelet, 192.168.18.148  Started container

`查看分配节点`
[ demo]# kubectl get pods -o wide
NAME          READY  STATUS    RESTARTS   AGE     IP            NODE       NOMINATED NODE
pod-example   1/1    Running   0          3m35s   172.17.32.2   192.168.18.148   <none>

故障排除

K8s之调度约束

1.查看pod事件

kubectl describe TYPE NAME_PREFIX

2.查看pod日志(Failed状态下)

kubectl logs POD_NAME

3.进入pod(状态为running,但是服务没有提供)

kubectl exec –it POD_NAME bash