转自:https://blog.csdn.net/yan234280533/article/details/72567261
Pod是Kubernetes中最基本的部署调度单元,可以包含container,逻辑上表示某种应用的一个实例。例如一个web站点应用由前端、后端及数据库构建而成,这三个组件将运行在各自的容器中,那么我们可以创建包含三个container的pod。本文将对Kubernetes的基本处理流程做一个简单的分析。
Pod的创建(单独创建,或者通过deployment创建和管理多个pod)流程如下图所示(图片来源于网络):
具体的创建步骤包括:
1、客户端提交创建请求,可以通过API Server的Restful API,也可以使用kubectl命令行工具。支持的数据类型包括JSON和YAML。
2、API Server处理用户请求,存储Pod数据到etcd。
3、调度器通过API Server查看未绑定的Pod。尝试为Pod分配主机。
4、过滤主机 (调度预选):调度器用一组规则过滤掉不符合要求的主机。比如Pod指定了所需要的资源量,那么可用资源比Pod需要的资源量少的主机会被过滤掉。
5、主机打分(调度优选):对第一步筛选出的符合要求的主机进行打分,在主机打分阶段,调度器会考虑一些整体优化策略,比如把容一个Replication Controller的副本分布到不同的主机上,使用最低负载的主机等。
6、选择主机:选择打分最高的主机,进行binding操作,结果存储到etcd中。
7、kubelet根据调度结果执行Pod创建操作: 绑定成功后,scheduler会调用APIServer的API在etcd中创建一个boundpod对象,描述在一个工作节点上绑定运行的所有pod信息。运行在每个工作节点上的kubelet也会定期与etcd同步boundpod信息,一旦发现应该在该工作节点上运行的boundpod对象没有更新,则调用Docker API创建并启动pod内的容器。
另一张更为简洁(来自于网络):
通过 yaml创建简单pod
[root@k8s-master k8s-objs]# cat pod-solo.yaml
apiVersion: v1
kind: Pod
metadata:
name: simple-sample-pod
labels:
purpose: pod-test
spec:
containers:
- name: test-container
image: tomcat:8
#command: ["/bin/bash","-c","echo hihihi"] ##加上此句会报错,不知为何,kubectl explain pod.spec.containers.command #查看yaml该项,说The docker image's ENTRYPOINT is used if this is not provided
env:
- name: DEMO_GREETING
value: "hellothefirstk8spod"
restartPolicy: Never
[root@k8s-master k8s-objs]# [root@k8s-master k8s-objs]# kubectl create -f pod-solo.yaml pod/simple-sample-pod created [root@k8s-master k8s-objs]# kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-5cd4456b66-gstq6 1/1 Running 1 7d21h
hello-5cd4456b66-sb5px 1/1 Running 1 7d21h
simple-sample-pod 1/1 Running 0 15s [root@k8s-master k8s-objs]# kubectl logs simple-sample-pod 11-Apr-2019 11:12:32.972 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version: Apache Tomcat/8.5.39
11-Apr-2019 11:12:33.018 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Mar 14 2019 11:24:26 UTC
11-Apr-2019 11:12:33.018 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number: 8.5.39.0
11-Apr-2019 11:12:33.018 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Linux
[root@k8s-master k8s-objs]# kubectl describe pod simple-sample-pod
Name: simple-sample-pod
Namespace: default
Priority: 0
PriorityClassName: <none>
Node: k8s-node1/192.168.111.131
Start Time: Thu, 11 Apr 2019 19:12:15 +0800
Labels: purpose=pod-test
Annotations: <none>
Status: Running
IP: 10.244.1.68
Containers:
test-container:
Container ID: docker://fd1c4cdad785bd1ec39372c45d44dbc1fe4c4cb2b1dbbfb8d2ecbf004b13d474
Image: tomcat:8
Image ID: docker-pullable://tomcat@sha256:3e3d18321127bb9114f4226f95802d3899aeec4c36df84d0359e5da300e9bc72
Port: <none>
Host Port: <none>
State: Running
Started: Thu, 11 Apr 2019 19:12:27 +0800
Ready: True
Restart Count: 0
Environment:
DEMO_GREETING: hellothefirstk8spod
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-92rjn (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-92rjn:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-92rjn
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 2m53s default-scheduler Successfully assigned default/simple-sample-pod to k8s-node1
Normal Pulled 2m42s kubelet, k8s-node1 Container image "tomcat:8" already present on machine
Normal Created 2m42s kubelet, k8s-node1 Created container test-container
Normal Started 2m41s kubelet, k8s-node1 Started container test-container
[root@k8s-master k8s-objs]#
删除:
[root@k8s-master k8s-objs]# ls pod-solo.yaml [root@k8s-master k8s-objs]# kubectl get pod
NAME READY STATUS RESTARTS AGE
hello-2-5bf86f6c95-rpvxb 1/1 Running 0 106m
hello-5cd4456b66-gstq6 1/1 Running 1 8d
hello-5cd4456b66-sb5px 1/1 Running 1 8d
simple-sample-pod 1/1 Running 0 4m30s
[root@k8s-master k8s-objs]# [root@k8s-master k8s-objs]# kubectl delete -f pod-solo.yaml pod "simple-sample-pod" deleted [root@k8s-master k8s-objs]# kubectl get pods NAME READY STATUS RESTARTS AGE hello-5cd4456b66-gstq6 1/1 Running 1 7d21h hello-5cd4456b66-sb5px 1/1 Running 1 7d21h [root@k8s-master k8s-objs]#