文章目录
- pod介绍
- pod 生命周期
- init 容器
- 容器handler
- pod中容器共享进程空间
- sidecar 容器共享
- 参考链接
pod介绍
最小的容器单元
为啥需要pod?
答: 多个进程丢一个容器里,会因为容器里个别进程出问题而出现蝴蝶效应,pod 是更高级的处理方式pod 如何共享相同的ip和端口
答: 由于它们在相同的网络名称和空间中运行如何查看pod 版本
答: kubectl explain pod
[root@k8s-01 chapter03]# kubectl explain pod
KIND: Pod
VERSION: v1如何查看pod 的信息
答:kubectl get pod,svc
[root@k8s-01 chapter03]# kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/busybox 1/1 Running 11 11h
pod/nginx-97499b967-jzxwg 1/1 Running 0 11h
pod/two-containers 1/2 NotReady 0 34mNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 11h
service/nginx NodePort 10.104.210.165 <none> 80:30001/TCP 11hpod创建的小案例演示一下?
答:
cat two-container-pod.yaml
[root@k8s-01 chapter03]# cat two-container-pod.yaml
apiVersion: v1
kind: Pod
metadata:name: two-containers
spec:restartPolicy: Nevervolumes:- name: shared-dataemptyDir: {}containers:- name: nginx-containerimage: nginxvolumeMounts:- name: shared-datamountPath: /usr/share/nginx/html- name: debian-containerimage: debianvolumeMounts:- name: shared-datamountPath: /pod-datacommand: ["/bin/sh"]args: ["-c", "echo Hello from the debian container > /pod-data/index.html"]执行如下命令创建Pod
# kubectl apply –f two-container-pod.yaml查看关于Pod的信息
# kubectl get pod two-containers –-output=yaml进入nginx的容器进行校验结果
# kubectl exec –it two-containers –c nginx-container -- /bin/bash
# apt-get update
# apt-get install curl procps
# ps aux
# curl localhost
pod 生命周期
pod 可能存在的状态
Pending 等待中
Running 运行中
Succeeded 正常终止
Failed 异常停止
Unkonwn 未知状态PendingPod已经被创建,但还没有完成调度,或者说有一个或多个镜像正处于从远程仓库下载的过程。处在这个阶段的Pod可能正在写数据到etcd中、调度、pull镜像或启动容器。Running该 Pod 已经绑定到了一个节点上,Pod 中所有的容器都已被创建。至少有一个容器正在运行,或者正处于启动或重启状态。SucceededPod中的所有的容器已经正常的执行后退出,并且不会自动重启,一般会是在部署job的时候会出现。FailedPod 中的所有容器都已终止了,并且至少有一个容器是因为失败终止。也就是说,容器以非0状态退出或者被系统终止。UnkonwnAPI Server无法正常获取到Pod对象的状态信息,通常是由于其无法与所在工作节点的kubelet通信所致。容器的探测
为了探测Pod是否处于健康状态,kubernetes提供三种探测方式:
ExecAction
TCPSocketAction
HTTPGetAction探测有以下三种结果之一
Success
Failure
Unknown探测的三种类型
livenessProbe
readlinessProbe
startupProbeStartupProbe:k8s 1.16版本后新加的探测方式,用于判断容器内应用程序是否已经启动。如果配置了startupProbe,就会先禁止其他的探测,直到它成功为止,成功后将不再进行探测。比较适用于容器启动时间长的场景。
LivenessProbe:用于探测容器是否运行,如果探测失败,kubelet会根据配置的重启策略进行相应的处理。若没有配置该探针,默认就是success。
ReadinessProbe:一般用于探测容器内的程序是否健康,它的返回值如果为success,那么久代表这个容器已经完成启动,并且程序已经是可以接受流量的状态。pod 的生命周期如何被停止
答; 人为的停止它
或者删除它所属的控制器
在一段时间内,阶段为成功或失败的pod 将过期并自动销毁pod 生命周期有啥案列分享?
使用命令行方式
[root@k8s-01 chapter03]# cat exec-liveness.yaml
apiVersion: v1
kind: Pod
metadata:labels:test: livenessname: liveness-exec
spec:containers:- name: liveness # 使用liveness的方式进行健康探测image: busyboxargs:- /bin/sh- -c- touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600 # 由于这里创建一个文件后,5秒钟后又把文件删除,所以5分钟后探测应处于失败状态livenessProbe:exec: # 执行如下命令行,如果返回结果的状态码为0,证明探测成功,否则证明失败。执行重启策略,默认为always.command:- cat- /tmp/healthyinitialDelaySeconds: 5 # 第一次探测在等待容器启动后多少秒开启执行,此处设置为5秒。periodSeconds: 5 # 设置多长时间探测一次,这里设置为5秒。Events:Type Reason Age From Message---- ------ ---- ---- -------Normal Scheduled 106s default-scheduler Successfully assigned default/liveness-exec to k8s-02Normal Pulled 88s kubelet, k8s-02 Successfully pulled image "busybox"Normal Created 87s kubelet, k8s-02 Created container livenessNormal Started 87s kubelet, k8s-02 Started container livenessWarning Unhealthy 45s (x3 over 55s) kubelet, k8s-02 Liveness probe failed: cat: can't open '/tmp/healthy': No such file or directoryNormal Killing 45s kubelet, k8s-02 Container liveness failed liveness probe, will be restartedNormal Pulling 15s (x2 over 106s) kubelet, k8s-02 Pulling image "busybox"
[root@k8s-01 chapter03]# kubectl describe pod liveness-exec使用http方式进行探测
[root@k8s-01 chapter03]# cat http-liveness.yaml
apiVersion: v1
kind: Pod
metadata:labels:test: livenessname: liveness-http
spec:containers:- name: livenessimage: nginxlivenessProbe:httpGet:path: /port: 80initialDelaySeconds: 3periodSeconds: 3使用tcp方式
[root@k8s-01 chapter03]# cat tcp-liveness-readiness.yaml
apiVersion: v1
kind: Pod
metadata:name: nginxlabels:app: nginx
spec:containers:- name: nginximage: nginxports:- containerPort: 80readinessProbe:tcpSocket:port: 80initialDelaySeconds: 5periodSeconds: 10livenessProbe:tcpSocket:port: 80initialDelaySeconds: 15periodSeconds: 20
init 容器
就是初始化容器,启动之前就会开始执行
init 容器始终运行到完成
每个init 容器必须在下一个容器启动之前成功完成 创建pod
# kubectl apply –f init-pod.yaml查看pod的状态
# kubectl get –f init-pod.yaml查看Pod的详细信息
# kubectl describe –f init-pod.yaml查看pod中的init容器日志
# kubectl logs myapp-pod –c init-myservice
# kubectl logs myapp-pod –c init-mydb创建mydb和myservice服务,并再次查看pod状态
# Kubectl apply –f init-pod-service.yaml
# Kubectl get –f init-pod.yaml有没有案列来告诉?
答:
[root@k8s-01 chapter03]# cat init-pod.yaml
apiVersion: v1
kind: Pod
metadata:name: myapp-podlabels:app: myapp
spec:containers:- name: myapp-containerimage: busybox:1.28command: ['sh', '-c', 'echo The app is running! && sleep 3600']initContainers:- name: init-myserviceimage: busybox:1.28command: ['sh', '-c', 'until nslookup myservice; do echo waiting for myservice; sleep 2; done;']- name: init-mydbimage: busybox:1.28command: ['sh', '-c', 'until nslookup mydb; do echo waiting for mydb; sleep 2; done;'][root@k8s-01 chapter03]#
[root@k8s-01 chapter03]#
[root@k8s-01 chapter03]# cat init-pod-service.yaml
apiVersion: v1
kind: Service
metadata:name: myservice
spec:ports:- protocol: TCPport: 80targetPort: 9376
---
apiVersion: v1
kind: Service
metadata:name: mydb
spec:ports:- protocol: TCPport: 80targetPort: 9377演示效果:
**nslookup: can't resolve 'myservice'**
waiting for myservice
[root@k8s-01 chapter03]# kubectl create -f init-
init-containers.yaml init-pod-service.yaml init-pod.yaml
[root@k8s-01 chapter03]# kubectl create -f init-pod-service.yaml
service/myservice created
service/mydb created
[root@k8s-01 chapter03]# kubectl get pod
NAME READY STATUS RESTARTS AGE
busybox 1/1 Running 12 12h
myapp-pod 1/1 Running 0 8m27s
nginx-97499b967-jzxwg 1/1 Running 0 12h
容器handler
它是用来处理程序附加到容器生命周期中的事件,支持postStart和preStop事件。比如在容器启动后立即发送postStart事件,在容器终止前发送preStop事件[root@k8s-01 chapter03]# cat lifecycle-events.yaml
apiVersion: v1
kind: Pod
metadata:name: lifecycle-demo
spec:containers:- name: lifecycle-demo-containerimage: nginxlifecycle:postStart:exec:command: ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]preStop:exec:command: ["/bin/sh","-c","nginx -s quit; while killall -0 nginx; do sleep 1; done"]创建pod
# kubectl apply –f lifecycle-events.yaml校验Pod中的容器是否运行
# kubectl get pod lifecycle-demo通过以下命令校验postStart和preStop是否执行
# kubectl exec –it lifecycle-demo -- /bin/bash
/# cat /usr/share/message
pod中容器共享进程空间
当启用进程名称空间共享时,容器中的进程对该pod中的所有容器都是可见的。
[root@k8s-01 chapter03]# cat share-process-namespace.yaml
apiVersion: v1
kind: Pod
metadata:name: nginx
spec:shareProcessNamespace: truecontainers:- name: nginximage: nginx- name: shellimage: busyboxsecurityContext:capabilities:add:- SYS_PTRACEstdin: truetty: true创建pod
# kubectl apply –f share-process-namespace.yaml关联shell容器和运行ps
# kubectl attach –it nginx –c shell
/# ps ax
sidecar 容器共享
Sidecar容器在不改变现有容器的情况下扩展和增加pod的功能,也就是说其中一个容器增加了另一个预存在的容器功能[root@k8s-01 chapter03]# cat sidecar.yaml
apiVersion: v1
kind: Pod
metadata:name: pod-with-sidecar
spec:volumes:- name: shared-logsemptyDir: {}containers:- name: sidecar-containerimage: alpinecommand: ["/bin/sh"]args: ["-c", "while true; do date >> /var/log/app.txt; sleep 5;done"]volumeMounts:- name: shared-logsmountPath: /var/log- name: app-containerimage: nginx:1.7.9ports:- containerPort: 80volumeMounts:- name: shared-logsmountPath: /usr/share/nginx/html
参考链接
https://edu.csdn.net/learn/27762/375863?spm=3001.4143