Pod是Kubernetes的最小部署单元,但是它是如何被创建运行起来的?具体过程是怎样的?涉及到了哪些组件?
带着这些疑问我就在网上查呀查呀,终于在查阅了许多文章之后发现了一个图片,根据内容整理了一下步骤。
1.kubectl > apiserver
kubectl发起请求到apiserver,告知需要创建pod
2.apiserver > etcd
apiserver将pod初始化信息存入etcd
3.apiserver > scheduler
apiserver通知scheduler,告知有新的pod,需要帮忙选择一个最佳的node节点
4.scheduler > apiserver
scheduler根据策略给pod选择最佳的node节点后,将结果反馈给apiserver
5.apiserver > etcd
apiserver将其存入etcd
6.apiserver > kubelet
apiserver通知kubelet,告知要创建新的pod
7.kubelet > docker
kubelet调用docker来启动容器
8.kubelet > apiserver
待容器成功运行后,kubelet将结果反馈给apiserver
9.apiserver > etcd
apiserver将其存入etcd
创建Pod的过程涉及到了 apiserver
、scheduler
、kubelet
3个组件,controller-manager
没有参与进来,事实是否真的如此?
可以测试一波,首先将 controller-manager
组件给停掉,然后尝试创建Pod,看下能否正常创建成功!
# 将 kube-controller-manager 停掉
[root@k8s-master-01 ~]# ss -lnpt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:10248 *:* users:(("kubelet",pid=29820,fd=24))
LISTEN 0 128 127.0.0.1:9099 *:* users:(("calico-node",pid=28589,fd=8))
LISTEN 0 128 127.0.0.1:37967 *:* users:(("containerd",pid=951,fd=15))
LISTEN 0 128 127.0.0.1:10259 *:* users:(("kube-scheduler",pid=2220,fd=7))
LISTEN 0 8 *:179 *:* users:(("bird",pid=28684,fd=7))
LISTEN 0 128 *:22 *:* users:(("sshd",pid=945,fd=3))
LISTEN 0 128 [::]:10249 [::]:* users:(("kube-proxy",pid=29819,fd=13))
LISTEN 0 128 [::]:10250 [::]:* users:(("kubelet",pid=29820,fd=20))
LISTEN 0 128 [::]:6443 [::]:* users:(("kube-apiserver",pid=3268,fd=7))
LISTEN 0 128 [::]:10256 [::]:* users:(("kube-proxy",pid=29819,fd=12))
LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=945,fd=4))
[root@k8s-master-01 ~]# kubectl get pods -A
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system calico-kube-controllers-67c567bd68-66v8h 1/1 Running 3 (98m ago) 109m
kube-system calico-node-489nn 1/1 Running 0 107m
kube-system calico-node-fnmk7 1/1 Running 0 106m
kube-system calico-node-r274n 1/1 Running 0 106m
kube-system coredns-759dfbbc5c-8r7kt 1/1 Running 0 104m
[root@k8s-master-01 ~]#
[root@k8s-master-01 ~]# cat simple-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
[root@k8s-master-01 ~]# kubectl apply -f simple-pod.yaml
pod/nginx created
[root@k8s-master-01 ~]#
[root@k8s-master-01 ~]#
[root@k8s-master-01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 32s
[root@k8s-master-01 ~]#
[root@k8s-master-01 ~]# kubectl get pods -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx 1/1 Running 0 106s 172.21.176.200 k8s-master-02 <none> <none>
[root@k8s-master-01 ~]# curl http://172.21.176.200
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@k8s-master-01 ~]#
经过我的测试,没有 controller-manager
确实是可以成功创建Pod,ok说明创建Pod不依赖 controller-manager
。
那如果将 scheduler
也停掉呢?新创建的Pod会如何?
[root@k8s-master-01 ~]# kubectl delete -f simple-pod.yaml
pod "nginx" deleted
[root@k8s-master-01 ~]# kubectl get pods
No resources found in default namespace.
[root@k8s-master-01 ~]#
[root@k8s-master-01 ~]# systemctl stop kube-scheduler
[root@k8s-master-01 ~]# ss -lnpt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:10248 *:* users:(("kubelet",pid=29820,fd=24))
LISTEN 0 128 127.0.0.1:9099 *:* users:(("calico-node",pid=28589,fd=8))
LISTEN 0 128 127.0.0.1:37967 *:* users:(("containerd",pid=951,fd=15))
LISTEN 0 8 *:179 *:* users:(("bird",pid=28684,fd=7))
LISTEN 0 128 *:22 *:* users:(("sshd",pid=945,fd=3))
LISTEN 0 128 [::]:10249 [::]:* users:(("kube-proxy",pid=29819,fd=13))
LISTEN 0 128 [::]:10250 [::]:* users:(("kubelet",pid=29820,fd=20))
LISTEN 0 128 [::]:6443 [::]:* users:(("kube-apiserver",pid=3268,fd=7))
LISTEN 0 128 [::]:10256 [::]:* users:(("kube-proxy",pid=29819,fd=12))
LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=945,fd=4))
[root@k8s-master-01 ~]#
[root@k8s-master-01 ~]# kubectl apply -f simple-pod.yaml
pod/nginx created
[root@k8s-master-01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 0/1 Pending 0 6s
[root@k8s-master-01 ~]# kubectl describe pods nginx
Name: nginx
Namespace: default
Priority: 0
Node: <none>
Labels: <none>
Annotations: <none>
Status: Pending
IP:
IPs: <none>
Containers:
nginx:
Image: nginx:latest
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-465ht (ro)
Volumes:
kube-api-access-465ht:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
ConfigMapOptional: <nil>
DownwardAPI: true
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events: <none>
[root@k8s-master-01 ~]#
[root@k8s-master-01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 0/1 Pending 0 44s
[root@k8s-master-01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 0/1 Pending 0 50s
[root@k8s-master-01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 0/1 Pending 0 54s
[root@k8s-master-01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 0/1 Pending 0 73s
[root@k8s-master-01 ~]#
[root@k8s-master-01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 0/1 Pending 0 2m8s
[root@k8s-master-01 ~]#
[root@k8s-master-01 ~]# systemctl start kube-scheduler
[root@k8s-master-01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 0/1 ContainerCreating 0 2m29s
[root@k8s-master-01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 2m33s
[root@k8s-master-01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 2m37s
[root@k8s-master-01 ~]#
如果 scheduler
停掉后,Pod会一直处于 Pending
状态,然后启动 scheduler
,Pod就正常创建了。
如果 kubelet
有异常呢?又会如何?自己去试试吧,哈哈哈!!!!!!
温馨提示
etcd 提供了watch
机制,可以查看到apiserver
与etcd
的交互数据。
相关命令:etcdctl -w json --endpoints=192.168.3.201:2379 --cacert=etcd-ca.pem --cert=etcd-server.pem --key=etcd-server-key.pem watch / --prefix=true
执行上面的命令后,通过kubectl
创建Pod就知道了,赶紧去试试吧,哈哈哈!!!!!!
总结一波。
1.Pod创建过程:kubectl > apiserver > scheduler > kubelet。
2.不依赖 controller-manager
组件。
3.scheduler
组件异常,Pod会一直处于 Pending
状态。
网友评论