1. 创建一个 nginx Deployment,nginx-pod.yml 文件内容如下:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 1
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
2. 执行命令
cluster/kubectl.sh apply -f nginx-pod.yml
3. 在 pkg/scheduler/schedule_one.go:66 断点,调用栈如下
4. 略去 scheduler 的 filter 和 score 环节,我们重点看 bind 方法,把 pod 和 node 绑定起来
5. scheduler 为 pod 选取合适的 node 后,进行绑定,这个绑定操作到底做了什么呢?继续跟代码
我们看到,bind 操作其实是向 apiserver 发送了一个 post 请求,大致看下 post 请求的 body,能猜出来是 pod 和 node 的信息
6. 那么 apiserver 是如何处理这个请求的呢 ?pkg/registry/core/pod/storage/storage.go:238 中断点,得到调用栈如下
apiserver 把 scheduler 的分配结果写到 etcd 中,猜想应该是通过 list-watch 机制告知 ReplicaSetController,最终引导 kubelet 拉起 pod 进程。
查看 pod 日志,印证了猜想:
因为开发环境没有进行配置代理和国内镜像源,导致无法从 k8s.gcr.io 拉取镜像。